(三)ElasticSearch7.X之聚合查询java代码(所有队伍的valueCount)

本文展示了一种使用Elasticsearch进行NBA球员数据查询的方法,通过限定查询特定球队(如休斯顿火箭队),并利用聚合查询功能统计球员的年度数量。代码示例展示了如何构建查询请求、设置查询条件及聚合字段,并解析返回的聚合结果。
POST /nba/_search
{
    "query": {
    "term": {
    "teamNameEn.keyword": {
        "value": "Rockets"
        }
      }
},
"aggs": {
    "countPlayerYear": {
    "value_count": {
    "field": "playYear"
      }
    }
},
"size": 0
}
    @Test
    public void test02() throws IOException {
   
   
        SearchRequest searchRequest = new SearchRequest("nba");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //限定了查询的队伍名称
//      searchSourceBuilder.query(QueryBuilders.termQuery("teamName.keyword","火箭")).size(100);
    
在使用Elasticsearch Java API Client 8.x进行线路个数分组统计,通常会使用聚合查询。以下是一个示例代码,假设你有一个包含线路信息的索引,并且要根据线路名称进行分组统计线路的个数: ```java import co.elastic.clients.elasticsearch.ElasticsearchClient; import co.elastic.clients.elasticsearch._types.Aggregation; import co.elastic.clients.elasticsearch._types.Script; import co.elastic.clients.elasticsearch._types.query_dsl.MatchAllQuery; import co.elastic.clients.elasticsearch._types.query_dsl.Query; import co.elastic.clients.elasticsearch.core.SearchRequest; import co.elastic.clients.elasticsearch.core.SearchResponse; import co.elastic.clients.elasticsearch.core.search.AggregationResult; import co.elastic.clients.elasticsearch.core.search.TermsAggregation; import co.elastic.clients.elasticsearch.core.search.TermsBucket; import co.elastic.clients.json.jackson.JacksonJsonpMapper; import co.elastic.clients.transport.ElasticsearchTransport; import co.elastic.clients.transport.rest_client.RestClientTransport; import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import java.io.IOException; import java.util.Map; public class ElasticsearchLineCountAggregation { public static void main(String[] args) throws IOException { // 创建Elasticsearch客户端 RestClient restClient = RestClient.builder( new HttpHost("localhost", 9200)).build(); // 创建传输层 ElasticsearchTransport transport = new RestClientTransport( restClient, new JacksonJsonpMapper()); // 创建Elasticsearch客户端实例 ElasticsearchClient client = new ElasticsearchClient(transport); // 构建聚合查询 Aggregation aggregation = Aggregation.of(a -> a .terms(TermsAggregation.of(t -> t .field("line_name") // 假设线路名称字段为line_name .size(100) // 设置返回的桶的最大数量 )) ); // 构建查询请求 Query query = MatchAllQuery.of(m -> m)._toQuery(); SearchRequest searchRequest = SearchRequest.of(s -> s .index("your_index_name") // 替换为你的索引名称 .query(query) .aggregations("line_count_agg", aggregation) ); // 执行查询 SearchResponse<Void> searchResponse = client.search(searchRequest, Void.class); // 处理聚合结果 Map<String, AggregationResult> aggregations = searchResponse.aggregations(); AggregationResult lineCountAgg = aggregations.get("line_count_agg"); for (TermsBucket bucket : lineCountAgg.terms().buckets().array()) { String lineName = bucket.key().stringValue(); long count = bucket.docCount(); System.out.println("线路名称: " + lineName + ", 数量: " + count); } // 关闭客户端 restClient.close(); } } ``` ### 代码解释 1. **创建Elasticsearch客户端**:使用`RestClient`和`ElasticsearchTransport`创建`ElasticsearchClient`实例。 2. **构建聚合查询**:使用`Aggregation`和`TermsAggregation`构建按线路名称分组的聚合查询。 3. **构建查询请求**:使用`SearchRequest`构建查询请求,指定索引名称、查询条件和聚合查询。 4. **执行查询**:调用`client.search`方法执行查询。 5. **处理聚合结果**:从`SearchResponse`中获取聚合结果,并遍历每个桶,输出线路名称和数量。 6. **关闭客户端**:关闭`RestClient`。 ### 注意事项 - 请将`your_index_name`替换为你实际的索引名称。 - 请将`line_name`替换为你实际的线路名称字段。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值