弹性搜索匹配与术语查询

我使用匹配查询搜索“ request.method”: “ GET”:

    {
"query": {
"filtered": {
"query": {
"match": {
"request.method": "GET"
}
},
"filter": {
"bool": {
"must": [
...

正如预期的那样,Match 查询可以得到结果,如下所示:

enter image description here

但问题是,在使用“术语”查询时,没有结果。

更新查询,将“ match”更改为“ term”,并保持其他部分不变:

{
"query": {
"filtered": {
"query": {
"term": {
"request.method": "GET"
}
},
"filter": {
"bool": {
"must": [
...

我认为术语查询是 Match 查询的“未分析”版本。如上图所示,至少有一条记录的“ request.method”等于“ GET”。为什么上述术语查询没有结果?谢谢你。

enter image description here

75807 次浏览

Assuming you are using the Standard Analyzer GET becomes get when stored in the index. The source document will still have the original "GET".

The match query will apply the same standard analyzer to the search term and will therefore match what is stored in the index. The term query does not apply any analyzers to the search term, so will only look for that exact term in the inverted index.

To use the term query in your example, change the upper case "GET" to lower case "get" or change your mapping so the request.method field is set to not_analyzed.

The difference between term and match in elasticsearch

Term is an exact query

Match is a fuzzy query

The term is a perfect match, that is, an exact query. The search term will not be segmented before the search, so our search term must be one of the document segmentation sets. Let’s say we want to find all the documents titled Jesus Verma.

 $curl -XGET http://localhost:9200/index/doc/_search?pretty -d 
'{
  "query":{
    "term":{
"title": "Jesus Verma"
    }
  }
}'

The match query will first classify the search words. After the word segmentation, the word segmentation results will be matched one by one. Therefore, compared to the exact search of term, match is a participle match search, and match search has two variants of similar functions. One is match_phrase. One is multi_match

$curl -XGET http://localhost:9200/index/doc/_search?pretty -d 
'{
    "query": {
        "match": {
"content": "Banglore, India"
        }
    }
}'