通过Logstash转储数据时如何复制elasticsearch索引的_id和_type

How do I replicate the _id and _type of elasticsearch index when dumping data through Logstash

我有一个 "Index":samcorp 和 "type":"sam"。

其中一个如下所示:

{
  "_index": "samcorp",
  "_type": "sam",
  "_id": "1236",
  "_version": 1,
  "_score": 1,
  "_source": {
    "name": "Sam Smith",
    "age": 22,
    "confirmed": true,
    "join_date": "2014-06-01"
  }
}

我想将相同的数据复制到具有相同 "type" 和相同 "id"

的不同 "index" 名称 "jamcorp"

我正在使用 Logstash 来做:

我在 logstash 的配置文件中使用了下面的代码,我最终得到了错误的 ID 和类型

input {
  elasticsearch {
   hosts => ["127.0.0.1:9200"]     
   index => "samcorp"
  }
}
filter {
 mutate {
  remove_field => [ "@version", "@timestamp" ]
 }
}
output {
 elasticsearch {
   hosts => ["127.0.0.1:9200"]
   manage_template => false
   index => "jamcorp"
   document_type => "%{_type}"
   document_id => "%{_id}"
 }
}

我已经尝试了所有可能的组合,得到了以下输出:

输出:

{
  "_index": "jamcorp",
  "_type": "%{_type}",
  "_id": "%{_id}",
  "_version": 4,
  "_score": 1,
  "_source": {
    "name": "Sam Smith",
    "age": 22,
    "confirmed": true,
    "join_date": "2014-06-01"
  }
}

我需要的输出是:

{
  "_index": "jamcorp",
  "_type": "sam",
  "_id": "1236",
  "_version": 4,
  "_score": 1,
  "_source": {
    "name": "Sam Smith",
    "age": 22,
    "confirmed": true,
    "join_date": "2014-06-01"
  }
}

如有任何帮助,我们将不胜感激。 :) 谢谢

在您的 elasticsearch 输入中,您需要将 docinfo parameter 设置为 true

input {
  elasticsearch {
   hosts => ["127.0.0.1:9200"]     
   index => "samcorp"
   docinfo => true                            <--- add this
  }
}

因此,@metadata 散列将填充文档的 index_type_id,您可以在过滤器和输出中重复使用它:

output {
 elasticsearch {
   hosts => ["127.0.0.1:9200"]
   manage_template => false
   index => "jamcorp"
   document_type => "%{[@metadata][_type]}"   <--- use @metadata
   document_id => "%{[@metadata][_id]}"       <--- use @metadata
 }
}