# Getting item of a a given index from an array inside an indexed document

**URL:** <https://discuss.elastic.co/t/getting-item-of-a-a-given-index-from-an-array-inside-an-indexed-document/133494>\
**Category:** Elasticsearch\
**Created:** [May 28, 2018, 8:49am UTC](https://discuss.elastic.co/t/getting-item-of-a-a-given-index-from-an-array-inside-an-indexed-document/133494 "2018-05-28T08:49:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cboptakn](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@cboptakn](https://discuss.elastic.co/u/cboptakn)\
**Post date:** [May 28, 2018, 8:49am UTC](https://discuss.elastic.co/t/getting-item-of-a-a-given-index-from-an-array-inside-an-indexed-document/133494/1 "2018-05-28T08:49:38Z")

</div>

For an indexed JSON document with a key representing a JSON document X, I need to get a given element of the array given the index.

So, for example this is the document:

```
{
  "myKey" : ["A", "B", "C"]
}

```

I need to get the element on index 1, which would be "B."

Is this something that's possible in ElasticSearch queries?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 28, 2018, 9:31am UTC](https://discuss.elastic.co/t/getting-item-of-a-a-given-index-from-an-array-inside-an-indexed-document/133494/2 "2018-05-28T09:31:58Z")

</div>

If you want to search for a key, why not indexing keys instead?

Like:

```auto
POST keys/_doc
{
  "key": "A"
}
POST keys/_doc
{
  "key": "B"
}
POST keys/_doc
{
  "key": "C"
}

```

I don't understand the use case here.

---

<div class="post-metadata">

**Author:** ![cboptakn](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@cboptakn](https://discuss.elastic.co/u/cboptakn)\
**Post date:** [May 28, 2018, 9:49am UTC](https://discuss.elastic.co/t/getting-item-of-a-a-given-index-from-an-array-inside-an-indexed-document/133494/3 "2018-05-28T09:49:43Z")

</div>

Indexing the keys can be a possible alternative, it's just that we already have an array and it would be cleaner to be able to do array operations by getting elements of a given index.

But yeah, if there is no direct way to do it, indexing keys is a good solution.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 28, 2018, 10:01am UTC](https://discuss.elastic.co/t/getting-item-of-a-a-given-index-from-an-array-inside-an-indexed-document/133494/4 "2018-05-28T10:01:57Z")

</div>

> it would be cleaner to be able to do array operations by getting elements of a given index.

I don't understand TBH.  
May be you would like to use the nested document feature and use [Inner hits | Elasticsearch Reference [6.2] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-request-inner-hits.html) ?

---

<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:** [June 25, 2018, 10:01am UTC](https://discuss.elastic.co/t/getting-item-of-a-a-given-index-from-an-array-inside-an-indexed-document/133494/5 "2018-06-25T10:01:59Z")

</div>

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