简体   繁体   English

在Elasticsearch中更改类型和重新索引

[英]Change type and reindex in Elasticsearch

I recently upgraded my ELK stack (logstash 2.3.4 using redis 3.2.3, Elasticsearch 2.3.5 and Kibana 4.5.4) from (logstash 1.4.1/1.4.2 using redis 2.8.24, Elasticsearch 1.2.2 and Kibana 3.1.1). 我最近使用redis 2.8.24,Elasticsearch 1.2.2和Kibana 3.1从(logstash 1.4.1 / 1.4.2)升级了我的ELK堆栈(logstash 2.3.4使用redis 3.2.3,Elasticsearch 2.3.5和Kibana 4.5.4) 0.1)。 The upgrade went well but after the upgrade I had some fields that had conflicting types. 升级进行得很顺利,但升级后我有一些字段有冲突类型。 This specific fields were dynamically created by logstash so there was no overall mapping in Elasticsearch. 此特定字段由logstash动态创建,因此Elasticsearch中没有整体映射。 I spent a fair amount of time searching on how to change this. 我花了相当多的时间搜索如何改变它。 Every online article stated I couldn't simply change the field type of the existing data. 每篇在线文章都说我不能简单地改变现有数据的字段类型。 Many articles referenced I needed to reindex but failed to explain how. 我引用的许多文章都需要重新索引,但未能解释如何。 Below are the exact steps I did to change the type and reindex. 以下是我更改类型和重新索引的确切步骤。

Get the mapping from the current index needing the field type changed: 从需要更改字段类型的当前索引获取映射:

curl -XGET http://localhost:9200/logstash-2016.05.30/_mapping?pretty=1 > logstash-2016.05.30

Edit the logstash-2016.05.30 file removing the 2nd line (index name) and 2nd last line (curly bracket) in the file. 编辑logstash-2016.05.30文件,删除文件中的第二行(索引名称)和第二行(花括号)。 Failing to do this will NOT update the mappings. 如果不这样做,将不会更新映射。 I suppose if you edit the index name to the new name it would work but I didn't try that (should have tried I guess). 我想如果你将索引名称编辑为新名称它可以工作,但我没有尝试(应该尝试我猜)。

Edit the logstash-2016.05.30 file and change the type (ie long to string or string to long). 编辑logstash-2016.05.30文件并更改类型(即long为string或string为long)。 You can use the exactly definition used by a similar field. 您可以使用类似字段使用的确切定义。

"http_status" : {
  "type" : "string",
  "norms" : {
    "enabled" : false
  },
  "fields" : {
    "raw" : {
      "type" : "string",
      "index" : "not_analyzed",
      "ignore_above" : 256
    }
  }
},

Change to: 改成:

"http_status" : {
  "type" : "long"
},

Next create the new index (append _new or whatever you want) 接下来创建新索引(追加_new或任何你想要的)

curl -XPUT  http://localhost:9200/logstash-2016.05.30_new -d @logstash-2016.05.30

Double check the mapping was created correctly 仔细检查映射是否已正确创建

curl -XGET http://localhost:9200/logstash-2016.05.30_new/?pretty

Reindex using the following: 使用以下方法重新索引:

curl -XPOST  http://localhost:9200/_reindex -d '{
"source": {
"index" : "logstash-2016.05.30"
},
"dest" : {
"index" : "logstash-2016.05.30_new"
}
}'

Count the entries in both indexes (should be the same) 计算两个索引中的条目(应该是相同的)

curl -XGET http://localhost:9200/logstash-2016.05.30/_count
curl -XGET http://localhost:9200/logstash-2016.05.30_new/_count

Once satisfied the reindexing was successfully delete the old index 一旦满意,重建索引就成功删除旧索引

curl -XDELETE http://localhost:9200/logstash-2016.05.30

Create an alias so the old index name can still be used 创建别名,以便仍可以使用旧索引名称

curl -XPOST  http://localhost:9200/_aliases -d '{
"actions": [
{ "add": {
"alias": "logstash-2016.05.30",
"index": "logstash-2016.05.30_new"
}}
]
}'

Lastly, Navigate to Kibana and select Settings and the Index Pattern. 最后,导航到Kibana并选择“设置”和“索引模式”。 Click the reload icon to refresh the field list. 单击重新加载图标以刷新字段列表。 All conflicts should be removed. 应删除所有冲突。

Obliviously, this isn't really a question unless you feel this could be done another way or these is a problem with doing this. 不经意的是,这不是一个真正的问题,除非你觉得这可以通过另一种方式完成,或者这是一个问题。

For Elasticsearch 6, a few small changes are required. 对于Elasticsearch 6,需要进行一些小的更改。 Otherwise follow all the instructions closely. 否则请密切关注所有说明。

To obtain the mapping use pretty=true instead of pretty=1 : 要获取映射,请使用pretty=true而不是pretty=1

curl -XGET http://localhost:9200/logstash-2016.05.30/_mapping?pretty=true > logstash-2016.05.30

For all XPUT/XPOST requests, the content type must now be set to application/json. 对于所有XPUT / XPOST请求,现在必须将内容类型设置为application / json。

curl -XPUT  http://localhost:9200/logstash-2016.05.30_new \
  -H 'Content-Type: application/json' -d @logstash-2016.05.30

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

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