Must have all items in array query

Hello, i have the next document structure
{
id: 'offering_1',
qualification: ['11', '22']
},
{
id: 'offering_2',
qualification: ['11', '22', '33']
}

I wish to run a query that will return only records from index that all the array items in the index are part of the query array Example:

  1. Query with values of 11 will return nothing
  2. Query with values of 11,22 will return offering_1
  3. Query with values of 11,22,44 will return offering_1
  4. Query with values of 11,22,33 will return offering_1 and offering_2
  5. Query with values of 11,22,33,44 will return offering_1 and offering_2

Is there anyway to query like this in elastic?

Hi!

I believe there are a few ways to do this. Using the Terms set query you will get the results you want.

GET idx_test/_search
{
  "query": {
    "terms_set": {
      "qualification": {
        "terms": [11,22,33,44],
        "minimum_should_match_field": "required_matches"
      }
    }
  }
}

Thanks a lot i have created the below records and used your query and it worked, thanks a lot :slight_smile:
{
id: 'offering_1',
qualification: ['11', '22'],
required_matches: 2
},
{
id: 'offering_2',
qualification: ['11', '22', '33'],
required_matches: 3
}

2 Likes

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.