【问题标题】:How to copy some ElasticSearch data to a new index如何将一些 ElasticSearch 数据复制到新索引
【发布时间】:2021-09-30 14:52:53
【问题描述】:

假设我的 ElasticSearch 中有电影数据,我是这样创建它们的:

curl -XPUT "http://192.168.0.2:9200/movies/movie/1" -d'
{
    "title": "The Godfather",
    "director": "Francis Ford Coppola",
    "year": 1972
}'

我有很多不同年份的电影。我想复制特定年份(即 1972 年)的所有电影并将它们复制到“70sMovies”的新索引中,但我不知道该怎么做。

【问题讨论】:

  • v7.4 引入了 _clone api

标签: elasticsearch


【解决方案1】:

从 ElasticSearch 2.3 开始,您现在可以使用内置的_reindex API

例如:

POST /_reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter"
  }
}

或通过添加过滤器/查询仅特定部分

POST /_reindex
{
  "source": {
    "index": "twitter",
    "query": {
      "term": {
        "user": "kimchy"
      }
    }
  },
  "dest": {
    "index": "new_twitter"
  }
}

阅读更多:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html

【讨论】:

  • 完美运行,感谢分享(debian/jessie,ES 5.2.2,curl)。
  • 请注意,它不会复制映射和设置(例如,如果您的总字段设置大于默认值 1000)。在这种情况下,您应该使用设置创建一个新索引,然后运行 ​​_reindex。见:elastic.co/guide/en/elasticsearch/reference/5.4/…
  • 如果索引配置了_source为false就不行
  • AFAIK,目标索引必须预先配置 mappings
  • 如果您的模型遵循这些索引的特定命名模式,您可以使用 _template API - elastic.co/guide/en/elasticsearch/reference/6.8/…
【解决方案2】:

最好的方法是使用 elasticsearch-dump 工具https://github.com/taskrabbit/elasticsearch-dump

我使用的真实世界示例:

elasticdump \
  --input=http://localhost:9700/.kibana \
  --output=http://localhost:9700/.kibana_read_only \
  --type=mapping
elasticdump \
  --input=http://localhost:9700/.kibana \
  --output=http://localhost:9700/.kibana_read_only \
  --type=data

【讨论】:

  • 这个elasticsearch-dump是一个更好的选择,因为它可以在不同集群之间复制数据,并且支持多个ES版本。
【解决方案3】:

查看背包: https://github.com/jprante/elasticsearch-knapsack

安装并运行插件后,您可以通过查询导出部分索引。例如:

curl -XPOST 'localhost:9200/test/test/_export' -d '{
"query" : {
    "match" : {
        "myfield" : "myvalue"
    }
},
"fields" : [ "_parent", "_source" ]
}'

这将创建一个仅包含您的查询结果的 tarball,然后您可以将其导入另一个索引。

【讨论】:

    【解决方案4】:

    要将特定的type从源索引重新索引到目标索引type语法是

    POST _reindex/
     {
     "source": {
     "index": "source_index",
     "type": "source_type",
     "query": {
      // add filter criteria
       }
     },
     "dest": {
      "index": "dest_index",
      "type": "dest_type"
      }
    }
    

    【讨论】:

      【解决方案5】:

      您可以使用 elasticsearch-dump (https://github.com/taskrabbit/elasticsearch-dump) 分三个步骤轻松完成。在以下示例中,我将索引“thor”复制到“thor2”

      elasticdump --input=http://localhost:9200/thor --output=http://localhost:9200/thor2 --type=analyzer
      
      elasticdump --input=http://localhost:9200/thor --output=http://localhost:9200/thor2 --type=mapping
      
      elasticdump --input=http://localhost:9200/thor --output=http://localhost:9200/thor2 --type=data
      

      【讨论】:

        【解决方案6】:

        执行此操作的直接方法是编写代码,使用您选择的 API,查询“年份”:1972,然后将该数据索引到新索引中。您可以使用 Search api 或 Scan and Scroll API 来获取所有文档,然后将它们一一索引或使用 Bulk Api:

        http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-search.html

        http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-scroll.html

        http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-index_.html

        http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html

        假设您不想通过代码执行此操作,但正在寻找一种直接的方式来执行此操作,我建议您使用 Elasticsearch 快照和还原。基本上,您会拍摄现有索引的快照,将其恢复到新索引中,然后使用 Delete 命令删除所有年份不是 1972 年的文档。

        快照和恢复

        快照和恢复模块允许创建快照 单个索引或整个集群到远程存储库。在 最初发布的唯一共享文件系统存储库的时间是 支持,但现在可以通过官方获得一系列后端 支持的存储库插件。

        http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-snapshots.html

        按查询删除 API

        delete by query API 允许从一个或多个中删除文档 基于查询的索引和一种或多种类型。查询可以 使用简单的查询字符串作为参数提供,或使用 请求正文中定义的查询 DSL。

        http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete-by-query.html

        【讨论】:

          【解决方案7】:

          从 v7.4 开始,_clone api 被引入,可以轻松满足您的需求:(阅读相关的先决条件和所涉及的监控)

          POST /<index>/_clone/<target-index>
          

          或者:

          PUT /<index>/_clone/<target-index>
          

          【讨论】:

            【解决方案8】:

            如果打算将部分数据或整个数据复制到具有与原始索引相同的设置/映射的索引,则可以使用 clone api 来实现相同。如下所示:

            POST /&lt;index&gt;/_clone/&lt;target-index&gt;

            PUT /&lt;index&gt;/_clone/&lt;target-index&gt;

            但是,如果打算将数据复制到具有与原始索引不同的设置/映射的新索引,则可以使用 reindex api 来实现相同的目的。如下所示:

            POST _reindex/
            
             {
            
                 "source": {
            
                     "index": "source_index",
            
                     "type": "source_type",
            
                     "query": {
            
                          // add filter criteria
            
                     }
            
                },
            
               "dest": {
            
                   "index": "dest_index",
            
                   "type": "dest_type"
            
               }
            
            }
            

            *注意:如果是重新索引 api,则必须在实际调用 api 之前创建目标索引。

            如需进一步了解 clonereindex 之间的区别,请参阅 What's the difference between cloning and reindexing an index in Elasticsearch?

            【讨论】:

              猜你喜欢
              • 2021-04-16
              • 1970-01-01
              • 1970-01-01
              • 2017-09-30
              • 1970-01-01
              • 2015-04-07
              • 2018-06-10
              • 2013-07-26
              • 1970-01-01
              相关资源
              最近更新 更多