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

**URL:** https://discuss.elastic.co/t/in-elastic-search-can-we-change-the-names-of-retrieved-fields-of-searched-response-dynamically-like-mongodb-projection/330599
**Category:** Elasticsearch
**Created:** [April 24, 2023, 6:06am UTC](https://discuss.elastic.co/t/in-elastic-search-can-we-change-the-names-of-retrieved-fields-of-searched-response-dynamically-like-mongodb-projection/330599 "2023-04-24T06:06:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![cvam199](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cvam199/32/120120_2.png) [@cvam199](https://discuss.elastic.co/u/cvam199)
#### Post date: [April 24, 2023, 6:06am UTC](https://discuss.elastic.co/t/in-elastic-search-can-we-change-the-names-of-retrieved-fields-of-searched-response-dynamically-like-mongodb-projection/330599/1 "2023-04-24T06:06:20Z")

</div>

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

Response:

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

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

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

---

<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: [April 24, 2023, 6:12am UTC](https://discuss.elastic.co/t/in-elastic-search-can-we-change-the-names-of-retrieved-fields-of-searched-response-dynamically-like-mongodb-projection/330599/2 "2023-04-24T06:12:09Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [April 24, 2023, 6:27am UTC](https://discuss.elastic.co/t/in-elastic-search-can-we-change-the-names-of-retrieved-fields-of-searched-response-dynamically-like-mongodb-projection/330599/3 "2023-04-24T06:27:01Z")

</div>

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

---

<div class="post-metadata">

### Author: ![cvam199](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cvam199/32/120120_2.png) [@cvam199](https://discuss.elastic.co/u/cvam199)
#### Post date: [April 24, 2023, 6:42am UTC](https://discuss.elastic.co/t/in-elastic-search-can-we-change-the-names-of-retrieved-fields-of-searched-response-dynamically-like-mongodb-projection/330599/4 "2023-04-24T06:42:21Z")

</div>

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

---

<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 22, 2023, 6:43am UTC](https://discuss.elastic.co/t/in-elastic-search-can-we-change-the-names-of-retrieved-fields-of-searched-response-dynamically-like-mongodb-projection/330599/5 "2023-05-22T06:43:17Z")

</div>

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