繁体   English   中英

如何将一些 ElasticSearch 数据复制到新索引

[英]How to copy some ElasticSearch data to a new index

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

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

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

从 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

最好的方法是使用 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

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

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

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

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

将特定类型从源索引重新索引到目标索引类型语法是

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

您可以使用 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

这样做的直接方法是编写代码,使用您选择的 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 Snapshot and Restore。 基本上,您将拍摄现有索引的快照,将其恢复为新索引,然后使用 Delete 命令删除年份不是 1972 的所有文档。

快照和恢复

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

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

按查询删除 API

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

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

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

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

或者:

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

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

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

或者

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

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

POST _reindex/

 {

     "source": {

         "index": "source_index",

         "type": "source_type",

         "query": {

              // add filter criteria

         }

    },

   "dest": {

       "index": "dest_index",

       "type": "dest_type"

   }

}

*注意:在重新索引 api 的情况下,必须在实际 api 调用之前创建目标索引。

有关克隆和重新索引之间差异的进一步阅读,请参阅在Elasticsearch克隆和重新索引索引有什么区别?

您可以使用 elasticdump --searchBody

# Copy documents from movies to 70sMovies (filtering using query)
elasticdump \
  --input=http://localhost:9200/movies \
  --output=http://localhost:9200/70sMovies \
  --type=data \
  --searchBody="{\"query\":{\"term\":{\"username\": \"admin\"}}}" # <--- Your query here

更多关于弹性转储选项的信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM