Update field


(Frank LaRosa) #1

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


(Benjamin Devèze) #2

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:


(Frank LaRosa) #3

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 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:https://github.com/elasticsearch/elasticsearch/issues/1583


(Shay Banon) #4

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 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 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:
https://github.com/elasticsearch/elasticsearch/issues/1583


(Frank LaRosa) #5

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 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 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 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:
https://github.com/elasticsearch/elasticsearch/issues/1583


(Shay Banon) #6

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 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 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
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 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:
https://github.com/elasticsearch/elasticsearch/issues/1583


(Frank LaRosa) #7

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 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 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 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
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 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:
https://github.com/elasticsearch/elasticsearch/issues/1583


(Shay Banon) #8

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 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 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
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 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
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
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:
https://github.com/elasticsearch/elasticsearch/issues/1583


(system) #9