# Add new field to a nested object

**URL:** <https://discuss.elastic.co/t/add-new-field-to-a-nested-object/63891>\
**Category:** Elasticsearch\
**Created:** [October 25, 2016, 2:50pm UTC](https://discuss.elastic.co/t/add-new-field-to-a-nested-object/63891 "2016-10-25T14:50:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![trex](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trex/32/34230_2.png) [@trex](https://discuss.elastic.co/u/trex)\
**Post date:** [October 25, 2016, 2:50pm UTC](https://discuss.elastic.co/t/add-new-field-to-a-nested-object/63891/1 "2016-10-25T14:50:01Z")

</div>

_Elasticsearch 2.3.3_

I can't add a new field into an existing document via [update](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html) API.  
How can I add it?

Below you can see [Elasticsearch Python API](https://elasticsearch-py.readthedocs.io/en/master/api.html) script for the document update. The update query is assigned to the `query` variable.

```
import pdb, json
from elasticsearch import Elasticsearch
from settings import *

def main():
    es = Elasticsearch(hosts = [{'host': es_hosts, 'port': es_port}], timeout = 60)

    query = {
        "script" : "ctx._source.Demographic_Details[0].Match = 1"
    }

    result = es.update(index = es_index, \
            id = "506GBBO25953J", doc_type = "User", body = query, refresh = "true")

    print(json.dumps(result, indent = 2))

if __name__ == " __main__":
    main()

```

I use nested objects. There are no problems with updating not nested fields.

A piece of Elasticsearch document:

```
{
  "hits": {
    "hits": [
      {
        "_score": 0.0, 
        "_type": "User", 
        "_id": "506GBBO25953J", 
        "_source": {
          ...
          "Demographic_Details": [
            {
              "comment": null,
              ... 
              "Occupation": ""
            }
          ], 
          ...
        }, 
        "_index": "logic"
      }
    ], 
    "total": 1, 
    "max_score": 0.0
  }, 
  ...
}

```

A piece of Elasticsearch mappings:

```
{
  "logic" : {
    "mappings" : {
      "Patient" : {
        "_all" : {
          "analyzer" : "edge_ngram_analyzer",
          "search_analyzer" : "keyword_analyzer"
        },
        "properties" : {
          "ID" : {
            "type" : "string"
          },
          "Demographic_Details" : {
            "type" : "nested",
            "properties" : {
              ...
              "Occupation" : {
                "type" : "string",
                "analyzer" : "edge_ngram_analyzer",
                "search_analyzer" : "keyword_analyzer"
              },
              "comment" : {
                "type" : "string",
                "analyzer" : "edge_ngram_analyzer",
                "search_analyzer" : "keyword_analyzer"
              },
              "Match" : {
                "type" : "long"
              }
            }
          },
          ...
        }
      }
    }
  }
}

```

The following error appears after update run:

```
Traceback (most recent call last):
  File "./update.py", line 26, in <module>
    main()
  File "./update.py", line 21, in main
    id = "506GBBO25953J", doc_type = "User", body = query, refresh = "true")
  File "/home/trex/Development/Sirius/new/project0/project0-venv/local/lib/python2.7/site-packages/elasticsearch/client/utils.py", line 69, in _wrapped
    return func(*args, params=params, **kwargs)
  File "/home/trex/Development/Sirius/new/project0/project0-venv/local/lib/python2.7/site-packages/elasticsearch/client/ __init__.py", line 460, in update
    doc_type, id, '_update'), params=params, body=body)
  File "/home/trex/Development/Sirius/new/project0/project0-venv/local/lib/python2.7/site-packages/elasticsearch/transport.py", line 329, in perform_request
    status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
  File "/home/trex/Development/Sirius/new/project0/project0-venv/local/lib/python2.7/site-packages/elasticsearch/connection/http_urllib3.py", line 109, in perform_request
    self._raise_error(response.status, raw_data)
  File "/home/trex/Development/Sirius/new/project0/project0-venv/local/lib/python2.7/site-packages/elasticsearch/connection/base.py", line 108, in _raise_error
    raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.RequestError: TransportError(400, u'illegal_argument_exception', u'[es2][192.168.1.10:9300][indices:data/write/update[s]]')

```

---

<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:** [July 5, 2017, 10:09pm UTC](https://discuss.elastic.co/t/add-new-field-to-a-nested-object/63891/2 "2017-07-05T22:09:58Z")

</div>


