2

I have "routes" field as long type (Im storing array of values in that Example 1. [5463, 3452] , 2. [5467, 3452]) in mapping. In the following query i want to retrieve data which matches both 5463, 3452 in same record

GET /flight_routes/_search
{
    "query": {
    "bool": {
    "filter": {
        "terms": {
          "routes": [5463, 3452]
        }
      }
    }
    }
}

But it is returning document which matches with either one value. Should I have to migrate the mapping type to nested to handle this or any other way to get it through query itself?

syv
  • 3,528
  • 7
  • 35
  • 50
  • Possible duplicate of [Filter items which array contains any of given values](https://stackoverflow.com/questions/28001632/filter-items-which-array-contains-any-of-given-values) – Aman B May 29 '18 at 11:16
  • I know boolean - must filter along with multiple "term" i can achieve this. Would like to know is there any handy way get it. – syv May 29 '18 at 11:32

1 Answers1

3

You can use the terms_set query with a minimum_should_match_script that returns the length of the array

POST /flight_routes/_search
{
    "query": {
        "terms_set": {
            "routes" : {
                "terms" : [5463, 3452],
                "minimum_should_match_script": {
                   "source": "params.nb_terms",
                   "params": {
                      "nb_terms": 2
                   }
                }
            }
        }
    }
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • This will apply "OR" Condition., I want to retrieve routes which has both 5463 and 3452. – syv May 29 '18 at 11:35
  • Have you tried it? `minimum_should_match_script` will return 2 in this case and hence 2 terms must match – Val May 29 '18 at 11:37
  • Yes I tried., it returns documents which has both 5463, 3452 and 5463 alone. I need only documents which has 5463, 3452 (exact match) – syv May 29 '18 at 11:46
  • 1
    My bad, I've updated the script, actually you need to pass the number of terms in the query and that will work. – Val May 29 '18 at 11:49