Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I update multiple items in ElasticSearch?

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?

like image 682
Doron Avatar asked Apr 17 '12 09:04

Doron


2 Answers

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"
}
}
like image 167
Varshaan Avatar answered Oct 03 '22 00:10

Varshaan


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
like image 35
Rockoder Avatar answered Oct 02 '22 23:10

Rockoder