Django全文检索(django-haystack+whoosh+jieba)

前言:

全文检索就是针对所有内容进行动态匹配搜索的概念,针对特定的关键词建立索引并精确匹配达到性能优化的目的

 

 

 

 

class Whoose_seach(object):
    analyzer = ChineseAnalyzer()  # 导入中文分词工具
    def __init__(self,table,field,url):
        self.field=field
        self.schema_dict={"pk":ID(stored=True),"url":TEXT(stored=True),self.field:TEXT(stored=True, analyzer=self.analyzer)}
        self.url=url
        self.table=table
        #self.schema = Schema(pk=ID(stored=True), desc=TEXT(stored=True, analyzer=self.analyzer), url=TEXT(stored=True))  # 创建索引结构
        self.schema = Schema(**self.schema_dict)
        self.ix = create_in("path", schema=self.schema, indexname=self.table._meta.model_name)  # 创建索引(path 为创建索引的目录,indexname为索引名称)
        self.index = open_dir("path", indexname=self.table._meta.model_name)  # 读取建立好的索引

    def index_building(self):
        writer =self.ix.writer()    #填充内容
        query=self.table.objects.all()
        for item in query:
            #url='http://172.17.10.112:8001/arya/cmdb/worker_order/see/?id=%s'%(item.pk)
            url = self.url%(item.pk)
            writer.add_document(pk=str(item.pk),desc=item.desc,url=url)  # 此处为添加的内容
        writer.commit()

    def seach(self,key_word):
        self.index_building()
        ret=[]
        with self.index.searcher() as searcher:
            parser = QueryParser(self.field,self.schema)
            myquery = parser.parse(key_word)
            facet = FieldFacet("pk", reverse=False)  # 按序排列搜索结果
            results = searcher.search(myquery, limit=None,sortedby=facet)  # limit为搜索结果的限制,默认为10,详见博客开头的官方文档
            for result1 in results:
                ret.append(dict(result1))
            else:
                return ret



if __name__ == '__main__':
    from cmdb import models
    Whoose_seach_interface = Whoose_seach(models.Worker_order, 'desc','http://172.17.10.112:8001/arya/cmdb/worker_order/see/?id=%s')
    print(Whoose_seach_interface.seach('张根'))
whoose

 

from haystack.query import SearchQuerySet
from haystack.inputs import AutoQuery
sqs = SearchQuerySet().filter(desc=AutoQuery('张根'))
for i in sqs:
    print(i)
调用haystack的搜素API

 

 

 Django全文检索(django-haystack+whoosh+jieba)_第1张图片

 

Django全文检索(django-haystack+whoosh+jieba)_第2张图片

 

 

 

 

Haystack的API

https://www.jianshu.com/p/e323e0d37c2b

whoosh参考

胥爱军

转载于:https://www.cnblogs.com/sss4/p/10315632.html

你可能感兴趣的:(Django全文检索(django-haystack+whoosh+jieba))