Home > front end >  Elastic Search Multiple Filter values for the same field
Elastic Search Multiple Filter values for the same field

Time:10-23

Say that I have to filter cars constructors in a Elastic Search Index (ES 7.15), where the field car_maker is mapped to keyword, having it a limited number of possibilities among car makers string names:

{
    "mappings": {
        "properties": {
            "car_maker": {
                "type": "keyword"
            }
        }
    }
}

GET /cars/_search
{

    "query": {
        "bool": {
            "filter": [{
                "term": {
                    "car_maker": "Honda"
                }
            }]
        }
    }
}

This, along with a matching query will work ok. The filter will not participate to score calculation as desired. Now I would like to to filter more car makers for that query (let's say a should query):

{
 "query": {
    "bool": {
        "filter" : [
            {"term" : { "car_maker" : "Honda"}},
            {"term" : { "car_maker" : "Ferrari"}}
        ]
    }
 }
}

this is not going to work. I will have any error from ES query engine, but any result too. Of course is always possibile to apply more filters to different fields like car_maker and car_color, but how to do the opposite: apply more values (Honda, Ferrari, etc.) to the same filter field car_maker like in the example above, without conditioning the score calculation?

CodePudding user response:

You might want to try the following filter query:

{
  "query" : {
    "bool" : {
      "filter" : {
        "terms" : {
          "car_maker" : ["Honda", "Ferrari"]
        }
      }
    }
  }
}
  • Related