Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
}
like image 300
J.Done Avatar asked Jul 29 '16 06:07

J.Done


People also ask

What is nested in elastic search?

The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way that they can be queried independently of each other.

What is a nested field?

When a packed class contains an instance field that is a packed type, the data for that field is packed directly into the containing class. The field is known as a nested field .

What is nested datatype?

Nested data types are structured data types for some common data patterns. Nested data types support structs, arrays, and maps. A struct is similar to a relational table. It groups object properties together.


1 Answers

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": {}
    }
  }
}
like image 106
Andrei Stefan Avatar answered Sep 28 '22 09:09

Andrei Stefan