通过查询更新Elasticsearch-PHP Client 2.0+

时间:2016-03-30 07:00:32

标签: php elasticsearch elasticsearch-plugin

我使用Elasticsearch 2.1.1 php客户端进行ES操作。我想通过查询更新我的文档。为此,我找到updatebyquery,但这不适用于ES的2.0+ versions。那么有什么新方法可以通过查询更新我的文档吗?

1 个答案:

答案 0 :(得分:1)

我也在努力使用 updateByQuery ,看看http://blog.stephencleary.com/2012/02/async-and-await.html

首先,您需要编辑elasticsearch.yml以允许脚本编写。在末尾添加以下行。

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

然后构建一个查询,以更新符合条件的所有记录。

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