匹配查询
在特定文档字段上使用 match
查询进行全文搜索。如果您在 text
字段上运行 match
查询,match
查询会分析提供的搜索字符串,并返回与字符串中任何词元匹配的文档。如果您在精确值字段上运行 match
查询,它会返回与精确值匹配的文档。搜索精确值字段的首选方法是使用过滤器,因为与查询不同,过滤器会被缓存。
以下示例显示了在 title
字段中搜索单词 wind
的基本 match
查询。
GET _search
{
"query": {
"match": {
"title": "wind"
}
}
}
要传递附加参数,可以使用扩展语法
GET _search
{
"query": {
"match": {
"title": {
"query": "wind",
"analyzer": "stop"
}
}
}
}
示例
在以下示例中,您将使用包含以下文档的索引:
PUT testindex/_doc/1
{
"title": "Let the wind rise"
}
PUT testindex/_doc/2
{
"title": "Gone with the wind"
}
PUT testindex/_doc/3
{
"title": "Rise is gone"
}
运算符
如果在 text
字段上运行 match
查询,文本将使用 analyzer
参数中指定的分析器进行分析。然后,生成的词元将使用 operator
参数中指定的操作符组合成一个布尔查询。默认操作符是 OR
,因此查询 wind rise
会变为 wind OR rise
。在此示例中,此查询会返回文档 1-3,因为每个文档都有一个与查询匹配的词元。要指定 and
操作符,请使用以下查询:
GET testindex/_search
{
"query": {
"match": {
"title": {
"query": "wind rise",
"operator": "and"
}
}
}
}
查询构造为 wind AND rise
,并返回文档 1 作为匹配文档。
响应
{
"took": 17,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.2667098,
"hits": [
{
"_index": "testindex",
"_id": "1",
"_score": 1.2667098,
"_source": {
"title": "Let the wind rise"
}
}
]
}
}
最小匹配度
您可以通过指定 minimum_should_match
参数来控制文档必须匹配的最小词元数,以便在结果中返回。
GET testindex/_search
{
"query": {
"match": {
"title": {
"query": "wind rise",
"operator": "or",
"minimum_should_match": 2
}
}
}
}
现在文档必须匹配两个词元,因此只返回文档 1(这相当于 and
操作符)。
响应
{
"took": 23,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.2667098,
"hits": [
{
"_index": "testindex",
"_id": "1",
"_score": 1.2667098,
"_source": {
"title": "Let the wind rise"
}
}
]
}
}
分析器
因为在此示例中您没有明确指定分析器,所以使用了默认的 standard
分析器。默认分析器不执行词干提取,因此如果您运行查询 the wind rises
,您将不会收到任何结果,因为词元 rises
不匹配词元 rise
。要更改搜索分析器,请在 analyzer
字段中指定它。例如,以下查询使用 english
分析器:
GET testindex/_search
{
"query": {
"match": {
"title": {
"query": "the wind rises",
"operator": "and",
"analyzer": "english"
}
}
}
}
english
分析器会删除停用词 the
并执行词干提取,生成词元 wind
和 rise
。后一个词元与文档 1 匹配,该文档在结果中返回。
响应
{
"took": 19,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.2667098,
"hits": [
{
"_index": "testindex",
"_id": "1",
"_score": 1.2667098,
"_source": {
"title": "Let the wind rise"
}
}
]
}
}
空查询
在某些情况下,分析器可能会从查询中删除所有词元。例如,english
分析器会删除停用词,因此在查询 and OR or
中,所有词元都被删除。要检查分析器行为,您可以使用 Analyze API。
GET testindex/_analyze
{
"analyzer" : "english",
"text" : "and OR or"
}
如预期,查询未产生任何词元。
{
"tokens": []
}
您可以在 zero_terms_query
参数中指定空查询的行为。zero_terms_query
设置为 all
会返回索引中的所有文档,设置为 none
则不返回任何文档。
GET testindex/_search
{
"query": {
"match": {
"title": {
"query": "and OR or",
"analyzer" : "english",
"zero_terms_query": "all"
}
}
}
}
模糊性
为了处理拼写错误,您可以将查询的 fuzziness
指定为以下任意一种情况:
- 一个整数,指定此编辑允许的最大 Damerau–Levenshtein 距离。
AUTO
:- 0-2 个字符的字符串必须精确匹配。
- 3-5 个字符的字符串允许 1 次编辑。
- 超过 5 个字符的字符串允许 2 次编辑。
在大多数情况下,将 fuzziness
设置为默认值 AUTO
效果最佳。
GET testindex/_search
{
"query": {
"match": {
"title": {
"query": "wnid",
"fuzziness": "AUTO"
}
}
}
}
词元 wnid
匹配 wind
,查询返回文档 1 和 2。
响应
{
"took": 31,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.47501624,
"hits": [
{
"_index": "testindex",
"_id": "1",
"_score": 0.47501624,
"_source": {
"title": "Let the wind rise"
}
},
{
"_index": "testindex",
"_id": "2",
"_score": 0.47501624,
"_source": {
"title": "Gone with the wind"
}
}
]
}
}
前缀长度
拼写错误很少发生在单词的开头。因此,您可以指定匹配前缀的最小长度,以便在结果中返回文档。例如,您可以更改前面的查询以包含 prefix_length
。
GET testindex/_search
{
"query": {
"match": {
"title": {
"query": "wnid",
"fuzziness": "AUTO",
"prefix_length": 2
}
}
}
}
前面的查询不返回任何结果。如果您将 prefix_length
更改为 1,则会返回文档 1 和 2,因为词元 wnid
的第一个字母没有拼写错误。
换位
在前面的示例中,单词 wnid
包含换位(in
变为 ni
)。默认情况下,模糊匹配允许换位,但您可以通过将 fuzzy_transpositions
设置为 false
来禁止它们。
GET testindex/_search
{
"query": {
"match": {
"title": {
"query": "wnid",
"fuzziness": "AUTO",
"fuzzy_transpositions": false
}
}
}
}
现在查询不返回任何结果。
同义词
如果您使用 synonym_graph
过滤器,并且 auto_generate_synonyms_phrase_query
设置为 true
(默认值),OpenSearch 会将查询解析为词元,然后将这些词元组合以生成多词元同义词的 短语查询。例如,如果您指定 ba,batting average
作为同义词并搜索 ba
,OpenSearch 会搜索 ba OR "batting average"
。
要用连词匹配多词元同义词,请将 auto_generate_synonyms_phrase_query
设置为 false
。
GET /testindex/_search
{
"query": {
"match": {
"text": {
"query": "good ba",
"auto_generate_synonyms_phrase_query": false
}
}
}
}
生成的查询是 ba OR (batting AND average)
。
参数
查询接受字段名称 (<field>
) 作为顶级参数
GET _search
{
"query": {
"match": {
"<field>": {
"query": "text to search for",
...
}
}
}
}
的 <field>
接受以下参数。除 query
外,所有参数均为可选。
参数 | 数据类型 | 描述 |
---|---|---|
query | 字符串 | 用于搜索的查询字符串。必填。 |
auto_generate_synonyms_phrase_query | 布尔型 | 指定是否自动为多词元同义词创建匹配短语查询。例如,如果您指定 ba,batting average 作为同义词并搜索 ba ,OpenSearch 会搜索 ba OR "batting average" (如果此选项为 true )或 ba OR (batting AND average) (如果此选项为 false )。默认值为 true 。 |
分析器 | 字符串 | 用于将查询字符串文本分词的分析器。默认值为为 default_field 指定的索引时间分析器。如果未为 default_field 指定分析器,则 analyzer 是索引的默认分析器。有关 index.query.default_field 的更多信息,请参阅动态索引级索引设置。 |
提升 | 浮点数 | 按给定乘数提升子句。在复合查询中,用于衡量子句的权重。值在 [0, 1) 范围内会降低相关性,大于 1 的值会增加相关性。默认值为 1 。 |
enable_position_increments | 布尔型 | 当为 true 时,生成的查询会考虑位置增量。当停用词的删除在词元之间留下不需要的“间隙”时,此设置很有用。默认值为 true 。 |
fuzziness | 字符串 | 在确定词元是否与值匹配时,将一个单词更改为另一个单词所需的字符编辑(插入、删除、替换或换位)次数。例如,wined 和 wind 之间的距离为 1。有效值为非负整数或 AUTO 。默认值 AUTO 会根据每个词元的长度选择一个值,这在大多数用例中都是一个不错的选择。 |
fuzzy_rewrite | 字符串 | 确定 OpenSearch 如何重写查询。有效值为 constant_score 、scoring_boolean 、constant_score_boolean 、top_terms_N 、top_terms_boost_N 和 top_terms_blended_freqs_N 。如果 fuzziness 参数不是 0 ,查询默认使用 top_terms_blended_freqs_${max_expansions} 的 fuzzy_rewrite 方法。默认值为 constant_score 。 |
fuzzy_transpositions | 布尔型 | 将 fuzzy_transpositions 设置为 true (默认值)会在 fuzziness 选项的插入、删除和替换操作中添加相邻字符的交换。例如,如果 fuzzy_transpositions 为 true(交换“n”和“i”),则 wind 和 wnid 之间的距离为 1;如果为 false(删除“n”,插入“n”),则距离为 2。如果 fuzzy_transpositions 为 false,则 rewind 和 wnid 与 wind 的距离相同(2),尽管从更以人为中心的角度来看,wnid 是一个明显的拼写错误。默认值适用于大多数用例。 |
lenient | 布尔型 | 将 lenient 设置为 true 会忽略查询与文档字段之间的数据类型不匹配。例如,查询字符串 "8.2" 可以匹配 float 类型的字段。默认值为 false 。 |
max_expansions | 正整数 | 查询可以扩展到的最大词元数。模糊查询会“扩展”到 fuzziness 中指定的距离内的多个匹配词元。然后 OpenSearch 会尝试匹配这些词元。默认值为 50 。 |
minimum_should_match | 正或负整数,正或负百分比,组合 | 如果查询字符串包含多个搜索词元并且您使用 or 操作符,则文档被视为匹配所需的匹配词元数。例如,如果 minimum_should_match 为 2,则 wind often rising 不会匹配 The Wind Rises. 。如果 minimum_should_match 为 1 ,则会匹配。有关详细信息,请参阅Minimum should match。 |
operator | 字符串 | 如果查询字符串包含多个搜索词元,则文档被视为匹配是需要所有词元都匹配 (AND ) 还是只需要一个词元匹配 (OR )。有效值为:- OR :字符串 to be 被解释为 to OR be - AND :字符串 to be 被解释为 to AND be 默认值为 OR 。 |
prefix_length | 非负整数 | 在模糊性中不考虑的前导字符数。默认值为 0 。 |
zero_terms_query | 字符串 | 在某些情况下,分析器会从查询字符串中删除所有词元。例如,stop 分析器会删除字符串 an but this 中的所有词元。在这些情况下,zero_terms_query 指定是不匹配任何文档 (none ) 还是匹配所有文档 (all )。有效值为 none 和 all 。默认值为 none 。 |