这个问题来自stackoverflowjson
https://stackoverflow.com/questions/23150670/elasticsearch-match-vs-term-queryapp
建立索引test
,含有一个_doc
类型elasticsearch
PUT /test { "mappings" : { "_doc" : { "properties" : { "field1" : { "type" : "text", "analyzer" : "standard" } } } } }
索引一个_doc
类型的文档到test
中code
POST /test/_doc { "field1": "GET" }
经过match
查找GET
,能够找到结果索引
GET /test/_doc/_search { "query": { "bool": { "must": [ {"match": {"field1": "GET"}} ] } } }
经过term
查找GET
,找不到结果token
GET /test/_doc/_search { "query": { "bool": { "must": [ {"term": {"field1": "GET"}} ] } } }
使用match
查询request.method:GET
文档
{ "query": { "filtered": { "query": { "match": { "request.method": "GET" } }, "filter": { "bool": { "must": [ ...
match
查询能够拿到结果,但问题是当使用term
来进行查询时没有任何结果get
{ "query": { "filtered": { "query": { "term": { "request.method": "GET" } }, "filter": { "bool": { "must": [ ...
可能使用了Standard Analyzer
,在对文档进行索引的时候GET
变成了get
,而文档的_source
依然是GET
。it
GET /_analyze { "analyzer": "standard", "text": "GET" } # response # 能够看到token是get { "tokens": [ { "token": "get", "start_offset": 0, "end_offset": 3, "type": "<ALPHANUM>", "position": 0 } ] }
match
查询将会对搜索的句子应用Standard Analyzer
,即搜索中的GET
会变成get
,那么就会命中文档。而term
查询并不会对搜索的内容进行分析,所以会直接查找get
,那么就找不到该文档。io
若是想要term查询能够生效,那么能够:
GET
变为小写的get
request.method
字段的类型为not_analyzed
request.method
字段的类型为keyword
ElasticSearch官方文档对match
和term
的说明
match
接受文本/数字/日期,并分析它们
term
根据提供的确切值查找文本