# List query must contain all elements

**URL:** <https://discuss.elastic.co/t/list-query-must-contain-all-elements/194297>\
**Category:** Elasticsearch\
**Created:** [August 7, 2019, 5:33pm UTC](https://discuss.elastic.co/t/list-query-must-contain-all-elements/194297 "2019-08-07T17:33:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiboxiv](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jiboxiv](https://discuss.elastic.co/u/jiboxiv)\
**Post date:** [August 7, 2019, 5:33pm UTC](https://discuss.elastic.co/t/list-query-must-contain-all-elements/194297/1 "2019-08-07T17:33:12Z")

</div>

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

Lets say we have a document:

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

```

And I have two queries:

Query 1:

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

```

Query 2:

```auto
"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](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html) but this would require I change my mappings which I would like to avoid.

Any advice would be appreciated!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 9, 2019, 6:12am UTC](https://discuss.elastic.co/t/list-query-must-contain-all-elements/194297/2 "2019-08-09T06:12:32Z")

</div>

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

```auto
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"
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 6, 2019, 6:12am UTC](https://discuss.elastic.co/t/list-query-must-contain-all-elements/194297/3 "2019-09-06T06:12:42Z")

</div>

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