php - Elasticsearch 部分批量更新

标签 php json elasticsearch bigdata bulk

我有 6k 的数据要在 ElasticSearch 中更新。而且我必须使用 PHP。 我在文档中搜索并找到了这个,Bulk Indexing但这并没有保留以前的数据。

我有结构:

[
  {
    'name': 'Jonatahn',
    'age' : 21
  }
]

我要更新的代码片段:

$params =[
    "index" => "customer",
    "type" => "doc",
    "body" => [
        [
            "index" => [
                "_index" => "customer",
                "_type" => "doc",
                "_id" => "09310451939"
            ]
        ],
        [
            "name" => "Jonathan"
        ]
    ]
];

$client->bulk($params);

当我发送 ['name' => 'Jonathan'] 时,我希望 name 会更新并保持 age,但是age 被删除。 当然,我仍然可以逐个更新数据,但这需要很长时间,有没有更好的方法来做到这一点?

最佳答案

我的错误是使用"index",但正确的方法是"update"

最终代码为:

$params =[
"index" => "customer",
"type" => "doc",
"body" => [
    [
        "update" => [
    //   ^^^^^^ Here I change from index to update
            "_index" => "customer",
            "_type" => "doc",
            "_id" => "09310451939"
        ]
    ],
    [
        "doc" => [
            "name" => "Jonathan"
        ]
    ]
]
];

$client->bulk($params);

使用上面的代码,我的数据保留了以前的数据,只是更新了我传入的参数。

响应:

Array
(
    [took] => 7
    [timed_out] =>
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [skipped] => 0
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 1
            [max_score] => 1
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => customer
                            [_type] => doc
                            [_id] => 09310451939
                            [_score] => 1
                            [_source] => Array
                                (
                                    [name] => Jonathan
                                    [age] => 23
                                )

                        )

                )

        )

)

关于php - Elasticsearch 部分批量更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47759682/

相关文章:

php - 将 mysql 数组结果分配给 php 变量

iphone - 如何从应用程序中本地访问 json 文件

java - boolean 值总是返回 true

javascript - 如何以 JSON 或 CSV 格式写入数据?

sql - 具有多个字段和条件的Elasticsearch查询(非过滤器)

email - Elasticsearch是否有任何电子邮件警报功能?

php - 从 PHP 打印 MongoDB 日期

php - 使用javascript逐字符显示标签

database - 将数据从 Splunk 移动到 Elastic Search

php - 为什么我们应该总是从函数返回值?