How do I update multiple items in ElasticSearch?
Asked Answered
B

9

15

Let's 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 to that tag. Here is an example item:

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

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

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

I know I can just search for the tag word, get its document, search to see if the alias already exists in the array of aliases, if not add it, than save. However - this does not sound like a good solution.

Is there a way to do a bulk update?

Bout answered 17/4, 2012 at 9:56 Comment(0)
Z
14

Try this 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"
}
}
Zenaidazenana answered 8/6, 2015 at 11:16 Comment(3)
Thanks for the example. It's not need to specify _index and _type fields in every update: {} object, as it is already specified in request URL: http://127.0.0.1:9200/myindex/type/_bulkCheerio
To make this example working we need to write "object per line": {"update": {"_id": "myId0"}} {"doc": {"field": "new value 0"}} {"update": {"_id": "myId1}} {"doc": {"field": "new value 1"}} Also, make sure you have added Content-Type: application/x-ndjson header to the request.Cheerio
@Varshaan: You have made my day!! Thanks Buddy!! :)Multitudinous
L
11

All updates in ElasticSearch are done by finding the record, deleting the old version and adding the new version. You can save a little bit on moving records all the way to the client by using Update API. It would still require finding the record though.

What you, probably, want is Update by query.

Littoral answered 17/4, 2012 at 13:16 Comment(2)
Update by query is still not added to Elastic, but a plugin exists.Ellieellinger
As of Elasticsearch 2.3, update-by-query is available - elastic.co/guide/en/elasticsearch/reference/2.3/…Indent
U
5

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" }

Command:

curl -k -XPOST 'https://my_host:9200/my_url/_bulk' --data-binary "@input_list.dat"; echo
Unassuming answered 21/1, 2016 at 1:50 Comment(0)
I
4

Elasticsearch 2.3.0 introduced the Update By Query API as part of the long awaited Reindex API.

As an example, here is how you could update all documents to delete a certain 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 scripting, so be sure to enable it in elasticsearch.yml with script.inline: on.

Indent answered 6/5, 2016 at 17:23 Comment(0)
I
2

Elastic Search has a Update API. With that API you can do the following:

curl -XPOST 'localhost:9200/test/tag/weak/_update' -d '{
    "script" : "ctx._source.aliases += faint"
}'
Inadvertence answered 9/4, 2013 at 15:50 Comment(1)
Can you please tell me how can I implement update a single record in php?Cultured
T
1

You can use the ElasticSeach Bulk API for updating the multiple documents using a single API call

CURL example

curl --location --request POST 'localhost:9200/whatsapp/_bulk' \
--header 'Content-Type: application/json' \
--data-raw '{ "update" : {"_id" : 692, "_index" : "whatsapp","_type":"_doc","retry_on_conflict" : 3} }
{ "doc" : {"thread_status" : 1} }
{ "update" : {"_id" : 693, "_index" : "whatsapp","_type":"_doc","retry_on_conflict" : 3} }
{ "doc" : {"thread_status" : 1} }

'

NOTE The final line of data must end with a newline character \n. That' why you will notice ' at the end line of json.

Tan answered 9/3, 2020 at 6:15 Comment(0)
R
0

Also if you add same value with same id, it will automatically update older data.

Refutative answered 1/4, 2014 at 14:4 Comment(1)
Update or replace?Hagiographer
I
0

Elasticsearch's bulk APIs can be used for update requests as well, 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 scripting is disabled in newer versions of elasticsearch. Either enable that or use pre-compiled scripts for using this feature.

Inwards answered 14/9, 2015 at 10:32 Comment(0)
C
0

You can do the same using spring java client using the following code. Following 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);
}
Contraception answered 10/11, 2015 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.