Retieve element in an array object

Hi everyone,
I have a question about array object in elasticsearch
For example i have a mapping like this one:

PUT test
{
  "settings": { ...},
  "mappings": {
    "properties": {
      "id":{
        "type": "keyword"
      },
      "levels":{
        "properties": {
          "translations":{
            "type": "text",
            "analyzer": "english"
          },
          "id":{
            "type": "keyword"
          }
        }
      }
    }
  }
}

And then, i add documents in index test:

{
  "id": 1,
  "levels":[
    {"id": 1, "translations": "lalaland"},
    {"id": 2, "translations": "lili"},
    {"id": 3, "translations": "lalaland"}
  ]
}

Then, i launch some search

GET test/_search
{
  "_source": "levels.translations", 
  "query": {
    "match": {
      "levels.translations": "lalaland"
    }
  },
  "highlight": {
    "fields": {"levels.translations": {}}
  }
}

The result is:

"hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.3955629,
        "_source" : {
          "levels" : [
            {
             "id": 1,
              "translations" : "lalaland"
            },
            {
              "id": 2,
              "translations" : "lili"
            },
            {
              "id": 3,
              "translations" : "lalaland"
            }
          ]
        },
        "highlight" : {
          "levels.translations" : [
            "<em>lalaland</em>",
            "<em>lalaland</em>"
          ]
        }
      }
    ]

Are there anyway to retrieve just the first and the thirst element in levels field. I wonder if i can do it with nested ?
Thank you all.

Hi

May be with this: https://www.elastic.co/guide/en/elasticsearch/reference/7.5/search-request-body.html#request-body-search-inner-hits

Thank you for you help.
That is exactly what i'm looking for.
Just to be sure, to use inner hits, my datatype field has to be nested and not object ?

Yes. It won't work with object.

Thank a lot.

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