# Unable to automatically index nested json as nested type elastic search

**URL:** <https://discuss.elastic.co/t/unable-to-automatically-index-nested-json-as-nested-type-elastic-search/270627>\
**Category:** Elasticsearch\
**Created:** [April 19, 2021, 9:07pm UTC](https://discuss.elastic.co/t/unable-to-automatically-index-nested-json-as-nested-type-elastic-search/270627 "2021-04-19T21:07:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![schawla](https://avatars.discourse-cdn.com/v4/letter/s/87869e/32.png) [@schawla](https://discuss.elastic.co/u/schawla)\
**Post date:** [April 19, 2021, 9:07pm UTC](https://discuss.elastic.co/t/unable-to-automatically-index-nested-json-as-nested-type-elastic-search/270627/1 "2021-04-19T21:07:50Z")

</div>

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 -

```auto
    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 ?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [April 19, 2021, 9:12pm UTC](https://discuss.elastic.co/t/unable-to-automatically-index-nested-json-as-nested-type-elastic-search/270627/3 "2021-04-19T21:12:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![schawla](https://avatars.discourse-cdn.com/v4/letter/s/87869e/32.png) [@schawla](https://discuss.elastic.co/u/schawla)\
**Post date:** [April 19, 2021, 9:31pm UTC](https://discuss.elastic.co/t/unable-to-automatically-index-nested-json-as-nested-type-elastic-search/270627/4 "2021-04-19T21:31:48Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [April 19, 2021, 9:52pm UTC](https://discuss.elastic.co/t/unable-to-automatically-index-nested-json-as-nested-type-elastic-search/270627/5 "2021-04-19T21:52:29Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/f/5/f510feafc19d345c85f2150796fa8caf9a58962c.png)

---

<div class="post-metadata">

**Author:** ![schawla](https://avatars.discourse-cdn.com/v4/letter/s/87869e/32.png) [@schawla](https://discuss.elastic.co/u/schawla)\
**Post date:** [April 20, 2021, 6:59am UTC](https://discuss.elastic.co/t/unable-to-automatically-index-nested-json-as-nested-type-elastic-search/270627/6 "2021-04-20T06:59:22Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [April 20, 2021, 7:20am UTC](https://discuss.elastic.co/t/unable-to-automatically-index-nested-json-as-nested-type-elastic-search/270627/7 "2021-04-20T07:20:05Z")

</div>

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](https://www.slideshare.net/MarkHarwood/proposal-for-nested-document-support-in-lucene?from_m_app=ios)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 18, 2021, 7:20am UTC](https://discuss.elastic.co/t/unable-to-automatically-index-nested-json-as-nested-type-elastic-search/270627/8 "2021-05-18T07:20:24Z")

</div>

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