GET /shop-pos-order-log-2022-06 //查询索引结构
GET shop-pos-order-log-2022-06/_search //按条件查询
{
"query": {
"bool": {
"must": [
{"term": {"itemno": {"value": "03010013"}}}, //"itemno"表示查询字段,"03010013"查询值
{"term": {"action": {"value": 301}}},
{"term": {"shop_id": {"value": 109792}}},
{"term": {"opttime": {"value": "2022-06-05"}}}
]
}
}
}GET shop-pos-order-log-2022-06/_search //聚合查询
{ "size": 0,
"aggs": { //aggs表示聚合查询
"posOrder": { //posOrder起的聚合名
"terms": { // terms表示 按照文档字段分组
"field": "itemno", //聚合字段itemno,相当于MySQL根据itemno字段分组
"size": 100000 //数量
},
"aggs": { //对itemno分组后的计算 同一itemno根据qty字段求和
"sumAgg": { //sumAgg求和结果的名字,相当MySQL起的别名
"sum": { //求和
"field": "qty" //聚合计算字段
},"aggs": { //对上面的聚合后的doc再按照dish_name.keyword 进行分组聚合
"terms": {
"field": "dish_name.keyword",
"size": 10
}
}
}
}
}
},
"query": { //对上面聚合的条件,相当于MySQL根据where条件进行group by
"bool": {
"must": [ //下面都是条件
{"term": {"action": {"value": 301}}},
{"term": {"shop_id": {"value": 109792}}},
{"term": {"opttime": {"value": "2022-06-05"}}}
]
}
}
}
聚合结果:
{
"took" : 516,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5070,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"posOrder" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "14010105",
"doc_count" : 241,
"sumAgg" : {
"value" : 241.0
},
"aggs" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ ]
}
},
{
"key" : "70002045",
"doc_count" : 130,
"sumAgg" : {
"value" : 131.0
},
"aggs" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "虾滑",
"doc_count" : 115
}
]
}
},
{
"key" : "03010013",
"doc_count" : 83,
"sumAgg" : {
"value" : 87.0
},
"aggs" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "捞派麻辣滑牛肉",
"doc_count" : 83
}
]
}
},
{
"key" : "12020042",
"doc_count" : 79,
"sumAgg" : {
"value" : 111.0
},
"aggs" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ ]
}
}
{
"key" : "10020075",
"doc_count" : 68,
"sumAgg" : {
"value" : 72.0
},
"aggs" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "包心生菜",
"doc_count" : 68
}
]
}
},
{
"key" : "10010017",
"doc_count" : 41,
"sumAgg" : {
"value" : 42.0
},
"aggs" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "冻豆腐",
"doc_count" : 41
}
]
}
},
]
}
}
}
GET shop-pos-order-log-2022-06/_
search
{ "size": 0,
"aggs": {
"posOrder": {
"terms": {
"field": "itemno",
"size": 100000
},
"aggs": {
"sumAgg": {
"sum": {
"field": "qty"
}
},"aggs": {
"terms": {
"field": "dish_name.keyword",
"size": 10
}
}
}
}
},
"query": {
"bool": {
"must": [
{"term": {"action": {"value": 301}}},
{"term": {"shop_id": {"value": 109792}}},
{"term": {"opttime": {"value": "2022-06-05"}}}
]
}
}
}