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

前言:

全文檢索就是針對全部內容進行動態匹配搜索的概念,針對特定的關鍵詞創建索引並精確匹配達到性能優化的目的html

 

 

 

 

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

 

 

 

 

 

 

 

 

Haystack的APIdjango

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

whoosh參考性能優化

胥愛軍app

相關文章
相關標籤/搜索