Elasticsearch vs. Field Matching List

I have a list, an array, or whatever language you are familiar with. For example. names: ["John","Bas","Peter"] , and I want to query the name field if it matches one of these names.

One way is by using an OR filter. eg.

 { "filtered" : { "query" : { "match_all": {} }, "filter" : { "or" : [ { "term" : { "name" : "John" } }, { "term" : { "name" : "Bas" } }, { "term" : { "name" : "Peter" } } ] } } } 

Love way? Better if it's a query than a filter.

+7
elasticsearch
source share
1 answer
 { "query": { "filtered" : { "filter" : { "terms": { "name": ["John","Bas","Peter"] } } } } } 

Which Elasticsearch is being rewritten, as if you were using this

 { "query": { "filtered" : { "filter" : { "bool": { "should": [ { "term": { "name": "John" } }, { "term": { "name": "Bas" } }, { "term": { "name": "Peter" } } ] } } } } } 

When using a boolean filter, most of the time it is better to use a bool filter than and or or . The reason is explained on the Elasticsearch blog: http://www.elasticsearch.org/blog/all-about-elasticsearch-filter-bitsets/

+14
source share

All Articles