首页
开源项目
闲篇
元编程
Golang
精粹
技术探讨
服务
首页
开源项目
大会
报名参会
报名查询
加入中国C++ User Group
大会调查
社区服务
网站首页
文章专栏
python的全文检索whoosh
python的全文检索whoosh
编辑时间:2024-03-25 15:23:42
作者:cocapai
0条评论
出自:cocapai
地址:
www.cocapai.com
转载请注明出处!
基于python+whoosh的全文检索实现 whoosh的官方介绍:http://whoosh.readthedocs.io/en/latest/quickstart.html 中文的全文检索需要导入jieba工具包以及whoosh工具包 1 from whoosh.qparser import QueryParser 2 from whoosh.index import create_in 3 from whoosh.index import open_dir 4 from whoosh.fields import * 5 from jieba.analyse import ChineseAnalyzer 6 from get_comment import SQL 7 from whoosh.sorting import FieldFacet 8 9 analyser = ChineseAnalyzer() #导入中文分词工具 10 schema = Schema(phone_name=TEXT(stored=True, analyzer=analyser), price=NUMERIC(stored=True), 11 phoneid=ID(stored=True))# 创建索引结构 12 ix = create_in("path", schema=schema, indexname='indexname') #path 为索引创建的地址,indexname为索引名称 13 writer = ix.writer() 14 writer.add_document(phone_name='name',price ="price",phoneid ="id") # 此处为添加的内容 15 print("建立完成一个索引") 16 writer.commit() 17 # 以上为建立索引的过程 18 ```new_list = [] 19 index = open_dir("indexpath", indexname='comment') #读取建立好的索引 20 with index.searcher() as searcher: 21 parser = QueryParser("要搜索的项目,比如“phone_name", index.schema) 22 myquery = parser.parse("搜索的关键字") 23 facet = FieldFacet("price", reverse=True) #按序排列搜索结果 24 results = searcher.search(myquery, limit=None, sortedby=facet) #limit为搜索结果的限制,默认为10,详见博客开头的官方文档 25 for result1 in results: 26 print(dict(result1)) 27 new_list.append(dict(result1)) 注: Whoosh 有一些很有用的预定义 field types,你也可以很easy的创建你自己的。 whoosh.fields.ID 这个类型简单地将field的值索引为一个独立单元(这意味着,他不被分成单独的单词)。这对于文件路径、URL、时间、类别等field很有益处。 whoosh.fields.STORED 这个类型和文档存储在一起,但没有被索引。这个field type不可搜索。这对于你想在搜索结果中展示给用户的文档信息很有用。 whoosh.fields.KEYWORD 这个类型针对于空格或逗号间隔的关键词设计。可索引可搜索(部分存储)。为减少空间,不支持短语搜索。 whoosh.fields.TEXT 这个类型针对文档主体。存储文本及term的位置以允许短语搜索。 whoosh.fields.NUMERIC 这个类型专为数字设计,你可以存储整数或浮点数。 whoosh.fields.BOOLEAN 这个类型存储bool型 whoosh.fields.DATETIME 这个类型为 datetime object而设计(更多详细信息) whoosh.fields.NGRAM 和 whoosh.fields.NGRAMWORDS 这些类型将fiel文本和单独的term分成N-grams(更多Indexing & Searching N-grams的信息)
来说两句吧
2090
提交评论
登录才能发表评论。
最新评论
COCAPAI
一个很酷的卡派
这里有创新的idea,这里有最酷的卡派
分类导航
精华
开源项目
活动
元编程
代码精粹
技术探讨
故事
友情链接
COCAPAI