Django本身不提供全文检索的功能,但django-haystack为其提供了全文检索的框架。
django-haystack能为Django提供whoosh,solr,Xapian和Elasticsearc四种全文检索引擎作为后端。
其中whoosh为纯python的实现,不是非常大型的应用,是没有问题的。
本文将介绍Django1.9中通过django-haystack与whoosh集成以及whoosh的中文支持。
安装依赖:
1 | pip install django-haystack |
建立模型
我们以文章为搜索目标,现在我的app名字为blog,
模型文件是mysite/blog/models.py :
1 | # coding=utf-8 |
search_indexes.py
在app目录下建立一个search_indexes.py(mysite/blog/search_indexes.py)代码如下:
1 | #!/usr/bin/env python |
备注:search_indexes.py文件名不能修改,否则报错:No fields were found in any search_indexes.
post_text.txt
因为在search_indexes.py使用了use_template=True,所以可以同时使用模板对索引字段进行定义。
如:mysite/blog/templates/search/indexes/blog/post_text.txt
:
1 | {{ object.title }} |
settings.py
1 | # Application definition |
urls.py
1 | urlpatterns = patterns( |
jieba中文分词
jieba其实已经提供了集成whoosh的ChineseAnalyzer,
也就是说不需要自己写ChineseAnalyzer了,直接在whoosh_backend.py中直接引用就好;
同时,不推荐将whoosh_backend.py放到Lib下面,这样移植性会有问题,自己的代码,还是放在项目下面为妙。
1. 将文件haystack.backends.whoosh_backend.py拷贝到app下面,并重命名为whoosh_cn_backend.py,
如blog/whoosh_cn_backend.py。重点的改造有:
增加:
1
from jieba.analyse import ChineseAnalyzer
修改
1
schema_fields[field_class.index_fieldname] = TEXT(stored=True, analyzer=ChineseAnalyzer(), field_boost=field_class.boost, sortable=True)
2. 修改后端引擎,setting.py配置:1
2
3
4
5
6
7# full text search
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'blog.whoosh_cn_backend.WhooshEngine',
'PATH': os.path.join(BASE_DIR, 'whoosh_index'),
},
}
重建索引
1 | python manage.py rebuild_index |
索引更新
最简单的办法就是在settings.py中添加:1
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
自定义搜索示例
(1) 先定义view:
1 | from haystack.forms import SearchForm |
(2) 然后在template页面中:1
2
3
4
5
6
7
8
9
10
11<!-- searchbox START -->
<div id="searchbox">
<form action="{% url 'blog.views.full_search' %}" method="get">
<div class="content">
<label>
<input type="text" class="textfield searchtip" name="q" size="24" value="">
</label>
<input type="submit" class="button" value="">
</div>
</form>
</div>
更详细内容请参考官方文档:http://django-haystack.readthedocs.org/en/latest/