首页 > 解决方案 > 带有字段过滤器的多字段搜索嗖嗖声

问题描述

我正在使用 whoosh 来索引和搜索我的文档。我开发了一个 multi=field 搜索,但我想指定一些“必须”字段。

我想要的是:当我搜索带有查询 q1 的书时,它会搜索标题和摘要,但我想指定一些过滤器,如 autor='作者姓名'和 category=“书籍类别”。

结果必须考虑到两个“必须”字段并搜索另外两个。

谢谢您的帮助

标签: whoosh

解决方案


MultifieldParser您可以在这种情况下使用 whoosh

from whoosh.qparser import MultifieldParser

fields = ["title", "summary", "author", "category"]

query = MultifieldParser(fields, schema=idx.schema, group=qparser.OrGroup).parse(q1)
with idx.searcher() as searcher:
    results = searcher.search(query, limit=limit)
    ...........

以上使用 Or Group 将使用or运算符搜索所有字段。根据您的需要,您可以自定义它们。更多关于这里的运营商等and not


推荐阅读