1. The introduction of

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>{version}</version>
</dependency>
Copy the code
@Bean public RestHighLevelClient ESRestClient() { return new RestHighLevelClient(RestClient.builder(new HttpHost (" 127.0.0.1 ", 9200, "HTTP"))); }Copy the code
Possible error 1: Found interface org.elasticsearch.com mon. Bytes. BytesReference, but the class was expected - highlevelclient versions do not match es version, <properties> </ elasticSearch.version > </elasticsearch.version>Copy the code

2. Query with conditions

GET _CAT /indices GET KIbanA_sample_datA_ecommerce /_search {"query": {"match": {"customer_gender": "MALE" } }, "aggs": { "customerAgg": { "terms": { "field": "customer_id", "size": 10 } }, "priceAgg": { "avg": { "field": "taxful_total_price" } } } }Copy the code

DSL query result:

Corresponding code:

void searchDate() throws IOException { // 1. SearchRequest = new SearchRequest(); request.indices("kibana_sample_data_ecommerce"); SearchSourceBuilder builder = new SearchSourceBuilder(); request.source(builder); Query(QueryBuilders. MatchQuery ("customer_gender", "MALE")); // builder.from(0); // builder.size(20); / / in accordance with the customer_id aggregation builder. Aggregation (AggregationBuilders. Terms (" customerAgg "). The field (" customer_id "). The size (10)); // builder.aggregation(AggregationBuilders.avg("priceAgg").field("taxful_total_price")); SearchResponse Response = client.search(request, COMMON_OPTIONS); System.out.println(request.source())); SearchHit[] hits = response.getHits().getHits(); for (SearchHit hit: hits) { String sourceAsString = hit.getSourceAsString(); System.out.println(sourceAsString); } Terms customerAgg = response.getaggregations ().get("customerAgg"); customerAgg.getBuckets().forEach(bucket -> System.out.println(bucket.getKey())); }Copy the code