# Updating document and adding new field

**URL:** <https://discuss.elastic.co/t/updating-document-and-adding-new-field/61705>\
**Category:** Elasticsearch\
**Created:** [September 28, 2016, 2:10pm UTC](https://discuss.elastic.co/t/updating-document-and-adding-new-field/61705 "2016-09-28T14:10:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![v.kumar](https://avatars.discourse-cdn.com/v4/letter/v/f475e1/32.png) [@v.kumar](https://discuss.elastic.co/u/v.kumar)\
**Post date:** [September 28, 2016, 2:10pm UTC](https://discuss.elastic.co/t/updating-document-and-adding-new-field/61705/1 "2016-09-28T14:10:00Z")

</div>

Hi All,

We have usecase that data will be updated daily. Some of attributes of document changes and some of new record is there. Is it possible to reindex data with updated value, which is already there and add new reocord.  
if yes, please explain how.

Is it with update API?

---

<div class="post-metadata">

**Author:** ![v.kumar](https://avatars.discourse-cdn.com/v4/letter/v/f475e1/32.png) [@v.kumar](https://discuss.elastic.co/u/v.kumar)\
**Post date:** [September 28, 2016, 2:52pm UTC](https://discuss.elastic.co/t/updating-document-and-adding-new-field/61705/2 "2016-09-28T14:52:45Z")

</div>

I am indexing like this

```
 String json = getJsonMapper().writeValueAsString(data);
    bulkRequestBuilder.add(getClient().prepareIndex(indexName, typeName).setSource(json));

```

I am not passing any id. How can i update this. What is best way

---

<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:** [September 28, 2016, 3:13pm UTC](https://discuss.elastic.co/t/updating-document-and-adding-new-field/61705/3 "2016-09-28T15:13:40Z")

</div>

If you are not setting by yourself the `_id` and are not reading the generated `_id` by elasticsearch so you can't really update the document itself.

You can use the [Update by query](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html) feature though.

---

<div class="post-metadata">

**Author:** ![v.kumar](https://avatars.discourse-cdn.com/v4/letter/v/f475e1/32.png) [@v.kumar](https://discuss.elastic.co/u/v.kumar)\
**Post date:** [September 28, 2016, 3:47pm UTC](https://discuss.elastic.co/t/updating-document-and-adding-new-field/61705/4 "2016-09-28T15:47:21Z")

</div>

Hi David,

Thanks for reply. If i set id with below code. So ideally, i should delete those document which i need to update then index again.  
Is this the best approach? What you suggest.

public void add(String indexName, String type, String id, String parent, String routing, String ts, String json) {

```
    // make sure that these are not null
    Preconditions.checkNotNull(indexName);
    Preconditions.checkNotNull(type);
    Preconditions.checkNotNull(json);

    IndexRequestBuilder indexRequestBuilder = manager.getClient()
            .prepareIndex(indexName, type)
            .setSource(json);

    // / They didn't specify an ID, so we will create one for them.
    if(id != null)
        indexRequestBuilder.setId(id);

    if(ts != null)
        indexRequestBuilder.setTimestamp(ts);

    if(parent != null)
        indexRequestBuilder.setParent(parent);

    if(routing != null)
        indexRequestBuilder.setRouting(routing);

    add(indexRequestBuilder.request());
}
```

---

<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:** [September 28, 2016, 4:05pm UTC](https://discuss.elastic.co/t/updating-document-and-adding-new-field/61705/5 "2016-09-28T16:05:16Z")

</div>

If you set the `_id` then you don't need to delete the document and index it again but just send the index operation again using the same `index/type/id` and you're done.

Behind the scene, elasticsearch will delete the document and index a new version.

---

<div class="post-metadata">

**Author:** ![v.kumar](https://avatars.discourse-cdn.com/v4/letter/v/f475e1/32.png) [@v.kumar](https://discuss.elastic.co/u/v.kumar)\
**Post date:** [September 28, 2016, 8:22pm UTC](https://discuss.elastic.co/t/updating-document-and-adding-new-field/61705/6 "2016-09-28T20:22:16Z")

</div>

Is it. Then its cool. I will try that.

Then when we should use update and reindex api. What the use case for using that

---

<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:16pm UTC](https://discuss.elastic.co/t/updating-document-and-adding-new-field/61705/7 "2017-07-05T22:16:30Z")

</div>


