Julius Moshiro
Julius Moshiro

Reputation: 730

Update by query (updateByQuery) Elasticsearch-PHP

I am Using Elasticsearch 2.3 along with the official php driver. The updateByQuery is giving me troubles to use in php. A little help on how to use it will be appreciated.

        $client = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
        # Request
        $updateRequest = [
            'index'     => 'gorocket',
            'type'      => 'logs',
            'body' => [
                'query' => [ 
                    'filtered' => [
                        'filter' => [
                            'bool' =>   [
                                            'must' =>
                                            [
                                                [
                                                    'match' => [ 'enabled' => 1 ],
                                                ],
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                  ]
            ]
        ];
        # Update 
        $results = $client->updateByQuery($updateRequest);

Basically i want to update a couple of document fields(name,price) that matches a certain query

Thank you.

Upvotes: 0

Views: 6406

Answers (2)

WiRight
WiRight

Reputation: 363

I want to add a small addition to the previous answer

You may not add the following params in elasticsearch.yml

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

And your query will be:

$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' => [
            'lang' => 'painless',
            'source' => 'ctx._source.enabled = params.value',
            'params' => [
                'value' => 0
            ]
        ]
    ]
];

# Update 
$results = $client->updateByQuery($updateRequest);

Upvotes: 1

Julius Moshiro
Julius Moshiro

Reputation: 730

So, with the help of how the CURL api works i managed to come up with a way.

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

There after you can start doing batch updates using queries as the example bellow.

    $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);

That's it. It is not easily and well documented as of now so.

Upvotes: 2

Related Questions