How to update multiple items in ElasticSearch?

Say I have a tag type in an ElasticSearch index with the following mapping:

 { "tag": { "properties": { "tag": {"type": "string", "store": "yes"}, "aliases": {"type": "string"} } } } 

Each entry is a tag and an array of aliases for that tag. Here is an example:

 { "word": "weak", "aliases": ["anemic", "anaemic", "faint", "flimsy"] } 

From time to time, I want to add new tags with their aliases and add new aliases to existing tags.

Adding new tags with their aliases is easy, it's just a new document. However, how can I add new aliases to existing tag words in a reasonable way?

I know that I can just search for a tag word, get its document, look for whether an alias will already exist in the array of aliases, if not add it, except for saving. However, this does not seem like a good solution.

Is there a way to do a bulk update?

+7
source share
8 answers

There is no update operation in elasticsearch base storage, Lucene. Thus, all updates are performed by searching for the record, deleting the old version and adding the new version. In elasticsearch, you can save a little on moving records to the client using the update API. However, you still need to find the record. What you might want is an Upgrade on Demand , but unfortunately it has not yet been implemented.

+7
source

Try using _ bulk :

 http://127.0.0.1:9200/myindex/type/_bulk { "update": { "_index": "myindex", "_type": "type", "_id": "myid" } }{ "doc": { "field": "new value" } }{ "update": { "_index": "myindex", "_type": "type", "_id": "id" } }{ "doc": { "field": "new value" } } 
+7
source

Elastic Search has an update API . Using this API you can do the following:

 curl -XPOST 'localhost:9200/test/tag/weak/_update' -d '{ "script" : "ctx._source.aliases += faint" }' 
+2
source

This works for me.

input_list.dat:

 { "index" : { "_index": "my_index", "_type": "my_type", "_id": "existing-value" } } { "Field_to_update": "New_Value" } { "index" : { "_index": "my_index", "_type": "my_type", "_id": "existing_value" } } { "Field_to_update": "New_Value" } 

Team:

 curl -k -XPOST 'https://my_host:9200/my_url/_bulk' --data-binary "@input_list.dat"; echo 
+2
source

Elasticsearch 2.3.0 introduced the Update By Query API as part of the much anticipated Reindex API .

As an example, you can update all documents to remove a specific field if it exists:

 POST /myindex/mytype/_update_by_query { "script": { "inline": "ctx._source.remove(\"remove\")" }, "query": { "exists": { "field": "remove" } } } 

The above example uses inline scripts, so be sure to include it in elasticsearch.yml with script.inline: on .

+1
source

Also, if you add the same value with the same identifier, it will automatically update the old data.

0
source

Elasticsearch APIs can also be used for update requests, at least for the Java client.

 List list = new Arraylist(); list.add("hello"); BulkProcessor bulk = new BulkProcessor(); UpdateRequest update = new UpdateRequest("index", "type", "id1"); update.script("ctx._source.aliases+= newaliases"); //dynamic script update.addScriptParam("newaliases", list); bulk.add(update); 

Note that dynamic scripts are disabled in new versions of elasticsearch. Either enable this, or use pre-compiled scripts to use this function.

0
source

You can do the same with the spring java client using the following code. Below are the dependencies used in the code.

 import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.index.query.QueryBuilder; import org.springframework.data.elasticsearch.core.query.UpdateQuery; import org.springframework.data.elasticsearch.core.query.UpdateQueryBuilder; private UpdateQuery updateExistingDocument(String Id) { // Add updatedDateTime, CreatedDateTime, CreateBy, UpdatedBy field in existing documents in Elastic Search Engine UpdateRequest updateRequest = new UpdateRequest().doc("UpdatedDateTime", new Date(), "CreatedDateTime", new Date(), "CreatedBy", "admin", "UpdatedBy", "admin"); // Create updateQuery UpdateQuery updateQuery = new UpdateQueryBuilder().withId(Id).withClass(ElasticSearchDocument.class).build(); updateQuery.setUpdateRequest(updateRequest); // Execute update elasticsearchTemplate.update(updateQuery); } 
0
source

All Articles