• whoosh使用简介


    whoosh,纯python的全文搜索引擎。这里记录简单使用,参考官方文档。

    这里是我的代码,创建搜索文档(即索引文档),windows下操作。

    #coding=utf-8
    import os
    from whoosh.index import create_in,open_dir
    from whoosh import fields
    WHOOSH_ADD = 'E:\whoosh_index'
    WHOOSH_SCHEMA = fields.Schema(title=fields.TEXT(stored=True),
        content=fields.TEXT(stored=True),
        )
    if not os.path.exists(WHOOSH_ADD):
        os.mkdir(WHOOSH_ADD)
        ix = create_in(WHOOSH_ADD,schema=WHOOSH_SCHEMA,indexname='comment')
    ix = open_dir(WHOOSH_ADD,indexname='comment')
    writer = ix.writer()
    writer.add_document(title=u'chang yanjie add',content= u' zheng wen 我是正文',)
    
    writer.add_document(title=u'chang yan1 jie2 add',content= u' zheng wen 我是正文2',)
    writer.commit()

    学习使用的同学们自己更改地址WHOOSH_ADD,

    当然也有更新方法,

    writer.update_document(title=u"chang yanjie add", content="变啦",)

    搜索代码:

    #coding=utf-8
    from whoosh import index
    from whoosh.qparser import QueryParser
    ix = index.open_dir('E:\whoosh_index', indexname='comment')
    hits = []
    query = u' zheng'
    parser = QueryParser("content", schema=ix.schema)   
    try:
        word = parser.parse(query)
    except:
        word = None
    if word is not None:
        s = ix.searcher()
        hits = s.search(word)
        #with  ix.searcher() as s:              注意此处,如果使用with 方法的话,文件会自动closed()方法,下边将无法使用hits结果
        #    hits = s.search(word)
    print len(hits)

    正常结果应该是2,哈哈。

    随缘
  • 相关阅读:
    反射
    EFCore笔记之异步查询
    EFCore笔记之查询数据
    Json扩展 (转)
    C语言学习笔记之成员数组和指针
    asp中cookie欺骗/注入原理与防范
    简单的php Mysql类(查询 删除 更新)
    PHP四舍五入精确小数位及取整
    CentOS中配置LNMP环境打开提示File not found
    WIN中SharePoint Server 2010 入门安装部署详解
  • 原文地址:https://www.cnblogs.com/chang/p/2855223.html
Copyright © 2020-2023  润新知