//indexName索引名称,typeName类型名称
//查询所有用户名以张开头的用户
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
.must(QueryBuilders.prefixQuery("name", "张"));
/根据name字段聚合,聚合结果名称为aggName
TermsBuilder termsAgg = AggregationBuilders.terms("aggName").field("name");
//进行查询聚合操作
SearchResponse response = Client.prepareSearch("indexName").setTypes("typeName")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(queryBuilder)
.addAggregation(termsAgg)
.setSize(0)
.get();
1.查询接口
---构建查询,查询name为张三的所有数据
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
----must接必要条件
queryBuilder.must(queryBuilder.termQuery("name","张三");)
//精准查询,查询name为张三的数据
queryBuilder.termQuery("name","张三");
//多词匹配,匹配name为张三或者李四的数据
queryBuilder.termsQuery("name",new String[]{"张三", "李四"});
//前缀匹配,匹配name以张开头的数据
queryBuilder.prefixQuery("name","张");
//范围匹配,匹配time在startTime和endTime范围内的数据 get大于等于 lte小于等于
//--时间范围匹配,结果转换格式yyyy-MM-dd HH:mm:ss
QueryBuilders.rangeQuery("time").format("yyyy-MM-dd HH:mm:ss").gte(startTime).lte(endTime);
//--普通数据匹配
QueryBuilders.rangeQuery("time").gte(startTime).lte(endTime);
//存在匹配,匹配所有存在name字段的数据
QueryBuilders.existsQuery("name");
//正则匹配,匹配所有正则表达式匹配的内容
QueryBuilders.regexpQuery("name","正则表达式");
//通配符匹配,匹配name字段所有以洋结尾的数据的数据
QueryBuilders.wildcardQuery("name","*洋")
2.聚合接口
//Terms聚合,根据name字段做聚合,一般作为多重聚合第一层使用
TermsBuilder field = AggregationBuilders.terms("termsAgg").field("name");
//获取返回结果
Terms agg = response.getAggregations().get("termsAgg")
String name = agg.getBuckets().get(i).getKey();
//sum聚合,求所有score字段之和
SumAggregationBuilder sumAgg = AggregationBuilders.sum("scoreAgg").field("score");
//获取返回结果
InternalSum agg = response.getAggregations().get("scoreAgg");
int sumNum = (int)agg.getValue();
//count聚合,求总个数
ValueCountBuilder countAgg = AggregationBuilders.count("countAgg").field("name");
//获取返回结果
InternalValueCount agg = response.getAggregations().get("countAgg");
int countNum = (int)agg.getValue();
//直方图聚合,求按月聚合,每月的数量,通常和count或者sum一起多层聚合使用
DateHistogramAggregationBuilder dateHisAgg = AggregationBuilders.dateHistogram("timeAGG").field("time").format("yyyy年MM月")
.dateHistogramInterval(DateHistogramInterval.MONTH).minDocCount(0)
.extendedBounds(new ExtendedBounds(start, end));
//与sum聚合一起用
dateHisAgg.subAggregation(sumAgg);
//返回结果
InternalDateHistogram agg = response.getAggregations().get("timeAGG");
//遍历,返回日志和数量
for (int j = 0; j < agg.getBuckets().size(); j++) {
String edate = agg.getBuckets().get(j).getKeyAsString();
InternalSum agg1 = agg.getBuckets().get(j).getAggregations().get("sumAgg");
int sumNum = (int) agg1.getValue();
}
//去重聚合,在基数为100-40000的前提下,能够基本准确。
CardinalityBuilder cardAgg= AggregationBuilders.cardinality("cardAgg").field("name");
//返回结果
Cardinality agg = response.getAggregations().get("cardAgg");
agg .getValue();
//根据聚合的结果排序,根据name聚合求score总分数,然后根据总分数进行倒序排序
.addAggregation(AggregationBuilders.terms("termsAgg").field("name")
.order(Terms.Order.aggregation("sumAgg", "value", false))
.subAggregation(AggregationBuilders.sum("sumAgg").field("score"))
)
来源:CSDN
作者:冯囧囧
链接:https://blog.csdn.net/weixin_42699857/article/details/104038801