# How can we eliminate the root structure for nested elements in the query result in Elasticsearch?

**URL:** <https://discuss.elastic.co/t/how-can-we-eliminate-the-root-structure-for-nested-elements-in-the-query-result-in-elasticsearch/163308>\
**Category:** Elasticsearch\
**Created:** [January 8, 2019, 6:53am UTC](https://discuss.elastic.co/t/how-can-we-eliminate-the-root-structure-for-nested-elements-in-the-query-result-in-elasticsearch/163308 "2019-01-08T06:53:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinodanumarla](https://avatars.discourse-cdn.com/v4/letter/v/a698b9/32.png) [@vinodanumarla](https://discuss.elastic.co/u/vinodanumarla)\
**Post date:** [January 8, 2019, 6:53am UTC](https://discuss.elastic.co/t/how-can-we-eliminate-the-root-structure-for-nested-elements-in-the-query-result-in-elasticsearch/163308/1 "2019-01-08T06:53:38Z")

</div>

How to write a query to eliminate nested elements "results" and "lines" from the below document.

```
{
  "_id": "0048160a463a73faaa6c90f5af027772",

  "_rev": "1-ff6255309f1b873a4e482310843a8a15",

  "timestamp": 1496275536932.6602,

  "results": {

    "lines": {

      "S1": [

        {

          "needed_key": "foo",

          "not_needed_key": 1

        }

      ],

      "S2": [

        {

          "needed_key": "bar",

          "not_needed_key": 1

        },

        {

          "needed_key": "foo_bar",

          "not_needed_key": 1

        }

      ],

      ...

    }

  },

  "station": "5002270",

  "another_not_needed_key": "something"

}

```

How can I write an elasticsearch query to get the output with out root structure from above document?

Below is my expected output.

```auto
{
 "_id": "fd298368a7a344b217698677f3f5a07d",
 "timestamp": 1496275536932.6602,
 "station": "5002270",
 "S1": {
     "needed_key": "foo",
     "not_needed_key": 1
    }
}

```

Thanks in advance..!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 8, 2019, 7:36am UTC](https://discuss.elastic.co/t/how-can-we-eliminate-the-root-structure-for-nested-elements-in-the-query-result-in-elasticsearch/163308/2 "2019-01-08T07:36:28Z")

</div>

You can't rewrite a document in elasticsearch at search time.

But the question is may be why you did not index your document that way?

```auto
PUT /index/_doc/fd298368a7a344b217698677f3f5a07d
{
 "timestamp": 1496275536932.6602,
 "station": "5002270",
 "S1": {
     "needed_key": "foo",
     "not_needed_key": 1
    }
}

```

---

<div class="post-metadata">

**Author:** ![vinodanumarla](https://avatars.discourse-cdn.com/v4/letter/v/a698b9/32.png) [@vinodanumarla](https://discuss.elastic.co/u/vinodanumarla)\
**Post date:** [January 8, 2019, 8:23am UTC](https://discuss.elastic.co/t/how-can-we-eliminate-the-root-structure-for-nested-elements-in-the-query-result-in-elasticsearch/163308/3 "2019-01-08T08:23:21Z")

</div>

Thanks for the quick response @dadoonet

But my actual document consisting of 18 nested elements. So, which will be difficult to maintain each nested node in a separate index.

Thanks..!

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [January 8, 2019, 8:26am UTC](https://discuss.elastic.co/t/how-can-we-eliminate-the-root-structure-for-nested-elements-in-the-query-result-in-elasticsearch/163308/4 "2019-01-08T08:26:53Z")

</div>

Why would you not be able to store them in separate documents within the index? How frequently do you update these documents? Why would maintaining this be more difficult?

---

<div class="post-metadata">

**Author:** ![vinodanumarla](https://avatars.discourse-cdn.com/v4/letter/v/a698b9/32.png) [@vinodanumarla](https://discuss.elastic.co/u/vinodanumarla)\
**Post date:** [January 8, 2019, 8:36am UTC](https://discuss.elastic.co/t/how-can-we-eliminate-the-root-structure-for-nested-elements-in-the-query-result-in-elasticsearch/163308/5 "2019-01-08T08:36:56Z")

</div>

@Christian_Dahlqvist and @dadoonet : I need to write elasticsearch queries to fetch data from multiple nodes. So If I store multiple documents under single index would be problem for me to fetch the single search result from multiple documents.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [January 8, 2019, 8:39am UTC](https://discuss.elastic.co/t/how-can-we-eliminate-the-root-structure-for-nested-elements-in-the-query-result-in-elasticsearch/163308/6 "2019-01-08T08:39:32Z")

</div>

Then it sounds to me like you will need to do some client-side post processing no matter which solution you go for.

---

<div class="post-metadata">

**Author:** ![vinodanumarla](https://avatars.discourse-cdn.com/v4/letter/v/a698b9/32.png) [@vinodanumarla](https://discuss.elastic.co/u/vinodanumarla)\
**Post date:** [January 8, 2019, 8:53am UTC](https://discuss.elastic.co/t/how-can-we-eliminate-the-root-structure-for-nested-elements-in-the-query-result-in-elasticsearch/163308/7 "2019-01-08T08:53:21Z")

</div>

Okay.! thanks @Christian_Dahlqvist

So we don't have any prompt solution to remove the root structure while we query for nested elements during search time from elasticsearch side ? please correct me if I am wrong.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [January 8, 2019, 8:56am UTC](https://discuss.elastic.co/t/how-can-we-eliminate-the-root-structure-for-nested-elements-in-the-query-result-in-elasticsearch/163308/8 "2019-01-08T08:56:18Z")

</div>

No, as @dadoonet explained, you can not rewrite documents in Elasticsearch at search time.

---

<div class="post-metadata">

**Author:** ![vinodanumarla](https://avatars.discourse-cdn.com/v4/letter/v/a698b9/32.png) [@vinodanumarla](https://discuss.elastic.co/u/vinodanumarla)\
**Post date:** [January 8, 2019, 9:01am UTC](https://discuss.elastic.co/t/how-can-we-eliminate-the-root-structure-for-nested-elements-in-the-query-result-in-elasticsearch/163308/9 "2019-01-08T09:01:26Z")

</div>

@Christian_Dahlqvist : I don't want to rewrite the document in Elasticsearch, I Just wanted to restructure( like : removing the root structure, etc.. ) the elasticsearch results during search time

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 8, 2019, 9:01am UTC](https://discuss.elastic.co/t/how-can-we-eliminate-the-root-structure-for-nested-elements-in-the-query-result-in-elasticsearch/163308/10 "2019-01-08T09:01:44Z")

</div>

You can't.

---

<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:** [February 5, 2019, 9:01am UTC](https://discuss.elastic.co/t/how-can-we-eliminate-the-root-structure-for-nested-elements-in-the-query-result-in-elasticsearch/163308/11 "2019-02-05T09:01:52Z")

</div>

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