使用php在elasticsearch2中查找并更新文档


find and update a document in elasticsearch2 using php

我正在尝试使用ElasticSearch查找文档并更新其中的字段。代码为:

 require_once 'vendor/autoload.php';
$client = Elasticsearch'ClientBuilder::create()->build();
                        $params = [
    'index' => 'myIndex',
    'type' => 'myCollection',
    'body' => [
        'query'=> [
                'match'=> [
                                'accessToken' => $accessToken
                ]
        ] ,
        'script' => 'ctx._source.deviceId= deviceId',
        'params' => [
            'deviceId' => $deviceId
        ],
        'upsert' => [
            'counter' => 1
        ]
    ]
];

代码给出500 Internal Error。知道我错过了什么吗?

如果您想将设备ID的值增加1(意思是,如果设备ID值为4,则u可以通过向其添加1将其增加到5),则遵循以下代码

 'script' => 'ctx._source.deviceId= deviceId', 
    replace with 
     'script' => 'ctx._source.counter += deviceId',

示例:

$params = [
        'index' => 'my_index',
        'type' => 'my_type',
        'id' => 'my_id',
        'body' => [
            'script' => 'ctx._source.counter += count',
            'params' => [
                'count' => 4
            ],
            'upsert' => [
                'counter' => 1
            ]
        ]
    ];
    $response = $client->update($params);

检查i Elasticsearch php