1. Import Maven
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
Copy the code
Write the configuration and inject a RestHighLevelClient into the container
@configuration public class ElasticSearchConfig {static final RequestOptions COMMON_OPTIONS; static { RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); COMMON_OPTIONS = builder.build(); } @Bean public RestHighLevelClient esRestClient(){ RestClientBuilder builder = RestClient.builder(new HttpHost (" 192.168.0.24 ", 9200, "HTTP")); RestHighLevelClient client = new RestHighLevelClient(builder); return client; }}Copy the code
ElasticSearch test class
@RunWith(SpringRunner.class) @SpringBootTest public class ElasticSearchConfigTest { @Autowired private RestHighLevelClient client; /** * Stores data * @throws IOException */ @test public void indexData() throws IOException {IndexRequest IndexRequest = new IndexRequest("approval_list_zhc"); User user = new User(); user.setAge(1); user.setEmail("1"); user.setName("zs"); user.setSalary(200); indexRequest.source(JacksonUtils.obj2String(user), XContentType.JSON); / / perform operations IndexResponse index = client. Index (indexRequest, MON_OPTIONS ElasticSearchConfig.COM); System.out.println(" response: "+ index); } @Data class User{ private String name; private Integer age; private Integer salary; private String email; } @test public void searchData() throws IOException {SearchRequest SearchRequest = new SearchRequest(); Searchrequest. indices("approval_list_zhc"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); Sourcebuilder.query (QueryBuilders. MatchQuery ("name","wangcai")); sourceBuilder.from(0); sourceBuilder.size(2); sourceBuilder.sort("id", SortOrder.DESC); / / polymerization TermsAggregationBuilder ageAgg = AggregationBuilders. Terms (" ageAgg "). The field (" age "). The size (10); sourceBuilder.aggregation(ageAgg); AvgAggregationBuilder salaryAvg = AggregationBuilders. Avg ("salaryAvg"). Field ("salary"); sourceBuilder.aggregation(salaryAvg); System.out.println(" search criteria: "+ sourceBuilder); searchRequest.source(sourceBuilder); / / 2, executive searching the SearchResponse the SearchResponse = client. The search (searchRequest, MON_OPTIONS ElasticSearchConfig.COM); //3, searchResponse SearchHits hits = searchResponse.gethits (); SearchHit[] searchHits = hits.getHits(); for (SearchHit hit : searchHits) { String sourceAsString = hit.getSourceAsString(); System.out.println("sourceAsString:" + sourceAsString); } Aggregations aggregations = searchResponse.getAggregations(); Terms ageAgg1 = aggregations.get("ageAgg"); for (Terms.Bucket bucket : ageAgg1.getBuckets()) { String keyAsString = bucket.getKeyAsString(); System.out.println(" age: "+ keyAsString); } Avg salaryAvg1 = aggregations.get("salaryAvg"); System.out.println(" salaryavg1.getValue () "); } @test public void buildSearchRequest() throws IOException {//1. Create search criteria SearchRequest SearchRequest = new SearchRequest(); searchRequest.indices("approval_list_zhc"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); Bool BoolQueryBuilder boolQuery = QueryBuilders. BoolQuery (); boolQuery.must(QueryBuilders.matchQuery("name","zs")); boolQuery.filter(QueryBuilders.termQuery("age",1)); sourceBuilder.query(boolQuery); searchRequest.source(sourceBuilder); SearchResponse searchResponse = client.search(searchRequest, ElasticSearchConfig.COMMON_OPTIONS); SearchHits hits = searchResponse.getHits(); for (SearchHit hit : hits) { String sourceAsString = hit.getSourceAsString(); System.out.println(sourceAsString); } } @Test public void contextLoads(){ System.out.println(client); }}Copy the code