# How can I add an extra field to a nested document in ElasticSearch 5.6 when searching it

**URL:** <https://discuss.elastic.co/t/how-can-i-add-an-extra-field-to-a-nested-document-in-elasticsearch-5-6-when-searching-it/107538>\
**Category:** Elasticsearch\
**Created:** [November 14, 2017, 11:01am UTC](https://discuss.elastic.co/t/how-can-i-add-an-extra-field-to-a-nested-document-in-elasticsearch-5-6-when-searching-it/107538 "2017-11-14T11:01:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sujoymondal87](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sujoymondal87/32/24184_2.png) [@sujoymondal87](https://discuss.elastic.co/u/sujoymondal87)\
**Post date:** [November 14, 2017, 11:01am UTC](https://discuss.elastic.co/t/how-can-i-add-an-extra-field-to-a-nested-document-in-elasticsearch-5-6-when-searching-it/107538/1 "2017-11-14T11:01:21Z")

</div>

This is our nested document mapping:

```
{
 "properties" :{
  "user_id" :{"type": "integer"},  
  "images" : {
   "type": "nested",
   "properties": {
    "image_path": {"type": "string"}
     }
  }
 }
}

```

How it is indexed on ElasticSearch:

```
{
        "_index": "flags",
        "_type": "flag",
        "_id": "1",
        "_score": 1,
        "_source": {
          "user_id" : 2,
          "images" : [
                    {"image_path" : "123456.jpg"},
                    {"image_path" : "789101.jpg"},
             ]
        }
 }

```

We want to modify the path of "123456.jpg" and, depending of the specific API that was called, return a specific image path (folders name are static and known, we want to concat those with the picture name).

Desired result "upload/asd/123456.jpg":

```
 {
        "_index": "flags",
        "_type": "flag",
        "_id": "1",
        "_score": 1,
        "_source": {
          "user_id" : 2,
          "images" : [
                    {"image_path" : "upload/asd/123456.jpg"},
                    {"image_path" : "upload/qwe/789101.jpg"},
             ]
        }
 }

```

I have tried Script\_fields but it is not working with nested documents.

---

<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:** [November 14, 2017, 11:17am UTC](https://discuss.elastic.co/t/how-can-i-add-an-extra-field-to-a-nested-document-in-elasticsearch-5-6-when-searching-it/107538/2 "2017-11-14T11:17:51Z")

</div>

Why not doing that on the client side before injecting ?

---

<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:** [December 12, 2017, 11:17am UTC](https://discuss.elastic.co/t/how-can-i-add-an-extra-field-to-a-nested-document-in-elasticsearch-5-6-when-searching-it/107538/3 "2017-12-12T11:17:53Z")

</div>

Closed on December 12, 2017, 11:17am UTC

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