Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch Jest update a whole document

I have an elasticsearch server which i'm accessing via a java server using the Jest client and i was looking for the best way to update multiple fields of a document each time.

I have looked to the documentation so far, and i have found that there are two way for doing it :

  1. Partial update via a script : i don't think it is suitable for multiple field update (because i don't know the modified fields).
  2. Whole document update: via re-indexing the whole document.

My question is how could i update the whole document knowing that Jest provide only update via a script?

Is it the best way to delete a document and indexing the updated version?

like image 886
Master Mind Avatar asked May 13 '15 18:05

Master Mind


People also ask

Can you update a document in Elasticsearch?

DescriptioneditEnables you to script document updates. The script can update, delete, or skip modifying the document. The update API also supports passing a partial document, which is merged into the existing document. To fully replace an existing document, use the index API.

Is Elasticsearch good for updates?

Elasticsearch does make it clear that updating existing mappings in an index is usually not an option, but there seem to be some cases where you are able to change the mapping without triggering any error at the time of changing the mapping, but later no documents will be able to write to the index.

What is Elasticsearch Upsert?

Upserts are "Update or Insert" operations. This means an upsert attempts to run your update script, but if the document does not exist (or the field you are trying to update doesn't exist), default values are inserted instead.

What is update in Elasticsearch?

In addition to being able to index and replace documents, we can also update documents. Note that Elasticsearch does not actually do in-place updates under the hood. Whenever we do an update, Elasticsearch deletes the old document and then indexes a new document with the update applied to it in one shot.


1 Answers

Already answered this in the github issue you also opened but again:

You should use the second way you linked (Whole document update) and there is no special API for it, it's just a regular index request. So you can do it simply by sending your Index request against the id of the document you want to update.

For example assuming you have below document already indexed in Elasticsearch within index people, type food, id 9:

{"user": "kramer", "fav_food": "jello"}

Then you would do:

String source = "{\"user\": \"kramer\", \"fav_food\": \"pizza\"}";
JestResult result = client.execute(
            new Index.Builder(source)
                    .index("people")
                    .type("food")
                    .id(9)
                    .build()
    );
like image 116
Cihan Keser Avatar answered Sep 27 '22 21:09

Cihan Keser