python使用whoos搜索入门【上篇】

环境python2.7:

#  本来py3 的环境,由于一直报错找不到 whoosh 模块 换了2.7 就解决了

 

pip install whoosh

 

#!/user/bin/env python    
#-*- coding:utf-8 -*-



from whoosh.index import create_in
from whoosh.fields import *
from whoosh.qparser import QueryParser


#  建立索引对象 和字段
schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT)

ix = create_in("indexdir/", schema)  # indexdir  在这个目录下创建索引文件


writer = ix.writer() # 获取写入索引对象


writer.add_document(title=u"First document", path=u"/a",
                  content=u"This is the first document we've added!")  # 写入数据到对应的字段

writer.add_document(title=u"Second document", path=u"/b",
                   content=u"The second one is even more interesting!")


writer.commit()  # 写入完成提交


#  创建搜索对象写入查询数据
with ix.searcher() as searcher:
    query = QueryParser("content", ix.schema).parse("first")
    results = searcher.search(query)
    print(results[0])


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值