java api elasticsearch5.X 查询聚合
//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