query 模块

也见 whoosh.qparser 其中包含将用户查询解析为查询对象的代码。

基类

以下抽象基类被子类化以创建“real”查询操作。

class whoosh.query.Query

所有查询的抽象基类。

请注意,这个基类实现了uu或uuuu、uu和uuu,以及uuu sub_uuu,以便稍微更方便地组合查询对象:

>>> Term("content", u"a") | Term("content", u"b")
Or([Term("content", u"a"), Term("content", u"b")])

>>> Term("content", u"a") & Term("content", u"b")
And([Term("content", u"a"), Term("content", u"b")])

>>> Term("content", u"a") - Term("content", u"b")
And([Term("content", u"a"), Not(Term("content", u"b"))])
accept(fn)

将给定函数应用于此查询的子查询(如果有),然后应用于此查询本身:

def boost_phrases(q):
    if isintance(q, Phrase):
        q.boost *= 2.0
    return q

myquery = myquery.accept(boost_phrases)

This method automatically creates copies of the nodes in the original tree before passing them to your function, so your function can change attributes on nodes without altering the original tree.

这种方法比使用 Query.apply() (实际上,它是使用该方法实现的),但通常更简单。

all_terms(phrases=True)

返回此查询树中所有术语的集合。

此方法的存在是为了向后兼容。使用iter_all_terms()代替。

参数

phrases -- 是否添加在短语查询中找到的单词。

返回类型

set

all_tokens(boost=1.0)

返回的迭代器 analysis.Token 与此查询树中的所有术语对应的对象。令牌对象将具有 fieldnametextboost 属性集。如果查询是由查询分析器生成的,则它们标记对象也将具有 startcharendchar 属性索引到原始用户查询中。

apply(fn)

如果此查询有子级,则对每个子级调用给定函数,并返回此节点的新副本以及函数返回的新子级。如果这是一个叶节点,只需返回这个对象。

这对于编写转换查询树的函数很有用。例如,此函数将查询树中的所有术语对象更改为变体对象:

def term2var(q):
    if isinstance(q, Term):
        return Variations(q.fieldname, q.text)
    else:
        return q.apply(term2var)

q = And([Term("f", "alfa"),
         Or([Term("f", "bravo"),
             Not(Term("f", "charlie"))])])
q = term2var(q)

请注意,此方法不会自动创建节点的副本。为了避免修改原始树,函数应调用 Query.copy() 方法,然后更改节点的属性。

children()

返回此对象的子查询的迭代器。

copy()

已弃用,只需使用 copy.deepcopy .

deletion_docs(searcher)

返回与此查询匹配的docnum的迭代器以进行删除。这个 delete_by_query() 方法将在决定要删除哪些文档时使用此方法,允许特殊查询(例如嵌套查询)覆盖要删除的文档。默认实现只是转发到 Query.docs() .

docs(searcher)

返回与此查询匹配的docnum的迭代器。

>>> with my_index.searcher() as searcher:
...     list(my_query.docs(searcher))
[10, 34, 78, 103]
参数

searcher -- A whoosh.searching.Searcher 对象。

estimate_min_size(ixreader)

返回此查询可能匹配的最少文档数的估计值。

estimate_size(ixreader)

返回此查询可能匹配的文档数的估计值(例如,简单术语查询的估计大小是术语的文档频率)。可以高估,但不能低估。

existing_terms(ixreader, phrases=True, expand=False, fieldname=None)

返回此查询树中存在于给定IxReader中的一组所有字节。

参数
  • ixreader -- A whoosh.reading.IndexReader 对象。

  • phrases -- 是否添加在短语查询中找到的单词。

  • expand -- 如果为true,则匹配多个术语的查询将返回所有匹配的扩展。

返回类型

set

field()

返回此查询匹配的字段,如果此查询在单个字段中不匹配,则返回“无”。

has_terms()

如果此特定对象表示搜索特定术语(而不是模式,如通配符和前缀)或术语(即 replace() 方法对此实例执行了一些有意义的操作)。

is_leaf()

如果这是查询树中的叶节点,则返回true;如果此查询具有子查询,则返回false。

is_range()

如果此对象搜索范围内的值,则返回true。

iter_all_terms(phrases=True)

返回此查询树中所有术语的(fieldname,text)对迭代器。

>>> qp = qparser.QueryParser("text", myindex.schema)
>>> q = myparser.parse("alfa bravo title:charlie")
>>> # List the terms in a query
>>> list(q.iter_all_terms())
[("text", "alfa"), ("text", "bravo"), ("title", "charlie")]
>>> # Get a set of all terms in the query that don't exist in the index
>>> r = myindex.reader()
>>> missing = set(t for t in q.iter_all_terms() if t not in r)
set([("text", "alfa"), ("title", "charlie")])
>>> # All terms in the query that occur in fewer than 5 documents in
>>> # the index
>>> [t for t in q.iter_all_terms() if r.doc_frequency(t[0], t[1]) < 5]
[("title", "charlie")]
参数

phrases -- 是否添加在短语查询中找到的单词。

leaves()

以平序列形式返回此查询树中所有叶查询的迭代器。

matcher(searcher, context=None)

返回A Matcher 对象,可用于检索与此查询匹配的文档和分数。

返回类型

whoosh.matching.Matcher

normalize()

返回此查询的递归“规范化”形式。规范化表单删除冗余和空查询。这是在查询分析器创建的查询树上自动调用的,但如果您正在编写自己的分析器或构建自己的查询,则可能需要自己调用它。

>>> q = And([And([Term("f", u"a"),
...               Term("f", u"b")]),
...               Term("f", u"c"), Or([])])
>>> q.normalize()
And([Term("f", u"a"), Term("f", u"b"), Term("f", u"c")])

注意,这将返回 new, normalized 查询。它 does not 修改原始查询“就地”。

replace(fieldname, oldtext, newtext)

返回此查询的副本,其中oldtext被newtext替换(如果oldtext在此查询中的任何位置)。

注意,这将返回 new 替换给定文本的查询。它 does not 修改原始查询“就地”。

requires()

返回一组查询 known 必须匹配才能匹配整个查询。请注意,其他查询也可能是必需的,但不能通过检查静态查询来确定。

>>> a = Term("f", u"a")
>>> b = Term("f", u"b")
>>> And([a, b]).requires()
set([Term("f", u"a"), Term("f", u"b")])
>>> Or([a, b]).requires()
set([])
>>> AndMaybe(a, b).requires()
set([Term("f", u"a")])
>>> a.requires()
set([Term("f", u"a")])
simplify(ixreader)

返回此查询的递归简化形式,其中“二阶”查询(如前缀和变体)重新写入低级查询(如术语和或)。

terms(phrases=False)

生成此对象查询的零对或多对(fieldname,text)。You can check whether a query object targets specific terms before you call this method using Query.has_terms() .

要获取查询树中的所有术语,请使用 Query.iter_all_terms() .

tokens(boost=1.0, exreader=None)

产生零或更多 analysis.Token 与此查询对象搜索的术语相对应的对象。You can check whether a query object targets specific terms before you call this method using Query.has_terms() .

令牌对象将具有 fieldnametextboost 属性集。如果查询是由查询分析器生成的,则它们标记对象也将具有 startcharendchar 属性索引到原始用户查询中。

要获取查询树的所有标记,请使用 Query.all_tokens() .

参数

exreader -- 用于扩展多个术语查询(如前缀和通配符)的读卡器。默认值为无,表示不展开。

with_boost(boost)

返回此查询的副本,并将boost设置为给定值。

如果查询类型本身不接受Boost,它将尝试将Boost传递给它的子级(如果有的话)。

class whoosh.query.CompoundQuery(subqueries, boost=1.0)

用于合并或操作多个子查询结果的查询的抽象基类。

class whoosh.query.MultiTerm

对同一字段中的多个术语进行操作的查询的抽象基类。

class whoosh.query.ExpandingTerm

用于查询(如fuzzyTerm和变量)的中间基类,这些查询扩展为多个查询,但来自一个术语。

class whoosh.query.WrappingQuery(child)

查询类

class whoosh.query.Term(fieldname, text, boost=1.0, minquality=None)

匹配包含给定术语(fieldname+文本对)的文档。

>>> Term("content", u"render")
class whoosh.query.Variations(fieldname, text, boost=1.0)

自动在同一字段中搜索给定单词的形态变化的查询。

class whoosh.query.FuzzyTerm(fieldname, text, boost=1.0, maxdist=1, prefixlength=1, constantscore=True)

匹配包含与给定术语类似的词的文档。

参数
  • fieldname -- 要搜索的字段的名称。

  • text -- 要搜索的文本。

  • boost -- 用于与此查询匹配的多个文档的增强因子。

  • maxdist -- 与给定文本的最大编辑距离。

  • prefixlength -- 匹配的术语必须与“text”共享这许多初始字符。例如,如果文本为“light”,前缀长度为2,则仅检查以“li”开头的术语的相似性。

class whoosh.query.Phrase(fieldname, words, slop=1, boost=1.0, char_ranges=None)

匹配包含给定短语的文档。

参数
  • fieldname -- 要搜索的字段。

  • words -- a list of words (unicode strings) in the phrase.

  • slop -- 短语中每个“单词”之间允许的单词数;默认值1表示短语必须完全匹配。

  • boost -- 应用于与此查询匹配的文档的原始分数的增强因子。

  • char_ranges -- 如果查询分析器创建了一个短语对象,它会将此属性设置为与短语中的单词对应的(startchar、endchar)对列表。

class whoosh.query.And(subqueries, boost=1.0)

匹配匹配所有子查询的文档。

>>> And([Term("content", u"render"),
...      Term("content", u"shade"),
...      Not(Term("content", u"texture"))])
>>> # You can also do this
>>> Term("content", u"render") & Term("content", u"shade")
class whoosh.query.Or(subqueries, boost=1.0, minmatch=0, scale=None)

匹配匹配任何子查询的文档。

>>> Or([Term("content", u"render"),
...     And([Term("content", u"shade"), Term("content", u"texture")]),
...     Not(Term("content", u"network"))])
>>> # You can also do this
>>> Term("content", u"render") | Term("content", u"shade")
参数
  • subqueries -- 一览表 Query 要搜索的对象。

  • boost -- 用于所有匹配文档分数的增强因子。

  • minmatch -- 尚未实施。

  • scale -- “协调奖金”的比例系数。如果该值不是“无”,则它应该是大于0小于1的浮点数。匹配文档的分数根据文档中匹配的查询词数进行提升/惩罚。这个数字衡量奖金的效果。

class whoosh.query.DisjunctionMax(subqueries, boost=1.0, tiebreak=0.0)

匹配所有匹配任何子查询的文档,但使用子查询的最大分数对每个文档进行评分。

class whoosh.query.Not(query, boost=1.0)

排除与子查询匹配的任何文档。

>>> # Match documents that contain 'render' but not 'texture'
>>> And([Term("content", u"render"),
...      Not(Term("content", u"texture"))])
>>> # You can also do this
>>> Term("content", u"render") - Term("content", u"texture")
参数
  • query -- A Query 对象。The results of this query are excluded 来自父查询。

  • boost -- Boost对于排除的文档没有意义,但是为了一致的接口,这个关键字参数被接受了。

class whoosh.query.Prefix(fieldname, text, boost=1.0, constantscore=True)

匹配包含以给定文本开头的任何术语的文档。

>>> # Match documents containing words starting with 'comp'
>>> Prefix("content", u"comp")
class whoosh.query.Wildcard(fieldname, text, boost=1.0, constantscore=True)

匹配包含与“glob”模式匹配的任何术语的文档。看 Python fnmatch 有关globs的信息模块。

>>> Wildcard("content", u"in*f?x")
class whoosh.query.Regex(fieldname, text, boost=1.0, constantscore=True)

匹配包含与正则表达式匹配的任何术语的文档。看 Python re 有关正则表达式的信息模块。

class whoosh.query.TermRange(fieldname, start, end, startexcl=False, endexcl=False, boost=1.0, constantscore=True)

匹配包含给定范围内任何术语的文档。

>>> # Match documents where the indexed "id" field is greater than or equal
>>> # to 'apple' and less than or equal to 'pear'.
>>> TermRange("id", u"apple", u"pear")
参数
  • fieldname -- 要搜索的字段的名称。

  • start -- 匹配等于或大于此的项。

  • end -- Match terms equal to or less than this.

  • startexcl -- 如果为真,则范围开始是独占的。如果为false,则范围开始是包含的。

  • endexcl -- 如果为真,则范围结束是独占的。如果为false,则范围结束是包含的。

  • boost -- 应应用于与此查询匹配的结果的原始分数的增强因子。

class whoosh.query.NumericRange(fieldname, start, end, startexcl=False, endexcl=False, boost=1.0, constantscore=True)

数值字段的范围查询。利用分层索引,通过在边缘的高分辨率和中间的低分辨率匹配来加快大范围。

>>> # Match numbers from 10 to 5925 in the "number" field.
>>> nr = NumericRange("number", 10, 5925)
参数
  • fieldname -- 要搜索的字段的名称。

  • start -- 匹配等于或大于此数字的术语。这应该是数字类型,而不是字符串。

  • end -- 匹配等于或小于此数字的术语。这应该是数字类型,而不是字符串。

  • startexcl -- 如果为真,则范围开始是独占的。如果为false,则范围开始是包含的。

  • endexcl -- 如果为真,则范围结束是独占的。如果为false,则范围结束是包含的。

  • boost -- 应应用于与此查询匹配的结果的原始分数的增强因子。

  • constantscore -- 如果为true,则编译后的查询返回一个常量分数( boost 关键字参数)而不是对匹配项进行实际评分。这可以提高速度,而且在大多数情况下不会影响结果,因为数字范围几乎总是用作过滤器。

class whoosh.query.DateRange(fieldname, start, end, startexcl=False, endexcl=False, boost=1.0, constantscore=True)

这是一个非常细的子类 NumericRange that only overrides the initializer and __repr__() 方法来处理日期时间对象而不是数字。在内部,此对象将用其创建的日期时间对象转换为数字,否则其行为类似于 NumericRange 查询。

>>> DateRange("date", datetime(2010, 11, 3, 3, 0),
...           datetime(2010, 11, 3, 17, 59))
class whoosh.query.Every(fieldname=None, boost=1.0)

匹配包含给定字段中任何术语的每个文档的查询。如果不指定字段,则查询将匹配每个文档。

>>> # Match any documents with something in the "path" field
>>> q = Every("path")
>>> # Matcher every document
>>> q = Every()

未屏蔽的表单(匹配每个文档)是有效的。

The fielded is more efficient than a prefix query with an empty prefix or a '*' wildcard, but it can still be very slow on large indexes. 它要求搜索者阅读给定字段中每个术语的完整发布列表。

创建索引以包含出现在所有具有要匹配字段的文档中的单个术语,而不是使用此查询,这样效率更高。

例如,而不是:

# Match all documents that have something in the "path" field
q = Every("path")

索引时执行此操作:

# Add an extra field that indicates whether a document has a path
schema = fields.Schema(path=fields.ID, has_path=fields.ID)

# When indexing, set the "has_path" field based on whether the document
# has anything in the "path" field
writer.add_document(text=text_value1)
writer.add_document(text=text_value2, path=path_value2, has_path="t")

然后查找具有以下路径的所有文档:

q = Term("has_path", "t")
参数

fieldname -- 要匹配的字段的名称,或 None* 匹配所有文档。

whoosh.query.NullQuery

二进制查询

class whoosh.query.Require(a, b)

binary query返回第一个查询(也出现在第二个查询中)的结果,但只使用第一个查询的得分。这样可以过滤结果而不影响分数。

class whoosh.query.AndMaybe(a, b)

二进制查询从第一个查询中获取结果。如果并且仅当第二个查询的结果中也出现同一文档时,第二个查询的分数将添加到第一个查询的分数中。

class whoosh.query.AndNot(a, b)

“a而不是b”形式的二进制布尔查询,其中匹配b的文档将从a的匹配项中删除。

class whoosh.query.Otherwise(a, b)

仅在第一个子句与任何文档都不匹配时才与第二个子句匹配的二进制查询。

跨度查询

class whoosh.query.Span(start, end=None, startchar=None, endchar=None, boost=1.0)
classmethod merge(spans)

合并给定跨度列表中的重叠和接触跨度。

请注意,这会修改原始列表。

>>> spans = [Span(1,2), Span(3)]
>>> Span.merge(spans)
>>> spans
[<1-3>]
class whoosh.query.SpanQuery

基于跨度的查询的抽象基类。每个SPAN查询类型包装一个实现基本文档匹配功能的“常规”查询(例如,SPANAR包装一个和查询,因为SPANAR要求两个子查询发生在同一文档中。打包的查询存储在 q 属性。

子类通常只需要实现初始值设定项来设置包装的查询,并且 matcher() 返回一个支持范围的匹配器对象。

class whoosh.query.SpanFirst(q, limit=0)

匹配在前n个位置内结束的跨度。例如,您可以只匹配文档开头附近的术语。

参数
  • q -- 要匹配的查询。

  • limit -- 查询必须在文档开头的该位置内匹配。默认值为 0 ,这意味着查询必须在第一个位置匹配。

class whoosh.query.SpanNear(a, b, slop=1, ordered=True, mindist=1)

注意:对于新代码,请使用 SpanNear2 而不是这个类。扳手2接受子查询列表,而不是要求您创建查询对象的二进制树。

匹配彼此附近发生的查询。默认情况下,只匹配彼此相邻(slop=1)并按顺序(ordered=true)发生的查询。

例如,要查找“text”字段中“library”旁边出现“whoosh”的文档:

from whoosh import query, spans
t1 = query.Term("text", "whoosh")
t2 = query.Term("text", "library")
q = spans.SpanNear(t1, t2)

查找“库”前最多5个位置出现“喘息”的文档:

q = spans.SpanNear(t1, t2, slop=5)

查找“库”前后最多5个位置出现“喘息”的文档:

q = spans.SpanNear(t1, t2, slop=5, ordered=False)

你可以使用 phrase() 类方法来创建一个范围查询树以匹配术语列表:

q = spans.SpanNear.phrase("text", ["whoosh", "search", "library"],
                          slop=2)
参数
  • a -- 要匹配的第一个查询。

  • b -- 第二个查询必须出现在第一个查询的“slop”位置中。

  • slop -- 查询必须发生的位置数。默认值为1,表示查询必须紧挨着发生。

  • ordered -- a是否必须出现在b之前。默认值为true。

童车

查询之间允许的最小距离。

class whoosh.query.SpanNear2(qs, slop=1, ordered=True, mindist=1)

匹配彼此附近发生的查询。默认情况下,只匹配彼此相邻(slop=1)并按顺序(ordered=true)发生的查询。

新代码应使用此查询类型而不是 SpanNear .

(不像 SpanNear ,此查询采用子查询列表,而不是要求您构建查询对象的二进制树。由于开销较小,此查询也应该稍微快一点。)

例如,要查找“text”字段中“library”旁边出现“whoosh”的文档:

from whoosh import query, spans
t1 = query.Term("text", "whoosh")
t2 = query.Term("text", "library")
q = spans.SpanNear2([t1, t2])

查找“库”前最多5个位置出现“喘息”的文档:

q = spans.SpanNear2([t1, t2], slop=5)

查找“库”前后最多5个位置出现“喘息”的文档:

q = spans.SpanNear2(t1, t2, slop=5, ordered=False)
参数
  • qs -- 要匹配的子查询序列。

  • slop -- 查询必须发生的位置数。默认值为1,表示查询必须紧挨着发生。

  • ordered -- a是否必须出现在b之前。默认值为true。

童车

查询之间允许的最小距离。

class whoosh.query.SpanNot(a, b)

仅当第一个查询的跨度与第二个查询的跨度不重叠时,才匹配第一个查询的跨度。如果没有不重叠的跨度,则文档不匹配。

例如,要匹配“文本”字段中“apple”后最多2个位置包含“bear”的文档,但它们之间没有“可爱的”::

from whoosh import query, spans
t1 = query.Term("text", "apple")
t2 = query.Term("text", "bear")
near = spans.SpanNear(t1, t2, slop=2)
q = spans.SpanNot(near, query.Term("text", "cute"))
参数
  • a -- 要匹配的查询。

  • b -- 不匹配与此查询中的跨度重叠的任何跨度。

class whoosh.query.SpanOr(subqs)

匹配匹配任何子查询列表的文档。与query.or不同,当不同的子查询重叠时,此类将它们的匹配跨度合并在一起。

参数

subqs -- 要匹配的查询列表。

class whoosh.query.SpanContains(a, b)

匹配文档,其中第一个查询的跨度包含第二个查询的任何跨度。

例如,要匹配文档,其中“apple”最多出现在“text”字段中“bear”之前的10个位置,“cute”介于它们之间:

from whoosh import query, spans
t1 = query.Term("text", "apple")
t2 = query.Term("text", "bear")
near = spans.SpanNear(t1, t2, slop=10)
q = spans.SpanContains(near, query.Term("text", "cute"))
参数
  • a -- 要匹配的查询。

  • b -- 其跨距必须出现在第一个查询的匹配跨距内的查询。

class whoosh.query.SpanBefore(a, b)

匹配第一个查询的跨度出现在第二个查询的任何跨度之前的文档。

例如,要匹配“apple”出现在“bear”之前的任何位置的文档:

from whoosh import query, spans
t1 = query.Term("text", "apple")
t2 = query.Term("text", "bear")
q = spans.SpanBefore(t1, t2)
参数
  • a -- the query that must occur before the second.

  • b -- 必须在第一个查询之后发生的查询。

class whoosh.query.SpanCondition(a, b)

Matches documents that satisfy both subqueries, but only uses the spans from the first subquery.

当您希望在匹配项上放置条件,但不希望这些条件影响返回的跨度时,这非常有用。

例如,获取术语的跨度 alfa 在还必须包含该术语的文档中 bravo ::

SpanCondition(Term("text", u"alfa"), Term("text", u"bravo"))

特殊查询

class whoosh.query.NestedParent(parents, subq, per_parent_limit=None, score_fn=<built-in function sum>)

允许您搜索“嵌套”文档的查询,可以使用 group() 和/或 start_group() A方法 whoosh.writing.IndexWriter 表示层次结构相关的文档应放在一起:

schema = fields.Schema(type=fields.ID, text=fields.TEXT(stored=True))

with ix.writer() as w:
    # Say we're indexing chapters (type=chap) and each chapter has a
    # number of paragraphs (type=p)
    with w.group():
        w.add_document(type="chap", text="Chapter 1")
        w.add_document(type="p", text="Able baker")
        w.add_document(type="p", text="Bright morning")
    with w.group():
        w.add_document(type="chap", text="Chapter 2")
        w.add_document(type="p", text="Car trip")
        w.add_document(type="p", text="Dog eared")
        w.add_document(type="p", text="Every day")
    with w.group():
        w.add_document(type="chap", text="Chapter 3")
        w.add_document(type="p", text="Fine day")

这个 NestedParent 查询包含两个子查询:“父查询”与“父文档”类匹配。“子查询”与要查找的嵌套文档匹配。对于“子文档”,“子查询”找到的每个“子文档”,此查询就像找到了相应的“父文档”。

>>> with ix.searcher() as s:
...   r = s.search(query.Term("text", "day"))
...   for hit in r:
...     print(hit["text"])
...
Chapter 2
Chapter 3
参数
  • parents -- a query, DocIdSet object, or Results object representing the documents you want to use as the "parent" documents. 如果子查询匹配,则这些结果中的相应文档将作为匹配返回。

  • subq -- 与要查找的信息匹配的查询。

  • per_parent_limit -- 每个父级最多可搜索“子文档”。默认值为无,表示无限制。

  • score_fn -- 用于组合匹配子文档的分数以计算父文档返回的分数的函数。默认值为 sum 也就是说,将子文档的分数相加。

class whoosh.query.NestedChildren(parents, subq, boost=1.0)

这是一个 NestedParent 查询:该查询不采用与子项匹配但返回父项的查询,而是与父项匹配但返回子项。

例如,搜索专辑标题并返回专辑中的歌曲时,这很有用:

schema = fields.Schema(type=fields.ID(stored=True),
                       album_name=fields.TEXT(stored=True),
                       track_num=fields.NUMERIC(stored=True),
                       track_name=fields.TEXT(stored=True),
                       lyrics=fields.TEXT)
ix = RamStorage().create_index(schema)

# Indexing
with ix.writer() as w:
    # For each album, index a "group" of a parent "album" document and
    # multiple child "track" documents.
    with w.group():
        w.add_document(type="album",
                       artist="The Cure", album_name="Disintegration")
        w.add_document(type="track", track_num=1,
                       track_name="Plainsong")
        w.add_document(type="track", track_num=2,
                       track_name="Pictures of You")
        # ...
    # ...


# Find songs where the song name has "heaven" in the title and the
# album the song is on has "hell" in the title
qp = QueryParser("lyrics", ix.schema)
with ix.searcher() as s:
    # A query that matches all parents
    all_albums = qp.parse("type:album")

    # A query that matches the parents we want
    albums_with_hell = qp.parse("album_name:hell")

    # A query that matches the desired albums but returns the tracks
    songs_on_hell_albums = NestedChildren(all_albums, albums_with_hell)

    # A query that matches tracks with heaven in the title
    songs_with_heaven = qp.parse("track_name:heaven")

    # A query that finds tracks with heaven in the title on albums
    # with hell in the title
    q = query.And([songs_on_hell_albums, songs_with_heaven])
class whoosh.query.ConstantScoreQuery(child, score=1.0)

包装查询并使用匹配器,该匹配器始终为所有匹配的文档提供恒定的分数。当您不关心查询树某个分支的得分时,这是一个有用的优化,因为它只是作为一个过滤器。也见 AndMaybe 查询。

例外情况

exception whoosh.query.QueryError

运行查询时遇到错误。