Whoosh初次使用

1.Python代码

1.1 构建索引

def build_index(file_path):
        start_time = time.clock()
        schema  = Schema(title=TEXT(stored=True), path=ID(stored=False), content=TEXT(stored=True))
        ix = create_in("indexdir", schema)
        writer = ix.writer()
        with open(file_path, 'r') as filereader:
                num = 1
                for row in filereader:
                        title_num = str(num) + " line"
                        writer.add_document(title=title_num.decode("utf-8"), path=file_path.decode("utf-8"), content=row.decode("utf-8"))
                        num += 1
        writer.commit()
        elapsed = (time.clock() - start_time)
        return "Step One:  索引已经构建完成--------------------- " + str(elapsed)

1.2  检索

def search_word(search):
        start_time = time.clock()
        ix = open_dir("indexdir")
        with ix.searcher() as searcher:
                query = QueryParser("content", ix.schema).parse(search)
                results = searcher.search(query, limit = None)
                elapsed = (time.clock() - start_time)
                print "Step Two: 搜索已经完成--------------------- " + str(elapsed)
                pprint.pprint(results[:])
        return "Finished."

1.3 测试

if __name__ == '__main__':
        file_name = raw_input("Please input the path of file you want to build index: ")
        step_result = "Something went wrong......."
        try:
                step_result = build_index(file_name)
        finally:
                print step_result
        search = raw_input("Please input the word you want to search: ")
        step_result = "Something went wrong......"
        try:
                step_result = search_word(search)
        finally:
                print step_result

2.参考资料

1.https://whoosh.readthedocs.io/en/latest/releases/index.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值