弹性搜索中的聚合

时间:2019-03-12 02:46:32

标签: elasticsearch elasticsearch-aggregation

我正在弹性搜索中学习聚合。

索引文件:

{
    "_index": "Sample",
    "_type": "demo",
    "_id": "Demo1",
    "_score" : 1.0,
    "_source": {
      "NAME": " aaaesh",
       "EMAILADDRESS": "siillai@example.com",
       "LASTUPD": "25-NOV-2018 06:11:06",
    }
}

我需要按desc顺序和组电子邮件地址订购LASTUPD日期,并且仅获取最新记录。我试图在聚合中使用分区。

下面是我的Java代码:

    for (int i = 0; i < 10; i++) {
        SearchRequestBuilder requestBuilder = client.prepareSearch(index).setTypes(type).setFrom(from).setSize(size)
                .addSort("LASTUPD", SortOrder.DESC)
                .addAggregation(AggregationBuilders.terms("by_email").field("EMAILADDRESS").size(10000)
                        .subAggregation(AggregationBuilders.topHits("by_email_addr").size(1))
                        .includeExclude(new IncludeExclude(i, 10)));

        QueryBuilder qb = QueryBuilders.queryStringQuery(query);
        SearchResponse response = requestBuilder.setQuery(qb).get();

        Terms clientTerm = response.getAggregations().get("by_email");
        for (Terms.Bucket clientTermBucket : clientTerm.getBuckets()) {
            TopHits clientTermTopHits = clientTermBucket.getAggregations().get("by_email_addr");

            for (SearchHit d : clientTermTopHits.getHits().getHits()) {
                System.out.println(d.getSourceAsMap());
            }

        }
    }

挑战:

我还需要进行索引编制,就像我需要根据

的结果进行汇总一样
(client.prepareSearch(index).setTypes(type).setFrom(from).setSize(size))

示例:在请求中,如果我从0传递,作为大小传递10。它将获得从0到10的结果并执行汇总。

当前: 它获取索引中的所有文档,然后执行汇总。

0 个答案:

没有答案