Elasticsearch 中指定数组字段的统计查询记录
一、基本的数据结构说明
对应 ES 索引:sell_product_order
针对假设 ES 文档的基本结构内容如下:
{ "id": "2024041801000115936701", "sellingProducts": [ "FUND_20150718000230030000000000002549", "STOCK_656", "STOCK_4055", "STOCK_1720", "FUND_20180920000230030000000000015303" ] }
我们针对里面的 sellingProducts 字段进行一些基本的统计操作,本次记录一下相关的基本操作。
二、基本的统计记录
(一)统计当前索引中 sellingProducts 的所有类型
从 sell_product_order 索引中检索数据,然后根据 sellingProducts 字段中的内容,聚合出售产品的类型信息,并返回前 10 个最频繁出现的产品类型。
GET /sell_product_order/_search { "size": 0, "aggs": { "types": { "terms": { "script": { "source": """ HashSet types = new HashSet(); for (item in doc['sellingProducts']) { int delimiterIndex = item.indexOf('_'); if (delimiterIndex > -1) { types.add(item.substring(0, delimiterIndex)); } } return types; "


