Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch partial bulk update

I have 6k of data to update in ElasticSearch. And I have to use PHP. I search in the documentation and I have found this, Bulk Indexing but this is not keeping the previous data.

I have structure:

[
  {
    'name': 'Jonatahn',
    'age' : 21
  }
]

My code snippet to update:

$params =[
    "index" => "customer",
    "type" => "doc",
    "body" => [
        [
            "index" => [
                "_index" => "customer",
                "_type" => "doc",
                "_id" => "09310451939"
            ]
        ],
        [
            "name" => "Jonathan"
        ]
    ]
];

$client->bulk($params);

When I send ['name' => 'Jonathan'] I expect the name will be updated and keep the age, but the age gets deleted. Sure, I still can update data-by-data but this will take a long time, is there any better way to do that?

like image 731
Jonathan Machado Avatar asked Dec 11 '17 19:12

Jonathan Machado


2 Answers

My error was to using "index", but the correct way to do what I want, was "update".

The final code is:

$params =[
"index" => "customer",
"type" => "doc",
"body" => [
    [
        "update" => [
    //   ^^^^^^ Here I change from index to update
            "_index" => "customer",
            "_type" => "doc",
            "_id" => "09310451939"
        ]
    ],
    [
        "doc" => [
            "name" => "Jonathan"
        ]
    ]
]
];

$client->bulk($params);

Using the code above, my data keep previous data and just update the data I passing in params.

Response:

Array
(
    [took] => 7
    [timed_out] =>
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [skipped] => 0
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 1
            [max_score] => 1
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => customer
                            [_type] => doc
                            [_id] => 09310451939
                            [_score] => 1
                            [_source] => Array
                                (
                                    [name] => Jonathan
                                    [age] => 23
                                )

                        )

                )

        )

)
like image 130
Jonathan Machado Avatar answered Oct 14 '22 20:10

Jonathan Machado


As per docs, Bulk API possible actions are index, create, delete and update. update expects that the partial doc, upsert and script and its options are specified on the next line.

POST _bulk
{ "update" : {"_id" : "1", "_type" : "_doc", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
like image 21
panchicore Avatar answered Oct 14 '22 20:10

panchicore