匹配短语前缀查询

匹配短语前缀查询 #

使用 match_phrase_prefix 查询来指定要匹配的短语。包含您指定短语的文档将被返回。短语中的最后一个部分词被解释为前缀,因此任何包含以该短语和最后一个词的前缀开头的短语的文档都将被返回。

与匹配短语类似,但会从查询字符串中的最后一个词创建一个前缀查询。

对于 match_phrase_prefixmatch_bool_prefix 查询之间的差异,请参阅 match_bool_prefixmatch_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 外,所有参数都是可选的。

参数数据类型描述
queryString用于搜索的查询字符串。必填。
analyzerString用于分词查询的分词器。
max_expansionsPositive integer查询可以扩展到的最大词数。模糊查询会扩展到与指定距离( fuzziness )内的匹配词。然后 Easysearch 尝试匹配这些词。默认值为 50 。
slop0 (默认)或一个正整数控制查询中的词语可以错乱的程度,仍然被视为匹配的程度。来自 Lucene 文档的说明:“查询短语中允许插入的其他词语数量。例如,要交换两个词语的顺序需要两次移动(第一次移动将词语叠放在一起),因此为了允许短语的重排序,slop 值必须至少为 2。值为零则要求完全匹配。”