匹配短语前缀查询
使用 match_phrase_prefix
查询按顺序指定要匹配的短语。将返回包含您指定短语的文档。短语中的最后一个部分词条被解释为前缀,因此将返回包含以该短语和最后一个词条的前缀开头的短语的任何文档。
与 匹配短语 类似,但将查询字符串中的最后一个词条创建为 前缀查询。
关于 match_phrase_prefix
和 match_bool_prefix
查询之间的区别,请参阅 `match_bool_prefix` 和 `match_phrase_prefix` 查询。
以下示例展示了一个基本的 match_phrase_prefix
查询
GET _search
{
"query": {
"match_phrase_prefix": {
"title": "the wind"
}
}
}
要传递附加参数,可以使用扩展语法
GET _search
{
"query": {
"match_phrase_prefix": {
"title": {
"query": "the wind",
"analyzer": "stop"
}
}
}
}
示例
例如,考虑一个包含以下文档的索引
PUT testindex/_doc/1
{
"title": "The wind rises"
}
PUT testindex/_doc/2
{
"title": "Gone with the wind"
}
以下 match_phrase_prefix
查询搜索整个单词 wind
,后面跟着一个以 ri
开头的单词
GET testindex/_search
{
"query": {
"match_phrase_prefix": {
"title": "wind ri"
}
}
}
响应包含匹配的文档
响应
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.92980814,
"hits": [
{
"_index": "testindex",
"_id": "1",
"_score": 0.92980814,
"_source": {
"title": "The wind rises"
}
}
]
}
}
参数
查询接受字段名称 (<field>
) 作为顶级参数
GET _search
{
"query": {
"match_phrase": {
"<field>": {
"query": "text to search for",
...
}
}
}
}
的 <field>
接受以下参数。除 query
外,所有参数均为可选。
参数 | 数据类型 | 描述 |
---|---|---|
查询 | 字符串 | 用于搜索的查询字符串。必填。 |
分析器 | 字符串 | 用于对查询进行分词的分析器。 |
max_expansions | 正整数 | 查询可扩展到的最大词条数。模糊查询会“扩展到”在 fuzziness 中指定的距离内的多个匹配词条。然后 OpenSearch 会尝试匹配这些词条。默认值为 50 。 |
slop | 0 (默认值)或正整数 | 控制查询中单词错序后仍被视为匹配的程度。摘自 Lucene 文档:“查询短语中单词之间允许的其他单词的数量。例如,要交换两个单词的顺序需要两次移动(第一次移动将单词放置在彼此之上),因此为了允许短语重新排序,`slop` 必须至少为二。值为零表示需要精确匹配。” |