1

I'm using Elasticsearch 2.1.1 php client for my ES operations. I want to update my docs by query. For that I found updatebyquery but this is not working for 2.0+ versions of ES. So is there any new way for updating my docs by query?

1 Answer 1

1

I was struggling with updateByQuery as well, have a look at this quetion

First you need to edit your elasticsearch.yml to allow Scripting. Append the following lines at the end.

script.engine.groovy.inline.search: on
script.engine.groovy.inline.aggs: on
script.engine.groovy.inline.update: on

Then construct a query that will update all your records that match your conditions.

$client = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
    # Request
    $updateRequest = [
        'index'     => 'testindex',
        'type'      => 'logs',
        'conflicts' => 'proceed',
        'body' => [
            'query' => [ 
                'filtered' => [
                    'filter' => [
                        'bool' =>   [
                                        'must' =>
                                        [
                                            [
                                                'match' => [ 'enabled' => 1 ],
                                            ],
                                        ]
                                    ]
                                ]
                            ]
                        ],
            'script' => [
                    'inline' => 'ctx._source.enabled = value',
                    'params' => [
                        'value' => 0
                    ]
            ]
            ]
        ]
    ];
    # Update 
    $results = $client->updateByQuery($updateRequest);
Sign up to request clarification or add additional context in comments.

2 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
(To whoever down voted) Please give a reason to why you downvoted this answer. If my answer does not work i will be happy to fix it or maybe elaborate more. Otherwise i think the answer deserves an up vote to help other people who are stuck just like i was.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.