使用 man ascii 来查看 ASCII 表。

elasticsearch 添加新的mapping类型之后无法索引数据

Elasticsearch | 作者 lwzhuo | 发布于2019年10月07日 | 阅读数:4352

elasticsearch版本7.3.1
系统:mac os 10.14.6
具体情况是这样的,我有一批数据其中有一个以时间戳字符串显示的字段bike.start_ts,由于我将数据倒入es的时候没有预先设置mapping,es自动将其解析成text的格式。如下是一开始查询到的mapping类型。
"start_ts": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
为了进行时间聚合,我结合以下官方文档添加了一个fields类型https://www.elastic.co/guide/e ... .html
根据官方文档,我是这么做的
PUT _mapping
{
"properties": {
"bike.start_ts":{
"type":"text",
"fields":{
"ms":{"type": "date","format": "epoch_millis"}
}
}
}
}
在做完上述步骤之后查询到新的mapping结果如下:显示已经添加了一个新的类型
"start_ts": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
},
"ms": {
"type": "date",
"format": "epoch_millis"
}
}
}
于是我开始尝试进行时间聚合
{
"aggs" : {
"by_month" : {
"date_histogram" : {
"field" : "bike.start_ts.ms",
"calendar_interval" : "day",
"time_zone": "+08:00",
"format": "yyyy-MM-dd"
}
}
}
}

发现直接返回空的桶,也就是没有成功聚合。
 
于是我新建了一个索引,直接在建索引的过程中定义好start_ts的mapping类型
"start_ts": {
"type": "date",
"format":"epoch_millis",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}

 
然后我将数据copy到新的索引上来,并重新使用聚合,这样又能成功的分桶。
{
"aggs" : {
"by_month" : {
"date_histogram" : {
"field" : "bike.start_ts",
"calendar_interval" : "day",
"time_zone": "+08:00",
"format": "yyyy-MM-dd"
}
}
}
}

 
 
重新设置index可以成功聚合这个很好理解,但是为什么我第一种方式明明已经添加了新的mapping类型,并且聚合的时候也使用新的mapping类型进行聚合,却一直无法聚合,这是什么原因呢?
或者说我的添加mapping的方式不是正确的做法?请大家指教。谢谢!
已邀请:

mobikarl

赞同来自: lwzhuo febmark

其实最简单的办法是在原索引上执行一次update by query就行了
 
The simplest usage of _update_by_query just performs an update on every document in the index without changing the source. This is useful to pick up a new property or some other online mapping change. Here is the API:
POST twitter/_update_by_query?conflicts=proceed
 

strglee

赞同来自: lwzhuo

第一次更新索引后,对已经存在的数据无效,需要重新导入一遍数据。

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自: lwzhuo

如楼上,除了重新导入,也可以进行reindex进行索引数据的部分或者全量数据迁移,以使得新数据生效。

要回复问题请先登录注册