elasticsearch 進行聚合+去重查詢

已客戶customer為例
我想查詢每日的客戶數(shù)。
先按照日期分桶,然后在桶內(nèi)按照 姓名來去重 來計算客戶數(shù)(實際會按照客戶id 來區(qū)分客戶)
測試數(shù)據(jù)見 文章末尾
一共是9條數(shù)據(jù), 名字分別為:
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"
              }
          }
        }
      }
  }
}

查詢結(jié)果為:

{
  "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條數(shù)據(jù),去重后是6條。

現(xiàn)在就可以根據(jù) 查詢寫java代碼了

    @Test
    public void testAggAndDistinct(){
        //獲取注解,通過注解可以得到 indexName 和 type
        Document document = Customer.class.getAnnotation(Document.class);
        // dateHistogram  Aggregation 是時間柱狀圖聚合,按照天來聚合 , dataAgg 為聚合結(jié)果的名稱,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();

        // 聚合的結(jié)果
        Aggregations aggregations = elasticsearchTemplate.query(searchQuery, response -> response.getAggregations());
        Map<String, Aggregation> results = aggregations.asMap();
        Histogram histogram = (Histogram) results.get("dataAgg");
        // 將bucket list 轉(zhuǎn)換成 map , key -> 名字   value-> 出現(xiàn)次數(shù)
        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());
        });
    }

打印結(jié)果為

時間:2019-04-10 00:00:00
總數(shù) :9
去重后數(shù)量:6

這是我們期望的結(jié)果。

測試數(shù)據(jù)

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"
        }
      }
    ]
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 國家電網(wǎng)公司企業(yè)標準(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報批稿:20170802 前言: 排版 ...
    庭說閱讀 12,332評論 6 13
  • MYSQL 基礎(chǔ)知識 1 MySQL數(shù)據(jù)庫概要 2 簡單MySQL環(huán)境 3 數(shù)據(jù)的存儲和獲取 4 MySQL基本操...
    Kingtester閱讀 8,056評論 5 115
  • --- layout: post title: "如果有人問你關(guān)系型數(shù)據(jù)庫的原理,叫他看這篇文章(轉(zhuǎn))" date...
    藍墜星閱讀 914評論 0 3
  • 俗話說:“沒有規(guī)矩,不成方圓。”也就是說國有國法,校有校規(guī),家有家風。我家的家風是:“誠實守信,謙虛勤奮?!奔胰藗?..
    何佳歡閱讀 334評論 3 1
  • 個人認為我是一個很糾結(jié)的人,我喜歡與不喜歡一個人很難區(qū)分。我一直認為天秤座的形容很符合我。我沒有那么明顯的愛憎分明...
    陰影里的光閱讀 267評論 0 0

友情鏈接更多精彩內(nèi)容