生产环境中创建了一个字段th_val类型为keyword,后期需要对该字段进行聚合操作。
创建索引
PUT /test_index
{
"mappings": {
"properties": {
"th_val":{
"type": "keyword"
},
"user_id":{
"type": "long"
}
}
}
}
根据userId聚合th_val
GET test_index/_search
{
"query": {
"match_all": {}
},
"size":0,
"aggs": {
"user_id": {
"terms": {
"field": "user_id"
},
"aggs": {
"sum_val": {
"sum": {
"field": "th_val"
}
}
}
}
}
}
迁移索引
创建一个新的索引
PUT /test_index_v1
{
"mappings": {
"properties": {
"th_val":{
"type": "keyword"
},
"user_id":{
"type": "long"
}
}
}
}
POST _reindex
{
"source": {"index": "test_index"},
"dest": {"index": "test_index_v1"}
}
新增字段
POST /test_index_v1/_mapping
{
"properties":{
"th_val_new":{
"type": "long"
}
}
}
POST test_index/_update_by_query
{
"query": {
"match_all": {}
},
"script": {
"source": "ctx._source['th_val_new']=ctx._source['th_val']",
"lang": "painless"
}
}
运行时字段
GET test_index/_search
{
"runtime_mappings": {
"th_val_v2": {
"type": "double",
"script": {
"source": "emit(new BigDecimal(doc['th_val'].value).floatValue())"
}
}
},
"query": {
"match_all": {}
},
"size": 0,
"aggs": {
"user_id": {
"terms": {
"field": "user_id"
},
"aggs": {
"sum_val": {
"sum": {
"field": "th_val_v2"
}
}
}
}
}
}