haystack+whoosh+jieba 全文检索功能
前言
全文检索不同于特定字段的模糊查询,使用全文检索的效率更高,并且能够对于中文进行分词处理
haystack:django的一个包,可以方便地对model里面的内容进行索引、搜索,设计为支持
whoosh:纯Python编写的全文搜索引擎
haystack+whoosh
jieba:一款免费的中文分词包,如果觉得不好用可以使用一些收费产品。
一、在Django开发虚拟环境中安装包
> pip install django-haystack
pip install whoosh
pip install jieba
下载完成后重启服务。
二、编写搜索表单
此处必须为get方法 且查询name必须为q
<form method='get' action="/search/" target="_blank">
<input type="text" name="q">
<input type="submit" value="查询">
</form>
三、创建索引类
- 在应用目录下建立search_indexes.py文件
from haystack import indexes
from .models import Article
class ArticleIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField