whoosh mysql_Whoosh搜索引擎

Whoosh搜索引擎

whoosh 是一个纯python实现的全文搜索引擎,它小巧轻便,安装简单,设计上参考了 Lucene ,性能上虽有欠缺,但贵在方便,无需复杂安装,对于构建小型网站的搜索引擎来说,是一个不错的选择。

1. 快速入门

whoosh 可以使用pip进行安装

pip install whoosh

现在通过官网的例子,快速体验

from whoosh.index import create_in

from whoosh.fields import *

# 构建索引

schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT)

ix = create_in("indexdir", schema)

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()

# 搜索

from whoosh.qparser import QueryParser

with ix.searcher() as searcher:

query = QueryParser("content", ix.schema).parse("first")

results = searcher.search(query)

print(results[0])

程序最终输出结果为

官网上的例子,我没有做任何修改,只是添加了两行注释。整个程序分为两部分,第一部分是构建索引的过程,第二部分是搜索的过程。

2. 构建索引

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值