J.Done
J.Done

Reputation: 3033

Elasticsearch - get nested fields

I want to get only nested fields, but cannot since it's not leaf fields.

GET index/_search
{
    "size": 10,
    "fields": [
       "nested_fields"
    ]
}

ERROR : "reason": "field [nested_fields] isn't a leaf field"

I tried below but cannot match each id and name in nested object.

GET index/_search
    {
        "size": 10,
        "fields": [
           "nested_fields.id",
           "nested_fields.name"
        ]
    }

result :

"fields": {
               "events.id": [
                  "13342",
                  "24232",
                  "25534",
                  "63454"
               ],
               "events.name": [
                  "R1413",
                  "R1414",
                  "R1415",
                  "R1416",
               ]
            }

Here is my expected result:

fields" : {
  "evets" : {
      "id" : "234234",
      "name" : "RP1524"
   },
    .... so on
}

Upvotes: 8

Views: 11150

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52368

If you don't have a certain query that should match the nested fields somehow, you can do it like this:

GET /index/_search
{
  "size": 10,
  "_source": ["nested_fields.id", "nested_fields.name"]
}

If you also have a nested query and you want to return the nested docs that matched you can do it like this (with inner_hits):

{
  "query": {
    "nested": {
      "path": "nested_fields",
      "query": {"match_all": {}}, 
      "inner_hits": {}
    }
  }
}

Upvotes: 13

Related Questions