Tech

从零开始搭建Django Solr的环境

本文介绍在Ubuntu系统中,用django-haystack处理Django搜索请求的方法。如果有涉及连词、多条件等较为复杂的搜索需求,这是一个很不错的解决方案。如果只需要基本的搜索功能,或是刚接触Django的用户则不推荐使用,这个模块功能强大相应的配置也很繁琐,稍有不慎就会出错。

安装tomcat, solr, django-haystack:

$ sudo apt-get install tomcat6
$ sudo apt-get install solr-tomcat6 #Ubuntu 11.10 12.04里是 solr-tomcat
$ pip install django-haystack

安装成功后打开:http://127.0.0.1:8080/solr 可以看到Solr的管理界面。接下来开始配置Django,首先在项目目录中新建search_sites.py文件,内容是:


import haystack
haystack.autodiscover()


编辑settings.py文件加入haystack模块,指定Solr作为搜索引擎:


INSTALLED_APPS = (
#other apps
    'haystack',
)

HAYSTACK_SITECONF = 'search_sites' #之前创建的文件名
HAYSTACK_SEARCH_ENGINE = 'solr'
HAYSTACK_SOLR_URL = 'http://127.0.0.1:8080/solr/'
HAYSTACK_SOLR_TIMEOUT = 60 * 5
HAYSTACK_INCLUDE_SPELLING = True
HAYSTACK_BATCH_SIZE = 100

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
        'URL': 'http://127.0.0.1:8080/solr'
    },
}

urls.py里引入haystack:


url(r'^search/', include('haystack.urls')),

现在打开这个地址/search/会提示找不到相应的模板文件,在模板文件夹中创建/search/search.html:


{% block content %}
    <h2>Search</h2>

    <form method="get" action=".">
        <table>
            {{ form.as_table }}
            <tr>
                <td> </td>
                <td>
                    <input type="submit" class="btn btn-success" value="Search">
                </td>
            </tr>
        </table>

        {% if query %}
            <h3>Results</h3>

            {% for result in page.object_list %}
                <p>
                    <a href="#">{{ result.object.name }}</a>
                </p>
            {% empty %}
                <p>No results found.</p>
            {% endfor %}

            {% if page.has_previous or page.has_next %}
                <div>
                    {% if page.has_previous %}<a href="?q={{ query }}&page={{ page.previous_page_number }}">{% endif %}« Previous{% if page.has_previous %}</a>{% endif %}
                    |
                    {% if page.has_next %}<a href="?q={{ query }}&page={{ page.next_page_number }}">{% endif %}Next »{% if page.has_next %}</a>{% endif %}
                </div>
            {% endif %}
        {% else %}
            {# Show some example queries to run, maybe query syntax, something else? #}
        {% endif %}
    </form>
{% endblock %}

再次打开/search/,就可以看到haystack提供的search界面(如下图)了,它会提示:Select a valid choice. core.product is not one of the available choices. 因为还没有注册需要被搜索的模块。

假设我要搜索的Model是product应用中的Product。在product目录下创建search_indexes.py


from haystack import indexes, site
from apps.products.models import Product


class ProductIndex(indexes.SearchIndex):
    text = indexes.CharField(document=True, use_template=True)
    title = indexes.CharField(model_attr='name')
    

    def get_model(self):
        return Product

    def index_queryset(self):
        return self.get_model().objects.all()

site.register(Product, ProductIndex)

刷新页面,如果会出现名为Product的复选框,Django部分的设置就成功了。但现在还无法搜索,要生成一个与现有Model对应的schema,把它写入Solr:

$ python manage.py build_solr_schema > /etc/solr/conf/schema.xml

完成后需要重启tomcat6。现在执行 manger.py rebuild_index命令,提示:TemplateDoesNotExist: search/indexes/product/product_text.txt。因为在search_indexes.py的text字段指定了use_template=True,需要创建一个数据模板,在提示的模板目录中(本例是search/indexes/product/)创建product_text.txt


{{ object.name }}
{{ object.description }}

再次执行manger.py rebuild_index,成功后就可以使用Solr提供的搜索功能了:

如果出现错误可以先查看文档中的Debug部分或是在评论中提出。

0 0 投票数
文章评分
订阅评论
提醒
guest

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

4 评论
最新
最旧 最多投票
内联反馈
查看所有评论
马新民
11 年 前

谢谢你的文档!已经弄好了,是版本冲突的问题

马新民
11 年 前

为什么我的会有错误啊?

july
july
12 年 前

好强大,先mark