# ES query to match all elements in array

**URL:** <https://discuss.elastic.co/t/es-query-to-match-all-elements-in-array/250364>\
**Category:** Elasticsearch\
**Created:** [September 29, 2020, 1:20pm UTC](https://discuss.elastic.co/t/es-query-to-match-all-elements-in-array/250364 "2020-09-29T13:20:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![GeorgetaPojoga](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/georgetapojoga/32/76150_2.png) [@GeorgetaPojoga](https://discuss.elastic.co/u/GeorgetaPojoga)\
**Post date:** [September 29, 2020, 1:20pm UTC](https://discuss.elastic.co/t/es-query-to-match-all-elements-in-array/250364/1 "2020-09-29T13:20:22Z")

</div>

So I got this document with a nested array that I want to filter with this query.

I want ES to return all documents where all items have changes = 0 and that only. If document has even a single item in the list with a change = 1, that's discarded.

Is there any way I can achieve this starting from the query I have already wrote? Or should I use a script instead?

DOCUMENTS:

```auto
{
    "id": "abc",
    "_source" : {
        "trips" : [
            {
                "type" : "home",
                "changes" : 0
            },
            {
                "type" : "home",
                "changes" : 1
            }
        ]
    }
},
{
        "id": "def",
        "_source" : {
            "trips" : [
                {
                    "type" : "home",
                    "changes" : 0
                },
                {
                    "type" : "home",
                    "changes" : 0
                }
            ]
        }
    }

```

QUERY:

```auto
GET trips_solutions/_search

    {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "id": {
                  "value": "abc"
                }
              }
            },
            {
              "nested": {
                "path": "trips",
                "query": {
                  "range": {
                    "trips.changes": {
                      "gt": -1,
                      "lt": 1
                    }
                  }
                }
              }
            }
          ]
        }
      }
    }

```

EXPECTED RESULT:

```auto
{
            "id": "def",
            "_source" : {
                "trips" : [
                    {
                        "type" : "home",
                        "changes" : 0
                    },
                    {
                        "type" : "home",
                        "changes" : 0
                    }
                ]
            }
        }

```

**Elasticsearch version: 7.6.2**

Already read this answers but they didn't help me: [How to match all item in nested array](https://discuss.elastic.co/t/how-to-match-all-item-in-nested-array/163873) [ElasticSearch: How to query exact nested array](https://stackoverflow.com/questions/46104158/elasticsearch-how-to-query-exact-nested-array)

---

<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:** [September 30, 2020, 12:03pm UTC](https://discuss.elastic.co/t/es-query-to-match-all-elements-in-array/250364/2 "2020-09-30T12:03:24Z")

</div>

I might be missing something, but I don't think there is a proper query to make this work. I do have an idea though by using an ingest pipeline and another query by writing out a property if all the changes are unique.

```auto
DELETE test

POST _ingest/pipeline/_simulate?error_trace=true
{
  "docs": [
    {
      "_source": {
        "value": "abc",
        "trips": [
          {
            "type": "home",
            "changes": 0
          },
          {
            "type": "home",
            "changes": 1
          }
        ]
      }
    },
    {
      "_source": {
        "value": "abc",
        "trips": [
          {
            "type": "home",
            "changes": 0
          },
          {
            "type": "home",
            "changes": 0
          }
        ]
      }
    }
  ],
  "pipeline": {
    "processors": [
      { "script": {
        "lang": "painless",
        "source": "def changes = ctx.trips.stream().map(t -> t.changes).collect(Collectors.toSet()); if (changes.size() == 1) { ctx.trips_unique = changes.iterator().next(); }"
      }}
    ]
  }
}

PUT _ingest/pipeline/trips_pipeline
{
  "processors": [
    {
      "script": {
        "lang": "painless",
        "source": "def changes = ctx.trips.stream().map(t -> t.changes).collect(Collectors.toSet()); if (changes.size() == 1) { ctx.trips_unique = changes.iterator().next(); }"
      }
    }
  ]
}

PUT test 
{
  "settings": {
    "index.default_pipeline" : "trips_pipeline"
  }, 
  "mappings": {
    "properties": {
      "trips" : {
        "type": "nested"
      }
    }
  }
}

POST test/_doc
{
  "value": "abc",
  "trips": [
    {
      "type": "home",
      "changes": 0
    },
    {
      "type": "home",
      "changes": 1
    }
  ]
}

POST test/_doc
{
  "value": "abc",
  "trips": [
    {
      "type": "home",
      "changes": 0
    },
    {
      "type": "home",
      "changes": 0
    }
  ]
}

POST test/_refresh

GET test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "value.keyword": "abc"
          }
        },
        {
          "term": {
            "trips_unique": "0"
          }
        }
      ]
    }
  }
}

```

hope that helps

---

<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:** [October 28, 2020, 12:03pm UTC](https://discuss.elastic.co/t/es-query-to-match-all-elements-in-array/250364/3 "2020-10-28T12:03:27Z")

</div>

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