簡體   English   中英

通過查詢更新(updateByQuery)Elasticsearch-PHP

[英]Update by query (updateByQuery) Elasticsearch-PHP

我正在使用Elasticsearch 2.3官方php驅動程序 updateByQuery給我麻煩在php中使用。 對於如何使用它的一些幫助將不勝感激。

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

基本上我想更新幾個與某個查詢匹配的文檔字段(名稱,價格)

謝謝。

因此,借助CURL api的工作原理,我設法提出了一種方法。

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

而已。 到目前為止,它並不容易且沒有很好的文檔記錄。

我想在以前的答案中添加一點補充

您可能不會在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' => [
            'lang' => 'painless',
            'source' => 'ctx._source.enabled = params.value',
            'params' => [
                'value' => 0
            ]
        ]
    ]
];

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM