【问题标题】:how to copy one index documents to other index in elasticsearch?如何将一个索引文档复制到elasticsearch中的另一个索引?
【发布时间】:2016-01-22 02:37:10
【问题描述】:

我创建了一个带有映射的新索引。其中存储了 500 000 个文档。

我想更改索引的映射,但在弹性搜索中不可能。所以我用新的新映射创建了另一个索引,现在我试图将文档从旧索引复制到新索引。

我正在使用扫描和滚动类型从旧索引中检索文档并复制到新索引。因为复制需要更多时间并且系统速度变慢。

下面是我正在使用的代码。

$client= app('elastic_search');
    $params = [
        "search_type" => "scan",    // use search_type=scan
        "scroll" => "30s",          // how long between scroll requests. should be small!
        "size" => 500000,               // how many results *per shard* you want back
        "index" => "admin_logs422",
        "body" => [
            "query" => [
                "match_all" => []
            ]
        ]
    ];

    $docs = $client->search($params);   // Execute the search

    $scroll_id = $docs['_scroll_id'];   


    while (\true) {

        // Execute a Scroll request
        $response = $client->scroll([
                "scroll_id" => $scroll_id,  //...using our previously obtained _scroll_id
                "scroll" => "500s"           // and the same timeout window
            ]
        );

        if (count($response['hits']['hits']) > 0) {
            foreach($response['hits']['hits'] as $s)
            {

                $params =
                    [
                        'index' => 'admin_logs421',
                        'type' => 'admin_type421',
                        'id'=> $s['_id'],
                        'client' => [
                            'ignore' => [400, 404],
                            'verbose' => true,
                            'timeout' => 10,
                            'connect_timeout' => 10
                        ],
                        'body' => $s['_source']
                    ];

                $response = app('elastic_search')->create($params);

            }


            $scroll_id = $response['_scroll_id'];
        } else {
            // No results, scroll cursor is empty.  You've exported all the data
            return response("completed");
        }
    }

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您不应该编写这样的代码。有一些出色的工具可以为您完成。

    只需查看Taskrabbit's elasticdump 实用程序,它完全符合您的要求。

    elasticdump \
      --input=http://localhost:9200/source_index \
      --output=http://localhost:9200/target_index \
      --type=data
    

    或者您也可以非常轻松地利用 Logstash,如 this other answer 所示

    最后,既然你用的是Python,你也可以用elasticsearch-py reindex utility

    【讨论】:

    • 不工作。将数据从一个索引复制到另一个索引后,在新索引中找不到数据
    • 您已将 source_index 替换为 admin_logs422target_index 替换为 admin_logs421,对吧?
    • 我有一个索引 admin_logs221 并输入 admin_type221 ,其中包含映射和 30000 个文档。我创建了另一个索引,映射中的一个更改为 admin_logs mapping.iam 使用 docker 命令复制数据
    • docker run --rm -ti elasticdump --input=localhost:9200/admin_logs421 --output=localhost:9200/admin_logs422 --type=data --limit=10000 --bulk=true --scrollTime=30s
    • starting dump got 10000 objects from source elasticsearch (offset: 0) sent 10000 objects to destination elasticsearch, write 10000 got 570 objects from source elasticsearch (offset: 10000) sent 570 objects to destination elasticsearch, write 570从源 elasticsearch 获得 0 个对象(偏移量:10570)将 0 个对象发送到目标 elasticsearch,写入 0 Total Writes:10570 转储完成
    【解决方案2】:

    您可以尝试使用一些现有的重新索引插件

    https://github.com/codelibs/elasticsearch-reindexing

    通过发送以下请求

    localhost:9200/{fromindex}/{fromtype}/_reindex/{toindex}/{totype}
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 2016-03-13
    • 2015-02-11
    • 2021-09-25
    • 2015-10-29
    • 1970-01-01
    相关资源
    最近更新 更多