whoosh的官方介绍:http://whoosh.readthedocs.io/en/latest/quickstart.html

因为做的是中文的全文检索需要导入jieba工具包以及whoosh工具包
直接上代码吧
from whoosh.qparser import QueryParser
from whoosh.index import create_in
from whoosh.index import open_dir
from whoosh.fields import *
from jieba.analyse import ChineseAnalyzer
from get_comment import SQL
from whoosh.sorting import FieldFacet
analyser = ChineseAnalyzer() #导入中文分词工具
schema = Schema(phone_name=TEXT(stored=True, analyzer=analyser), price=NUMERIC(stored=True),
phoneid=ID(stored=True))# 创建索引结构
ix = create_in("path", schema=schema, indexname='indexname') #path 为索引创建的地址,indexname为索引名称
writer = ix.writer()
writer.add_document(phone_name='name',price ="price",phoneid ="id") # 此处为添加的内容
print("建立完成一个索引")
writer.commit()
# 以上为建立索引的过程
new_list = []
index = open_dir("indexpath", indexname='comment') #读取建立好的索引
with index.searcher() as searcher:
parser = QueryParser("要搜索的项目,比如“phone_name", index.schema)
myquery = parser.parse("搜索的关键字")
facet = FieldFacet("price", reverse=True) #按序排列搜索结果
results = searcher.search(myquery, limit=None, sortedby=facet) #limit为搜索结果的限制,默认为10,详见博客开头的官方文档
for result1 in results:
print(dict(result1))
new_list.append(dict(result1))另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。