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

时间:2016-01-19 13:52:25

标签: elasticsearch logstash

我有一个"索引":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"
  }
}

我想将相同的数据复制到另一个"索引"名字" jamcorp"使用相同的"类型"和#34; id"

我正在使用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"
  }
}

我要求的Ouptut是:

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

任何帮助将不胜感激。 :)谢谢

1 个答案:

答案 0 :(得分:2)

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

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

因此,@metadata哈希将填充文档的index_type_id,您可以在过滤器和输出中重复使用: / p>

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
 }
}