# Modifying response

**URL:** <https://discuss.elastic.co/t/modifying-response/197215>\
**Category:** Elasticsearch\
**Created:** [August 28, 2019, 7:49pm UTC](https://discuss.elastic.co/t/modifying-response/197215 "2019-08-28T19:49:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![haroutk](https://avatars.discourse-cdn.com/v4/letter/h/e274bd/32.png) [@haroutk](https://discuss.elastic.co/u/haroutk)\
**Post date:** [August 28, 2019, 7:49pm UTC](https://discuss.elastic.co/t/modifying-response/197215/1 "2019-08-28T19:49:35Z")

</div>

We have a need to modify the response document(s) from a query before returning it to the user.  
For example, we might need to rename a document field to a more user-friendly name.

I understand that elasticsearch has extensions or other means of doing this. So if anyone can point me in the right direction, i would greatly appreciate it.

thanks

---

<div class="post-metadata">

**Author:** ![wangqinghuan](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@wangqinghuan](https://discuss.elastic.co/u/wangqinghuan)\
**Post date:** [August 29, 2019, 3:15am UTC](https://discuss.elastic.co/t/modifying-response/197215/2 "2019-08-29T03:15:31Z")

</div>

[Painless Script](https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-field-context.html) is an option:

```
{
    "query" : {
        "match_all": {}
    },
    "script_fields" : {
        "alias_field" : {
            "script" : {
                "source": "doc['name.keyword'].value"
            }
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![haroutk](https://avatars.discourse-cdn.com/v4/letter/h/e274bd/32.png) [@haroutk](https://discuss.elastic.co/u/haroutk)\
**Post date:** [August 29, 2019, 3:46am UTC](https://discuss.elastic.co/t/modifying-response/197215/3 "2019-08-29T03:46:01Z")

</div>

Thank you for your reply. Much appreciate it.  
I’m sorry but I must not be getting it, because when I read the docs in painless APIs it seems to work for values of fields VS actual field names...but the Painless APIs might be the right answer and I am not getting it.  
Can you confirm how I can change the field name in a source doc, say from “random\_field\_name” to “friendly\_field\_name”

Thanks

---

<div class="post-metadata">

**Author:** ![wangqinghuan](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@wangqinghuan](https://discuss.elastic.co/u/wangqinghuan)\
**Post date:** [August 29, 2019, 3:55am UTC](https://discuss.elastic.co/t/modifying-response/197215/4 "2019-08-29T03:55:41Z")

</div>

> [@wangqinghuan](#):
>
> { "query" : { "match\_all": {} }, "script\_fields" : { "alias\_field" : { "script" : { "source": "doc['name.keyword'].value" } } } }

This create a script field 'friendly\_field\_name' to replace field 'random\_field\_name' in source doc:

```
  {
        "query" : {
            "match_all": {}
        },
        "script_fields" : {
            "friendly_field_name" : {
                "script" : {
                    "source": "doc['random_field_name.keyword'].value"
                }
            }
        }
    }

```

And it returns like:

```
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "test2",
        "_type": "doc",
        "_id": "2",
        "_score": 1,
        "fields": {
          "friendly_field_name": [
            "abc"
          ]
        }
      },
      {
        "_index": "test2",
        "_type": "doc",
        "_id": "1",
        "_score": 1,
        "fields": {
          "friendly_field_name": [
            "abc"
          ]
        }
      }
    ]
  }
}

```

---

<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:** [August 29, 2019, 4:07am UTC](https://discuss.elastic.co/t/modifying-response/197215/5 "2019-08-29T04:07:48Z")

</div>

As far as I know you can not change the source returned.

---

<div class="post-metadata">

**Author:** ![haroutk](https://avatars.discourse-cdn.com/v4/letter/h/e274bd/32.png) [@haroutk](https://discuss.elastic.co/u/haroutk)\
**Post date:** [August 29, 2019, 4:45am UTC](https://discuss.elastic.co/t/modifying-response/197215/6 "2019-08-29T04:45:26Z")

</div>

Thanks  
if i am understand your, this script will be added to the \_search endpoint and it will ADD new scripted field in the response, called "friendly\_field\_name" but the old field "random\_field\_name" will still be there too?

> [@wangqinghuan](#):
>
> { "query" : { "match\_all": {} }, "script\_fields" : { "friendly\_field\_name" : { "script" : { "source": "doc['random\_field\_name.keyword'].value" } } } }

If that is true, is there a way just to rename the field with a script without adding a new field?

---

<div class="post-metadata">

**Author:** ![wangqinghuan](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@wangqinghuan](https://discuss.elastic.co/u/wangqinghuan)\
**Post date:** [August 29, 2019, 4:58am UTC](https://discuss.elastic.co/t/modifying-response/197215/7 "2019-08-29T04:58:05Z")

</div>

> the old field "random\_field\_name" will still be there too?

The old field will not be in response.

> is there a way just to rename the field with a script without adding a new field

The source can not be changed. The option I have learned is adding a new script field using Painless Script.

---

<div class="post-metadata">

**Author:** ![haroutk](https://avatars.discourse-cdn.com/v4/letter/h/e274bd/32.png) [@haroutk](https://discuss.elastic.co/u/haroutk)\
**Post date:** [August 29, 2019, 5:34am UTC](https://discuss.elastic.co/t/modifying-response/197215/8 "2019-08-29T05:34:13Z")

</div>

@wangqinghuan this is awesome. Thanks.  
Where r u located? Are you interested in freelance work? We could use someone like you on our team.

---

<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:** [September 26, 2019, 5:34am UTC](https://discuss.elastic.co/t/modifying-response/197215/9 "2019-09-26T05:34:16Z")

</div>

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