elasticsearch 进行聚合+去重查询

已客户customer为例
我想查询每日的客户数。
先按照日期分桶,然后在桶内按照 姓名来去重 来计算客户数(实际会按照客户id 来区分客户)
测试数据见 文章末尾
一共是9条数据, 名字分别为:
river Lucy 1 Lucy frank tom lily lily tom tom
不同的名字是 6 个。

先看看 es 的 query 怎么写

GET /es-customer/_search
{
  "size" : 0,
  "aggs" : {
      "days" : {
        "date_histogram": {
          "field": "createTime",
          "interval": "day"
        },
        "aggs": {
          "distinct_name" : {
              "cardinality" : {
                "field" : "firstName"
              }
          }
        }
      }
  }
}

查询结果为:

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 2,
    "successful": 2,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 9,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "days": {
      "buckets": [
        {
          "key_as_string": "2019-04-10 00:00:00",
          "key": 1554854400000,
          "doc_count": 9,
          "distinct_name": {
            "value": 6
          }
        }
      ]
    }
  }
}

2019-04-10 当天查出了9条数据,去重后是6条。

现在就可以根据 查询写java代码了

    @Test
    public void testAggAndDistinct(){
        //获取注解,通过注解可以得到 indexName 和 type
        Document document = Customer.class.getAnnotation(Document.class);
        // dateHistogram  Aggregation 是时间柱状图聚合,按照天来聚合 , dataAgg 为聚合结果的名称,createTime 为字段名称
        // cardinality 用来去重
        SearchQuery searchQuery  = new NativeSearchQueryBuilder()
                .withQuery(matchAllQuery())
                .withSearchType(SearchType.QUERY_THEN_FETCH)
                .withIndices(document.indexName()).withTypes(document.type())
                .addAggregation(AggregationBuilders.dateHistogram("dataAgg").field("createTime").dateHistogramInterval(DateHistogramInterval.DAY)
                        .subAggregation(AggregationBuilders.cardinality("nameAgg").field("firstName")))
                .build();

        // 聚合的结果
        Aggregations aggregations = elasticsearchTemplate.query(searchQuery, response -> response.getAggregations());
        Map<String, Aggregation> results = aggregations.asMap();
        Histogram histogram = (Histogram) results.get("dataAgg");
        // 将bucket list 转换成 map , key -> 名字   value-> 出现次数
        histogram.getBuckets().stream().forEach(t->{
            Histogram.Bucket histogram1 = t;
            System.out.println(histogram1.getKeyAsString());
            Cardinality cardinality = histogram1.getAggregations().get("nameAgg");
            System.out.println(cardinality.getValue());
        });
    }

打印结果为

时间:2019-04-10 00:00:00
总数 :9
去重后数量:6

这是我们期望的结果。

测试数据

GET /es-customer/_search
{
  "query": {
    "match_all": {}
  }
}
{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 2,
    "successful": 2,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 9,
    "max_score": 1,
    "hits": [
      {
        "_index": "es-customer",
        "_type": "customer",
        "_id": "_z8_BmoB7Iqmj8bUCgie",
        "_score": 1,
        "_source": {
          "id": null,
          "firstName": "Lucy 1",
          "lastName": "001",
          "valid": null,
          "age": 13,
          "createTime": "2019-04-10 07:55:55"
        }
      },
      {
        "_index": "es-customer",
        "_type": "customer",
        "_id": "Aj8_BmoB7Iqmj8bUCwkY",
        "_score": 1,
        "_source": {
          "id": null,
          "firstName": "tom",
          "lastName": "001",
          "valid": null,
          "age": 44,
          "createTime": "2019-04-10 07:55:56"
        }
      },
      {
        "_index": "es-customer",
        "_type": "customer",
        "_id": "Az8_BmoB7Iqmj8bUCwk6",
        "_score": 1,
        "_source": {
          "id": null,
          "firstName": "lily",
          "lastName": "002",
          "valid": null,
          "age": 56,
          "createTime": "2019-04-10 07:55:56"
        }
      },
      {
        "_index": "es-customer",
        "_type": "customer",
        "_id": "BD8_BmoB7Iqmj8bUCwlc",
        "_score": 1,
        "_source": {
          "id": null,
          "firstName": "lily",
          "lastName": "004",
          "valid": null,
          "age": 53,
          "createTime": "2019-04-10 07:55:56"
        }
      },
      {
        "_index": "es-customer",
        "_type": "customer",
        "_id": "_j8_BmoB7Iqmj8bUCghV",
        "_score": 1,
        "_source": {
          "id": null,
          "firstName": "river",
          "lastName": "007",
          "valid": null,
          "age": 12,
          "createTime": "2019-04-10 07:55:55"
        }
      },
      {
        "_index": "es-customer",
        "_type": "customer",
        "_id": "AD8_BmoB7Iqmj8bUCgnH",
        "_score": 1,
        "_source": {
          "id": null,
          "firstName": "Lucy",
          "lastName": "002",
          "valid": null,
          "age": 22,
          "createTime": "2019-04-10 07:55:55"
        }
      },
      {
        "_index": "es-customer",
        "_type": "customer",
        "_id": "AT8_BmoB7Iqmj8bUCgnv",
        "_score": 1,
        "_source": {
          "id": null,
          "firstName": "frank",
          "lastName": "001",
          "valid": null,
          "age": 33,
          "createTime": "2019-04-10 07:55:56"
        }
      },
      {
        "_index": "es-customer",
        "_type": "customer",
        "_id": "BT8_BmoB7Iqmj8bUCwmC",
        "_score": 1,
        "_source": {
          "id": null,
          "firstName": "tom",
          "lastName": "002",
          "valid": null,
          "age": 66,
          "createTime": "2019-04-10 07:55:56"
        }
      },
      {
        "_index": "es-customer",
        "_type": "customer",
        "_id": "Bj8_BmoB7Iqmj8bUCwmp",
        "_score": 1,
        "_source": {
          "id": null,
          "firstName": "tom",
          "lastName": "005",
          "valid": null,
          "age": 33,
          "createTime": "2019-04-10 07:55:56"
        }
      }
    ]
  }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 14,044评论 6 13
  • MYSQL 基础知识 1 MySQL数据库概要 2 简单MySQL环境 3 数据的存储和获取 4 MySQL基本操...
    Kingtester阅读 12,404评论 5 116
  • --- layout: post title: "如果有人问你关系型数据库的原理,叫他看这篇文章(转)" date...
    蓝坠星阅读 4,294评论 0 3
  • 俗话说:“没有规矩,不成方圆。”也就是说国有国法,校有校规,家有家风。我家的家风是:“诚实守信,谦虚勤奋。”家人们...
    何佳欢阅读 2,405评论 3 1
  • 个人认为我是一个很纠结的人,我喜欢与不喜欢一个人很难区分。我一直认为天秤座的形容很符合我。我没有那么明显的爱憎分明...
    阴影里的光阅读 1,738评论 0 0

友情链接更多精彩内容