Unable to automatically index nested json as nested type elastic search

I am trying to index a nested json into elastic search using the bulk api. When i pass a nested json to the bulk api method, es was able to automatically identify the nested structure. Here is the json i passed -

    employee
    {
     "firstname":"manager",
     "email":"test",
    assistants:[
    {
       "firstname":"assistant1"
    },
    {
       "firstname":"assistant2"
    }
    ]
    }

Issue is that upon trying to use a nested query on the above documents , i get an error - nested object under path [assistants] is not of nested type

Do we need to explicitly define mapping for an index when we wish it to have nested structure where we define the types as nested ??
Doesnt es automatically pick this nested structure when we send a nested json to index. My first impression was that it did because when i used a MatchAll Query i got a json response in the exact format as my indexed json. Using the nested query is where i get the above error.

Any suggestions ?

Nested mappings are something you need to explicitly opt into.
If your objects only have a single field and value (like “lastname”) then there’s no benefit to mapping as a nested type

So my actual json is like this. The above was just a shortened version -

employee
    {
      "id":"1",
     "firstname":"managerFirst",
     "lastname":"managerLast"
     "email":"test",
     "address":"test",
     "gender":"male"
    assistants:[
    {
       "id":"122"
       "firstname":"assistant1",
       "lastname":"assistant1Last"
    },
    {
       "id":"234"
       "firstname":"assistant2",
       "lastname":"assistant2Last"
    }
    ]
    }

Given the above json do you recommend storing assistants as a nested type ? @Mark_Harwood
My requirement is that i want to be able to retrieve parent docs using properties of a child such that retrieve all managers who have an assistant named test.

I was able to search using a match query on assistants.firstname and assistants.lastname but could this cause me any problems in future that i am not aware of or is this way of searching totally fine given my requiremnt and document strcture

Generally the decision process for when to use nested mappings comes down to this

I am a little confused about which option to choose. So while searching my array of objects, I would be searching on two fields firstname and lastname. This is easily do-able using just assistants.firstname and assistants.lastname. As per the above flowchart, i should use nested json. @Mark_Harwood Can you please advice ?

The reason nested is required is to avoid “cross-matching”. This old slide deck illustrates how this problem happens in Lucene and what nested indexes/queries do to resolve it : Proposal for nested document support in Lucene

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