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


#1

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?


(David Pilato) #2

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.


#3

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.


(David Pilato) #4

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 https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-request-inner-hits.html ?


(system) #5

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