9

elasticsearch统计数量

 2 years ago
source link: https://blog.51cto.com/u_15261296/5940329
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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();
  • 打赏
  • 收藏
  • 评论
  • 分享
  • 举报

</div


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK