Elasticsearch 使用terms对long类型日期统计按月销售

索引mapping:

json 复制代码
{
      "properties" : {
        "_class" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "createDate" : {
          "type" : "long"
        },
        "goodsCode" : {
          "type" : "keyword"
        },
        "id" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "orderNumber" : {
          "type" : "keyword"
        },
        "payDate" : {
          "type" : "long"
        },
        "saleQty" : {
          "type" : "double"
        }
      }
    }

payDate字段映射为long类型,在使用date_histogram统计时,统计不出想要的效果。

json 复制代码
{
    ...
    "aggs": {
       "agg_name": {
            "date_histogram": {
                "field": "payDate",
                "interval": "1m",
                "format": "yyyy-MM",
                "min_doc_count": 0
            }
        }
    }
}

结果输出的是"yyyy-MM1577836800000",并不是期待的2023-06.

json 复制代码
{
    "key_as_string": "yyyy-MM1577836800000",
    "key": 1577836800000,
    "doc_count": 3
}

改用terms+script+子聚合方式统计:

json 复制代码
{
  "query": {
    "range": {
      "payDate": {
        "gte": 1685548800000
      }
    }
  },
  "size":0, 
  "aggs": {
    "month_sales":{
      "terms": {
        "script": {
          "source": """
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM").withZone(ZoneId.systemDefault());
           return df.format(Instant.ofEpochMilli(doc['payDate'].value));
        """,
        "lang":"painless"
        },
        "order": {
          "_key": "asc"
        }
      },
      "aggs": {
        "sale_sum": {
          "sum": {
            "field": "saleQty"
          }
        }
      }
    }
  }
}

script:使用脚本把pay格式化成yyyy-MM的字符串。

order:根据日期排序

sale_sum:统计每个桶里面商品销售数量

输出结果:

json 复制代码
{
    "month_sales": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 9723368,
        "buckets": [
            {
                "key": "2023-06",
                "doc_count": 49,
                "sale_sum": {
                    "value": 77.0
                }
            },
            {
                "key": "2023-07",
                "doc_count": 217,
                "sale_sum": {
                    "value": 40012.0
                }
            },
            {
                "key": "2023-08",
                "doc_count": 2187911,
                "sale_sum": {
                    "value": 3486633.0
                }
            },
            {
                "key": "2023-09",
                "doc_count": 2746235,
                "sale_sum": {
                    "value": 4556942.0
                }
            },
            {
                "key": "2023-10",
                "doc_count": 2346896,
                "sale_sum": {
                    "value": 4097690.0
                }
            },
            {
                "key": "2023-11",
                "doc_count": 2442224,
                "sale_sum": {
                    "value": 4120708.0
                }
            },
            {
                "key": "2023-12",
                "doc_count": 2359840,
                "sale_sum": {
                    "value": 4167825.0
                }
            },
            {
                "key": "2024-01",
                "doc_count": 3619202,
                "sale_sum": {
                    "value": 6506941.0
                }
            },
            {
                "key": "2024-02",
                "doc_count": 2673919,
                "sale_sum": {
                    "value": 5066871.0
                }
            },
            {
                "key": "2024-03",
                "doc_count": 3078439,
                "sale_sum": {
                    "value": 4810836.0
                }
            }
        ]
    }
}

key:每个月份

doc_count:每个月的订单数量

sale_sum:每个月商品销售数量

相关推荐
Dragon~Snow8 小时前
Linux Centos9 安装 Elasticsearch
linux·elasticsearch·jenkins
熊延8 小时前
麒麟V10系统安装部署elasticsearch
linux·运维·服务器·elasticsearch·搜索引擎·全文检索
weisian1518 小时前
Elasticsearch-1--什么是ES?
大数据·elasticsearch·搜索引擎
玄同7659 小时前
Git常用命令指南
大数据·git·elasticsearch·gitee·github·团队开发·远程工作
Elasticsearch11 小时前
弥合差距:从云原生到大型机的端到端可观测性
elasticsearch
java-yi14 小时前
Elasticsearch(ES)核心用法与实战技巧分享
大数据·elasticsearch·搜索引擎
星辰_mya15 小时前
Es之脑裂
大数据·elasticsearch·搜索引擎
历程里程碑18 小时前
普通数组-----除了自身以外数组的乘积
大数据·javascript·python·算法·elasticsearch·搜索引擎·flask
闲人编程1 天前
Elasticsearch搜索引擎集成指南
python·elasticsearch·搜索引擎·jenkins·索引·副本·分片
先跑起来再说1 天前
Git 入门到实战:一篇搞懂安装、命令、远程仓库与 IDEA 集成
ide·git·后端·elasticsearch·golang·intellij-idea