Update by query (updateByQuery) Elasticsearch-PHP

前端 未结 2 1227
心在旅途
心在旅途 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.

    0 讨论(0)
  • 2021-01-17 05:29

    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);
    
    0 讨论(0)
提交回复
热议问题