Django笔记(四) 搜索 django-haystack使用

搜索模块 Haystack

whoosh为例

安装

pip install django-haystack
pip install whoosh
pip install jieba

添加中文分词

在haystack包目录拷贝一份whoosh_backend.pywhoosh_cn_backend.py

#在whoosh_cn_backend.py里面  
.........
#直接使用jieba的ChineseAnalyzer,好像没必要按照网上的创建ChineseAnalyzer分词文件  
from jieba.analyse import ChineseAnalyzer  
.............  
#然后找到build_schema函数处,这是一个构建分词模式的,找到  
schema_fields[field_class.index_fieldname] = TEXT(stored=True, analyzer=StemmingAnalyzer(), field_boost=field_class.boost, sortable=True)
#修改为
schema_fields[field_class.index_fieldname] = TEXT(stored=True, analyzer=ChineseAnalyzer(), field_boost=field_class.boost, sortable=True)

修复haystack不兼容django 1.9 的问题

http://stackoverflow.com/questions/34479581/i-am-using-django-haystack-with-whoosh-but-there-some-error-after-entering-som
'NoneType' object has no attribute '_default_manager'
The django.db.model.get_model is not available any longer in 1.9, so None is returned when calling django.db.models.get_model, but in the more recent commit (from the 3rd of January) the utils.app_loading.py is used to either use the django.apps.apps.get_model when using Django 1.7 or higher, otherwise the old django.db.models._get_models is used.
So, best to upgrade to the latest development version git+https://github.com/django-haystack/django-haystack.git.

修改models.py https://github.com/barbuza/django-haystack/commit/c4e398319e8ff3e5049011a0078c81821760f78d

Django笔记(四) 搜索 django-haystack使用_第1张图片
捕获.PNG

创建必须文件

在app目录下创建search_indexes.py
http://django-haystack.readthedocs.org/en/v2.4.1/tutorial.html#creating-searchindexes

templates目录下创建search/search.html
http://django-haystack.readthedocs.org/en/v2.4.1/tutorial.html#search-template

改进长词搜索结果

http://whoosh.readthedocs.org/en/latest/parsing.html#searching-for-any-terms-instead-of-all-terms-by-default
修改whoosh_cn_backend.py

from whoosh import qparser
#120行左右添加
og = qparser.OrGroup.factory(0.9)
#120行左右添加group=og参数
self.parser = QueryParser(self.content_field_name, schema=self.schema,group=og)

搜索筛选

haystack search_viewHe basic_search中返回的form就是一段form搜索窗口代码,可用于搜索不同的视图

自定义search view

使用haystack的search_view_factory
继承SearchView,重写extra_context可以为搜索结果页添加额外数据(如热门搜索)

more_like_this

more_like_this 只对text字段有效(solr中,whoosh中忘了,此条不一定准确,也可能是我理解错误)

Django报utf8字符错误

创建haystack whoosh索引,django访问数据库以及打开页面 'utf8' codec can't decode
不加下面那一段,从老数据库查询,然后写入新数据库,可以解决单条记录utf8字符错误的问题

#search_indexes.py添加
from django.db import connection
connection.cursor()
connection.connection.text_factory = lambda x: unicode(x, "utf-8", "ignore")

搜索模板

简单搜索分页

    
{% if page.number == 2 %} « 上一页 {% else %} {% if page.has_previous %} « 上一页 {% else %} « 上一页 {% endif %} {% endif %} {% if page.has_next %}{% endif %}下一页 » {% if page.has_next %}{% endif %}

搜索模板中highlight标签使用

  1. 首先在模板头部引入highlight标签 {% highlight %}标签
  2. 在css样式中创建对应的class
  3. 给命中的搜索关键字加上样式class keyword {% highlight result.object.body with query css_class "keyword" %}

基本命令

重建索引./manage.py rebuild_index
更新索引 ./manage.py update_index

django-haystack 后端

  • whoosh简单方便,基本不用配置什么,数据量少也很快,多了就不行,感觉最多也就几十万条就不行了
  • solr 基于java开发,安装配置整合分词什么的比较麻烦,但是速度很快,几百万条数据也没什么问题(django-haystack和solr配置整理中)
  • 其他没用过,考虑过sphinx(只用过一次,之后一直用solr),速度上要比solr快,但是用起来比较麻烦,whoosh和solr支持more_like_this方法,sphinx不支持

你可能感兴趣的:(Django笔记(四) 搜索 django-haystack使用)