1、新建index
curl -X PUT "localhost:9200/test"
2、生成 mapping
curl -XPUT "localhost:9200/test/order/_mapping?pretty" -d ‘
{
"order": {
"properties": {
"product_code": {
"type": "string",
"store": "yes",
"fielddata": true, #es5.6之后,字符串做聚合必须开启此字段
"fields": {
"raw" : { #为了下面的此字段聚合不至于将字符串拆开计数
"type": "string",
"index": "not_analyzed"
}
}
},
"price": {
"type": "double"
},
"num": {
"type": "integer"
},
"pay_time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
}‘
3、查看mapping
curl -X GET "localhost:9200/test/_mapping?pretty"
4、插入数据
curl -X POST ‘localhost:9200/test/order/‘ -d ‘ { "product_code": "G-4-6", "price": 43, "num":2, "pay_time": "2018-01-16 00:00:13" }‘ curl -X POST ‘localhost:9200/test/order/‘ -d ‘ { "product_code": "G-4-5", "price": 43, "num":4, "pay_time": "2018-01-17 00:00:13" }‘ curl -X POST ‘localhost:9200/test/order/‘ -d ‘ { "product_code": "G-4-6", "price": 43, "num":40, "pay_time": "2017-12-30 01:12:13" }‘
5、按月统计售卖数量,并排序
curl ‘localhost:9200/test/order/_search?pretty‘ -d ‘
{
"size" : 0,
"aggs": {
"num": {
"date_histogram": {
"field": "pay_time",
"interval": "month",
"format": "yyyy-MM-dd",
"order": {"sum_this_month": "desc"}
},
"aggs": {
"sum_this_month": {
"sum": {
"field": "num"
}
}
}
}
}
}‘
6、按商品统计售卖数量
curl ‘localhost:9200/test/order/_search?pretty‘ -d ‘
{
"size" : 0,
"aggs" : {
"num" : {
"terms" : {
"field" : "product_code.raw"
},
"aggs": {
"sum_this_product": {
"sum": {
"field": "num"
}
}
}
}
}
}‘
感谢大神的文章
https://segmentfault.com/a/1190000004433446 系列文章