【ElasticSearch】聚合

【ElasticSearch】聚合

==========================================================

1、基本聚合

2、统计null值

==========================================================

1、基本聚合

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.size(0);

TermsAggregationBuilder aggregation = AggregationBuilders.terms("group").field("createBy").size(5000);
searchSourceBuilder.aggregation(aggregation);

MissingAggregationBuilder missing = AggregationBuilders.missing("nullValue").field("createBy");
searchSourceBuilder.aggregation(missing);

 

复制代码
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "status": {
              "value": 3,
              "boost": 1
            }
          }
        },
        {
          "range": {
            "createTime": {
              "from": "2020-12-01 00:00:00",
              "to": "2021-05-11 23:59:59",
              "include_lower": true,
              "include_upper": false,
              "boost": 1
            }
          }
        }
      ]
    }
  },
  "aggregations": {
    "createByGroup": { // 聚合名称
      "terms": {
        "field": "createBy", // 聚合字段
        "size": 5000,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [ // 聚合排序
          {
            "_count": "desc"
          },
          {
            "_key": "asc"
          }
        ]
      }
    },
    "nullCreateBy": {// 统计值为null的聚合
      "missing": {
        "field": "createBy"// null值字段
      }
    }
  }
}
复制代码

 

2、统计null值

MissingAggregationBuilder missing = AggregationBuilders.missing("nullValue").field("createBy");
searchSourceBuilder.aggregation(missing);
  "aggregations": {
    "nullValue": {
      "missing": {
        "field": "createBy"
      }
    }
  }

 

聚合排序

复制代码
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "position": {
              "value": "1227880568159477762"
            }
          }
        },
        {
          "range": {
            "date_time": {
              "from": "2020-03-21 17:26:00",
              "to": "2020-03-22 17:26:59",
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        }
      ]
    }
  },
  "sort": [
    {
      "date_time": {
        "order": "desc"
      }
    }
  ],
  "aggs": {
    "group_minute": {
      "date_histogram": {
        "field": "date_time",
        "interval": "minute",
        "format": "yyyy-MM-dd HH:mm",
        "order": {
          "_key": "desc"
        }
      },
      "aggs": {
        "avg_value": {
          "avg": {
            "field": "value"
          }
        }
      }
    }
  }
}
复制代码

 

posted @   翠微  阅读(331)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示