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

最佳答案

我也在努力使用 updateByQuery ,看看this quetion

首先,您需要编辑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);

关于php - 通过查询Elasticsearch-PHP Client 2.0+更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36301794/

10-09 07:21
查看更多