短语匹配查询

编辑

match_phrase 查询会分析文本,并从分析后的文本中创建一个 phrase 查询。例如:

resp = client.search(
    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)
const response = await client.search({
  query: {
    match_phrase: {
      message: "this is a test",
    },
  },
});
console.log(response);
GET /_search
{
  "query": {
    "match_phrase": {
      "message": "this is a test"
    }
  }
}

短语查询会匹配在可配置的 slop(默认为 0)范围内的任意顺序的词项。转置词项的 slop 为 2。

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

resp = client.search(
    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)
const response = await client.search({
  query: {
    match_phrase: {
      message: {
        query: "this is a test",
        analyzer: "my_analyzer",
      },
    },
  },
});
console.log(response);
GET /_search
{
  "query": {
    "match_phrase": {
      "message": {
        "query": "this is a test",
        "analyzer": "my_analyzer"
      }
    }
  }
}

此查询还接受 zero_terms_query,如 match 查询中所述。