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

Mik*_*ike 11 elasticsearch reindex logstash kibana elastic-stack

我最近使用redis 2.8.24,Elasticsearch 1.2.2和Kibana 3.1升级了我的ELK堆栈(使用redis 3.2.3,Elasticsearch 2.3.5和Kibana 4.5.4的logstash 2.3.4)(logstash 1.4.1/1.4.2) 0.1).升级进行得很顺利,但升级后我有一些字段有冲突类型.此特定字段由logstash动态创建,因此Elasticsearch中没有整体映射.我花了相当多的时间搜索如何改变它.每篇在线文章都说我不能简单地改变现有数据的字段类型.我引用的许多文章都需要重新索引,但未能解释如何.以下是我更改类型和重新索引的确切步骤.

从需要更改字段类型的当前索引获取映射:

curl -XGET http://localhost:9200/logstash-2016.05.30/_mapping?pretty=1 > logstash-2016.05.30
Run Code Online (Sandbox Code Playgroud)

编辑logstash-2016.05.30文件,删除文件中的第二行(索引名称)和第二行(花括号).如果不这样做,将不会更新映射.我想如果你将索引名称编辑为新名称它可以工作,但我没有尝试(应该尝试我猜).

编辑logstash-2016.05.30文件并更改类型(即long为string或string为long).您可以使用类似字段使用的确切定义.

"http_status" : {
  "type" : "string",
  "norms" : {
    "enabled" : false
  },
  "fields" : {
    "raw" : {
      "type" : "string",
      "index" : "not_analyzed",
      "ignore_above" : 256
    }
  }
},
Run Code Online (Sandbox Code Playgroud)

改成:

"http_status" : {
  "type" : "long"
},
Run Code Online (Sandbox Code Playgroud)

接下来创建新索引(追加_new或任何你想要的)

curl -XPUT  http://localhost:9200/logstash-2016.05.30_new -d @logstash-2016.05.30
Run Code Online (Sandbox Code Playgroud)

仔细检查映射是否已正确创建

curl -XGET http://localhost:9200/logstash-2016.05.30_new/?pretty
Run Code Online (Sandbox Code Playgroud)

使用以下方法重新索引:

curl -XPOST  http://localhost:9200/_reindex -d '{
"source": {
"index" : "logstash-2016.05.30"
},
"dest" : {
"index" : "logstash-2016.05.30_new"
}
}'
Run Code Online (Sandbox Code Playgroud)

计算两个索引中的条目(应该是相同的)

curl -XGET http://localhost:9200/logstash-2016.05.30/_count
curl -XGET http://localhost:9200/logstash-2016.05.30_new/_count
Run Code Online (Sandbox Code Playgroud)

一旦满意,重建索引就成功删除旧索引

curl -XDELETE http://localhost:9200/logstash-2016.05.30
Run Code Online (Sandbox Code Playgroud)

创建别名,以便仍可以使用旧索引名称

curl -XPOST  http://localhost:9200/_aliases -d '{
"actions": [
{ "add": {
"alias": "logstash-2016.05.30",
"index": "logstash-2016.05.30_new"
}}
]
}'
Run Code Online (Sandbox Code Playgroud)

最后,导航到Kibana并选择"设置"和"索引模式".单击重新加载图标以刷新字段列表.应删除所有冲突.

不经意的是,这不是一个真正的问题,除非你觉得这可以通过另一种方式完成,或者这是一个问题.

Ste*_* E. 1

对于 Elasticsearch 6,需要进行一些小的更改。否则请严格遵循所有说明。

要获取映射,pretty=true请使用pretty=1

curl -XGET http://localhost:9200/logstash-2016.05.30/_mapping?pretty=true > logstash-2016.05.30
Run Code Online (Sandbox Code Playgroud)

对于所有 XPUT/XPOST 请求,内容类型现在必须设置为 application/json。

curl -XPUT  http://localhost:9200/logstash-2016.05.30_new \
  -H 'Content-Type: application/json' -d @logstash-2016.05.30
Run Code Online (Sandbox Code Playgroud)