使用Haystack和Solr问题实现搜索

时间:2012-11-05 16:35:12

标签: django search solr django-haystack

我正在尝试将搜索功能实施到基于django的网站。

按照tutorial我发现了这个:

  

如果您使用的是Solr后端,则需要额外一步。 Solr的   配置是基于XML的,因此您需要手动重新生成   架构。你应该先run ./manage.py build_solr_schema放弃   Solr的schema.xml文件中的XML输出并重新启动Solr   服务器

首先,我不知道在哪里放置我的schema.xml,经过一些研究后,我想我会在我的项目中创建一个文件夹来放置它:myprojectname/solr/schema.xml。是吗?

第二次,如何重新启动Solr?

更新

我下载了Solr,解压缩它,并将schema.xml生成在example/solr/conf

然后我开始solr java -jar start.jar

但是当我尝试构建索引时:    ./manage.py rebuild_index

我明白了:

WARNING: This will irreparably remove EVERYTHING from your search index.
Your choices after this are to restore from backups or rebuild via the `rebuild_index` command.
Are you sure you wish to continue? [y/N] y

Removing all documents from your index because you said so.
All documents removed.
Indexing 1 News.
Failed to add documents to Solr: [Reason: None]
<response><lst name="responseHeader"><int name="status">400</int><int    name="QTime">4</int></lst><lst name="error"><str name="msg">ERROR: [doc=news.news.2]   unknown field 'django_id'</str><int name="code">400</int></lst></response>
Indexing 1 entries.

Failed to add documents to Solr: [Reason: None]
<response><lst name="responseHeader"><int name="status">400</int><int name="QTime">17</int></lst><lst name="error"><str name="msg">ERROR: [doc=zinnia.entry.2] unknown field 'django_id'</str><int name="code">400</int></lst></response>

我验证了我的schema.xml,我确实有:

<field name="django_ct" type="string" indexed="true" stored="true" multiValued="false" />
<field name="django_id" type="string" indexed="true" stored="true" multiValued="false" />

<子> 的 P.S。 我正在使用Django 1.2和Haystack 1.2.7

2 个答案:

答案 0 :(得分:1)

solr服务器需要有一个schema.xml的副本而不是django。我通常会在我的django项目中保留schema.xml的副本以进行版本控制,但是solr在那里找不到它。

你是当地的solr服务器吗?您使用托管或远程Solr服务吗?我在本地开发然后使用websolr b / c我不想为生产配置solr。

对于OSX上的本地开发

我假设这是OSX上的本地开发,并且你安装了自制软件(假设 - 如果不是这样的话,请给我更多信息):

brew install solr

这将在以下某处安装Solr:/usr/local/Cellar/solr/...

注意:当我在本地开发时,我喜欢使用fabric来运行部署和一些启动任务。

所以在我的fabfile.py中,我有一个fabric命令将我的schema.xml复制到正确的文件中并启动solr服务器(我只是在cmd行运行fab solr

def solr() :
    # build a new updated schema.xml (changes to indexes/models may require this so always do it for local testing)
    local('python manage.py build_solr_schema > schema.xml')
    # copy the schema.xml into the proper directory
    local('cp schema.xml /usr/local/Cellar/solr/3.6.0/libexec/example/solr/conf/schema.xml')
    # start the solr server
    local('cd /usr/local/Cellar/solr/3.6.0/libexec/example && java -jar start.jar')

注意:如果不使用fabric

,可以在命令行上运行这些命令

答案 1 :(得分:0)

我遇到了同样的问题,重建任务失败了。对我来说,解决方案是:

  1. 构建新的schema.xml并将其放在相应的文件夹

  2. 重启Solr

  3. 重建索引没有问题