Search code examples
elasticsearch

Elasticsearch query that requires all values in array to be present


Heres a sample query:

{
    "query":{
        "constant_score":{
            "filter":{
                "terms":{
                    "genres_slugs":["simulator", "strategy", "adventure"]
                }
            }
        }
    },
    "sort":{
        "name.raw":{
            "order":"asc"
        }
    }
}

The value mapped to the genres_slugs property is just a simple array.

What i'm trying to do here is match all games that have all the values in the array: ["simulator","strategy","adventure"]
As in, the resulting items MUST have all those values. What's returning instead are results that have only one value and not the others.

Been going at this for 6 hours now :(


Solution

  • Ok, if the resulting items MUST have all those values, use MUST param instead of FILTER.

    { "query": 
        { "constant_score" : 
              { "filter" : 
                   { "bool" : 
                        { "must" : [ 
                               { "term" : 
                                   {"genres_slugs":"simulator"}
                               },
                               { "term" : 
                                   {"genres_slugs":"strategy"}
                               },
                               { "term" : 
                                   {"genres_slugs":"adventure"}
                               }]
                        }
                   }
              }
        }
    } 
    

    This returns:

    {
        "took": 54,
        "timed_out": false,
        "_shards": {
            "total": 5,
            "successful": 5,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": 2,
            "max_score": 1,
            "hits": [
                {
                    "_index": "try",
                    "_type": "stackoverflowtry",
                    "_id": "123",
                    "_score": 1,
                    "_source": {
                        "genres_slugs": [
                            "simulator",
                            "strategy",
                            "adventure"
                        ]
                    }
                },
                {
                    "_index": "try",
                    "_type": "stackoverflowtry",
                    "_id": "126",
                    "_score": 1,
                    "_source": {
                        "genres_slugs": [
                            "simulator",
                            "strategy",
                            "adventure"
                        ]
                    }
                }
            ]
        }
    }
    

    Doc:

    https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

    https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_multiple_exact_values.html

    https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-common-terms-query.html