匹配短语查询编辑

match_phrase 查询会分析文本并根据分析后的文本创建一个 phrase 查询。例如

resp = client.search(
    body={"query": {"match_phrase": {"message": "this is a test"}}},
)
print(resp)
response = client.search(
  body: {
    query: {
      match_phrase: {
        message: 'this is a test'
      }
    }
  }
)
puts response
res, err := es.Search(
	es.Search.WithBody(strings.NewReader(`{
	  "query": {
	    "match_phrase": {
	      "message": "this is a test"
	    }
	  }
	}`)),
	es.Search.WithPretty(),
)
fmt.Println(res, err)
GET /_search
{
  "query": {
    "match_phrase": {
      "message": "this is a test"
    }
  }
}

短语查询匹配以任何顺序排列的、最多可配置 slop(默认为 0)个词项。颠倒词项的 slop 为 2。

可以设置 analyzer 来控制哪个分析器将对文本执行分析过程。它默认为字段显式映射定义,或者默认搜索分析器,例如

resp = client.search(
    body={
        "query": {
            "match_phrase": {
                "message": {
                    "query": "this is a test",
                    "analyzer": "my_analyzer",
                }
            }
        }
    },
)
print(resp)
response = client.search(
  body: {
    query: {
      match_phrase: {
        message: {
          query: 'this is a test',
          analyzer: 'my_analyzer'
        }
      }
    }
  }
)
puts response
res, err := es.Search(
	es.Search.WithBody(strings.NewReader(`{
	  "query": {
	    "match_phrase": {
	      "message": {
	        "query": "this is a test",
	        "analyzer": "my_analyzer"
	      }
	    }
	  }
	}`)),
	es.Search.WithPretty(),
)
fmt.Println(res, err)
GET /_search
{
  "query": {
    "match_phrase": {
      "message": {
        "query": "this is a test",
        "analyzer": "my_analyzer"
      }
    }
  }
}

match 查询中所述,此查询也接受 zero_terms_query