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

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?

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

Like:

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

I don't understand the use case here.

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.

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 ?

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