elasticsearch统计数量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了elasticsearch统计数量相关的知识,希望对你有一定的参考价值。

Elasticsearch根据条件统计结果数量

post ip:port/index/type/_search

"from": 0,
"size": 0,
"query":
"bool":
"must": [
"match_phrase":
"name":
"query": "aaaa",
"slop": 0,
"boost": 1


,

"range":
"startTime":
"from": 1661616000391,
"to": null,
"include_lower": true,
"include_upper": true,
"boost": 1



]

,
"_source":
"includes": [
"COUNT"
],
"excludes": []
,
"aggregations":
"COUNT(*)":
"value_count":
"field": "_index"




java代码:

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
AggregationBuilder sumAggregationBuilder = AggregationBuilders.count("count").field("_index");
sourceBuilder.aggregation(sumAggregationBuilder);

BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
//查询条件
queryBuilder.must(QueryBuilders.matchPhraseQuery("name.keyword",name));
queryBuilder.must(QueryBuilders.rangeQuery("startTime").gte(startTime));
//设置条件和结果数量
sourceBuilder.query(queryBuilder).from(0).size(0);

//查询索引对象
SearchRequest searchRequest = new SearchRequest(index).types(type).source(sourceBuilder);

//不同版本请求ES的方式不一样
SearchResponse response = client.search(searchRequest).actionGet();
SearchResponse response = client.getClient().search(rq,RequestOptions.DEFAULT);

//返回结果
long total = response.getHits().getTotalHits();

以上是关于elasticsearch统计数量的主要内容,如果未能解决你的问题,请参考以下文章

2021年大数据ELK(十七):Elasticsearch SQL 订单统计分析案例

ES 24 - 如何通过Elasticsearch进行聚合检索 (分组统计)

干货 | Elasticsearch 词频统计的四种方案

Elasticsearch操作

Elasticsearch 分组聚合查询(bucket) --- 2022-04-03

elasticsearch2