In Elastic search, can we change the names of retrieved fields of searched response dynamically, like mongoDB projection?

When I query on Elasticsearch (ES) to get some data, I get it in following format:

Response:

"hits" : [
      {
        "_index" : "testpoc",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "ext" : [
            {
              "sii" : {
                "1" : {
                  "csp" : 19.0
                }
              }
            }
          ],
          "pld" : "PDyfJ3MypQYK4MA2222",
          "tms" : "2022-11-29T11:15:02+0000"
        }
      }]

Current query:

GET testpoc/_search
{
   "_source": [
    "tms",
    "pld",
    "ext.sii.1.csp"
  ]
}

I want to receive response in a particular format, rather than getting "ext[0].sii.1.csp", I want to map it into "csp" field.

Note: "ext[0].sii.1.csp" is dynamic, I can query to get some "ext[0].sii.1.xyz", and I want to receive it as "xyz" field name. Requirement is somewhat similar to "projection" functionality of mongoDB (i.e. include->as method).

Desired response:

 "hits" : [
      {
        "_index" : "testpoc",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "csp" : 19.0,
          "pld" : "PDyfJ3MypQYK4MA2222",
          "tms" : "2022-11-29T11:15:02+0000"
        }
      }]

What should I add in query or search request to get the required response?

I read about the aliasing in ES, but for that I've to change the index mapping (in my project it's not an option), I did try ES scripts also, but it is somewhat similar to processing the response in java code that can be done on project side too.

I want to know if there is anything that can be used to map or change the retrieved fields names of ES search response?

Elasticsearch will return the JSON stored (or selected fields) but I do not think there is any built in support for rewriting data. This is as far as I know something you need to handle client side.

1 Like

Christian is correct, there's no way to do this in Elasticsearch.

Thanks @Christian_Dahlqvist @warkolm. I will make the required changes in client side.

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