# Querying nested arrays

**URL:** <https://discuss.elastic.co/t/querying-nested-arrays/144381>\
**Category:** Elasticsearch\
**Created:** [August 14, 2018, 5:57pm UTC](https://discuss.elastic.co/t/querying-nested-arrays/144381 "2018-08-14T17:57:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Noronha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_noronha/32/35348_2.png) [@Michael\_Noronha](https://discuss.elastic.co/u/Michael_Noronha)\
**Post date:** [August 14, 2018, 5:57pm UTC](https://discuss.elastic.co/t/querying-nested-arrays/144381/1 "2018-08-14T17:57:33Z")

</div>

My current data representation is as follows:

```
{
  ...
  "_source": {
      "thing1": {
          "data": [[1,2,3], [4,5,6], [7,8,9]]
      },
      "thing2": {
          "data": [[1,2,3], [4,5,6], [7,8,9]]
      }
  }
}

```

Where I have a bunch of nested arrays. I want to be able to query to see if an element exists within a row. So for example, I'd want to see if 1 was in the first nested list of the `thing_1`. I'm not sure how to do this from reading the nested documentation, since those examples focus on nested objects.

How do I specify my mapping, and what do I need to do to make queries?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![jaddison](https://avatars.discourse-cdn.com/v4/letter/j/e5b9ba/32.png) [@jaddison](https://discuss.elastic.co/u/jaddison)\
**Post date:** [September 9, 2018, 9:28pm UTC](https://discuss.elastic.co/t/querying-nested-arrays/144381/2 "2018-09-09T21:28:04Z")

</div>

Based on the bullet points on [this doc page](https://www.elastic.co/guide/en/elasticsearch/reference/current/array.html), ES doesn't index the data as you'd hope it would. I think it would end up just as `[1,2,3,4,5,6,7,8,9]`.

I think - I could be wrong.

But perhaps you could query via a script to inspect the doc values?

---

<div class="post-metadata">

**Author:** ![Michael\_Noronha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_noronha/32/35348_2.png) [@Michael\_Noronha](https://discuss.elastic.co/u/Michael_Noronha)\
**Post date:** [September 9, 2018, 9:42pm UTC](https://discuss.elastic.co/t/querying-nested-arrays/144381/3 "2018-09-09T21:42:59Z")

</div>

Thank you _so_ much for responding to this.

I ended up getting this to work with a script that looked like this:

```auto
GET _search
{
  "query": {
    "bool":{
      "must": {
        "match_all": {}
      },
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "a": "3"
              }
            }
          ]
        }
      }
    }
  }
}

```

Now, hoping to figure out if I can tell where in the doc my match was, since not all my documents are structured the same, making it seem like highlights aren't well suited for what I'm trying to do. Do you think I could do something with scripts?

---

<div class="post-metadata">

**Author:** ![jaddison](https://avatars.discourse-cdn.com/v4/letter/j/e5b9ba/32.png) [@jaddison](https://discuss.elastic.co/u/jaddison)\
**Post date:** [September 10, 2018, 4:18pm UTC](https://discuss.elastic.co/t/querying-nested-arrays/144381/4 "2018-09-10T16:18:32Z")

</div>

I think the community needs to understand the problem a bit better - can you provide more detail? Actual sample documents, your index settings/document mappings.

But consider if you can restructure your data to denormalize it - ie. get rid of the array of arrays, in favour of duplicating some data. That usually makes things easier - but as always, it depends on how you need to query the data.

---

<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 8, 2018, 4:18pm UTC](https://discuss.elastic.co/t/querying-nested-arrays/144381/5 "2018-10-08T16:18:42Z")

</div>

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