1.首先安装haystack、whoosh、jieba,
pip install django-haystack
pip install whoosh
pip install jieba
haystack: django的一个包,可以方便地对model里面的内容进行索引、搜索。设计为支持whoosh,soir,Xapian,Elasticsearc四种全文检索引擎后端,属于一种全文检索的框架
whoosh: 纯Python编写的全文搜索引擎,虽然性能比不上sphix、xapian、Elasticsearc等,但是无二进制包,程序不会莫名其妙的奔溃,对于小型的站点,whoosh已经足够使用
jieba:一款免费的中文分词包
2.在settings中进行配置
在INSTALLED_APPS添加 haystack
INSTALLED_APPS = (
'haystack',
)
3.添加搜索引擎
在settings中进行配置
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_cn_backend.WhooshEngine',
'PATH': os.path.join(BASE_DIR, 'whoosh_index'),
}
}
4.在应用目录(即要设为检索关键字的应用目录下)下新建一个search_indexes.py文件(名字是固定的不能改)
from haystack import indexes
from .models import GoodsInfo
class GoodsInfoIndex(indexes.SearchIndex,indexes.Indexable):
text = indexes.CharField(document=True,use_template=True)
def get_model(self):</