【问题标题】:Whoosh NestedChildren search not returning all resultsWhoosh NestedChildren 搜索未返回所有结果
【发布时间】:2014-05-08 04:47:51
【问题描述】:

我正在制作一个必须支持嵌套数据层次结构的搜索索引。 出于测试目的,我正在制作一个非常简单的架构:

test_schema = Schema(
    name_ngrams=NGRAMWORDS(minsize=4, field_boost=1.2),
    name=TEXT(stored=True),
    id=ID(unique=True, stored=True),
    type=TEXT
)

对于测试数据,我使用这些:

test_data = [
    dict(
        name=u'The Dark Knight Returns',
        id=u'chapter_1',
        type=u'chapter'),
    dict(
        name=u'The Dark Knight Triumphant',
        id=u'chapter_2',
        type=u'chapter'),
    dict(
        name=u'Hunt The Dark Knight',
        id=u'chapter_3',
        type=u'chapter'),
    dict(
        name=u'The Dark Knight Falls',
        id=u'chapter_4',
        type=u'chapter')
]

parent = dict(
    name=u'The Dark Knight Returns',
    id=u'book_1',
    type=u'book')

我已将所有 (5) 个文档添加到索引中,如下所示

with index_writer.group():
    index_writer.add_document(
        name_ngrams=parent['name'],
        name=parent['name'],
        id=parent['id'],
        type=parent['type']
    )
    for data in test_data:
        index_writer.add_document(
            name_ngrams=data['name'],
            name=data['name'],
            id=data['id'],
            type=data['type']
        )

所以,为了获取一本书的所有章节,我创建了一个使用 NestedChildren 搜索的函数:

def search_childs(query_string):
    os.chdir(settings.SEARCH_INDEX_PATH)
    # Initialize index
    index = open_dir(settings.SEARCH_INDEX_NAME, indexname='test')
    parser = qparser.MultifieldParser(
        ['name',
         'type'],
        schema=index.schema)
    parser.add_plugin(qparser.FuzzyTermPlugin())
    parser.add_plugin(DateParserPlugin())

    myquery = parser.parse(query_string)

    # First, we need a query that matches all the documents in the "parent"
    # level we want of the hierarchy
    all_parents = And([parser.parse(query_string), Term('type', 'book')])

    # Then, we need a query that matches the children we want to find
    wanted_kids = And([parser.parse(query_string),
                       Term('type', 'chapter')])
    q = NestedChildren(all_parents, wanted_kids)
    print q

    with index.searcher() as searcher:
        #these results are the parents
        results = searcher.search(q)
        print "number of results:", len(results)
        if len(results):
            for result in results:
                print(result.highlights('name'))
                print(result)
            return results

但是对于我的测试数据,如果我搜索“dark knigth”,当它必须是 4 个搜索结果时,我只会得到 3 个结果。

我不知道丢失的结果是否因为与书名相同而被排除在外,但它根本没有显示在搜索结果中

我知道所有项目都在索引中,但我不知道我在这里缺少什么。

有什么想法吗?

【问题讨论】:

    标签: python full-text-search whoosh


    【解决方案1】:

    原来我用错了 NestedChildren。 以下是我从 Google Groups 中的 Matt Chaput 那里得到的答案:


    我正在制作一个必须支持嵌套数据层次结构的搜索索引。

    NestedChildren 的第二个参数不是你想的那样。

    TL;DR:您使用了错误的查询类型。让我知道你想做什么,我可以告诉你怎么做:)

    关于嵌套子代

    (注意,我发现了一个bug,见最后)

    NestedChildren 很难理解,但希望我可以尝试更好地解释它。

    NestedChildren 是关于搜索某些 PARENTS,但将他们的 CHILDREN 作为命中。

    第一个参数是匹配“父”类的所有文档的查询(例如“type:book”)。第二个参数是一个查询,它匹配与您的搜索条件匹配的父类的所有文档(例如“type:book AND name:dark”)。

    在您的示例中,这意味着搜索某本书,但将其章节作为搜索结果。

    这本身并不是很有用,但是您可以将它与对孩子的查询结合起来进行复杂的查询,例如“给我看名字中带有 'hunt' 的章节,这些章节在他们的名字中带有 'dark' ":

    # Find the children of books matching the book criterion
    all_parents = query.Term("type", "book")
    wanted_parents = query.Term("name", "dark")
    children_of_wanted_parents = query.NestedChildren(all_parents, wanted_parents)
    
    # Find the children matching the chapter criterion
    wanted_chapters = query.And([query.Term("type", "chapter"),
                                 query.Term("name", "hunted")])
    
    # The intersection of those two queries are the chapters we want
    complex_query = query.And([children_of_wanted_parents,
                               wanted_children])
    

    或者,至少,它应该是这样工作的。但是我刚刚在 NestedChildren 的 skip_to() 方法的实现中发现了一个错误,导致上面的示例无法正常工作:( :( :( 该错误现已在 Bitbucket 上修复,我将不得不发布一个新版本。

    干杯,

    马特

    【讨论】:

      猜你喜欢
      • 2019-12-05
      • 2020-08-30
      • 2018-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多