当前位置 : 主页 > 编程语言 > java >

ElasticSearch-整合Java High Level REST Client  测试

来源:互联网 收集:自由互联 发布时间:2023-03-22
导入依赖 导入pom文件 依据自己版本dependency groupIdorg.elasticsearch.client/groupId artifactIdelasticsearch-rest-high-level-client/artifactId version7.4.2/version/dependency设置版本properties java.version1.8/java.version e

导入依赖

导入pom文件 依据自己版本 <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.4.2</version> </dependency> 设置版本 <properties> <java.version>1.8</java.version> <elasticsearch.version>7.4.2</elasticsearch.version> </properties> //新建配置类 并设置Client和指定端口号 @Configuration public class GulimallElasticSearchConfig { @Bean //放到容器中 public RestHighLevelClient esRestClient(){ RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.56.10", 9200, "http"))); return client; } }

操作文档 https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html

GulimallElasticSearchConfig

@Configuration public class GulimallElasticSearchConfig { public static final RequestOptions COMMON_OPTIONS; static { RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); //builder.addHeader("Authorization", "Bearer " + TOKEN); //builder.setHttpAsyncResponseConsumerFactory( // new HttpAsyncResponseConsumerFactory // .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024)); COMMON_OPTIONS = builder.build(); } @Bean //放到容器中 public RestHighLevelClient esRestClient(){ RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.56.10", 9200, "http"))); return client; } }

测试简单索引

@Autowired private RestHighLevelClient client;

@Test //测试es 存储数据 public void indexData() throws IOException { IndexRequest indexRequest = new IndexRequest("users"); indexRequest.id("1"); User user = new User(); user.setUserName("咱三"); user.setAge(18); user.setGender("男"); String jsonString = JSON.toJSONString(user);//转换String类型 indexRequest.source(jsonString, XContentType.JSON);//保存内容 //执行操作 IndexResponse index = client.index(indexRequest, GulimallElasticSearchConfig.COMMON_OPTIONS); // System.out.println(index); }

测试复杂

@Test //复杂检索测试 public void searchData() throws IOException { //1.创建索引请求 SearchRequest searchRequest = new SearchRequest(); //2.指定索引 searchRequest.indices("bank"); //指定DSL检索条件 SearchSourceBuilder 封装的全部检索 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.matchQuery("address", "mill")); //年龄聚合 TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg").field("age").size(10); sourceBuilder.aggregation(ageAgg); //计算平均薪资 AvgAggregationBuilder balanceAvg = AggregationBuilders.avg("balanceAvg").field("balance"); sourceBuilder.aggregation(balanceAvg); System.out.println("检索条件" + sourceBuilder.toString()); searchRequest.source(sourceBuilder); //执行检索 SearchResponse searchResponse = client.search(searchRequest, GulimallElasticSearchConfig.COMMON_OPTIONS); //分析结果 System.out.println("检索结果" + searchResponse.toString()); //封装成map 数据从map中获取 //Map map = JSON.parseObject(searchResponse.toString(), Map.class); //获取所有查到的数据? 先拿到外边的hits SearchHits hits = searchResponse.getHits(); //拿到里面真是的hits SearchHit[] searchHits = hits.getHits(); for (SearchHit hit : searchHits) { //getSource转为string String string = hit.getSourceAsString(); //json转对象 把string转换成Accout.class类型的对象 Accout accout = JSON.parseObject(string, Accout.class); System.out.println("accout" + accout); } //获取聚合信息 Aggregations aggregations = searchResponse.getAggregations(); //将聚合信息都做成list 在增强for //for (Aggregation aggregation : aggregations.asList()) { // aggregation.getName(); //} //获取ageAgg 并转换 Terms Terms ageAgg1 = aggregations.get("ageAgg"); for (Terms.Bucket bucket : ageAgg1.getBuckets()) { String keyAsString = bucket.getKeyAsString(); System.out.println("年龄" + keyAsString + "--->" + bucket.getDocCount()); } //balanceAvg 并转换 Avg Avg balanceAvg1 = aggregations.get("balanceAvg"); double value = balanceAvg1.getValue(); System.out.println("平均薪资" + value); }
上一篇:ElasticSearch-商品上架+商品查询
下一篇:没有了
网友评论