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

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.

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

Thanks in advance..!

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?

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

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..!

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?

@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.

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

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.

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

@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

You can't.

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