List query must contain all elements

Hi all, I have a question regarding a query I am attempting to do.

Lets say we have a document:

{
"array": ["value1", "value2", "value3"]
}

And I have two queries:

Query 1:

"array": ["value1", "value2", "value3", "value4"]

Query 2:

"array": ["value1", "value3", "value4"]

The document should be returned for Query 1, but should not for Query 2 as the query does not contain all the elements in the document.

How do you guys suggest going about solving this issue? My two solutions are currently:

  1. Create a script that returns true or false based on if each element from the document is in the query.
  2. use the terms set query but this would require I change my mappings which I would like to avoid.

Any advice would be appreciated!

how about something this (on top of my head and untested, make sure you test some corner cases)?

PUT test/_doc/1
{
  "array": [
    "value1", 
    "value2",
    "value3",
    "value4"
  ]
}

PUT test/_doc/2?refresh
{
  "array": [
    "value1",
    "value2",
    "value4"
  ]
}

GET test/_search 
{
  "query": {
    "match": {
      "array": {
        "operator": "and",
        "query": "value1 value2 value3 value4"
      }
    }
  }
}
1 Like

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