elasticsearch Java High Level REST Client

maven

    
    <dependency>
      <groupId>org.elasticsearchgroupId>
      <artifactId>elasticsearchartifactId>
      <version>${elasticsearch.version}version>
    dependency>
    
    <dependency>
      <groupId>org.elasticsearch.clientgroupId>
      <artifactId>elasticsearch-rest-high-level-clientartifactId>
      <version>6.0.1version>
    dependency>
    
    <dependency>
      <groupId>org.apache.httpcomponentsgroupId>
      <artifactId>httpclientartifactId>
      <version>4.5.2version>
    dependency>

代码:

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;

public class test_agg {
    public static void main(String[] args) {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.123.111",9200,"http"),
                        new HttpHost("192.168.123.112",9200,"http")
                )
        );
        //设置index
        SearchRequest searchRequest = new SearchRequest("device_all");
        //设置type
        searchRequest.types("my_type");
        //创建SearchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //添加查询条件
        searchSourceBuilder.query(QueryBuilders.matchPhraseQuery("profession","IT"));
        //默认是10
        searchSourceBuilder.size(10);
        searchRequest.source(searchSourceBuilder);
        //根据年龄分组
        TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("by_age").field("age");
        //(去重加法)每个年龄段有多少种爱好
        aggregationBuilder.subAggregation(AggregationBuilders.cardinality("count").field("hobbies"));
        searchSourceBuilder.aggregation(aggregationBuilder);
        try {
            SearchResponse searchResponse = client.search(searchRequest);
            Aggregations aggregations = searchResponse.getAggregations();
            Terms byProvince = aggregations.get("by_age");
            for (Terms.Bucket bucket:byProvince.getBuckets()){
                Cardinality count = bucket.getAggregations().get("count");
                long value = count.getValue();
                System.out.println(bucket.getKey());
                System.out.println(value);
            }
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

参考博客链接:
http://blog.csdn.net/u012116196/article/details/51789253
官网链接:
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html

你可能感兴趣的:(elasticsearch)