Update by query (updateByQuery) Elasticsearch-PHP

前端 未结 2 1226
心在旅途
心在旅途 2021-01-17 04:34

I am Using Elasticsearch 2.3 along with the official php driver. The updateByQuery is giving me troubles to use in php. A

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-17 05:14

    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.

提交回复
热议问题