Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Whoosh NestedChildren搜索未返回所有结果_Python_Full Text Search_Whoosh - Fatal编程技术网

Python Whoosh NestedChildren搜索未返回所有结果

Python Whoosh NestedChildren搜索未返回所有结果,python,full-text-search,whoosh,Python,Full Text Search,Whoosh,我正在制作一个搜索索引,它必须支持嵌套的数据层次结构。 出于测试目的,我制作了一个非常简单的模式: test\u schema=schema( 名称\u ngrams=NGRAMWORDS(minsize=4,field\u boost=1.2), 名称=文本(存储=真), id=id(唯一=True,存储=True), 类型=文本 ) 对于测试数据,我使用这些: 我已经向索引中添加了所有(5)个文档,如下所示 所以,为了获得一本书的所有章节,我做了一个函数,它使用NestedChildr

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

test\u schema=schema(
名称\u ngrams=NGRAMWORDS(minsize=4,field\u boost=1.2),
名称=文本(存储=真),
id=id(唯一=True,存储=True),
类型=文本
)
对于测试数据,我使用这些:

我已经向索引中添加了所有(5)个文档,如下所示

所以,为了获得一本书的所有章节,我做了一个函数,它使用NestedChildren搜索:

但对于我的测试数据,如果我搜索“暗刀”,我只能得到3个结果,而它必须是4个搜索结果

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

我知道所有的项目都在索引中,但我不知道我在这里遗漏了什么


有什么想法吗?

结果表明我使用NestedChildren是错误的。 以下是我从谷歌集团的Matt Chaput那里得到的答案:


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

NestedChildren的第二个参数不是您所认为的

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

关于嵌套子对象

(注意,我发现了一个bug,请参见结尾)

嵌套的孩子很难理解,但希望我能更好地解释它

NestedChildren是关于寻找特定的父母,但让他们的孩子成为热门

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

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

这本身并不是非常有用,但您可以将其与对子对象的查询结合起来,以执行复杂的查询,如“向我展示名称中带有“hunt”的章节,这些章节在名称中带有“dark”的书籍中”:

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

干杯

马特

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')
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']
        )
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
# 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])