elasticsearch Java High Level REST Client
10085 단어 elasticsearch
<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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
kafka connect e elasticsearch를 관찰할 수 있습니다.No menu lateral do dashboard tem a opção de connectors onde ele mostra todos os clusters do kafka connect conectados atu...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.