1.创建模板
PUT _template/metering_template
{
"index_patterns": ["df_*_metering"],
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"dynamic_templates": [
{
"strings_as_keywords": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
],
"date_detection": false,
"properties": {
"count": {
"type": "long"
},
"time": {
"type": "date"
}
}
}
}
2.获取模板
GET _template/metering_template
{
"metering_template" : {
"order" : 0,
"index_patterns" : [
"df_*_metering"
],
"settings" : {
"index" : {
"number_of_shards" : "1",
"number_of_replicas" : "1"
}
},
"mappings" : {
"dynamic_templates" : [
{
"strings_as_keywords" : {
"mapping" : {
"type" : "keyword"
},
"match_mapping_type" : "string"
}
}
],
"date_detection" : false,
"properties" : {
"count" : {
"type" : "long"
},
"time" : {
"type" : "date"
}
}
},
"aliases" : { }
}
}
3.创建索引
PUT /df_test_metering
4.查看索引
GET /df_test_metering
{
"df_test_metering" : {
"aliases" : { },
"mappings" : {
"dynamic_templates" : [
{
"strings_as_keywords" : {
"match_mapping_type" : "string",
"mapping" : {
"type" : "keyword"
}
}
}
],
"date_detection" : false,
"properties" : {
"count" : {
"type" : "long"
},
"project" : {
"type" : "keyword"
},
"time" : {
"type" : "date"
} }
},
}
}
5.导入数据
PUT /df_test_metering/_doc/3
{
"project": "event",
"count": 44,
"time":"2022-03-29"
}
POST /df_test_metering/_doc
{
"project": "event",
"count": 44,
"time":"2022-03-29"
}
6.查看字段属性
GET /df_test_metering/_mapping
{
"df_test_metering" : {
"mappings" : {
"dynamic_templates" : [
{
"strings_as_keywords" : {
"match_mapping_type" : "string",
"mapping" : {
"type" : "keyword"
}
}
}
],
"date_detection" : false,
"properties" : {
"count" : {
"type" : "long"
},
"project" : {
"type" : "keyword"
},
"time" : {
"type" : "date"
} }
}
}
}
7.查询数据
查询条件:
GET /df_test_metering/_search
{
"aggs": {
"count": {
"aggs": {
"sum_count": {
"sum": {
"field": "count"
}
}
},
"terms": {
"field": "project"
}
}
},
"size":0
}
查询结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"count" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "logging",
"doc_count" : 3,
"sum_count" : {
"value" : 68.0
}
},
{
"key" : "event",
"doc_count" : 2,
"sum_count" : {
"value" : 38.0
}
}
]
}
}
}
|