"版本冲突,当前版本不同于提供的版本"在php脚本中运行update_by_query curl时

时间:2018-01-19 21:57:32

标签: elasticsearch elasticsearch-painless

我必须更新ES文档中的某些字段。

我有一个interger' objectID'字段,它是文档所关注对象的唯一ID。

我有一个String' objectType'字段,是文档关注的对象类型。

所有文档都描述了对象的操作,objectType和objecID始终存在于所有文档中。

不幸的是,有些文件带有objectType" post_image"已被编入索引为" post"。 objectID仍然是唯一且有效的,并且只有一种类型的文档具有错误的objectType。因此,所有对象至少具有另一个具有正确objectType和相同唯一objectID的文档。

我想使用update_by_query将objectType的值更新为" post_image"在所有文件中,objectType为" post"并且objectID位于objectType为" post_image"。

的任何其他文档中。

这是我的伪代码脚本:

{
"query": {
    "match" : { "objectType" : "post" } //all documents with objectType post
},
"script": {
    "lang": "painless",
  "source": "
//subquery selecting all objectIDs from documents with objectType "post_image"
    subQueryResults = "query": {
        "match" : { "objectType" : "post_image" }
        //I don't know to filter results to retrive objectID field only
        //no need for help here, i'll figure it out myself
    }
    if (/*ctx.source['objectID'] in subQueryResults*/){
        ctx._source['objectType'] = "post_image"
    }

  "
}

我是无痛脚本的新手,我不知道如何在我的脚本中放入另一个查询来获取所有" post_image" IDS。我知道我可以将参数传递给脚本,但我不知道是否或如何使用查询结果。

谢谢!

编辑:

我通过使用Kibana原始导出提取有关objectID的csv列表解决了部分问题,并且我已经创建了一个PHP脚本来解析每个objectID并将其放入我的update_by_query的查询字符串中只需查找具有匹配objectID的ALL文档,并将objectType字段值替换为" post_image"。

我使用php curl进行这些调用,尽管使用"冲突"我仍然存在版本冲突问题:"继续"在我的请求中。我在kibana的开发控制台中测试了相同的查询,它运行得很好,我无法找到解释为什么它从php运行时更新我的​​文档。

这是脚本:

<?php
$query = "";
$csvFile = file($argv[1]);
try{
        //$data = array();
    $query = "";
    $i = 0;
    $csv_headers = array();

    $uri = "http://ip/index/type/_update_by_query";

    $conn = curl_init();
    curl_setopt($conn, CURLOPT_URL, $uri);
    curl_setopt($conn, CURLOPT_TIMEOUT, 5);
    curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($conn, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($conn, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($conn, CURLOPT_FAILONERROR, FALSE);
    curl_setopt($conn, CURLOPT_CUSTOMREQUEST, strtoupper('POST'));
    curl_setopt($conn, CURLOPT_FORBID_REUSE, 0);

    foreach ($csvFile as $line) {
        try{    
            //WARNING: separator parameter of str_getcsv call is a risk or error based on the type of CSV used. 
            //skip header in CSV
            if ($i > 0){
                $data = str_getcsv($line,',');
                    //$data = explode(",", $line);
                $id = $data[0];
                echo $id.", ";
            //old query, wasn't working
            //     $query = "{
            //         \"conflicts\": \"proceed\",
            //         \"query\": {
            //             \"match\" : { \"objectID\" : ".$id."
            //         }
            //     },
            //     \"script\": {
            //         \"lang\": \"painless\",
            //         \"source\": \"ctx._source['objectType'] = '".$argv[2]."'\"
            //     }
            // }";
                $query = "{
                    \"conflicts\": \"proceed\",
                    \"query\": {
                       \"bool\": {
                        \"must\": {
                            \"match\": {
                                \"objectType\": \"Post\"
                            }
                        },
                        \"filter\": {
                            \"terms\": {
                                \"objectID\": [
                                    ".$id."
                                ]
                            }
                        }
                    }
                },
                \"script\": {
                    \"lang\": \"painless\",
                    \"source\": \"ctx._source['objectType'] = 'Post_image'\"
                }
            }";

            curl_setopt($conn, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($query))
        );
            curl_setopt($conn, CURLOPT_POSTFIELDS, json_encode($query));
            $response = curl_exec($conn);
            //sleep(1);
            echo $response;
        }
        $i++;
    }catch(Exception $e){
        echo $e->getMessage();
            //continue;
    }
}catch(Exception $e){
echo $e->getMessage();
}
}
echo $query;
echo "\nCompleted.\n\n";
?>

示例回复:

{"index":"index",
"type":"type",
"id":"AWB0YFcjAFB9uQAwMSKx",
"cause":{"type":"version_conflict_engine_exception",
"reason":"[type][AWB0YFcjAFB9uQAwMSKx]: version conflict,
 current version [27] is different than the one provided [26]",
"index_uuid":"yOD9SBy0RMmDZGK_N5o8qw",
"shard":"2",
"index":"index"},
"status":409}

这很奇怪,因为我没有在我的请求中提供任何文档版本。可能它与upbade_by_query API中的一些自动内部行为有关。

1 个答案:

答案 0 :(得分:2)

我终于解决了整个想法。

首先,我稍微改了一下我的查询:

$query = "{ \"query\": {
                       \"bool\": {
                        \"must\": {
                            \"match\": {
                                \"objectType\": \"Post\" <- more optimal!
                            }
                        },
                        \"filter\": {
                            \"term\": {
                                \"objectID\":
                                    \"".$id."\"
                            }
                        }
                    }
                },
                \"script\": {
                    \"lang\": \"painless\",
                    \"source\": \"ctx._source['content'] = '".$argv[2]."'\"
                }
            }";

argv [2]是我想要提供给我的文档的objectType。 (&#34; Post_image&#34)

然后,我必须删除curl_exec之前的行上的JSON_encode($ query)

curl_setopt($conn, CURLOPT_POSTFIELDS, $query);
        $response = curl_exec($conn);

然后我停止了错误但是我有很多空的结果很奇怪,因为查询在使用kibana dev工具时返回结果但后来我意识到我使用了错误的IP并且正在将所有内容发送到另一个并运行测试ES它具有相同的索引/类型但索引中没有任何实际文档,因此空结果没有实际错误。我觉得有点傻。

PS:特征请求:facepalm表情符号。

相关问题