# Update field

**URL:** https://discuss.elastic.co/t/update-field/6306
**Category:** Elasticsearch
**Created:** [January 6, 2012, 11:46pm UTC](https://discuss.elastic.co/t/update-field/6306 "2012-01-06T23:46:57Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Frank\_LaRosa](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@Frank\_LaRosa](https://discuss.elastic.co/u/Frank_LaRosa)
#### Post date: [January 6, 2012, 11:46pm UTC](https://discuss.elastic.co/t/update-field/6306/1 "2012-01-06T23:46:57Z")

</div>

Is there a way (using the Java API) to update a field without having  
to re-send all the other fields in the document?

---

<div class="post-metadata">

### Author: ![Benjamin\_Deveze](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benjamin_deveze/32/1577_2.png) [@Benjamin\_Deveze](https://discuss.elastic.co/u/Benjamin_Deveze)
#### Post date: [January 6, 2012, 11:54pm UTC](https://discuss.elastic.co/t/update-field/6306/2 "2012-01-06T23:54:30Z")

</div>

You are a lucky the update API has just been commited in master branch and  
will be available in the next released version.

Have a look here for more information:

> <https://github.com/elastic/elasticsearch/issues/1583>

---

<div class="post-metadata">

### Author: ![Frank\_LaRosa](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@Frank\_LaRosa](https://discuss.elastic.co/u/Frank_LaRosa)
#### Post date: [January 9, 2012, 5:09pm UTC](https://discuss.elastic.co/t/update-field/6306/3 "2012-01-09T17:09:38Z")

</div>

Thanks. In the meantime, will this work?

Suppose I have a SearchHit object resulting from a search, and I use  
sourceAsMap to get the source as a map:

Map\<String,Object\> source = hit.sourceAsMap();

Then I modify the map in some way:

source.put("somefield", "someUpdatedValue");

Then I re-index the source:

client.prepareIndex("myIndex", "myType",  
hit.id()).setSource(source).setOpType(OpType.INDEX).execute();

Will this correctly re-index the document with the new value?  
Specifically, is there any danger that in doing this, I might lose  
something that was previously part of the document (perhaps, a field  
that was indexed but not stored)?

Thanks,  
Frank

On Jan 6, 5:54 pm, Benjamin Devèze [benjamin.dev...@gmail.com](mailto:benjamin.dev...@gmail.com) wrote:

> You are a lucky the update API has just been commited in master branch and  
> will be available in the next released version.
> 
> Have a look here for more information:[Update API: Allow to update a document based on a script · Issue #1583 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/1583)

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [January 9, 2012, 7:32pm UTC](https://discuss.elastic.co/t/update-field/6306/4 "2012-01-09T19:32:42Z")

</div>

Yes, that would work. Note, you might want to use versioning to make sure  
an update did not happen in the meantime. When you search, specify that you  
want to get versions back (SearchRequestBuilder#setVersion(true)), and then  
index the document with the version you get. If there was an update while  
it happened, you will get a version conflict failure.

On Mon, Jan 9, 2012 at 7:09 PM, Frank LaRosa [frank@studyblue.com](mailto:frank@studyblue.com) wrote:

> Thanks. In the meantime, will this work?
> 
> Suppose I have a SearchHit object resulting from a search, and I use  
> sourceAsMap to get the source as a map:
> 
> Map\<String,Object\> source = hit.sourceAsMap();
> 
> Then I modify the map in some way:
> 
> source.put("somefield", "someUpdatedValue");
> 
> Then I re-index the source:
> 
> client.prepareIndex("myIndex", "myType",  
> hit.id()).setSource(source).setOpType(OpType.INDEX).execute();
> 
> Will this correctly re-index the document with the new value?  
> Specifically, is there any danger that in doing this, I might lose  
> something that was previously part of the document (perhaps, a field  
> that was indexed but not stored)?
> 
> Thanks,  
> Frank
> 
> On Jan 6, 5:54 pm, Benjamin Devèze [benjamin.dev...@gmail.com](mailto:benjamin.dev...@gmail.com) wrote:
> 
> > You are a lucky the update API has just been commited in master branch  
> > and  
> > will be available in the next released version.
> > 
> > Have a look here for more information:  
> > [Update API: Allow to update a document based on a script · Issue #1583 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/1583)

---

<div class="post-metadata">

### Author: ![Frank\_LaRosa](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@Frank\_LaRosa](https://discuss.elastic.co/u/Frank_LaRosa)
#### Post date: [January 9, 2012, 8:22pm UTC](https://discuss.elastic.co/t/update-field/6306/5 "2012-01-09T20:22:34Z")

</div>

Great, thanks for the response. I'll add the version number, too.

If there's a version conflict, will it be thrown immediately by  
client.prepareIndex(..).execute(), or do I need to call actionGet() to  
examine the response?

On Jan 9, 1:32 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> Yes, that would work. Note, you might want to use versioning to make sure  
> an update did not happen in the meantime. When you search, specify that you  
> want to get versions back (SearchRequestBuilder#setVersion(true)), and then  
> index the document with the version you get. If there was an update while  
> it happened, you will get a version conflict failure.
> 
> On Mon, Jan 9, 2012 at 7:09 PM, Frank LaRosa [fr...@studyblue.com](mailto:fr...@studyblue.com) wrote:
> 
> > Thanks. In the meantime, will this work?
> 
> > Suppose I have a SearchHit object resulting from a search, and I use  
> > sourceAsMap to get the source as a map:
> 
> > Map\<String,Object\> source = hit.sourceAsMap();
> 
> > Then I modify the map in some way:
> 
> > source.put("somefield", "someUpdatedValue");
> 
> > Then I re-index the source:
> 
> > client.prepareIndex("myIndex", "myType",  
> > hit.id()).setSource(source).setOpType(OpType.INDEX).execute();
> 
> > Will this correctly re-index the document with the new value?  
> > Specifically, is there any danger that in doing this, I might lose  
> > something that was previously part of the document (perhaps, a field  
> > that was indexed but not stored)?
> 
> > Thanks,  
> > Frank
> 
> > On Jan 6, 5:54 pm, Benjamin Devèze [benjamin.dev...@gmail.com](mailto:benjamin.dev...@gmail.com) wrote:
> > 
> > > You are a lucky the update API has just been commited in master branch  
> > > and  
> > > will be available in the next released version.
> 
> > > Have a look here for more information:  
> > > [Update API: Allow to update a document based on a script · Issue #1583 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/1583)

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [January 9, 2012, 9:09pm UTC](https://discuss.elastic.co/t/update-field/6306/6 "2012-01-09T21:09:05Z")

</div>

execute just executes the operation, you get back a future to actually get  
the response / exception, so yes, you need to call get to know what  
happened.

On Mon, Jan 9, 2012 at 10:22 PM, Frank LaRosa [frank@studyblue.com](mailto:frank@studyblue.com) wrote:

> Great, thanks for the response. I'll add the version number, too.
> 
> If there's a version conflict, will it be thrown immediately by  
> client.prepareIndex(..).execute(), or do I need to call actionGet() to  
> examine the response?
> 
> On Jan 9, 1:32 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:
> 
> > Yes, that would work. Note, you might want to use versioning to make sure  
> > an update did not happen in the meantime. When you search, specify that  
> > you  
> > want to get versions back (SearchRequestBuilder#setVersion(true)), and  
> > then  
> > index the document with the version you get. If there was an update while  
> > it happened, you will get a version conflict failure.
> > 
> > On Mon, Jan 9, 2012 at 7:09 PM, Frank LaRosa [fr...@studyblue.com](mailto:fr...@studyblue.com)  
> > wrote:
> > 
> > > Thanks. In the meantime, will this work?
> > 
> > > Suppose I have a SearchHit object resulting from a search, and I use  
> > > sourceAsMap to get the source as a map:
> > 
> > > Map\<String,Object\> source = hit.sourceAsMap();
> > 
> > > Then I modify the map in some way:
> > 
> > > source.put("somefield", "someUpdatedValue");
> > 
> > > Then I re-index the source:
> > 
> > > client.prepareIndex("myIndex", "myType",  
> > > hit.id()).setSource(source).setOpType(OpType.INDEX).execute();
> > 
> > > Will this correctly re-index the document with the new value?  
> > > Specifically, is there any danger that in doing this, I might lose  
> > > something that was previously part of the document (perhaps, a field  
> > > that was indexed but not stored)?
> > 
> > > Thanks,  
> > > Frank
> > 
> > > On Jan 6, 5:54 pm, Benjamin Devèze [benjamin.dev...@gmail.com](mailto:benjamin.dev...@gmail.com) wrote:
> > > 
> > > > You are a lucky the update API has just been commited in master  
> > > > branch  
> > > > and  
> > > > will be available in the next released version.
> > 
> > > > Have a look here for more information:  
> > > > [Update API: Allow to update a document based on a script · Issue #1583 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/1583)

---

<div class="post-metadata">

### Author: ![Frank\_LaRosa](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@Frank\_LaRosa](https://discuss.elastic.co/u/Frank_LaRosa)
#### Post date: [January 9, 2012, 9:53pm UTC](https://discuss.elastic.co/t/update-field/6306/7 "2012-01-09T21:53:31Z")

</div>

I'm having some trouble. Sometimes it works, and other times it throws  
the VersionConflictEngineException.

I suspect this is happening because in my initial test case I'm simply  
trying to update the same document several times in a tight loop. I  
can see how this might cause version conflicts in a true distributed  
environment, although I'm not sure why it does even in my test  
environment which only has one server.

I tried to solve the problem by  
adding .setConsistencyLevel(WriteConsistencyLevel.ALL) to my write  
operation. However, this causes the write operation to hang  
indefinitely for some reason.

Any suggestion as to what else I can do here?

On Jan 9, 3:09 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> execute just executes the operation, you get back a future to actually get  
> the response / exception, so yes, you need to call get to know what  
> happened.
> 
> On Mon, Jan 9, 2012 at 10:22 PM, Frank LaRosa [fr...@studyblue.com](mailto:fr...@studyblue.com) wrote:
> 
> > Great, thanks for the response. I'll add the version number, too.
> 
> > If there's a version conflict, will it be thrown immediately by  
> > client.prepareIndex(..).execute(), or do I need to call actionGet() to  
> > examine the response?
> 
> > On Jan 9, 1:32 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:
> > 
> > > Yes, that would work. Note, you might want to use versioning to make sure  
> > > an update did not happen in the meantime. When you search, specify that  
> > > you  
> > > want to get versions back (SearchRequestBuilder#setVersion(true)), and  
> > > then  
> > > index the document with the version you get. If there was an update while  
> > > it happened, you will get a version conflict failure.
> 
> > > On Mon, Jan 9, 2012 at 7:09 PM, Frank LaRosa [fr...@studyblue.com](mailto:fr...@studyblue.com)  
> > > wrote:
> > > 
> > > > Thanks. In the meantime, will this work?
> 
> > > > Suppose I have a SearchHit object resulting from a search, and I use  
> > > > sourceAsMap to get the source as a map:
> 
> > > > Map\<String,Object\> source = hit.sourceAsMap();
> 
> > > > Then I modify the map in some way:
> 
> > > > source.put("somefield", "someUpdatedValue");
> 
> > > > Then I re-index the source:
> 
> > > > client.prepareIndex("myIndex", "myType",  
> > > > hit.id()).setSource(source).setOpType(OpType.INDEX).execute();
> 
> > > > Will this correctly re-index the document with the new value?  
> > > > Specifically, is there any danger that in doing this, I might lose  
> > > > something that was previously part of the document (perhaps, a field  
> > > > that was indexed but not stored)?
> 
> > > > Thanks,  
> > > > Frank
> 
> > > > On Jan 6, 5:54 pm, Benjamin Devèze [benjamin.dev...@gmail.com](mailto:benjamin.dev...@gmail.com) wrote:
> > > > 
> > > > > You are a lucky the update API has just been commited in master  
> > > > > branch  
> > > > > and  
> > > > > will be available in the next released version.
> 
> > > > > Have a look here for more information:  
> > > > > [Update API: Allow to update a document based on a script · Issue #1583 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/1583)

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [January 10, 2012, 8:55am UTC](https://discuss.elastic.co/t/update-field/6306/8 "2012-01-10T08:55:32Z")

</div>

Note, even if you have a single thread doing search / update, the index is  
refreshed every 1 second to have changes visible for search, so, if you  
search, you might get "stale" values. If you did "get" and then "index"  
(thanks to realtime get), you will always get the latest value of the  
document.

On Mon, Jan 9, 2012 at 11:53 PM, Frank LaRosa [frank@studyblue.com](mailto:frank@studyblue.com) wrote:

> I'm having some trouble. Sometimes it works, and other times it throws  
> the VersionConflictEngineException.
> 
> I suspect this is happening because in my initial test case I'm simply  
> trying to update the same document several times in a tight loop. I  
> can see how this might cause version conflicts in a true distributed  
> environment, although I'm not sure why it does even in my test  
> environment which only has one server.
> 
> I tried to solve the problem by  
> adding .setConsistencyLevel(WriteConsistencyLevel.ALL) to my write  
> operation. However, this causes the write operation to hang  
> indefinitely for some reason.
> 
> Any suggestion as to what else I can do here?
> 
> On Jan 9, 3:09 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:
> 
> > execute just executes the operation, you get back a future to actually  
> > get  
> > the response / exception, so yes, you need to call get to know what  
> > happened.
> > 
> > On Mon, Jan 9, 2012 at 10:22 PM, Frank LaRosa [fr...@studyblue.com](mailto:fr...@studyblue.com)  
> > wrote:
> > 
> > > Great, thanks for the response. I'll add the version number, too.
> > 
> > > If there's a version conflict, will it be thrown immediately by  
> > > client.prepareIndex(..).execute(), or do I need to call actionGet() to  
> > > examine the response?
> > 
> > > On Jan 9, 1:32 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:
> > > 
> > > > Yes, that would work. Note, you might want to use versioning to make  
> > > > sure  
> > > > an update did not happen in the meantime. When you search, specify  
> > > > that  
> > > > you  
> > > > want to get versions back (SearchRequestBuilder#setVersion(true)),  
> > > > and  
> > > > then  
> > > > index the document with the version you get. If there was an update  
> > > > while  
> > > > it happened, you will get a version conflict failure.
> > 
> > > > On Mon, Jan 9, 2012 at 7:09 PM, Frank LaRosa [fr...@studyblue.com](mailto:fr...@studyblue.com)  
> > > > wrote:
> > > > 
> > > > > Thanks. In the meantime, will this work?
> > 
> > > > > Suppose I have a SearchHit object resulting from a search, and I  
> > > > > use  
> > > > > sourceAsMap to get the source as a map:
> > 
> > > > > Map\<String,Object\> source = hit.sourceAsMap();
> > 
> > > > > Then I modify the map in some way:
> > 
> > > > > source.put("somefield", "someUpdatedValue");
> > 
> > > > > Then I re-index the source:
> > 
> > > > > client.prepareIndex("myIndex", "myType",  
> > > > > hit.id()).setSource(source).setOpType(OpType.INDEX).execute();
> > 
> > > > > Will this correctly re-index the document with the new value?  
> > > > > Specifically, is there any danger that in doing this, I might lose  
> > > > > something that was previously part of the document (perhaps, a  
> > > > > field  
> > > > > that was indexed but not stored)?
> > 
> > > > > Thanks,  
> > > > > Frank
> > 
> > > > > On Jan 6, 5:54 pm, Benjamin Devèze [benjamin.dev...@gmail.com](mailto:benjamin.dev...@gmail.com)  
> > > > > wrote:
> > > > > 
> > > > > > You are a lucky the update API has just been commited in master  
> > > > > > branch  
> > > > > > and  
> > > > > > will be available in the next released version.
> > 
> > > > > > Have a look here for more information:  
> > > > > > [Update API: Allow to update a document based on a script · Issue #1583 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/1583)

---

<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 6, 2017, 3:43am UTC](https://discuss.elastic.co/t/update-field/6306/9 "2017-07-06T03:43:16Z")

</div>


