qparser 模块¶
解析器对象¶
-
class
whoosh.qparser.QueryParser(fieldname, schema, plugins=None, termclass=<class 'whoosh.query.terms.Term'>, phraseclass=<class 'whoosh.query.positional.Phrase'>, group=<class 'whoosh.qparser.syntax.AndGroup'>)¶ 一种基于模块化插件的手写查询解析器。默认配置实现了与Lucene类似的强大的字段化查询语言。
你可以使用
plugins创建对象以重写插件的默认列表时的参数,和/或使用add_plugin()和/或remove_plugin_class()更改解析器中包含的插件。>>> from whoosh import qparser >>> parser = qparser.QueryParser("content", schema) >>> parser.remove_plugin_class(qparser.WildcardPlugin) >>> parser.add_plugin(qparser.PrefixPlugin()) >>> parser.parse(u"hello there") And([Term("content", u"hello"), Term("content", u"there")])
- 参数
fieldname -- 默认字段——解析器将此字段用作没有显式字段的任何术语的字段。
schema -- 一
whoosh.fields.Schema解析时要使用的对象。模式中的适当字段将用于在术语/短语转换为查询对象之前标记它们。您可以为模式指定none来创建不分析查询文本的解析器,通常用于测试目的。plugins -- 要使用的插件列表。WhitespacePlugin是自动包含的,不要放在这个列表中。这将覆盖插件的默认列表。列表中的类将自动实例化。
termclass -- 用于单个搜索词的查询类。默认值为
whoosh.query.Term.phraseclass -- 用于短语的查询类。默认值为
whoosh.query.Phrase.group -- 默认分组。
AndGroup默认情况下提供所需的条款。OrGroupmakes terms optional by default.
-
add_plugin(pin)¶ 将给定的插件添加到此分析器中的插件列表中。
-
add_plugins(pins)¶ 将给定的插件列表添加到此分析器的插件列表中。
-
default_set()¶ 返回要使用的插件的默认列表。
-
filterize(nodes, debug=False)¶ 获取一组节点并运行解析器插件提供的过滤器。
-
filters()¶ 返回由分析器当前配置的插件提供的筛选函数的优先级列表。
-
multitoken_query(spec, texts, fieldname, termclass, boost)¶ 返回对多个文本的查询。此方法实现字段中指定的意图
multitoken_query属性,它指定当解析器中看起来像单个术语的字符串在分析时生成多个标记时要做什么。- 参数
spec -- 描述如何将文本字符串联接到查询中的字符串。这通常是字段的值
multitoken_query属性。texts -- a list of token strings.
fieldname -- the name of the field.
termclass -- 用于单个术语的查询类。
boost -- 原始术语在查询字符串中的增强应该应用于返回的查询对象。
-
parse(text, normalize=True, debug=False)¶ 分析输入字符串并返回
whoosh.query.Query对象/树。- 参数
text -- 要分析的Unicode字符串。
normalize -- 是否在返回查询对象/树之前对其调用normalize()。除非您尝试调试解析器输出,否则应该保持此状态。
- 返回类型
-
process(text, pos=0, debug=False)¶ 返回与给定文本对应的一组语法节点,由插件标记器标记并由插件筛选器筛选。
- 参数
text -- 要标记的文本。
pos -- 文本中开始标记的位置。
-
remove_plugin(pi)¶ 从该分析器的插件列表中删除给定的插件对象。
-
remove_plugin_class(cls)¶ 从此分析器中删除给定类的任何插件。
-
replace_plugin(plugin)¶ 删除给定插件类的任何插件,然后添加它。这是一种避免调用的方便方法
remove_plugin_class然后add_plugin每次需要重新配置默认插件时。>>> qp = qparser.QueryParser("content", schema) >>> qp.replace_plugin(qparser.NotPlugin("(^| )-"))
-
tag(text, pos=0, debug=False)¶ Returns a group of syntax nodes corresponding to the given text, created by matching the Taggers provided by the parser's plugins.
- 参数
text -- 要标记的文本。
pos -- 文本中开始标记的位置。
-
taggers()¶ 返回由分析器当前配置的插件提供的标记器对象的优先级列表。
-
term_query(fieldname, text, termclass, boost=1.0, tokenize=True, removestops=True)¶ 为查询字符串中的单个术语返回适当的查询对象。
预制配置¶
以下函数返回预先配置的QueryParser对象。
-
whoosh.qparser.MultifieldParser(fieldnames, schema, fieldboosts=None, **kwargs)¶ 返回配置为在多个字段中搜索的QueryParser。
该解析器不将未屏蔽子句分配给默认字段,而是将它们转换为搜索字段列表的OR子句。例如,如果多字段列表为“f1”、“f2”,查询字符串为“hello there”,则类将解析“(f1:hello或f2:hello)(f1:there或f2:there)”。当您有两个文本字段(例如“标题”和“内容”)希望在默认情况下搜索时,这非常有用。
- 参数
fieldnames -- 要搜索的字段名列表。
fieldboosts -- 将字段名映射到提升的可选字典。
-
whoosh.qparser.SimpleParser(fieldname, schema, **kwargs)¶ 返回配置为仅支持+、-和短语语法的QueryParser。
-
whoosh.qparser.DisMaxParser(fieldboosts, schema, tiebreak=0.0, **kwargs)¶ 返回一个配置为仅支持+、-和短语语法的QueryParser,它将单个术语转换为跨一组字段的析取max查询。
- 参数
fieldboosts -- 将字段名映射到升序的字典。
插件¶
-
class
whoosh.qparser.Plugin¶ 分析器插件的基类。
-
filters(parser)¶ 应返回
(filter_function, priority)要添加到分析器的元组。优先级较低的数字优先。将使用调用筛选函数
(parser, groupnode)并且应该返回一个组节点。
-
taggers(parser)¶ 应返回
(Tagger, priority)要添加到解析器理解的语法中的元组。低优先级优先。
-
-
class
whoosh.qparser.SingleQuotePlugin(expr=None)¶ 通过将空格括在单引号中,添加指定包含空格的单个“术语”的功能。
-
class
whoosh.qparser.PrefixPlugin(expr=None)¶ 添加通过以星号结束术语来指定前缀查询的功能。
如果希望用户能够创建前缀而不是通配符查询(出于性能原因),则此插件非常有用。If you are including the wildcard plugin, you should not include this plugin as well.
>>> qp = qparser.QueryParser("content", myschema) >>> qp.remove_plugin_class(qparser.WildcardPlugin) >>> qp.add_plugin(qparser.PrefixPlugin()) >>> q = qp.parse("pre*")
-
class
whoosh.qparser.WildcardPlugin(expr=None)¶
-
class
whoosh.qparser.RegexPlugin(expr=None)¶ 添加指定正则表达式术语查询的功能。
正则表达式术语的默认语法是
r"termexpr".>>> qp = qparser.QueryParser("content", myschema) >>> qp.add_plugin(qparser.RegexPlugin()) >>> q = qp.parse('foo title:r"bar+"')
-
class
whoosh.qparser.BoostPlugin(expr=None)¶ 添加使用扬抑符提升查询子句的能力。
>>> qp = qparser.QueryParser("content", myschema) >>> q = qp.parse("hello there^2")
-
class
whoosh.qparser.GroupPlugin(openexpr='[(]', closeexpr='[)]')¶ 添加使用括号对子句分组的功能。
-
class
whoosh.qparser.EveryPlugin(expr=None)¶
-
class
whoosh.qparser.FieldsPlugin(expr='(?P<text>\w+|[*]):', remove_unknown=True)¶ 添加指定子句字段的功能。
- 参数
expr -- 用于标记字段的正则表达式。
remove_unknown -- 如果为true,则将不在架构中的字段的字段规范转换为常规文本。
-
class
whoosh.qparser.PhrasePlugin(expr='"(?P<text>.*?)"(~(?P<slop>[1-9][0-9]*))?')¶ 添加在双引号内指定短语查询的功能。
-
class
whoosh.qparser.RangePlugin(expr=None, excl_start='{', excl_end='}')¶ 添加指定术语范围的功能。
-
class
whoosh.qparser.OperatorsPlugin(ops=None, clean=False, And='(?<=\s)AND(?=\s)', Or='(?<=\s)OR(?=\s)', AndNot='(?<=\s)ANDNOT(?=\s)', AndMaybe='(?<=\s)ANDMAYBE(?=\s)', Not='(^|(?<=(\s|[()])))NOT(?=\s)', Require='(^|(?<=\s))REQUIRE(?=\s)')¶ 默认情况下,将and、or、and not、andmaybe和not运算符添加到解析器语法中。此插件扫描令牌流中的子类
Operator调用给他们Operator.make_group()方法来允许它们操作流。有两个级别的配置可用。
第一个级别是更改默认运算符的正则表达式,使用
And,Or,AndNot,AndMaybe和/或Notkeyword arguments. 关键字值可以是模式字符串或已编译表达式,也可以是无以删除运算符::qp = qparser.QueryParser("content", schema) cp = qparser.OperatorsPlugin(And="&", Or="\|", AndNot="&!", AndMaybe="&~", Not=None) qp.replace_plugin(cp)
您还可以指定
(OpTagger, priority)成对作为初始值设定项的第一个参数使用自定义运算符。见 创建自定义运算符 有关此的详细信息。
-
class
whoosh.qparser.PlusMinusPlugin(plusexpr='\+', minusexpr='-')¶ 添加在平面或查询中使用+和-的功能,以指定必需和禁止的术语。
这是返回的分析器配置的基础
SimpleParser().
-
class
whoosh.qparser.GtLtPlugin(expr=None)¶ Allows the user to use greater than/less than symbols to create range queries:
a:>100 b:<=z c:>=-1.4 d:<mz
这相当于:
a:{100 to] b:[to z] c:[-1.4 to] d:[to mz}
插件识别
>,<,>=,<=,=>和=<在字段说明符之后。需要字段说明符。您不能执行以下操作:>100
此插件需要FieldsPlugin和RangePlugin才能工作。
-
class
whoosh.qparser.MultifieldPlugin(fieldnames, fieldboosts=None, group=<class 'whoosh.qparser.syntax.OrGroup'>)¶ 将任何未筛选的字词转换为或子句,以便在指定的字段列表中搜索字词。
>>> qp = qparser.QueryParser(None, myschema) >>> qp.add_plugin(qparser.MultifieldPlugin(["a", "b"]) >>> qp.parse("alfa c:bravo") And([Or([Term("a", "alfa"), Term("b", "alfa")]), Term("c", "bravo")])
这个插件是
MultifieldParser.- 参数
fieldnames -- 要搜索的字段列表。
fieldboosts -- 一个可选的字典,将字段名映射到用于该字段的boost。
group -- 用于将字段化术语相互关联的组。
-
class
whoosh.qparser.FieldAliasPlugin(fieldmap)¶ 添加在查询字符串中使用字段“别名”的功能。
这个插件非常有用,它允许不能用ASCII表示的语言的用户使用自己语言中的字段名,并将它们转换为“真实”字段名,这些字段名必须是有效的Python标识符。
>>> # Allow users to use 'body' or 'text' to refer to the 'content' field >>> parser.add_plugin(FieldAliasPlugin({"content": ["body", "text"]})) >>> parser.parse("text:hello") Term("content", "hello")
-
class
whoosh.qparser.CopyFieldPlugin(map, group=<class 'whoosh.qparser.syntax.OrGroup'>, mirror=False)¶ 查找特定字段中出现的基本语法节点(术语、前缀、通配符、短语等),并将其替换为包含原始标记和复制到新字段的标记的组(默认情况下为或)。
例如,查询:
hello name:matt
可由自动转换
CopyFieldPlugin({{"name", "author"}})到:hello (name:matt OR author:matt)
当一个字段被另一个字段的不同分析副本编入索引,并且您希望查询搜索这两个字段时,这非常有用。
可以使用
group关键字。您还可以指定group=None, in which case the copied node is inserted "inline" next to the original, instead of in a new group:hello name:matt author:matt
- 参数
map -- 将字段名称映射到目标字段名称的字典。
group -- 要代替原始标记创建的组的类型。您可以指定
group=None将复制的节点“inline”放在原始节点旁边,而不是放在新组中。two_way -- 如果为true,则插件将双向复制,因此如果用户在“toname”字段中指定查询,则该查询将复制到“fromname”字段。
语法节点对象¶
基本节点¶
-
class
whoosh.qparser.SyntaxNode¶ 构成已分析用户查询字符串的抽象语法树(ast)的节点的基类。AST是一个中间步骤,由查询字符串生成,然后转换为
whoosh.query.Query树通过调用query()节点上的方法。实例具有以下必需的属性:
has_fieldname如果此节点具有
fieldname属性。has_text如果此节点具有
text属性has_boost如果此节点具有
boost属性。startchar原始文本中开始此节点的字符位置。
endchar原始文本中结束此节点的字符位置。
-
is_ws()¶ 如果此节点是可忽略的空白,则返回true。
-
query(parser)¶ 返回A
whoosh.query.Query与此语法树节点对应的实例。
-
r()¶ 返回此节点的基本表示形式。基类的
__repr__方法调用此函数,然后在适当的情况下添加FieldName和Boost来完成额外的繁忙工作。
-
set_boost(boost)¶ Sets the boost associated with this node.
For nodes that don't have a boost, this is a no-op.
-
set_fieldname(name, override=False)¶ 设置与此节点关联的字段名。如果
override为false(默认值),仅当此节点尚未设置字段名时,才会替换字段名。对于没有字段名的节点,这是禁止操作。
-
set_range(startchar, endchar)¶ 设置与此节点关联的字符范围。
结点¶
-
class
whoosh.qparser.FieldnameNode(fieldname, original)¶ 字段名分配的抽象语法树节点。
-
class
whoosh.qparser.TextNode(text)¶ 用于搜索文本的基本节点(如术语查询、通配符、前缀等)的中间基类。
实例具有以下属性:
qclass如果子类不重写
query(),基类将使用该类构造查询。tokenize如果为true且子类不重写
query(),在构造查询之前,将标记节点的文本。removestops如果为true且子类不重写
query(),并且字段的分析器有一个停止字筛选器,在构造查询之前,将从文本中删除停止字。
-
class
whoosh.qparser.WordNode(text)¶ 术语查询的语法节点。
-
class
whoosh.qparser.RangeNode(start, end, startexcl, endexcl)¶ 范围查询的语法节点。
-
class
whoosh.qparser.MarkerNode¶ 只存在于树中标记位置的节点的基类。
群节点¶
-
class
whoosh.qparser.GroupNode(nodes=None, boost=1.0, **kwargs)¶ 将子节点组合在一起的抽象语法树节点类型的基类。
实例具有以下属性:
merging如果可以将此组的并行实例合并到单个组中,则为true。
qclass如果子类不重写
query()基类将简单地围绕子节点返回的查询包装该类。
此类实现了许多用于在子节点上操作的列表方法。
-
class
whoosh.qparser.BinaryGroup(nodes=None, boost=1.0, **kwargs)¶ 具有两个子节点和其子节点的组节点的中间基类
qclass初始值设定项采用两个参数而不是一个列表。
-
class
whoosh.qparser.ErrorNode(message, node=None)¶
-
class
whoosh.qparser.AndGroup(nodes=None, boost=1.0, **kwargs)¶
-
class
whoosh.qparser.OrGroup(nodes=None, boost=1.0, **kwargs)¶
-
class
whoosh.qparser.AndNotGroup(nodes=None, boost=1.0, **kwargs)¶
-
class
whoosh.qparser.AndMaybeGroup(nodes=None, boost=1.0, **kwargs)¶
-
class
whoosh.qparser.DisMaxGroup(nodes=None, boost=1.0, **kwargs)¶
-
class
whoosh.qparser.RequireGroup(nodes=None, boost=1.0, **kwargs)¶
-
class
whoosh.qparser.NotGroup(nodes=None, boost=1.0, **kwargs)¶
算子¶
-
class
whoosh.qparser.Operator(text, grouptype, leftassoc=True)¶ PrefixOperator、PostfixOperator和InfixOperator的基类。
运算符的工作方式是将它们应用到的节点(例如,对于前缀运算符、上一个节点、对于中缀运算符、任一侧的节点等)移动到一个组节点中。该组提供了如何处理节点的代码。
- 参数
text -- 查询字符串中运算符的文本。
grouptype -- 要创建的组的类型,以代替运算符及其操作的节点。
leftassoc -- 对于中缀运算符,该运算符是否是左关联的。使用
leftassoc=False用于右关联中缀运算符。
-
class
whoosh.qparser.PrefixOperator(text, grouptype, leftassoc=True)¶ - 参数
text -- 查询字符串中运算符的文本。
grouptype -- 要创建的组的类型,以代替运算符及其操作的节点。
leftassoc -- 对于中缀运算符,该运算符是否是左关联的。使用
leftassoc=False用于右关联中缀运算符。
-
class
whoosh.qparser.PostfixOperator(text, grouptype, leftassoc=True)¶ - 参数
text -- 查询字符串中运算符的文本。
grouptype -- 要创建的组的类型,以代替运算符及其操作的节点。
leftassoc -- 对于中缀运算符,该运算符是否是左关联的。使用
leftassoc=False用于右关联中缀运算符。
-
class
whoosh.qparser.InfixOperator(text, grouptype, leftassoc=True)¶ - 参数
text -- 查询字符串中运算符的文本。
grouptype -- 要创建的组的类型,以代替运算符及其操作的节点。
leftassoc -- 对于中缀运算符,该运算符是否是左关联的。使用
leftassoc=False用于右关联中缀运算符。