elasticsearch Java High Level REST Client

10085 단어 elasticsearch
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

좋은 웹페이지 즐겨찾기