最近新出来了一个框架GitHub - dromara/easy-es ,介绍说类似mybatis-plus写法写es,也许可以抽时间试试🤔。
本笔记基于rest-high-level-client,但是在ES的7.15版本rest-high-level-client现在已经被标记为过时了。推荐使用spring-boot-starter-data-elasticsearch依赖。本篇可以仅作了解。
出现了一个名称为Easy-Es 的ORM框架,介绍说使用类似于mybatis-plus,底层使用RestHighLevelClient。
1.建微服务
为ES单独建立一个微服务,模板建立的时候仅选择Spring-web依赖即可,我这里的spring-boot版本选择的2.6.8与其他微服务一致。
2.引入依赖
引入common微服务依赖。
1 2 3 4 5 <dependency > <groupId > com.example.mall</groupId > <artifactId > mall-common</artifactId > <version > 0.0.1-SNAPSHOT</version > </dependency >
引入es依赖
1 2 3 4 5 <dependency > <groupId > org.elasticsearch.client</groupId > <artifactId > elasticsearch-rest-high-level-client</artifactId > <version > 7.4.2</version > </dependency >
指定ES版本,因为是SpringBoot项目,所以ES版本会被SpringBoot版本管理,需要手动改为与自己安装的ES版本一致。按图依次点击即可查看Spring-boot管理的版本:
1 2 3 4 5 <properties > <java.version > 1.8</java.version > <elasticsearch.version > 7.4.2</elasticsearch.version > </properties >
3.启动报错
报错信息:Error creating bean with name ‘configurationPropertiesBeans’
1 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configurationPropertiesBeans' defined in class path resource [ org/springframework/cloud/autoconfigure/ConfigurationPropertiesRebinderAutoConfiguration.class] : Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [ org.springframework.cloud.context.properties.ConfigurationPropertiesBeans] from ClassLoader [ sun.misc.Launcher$AppClassLoader@18 b4aac2]
报错原因:common中的alibaba-spring-cloud-nacos-discover,alibaba-spring-cloud-nacos-config两个依赖中的内部两个依赖:spring-cloud-commons
、spring-cloud-context
的版本,和spring-cloud-starter的内部两个依赖版本不同。
其他依赖common的微服务没有问题,是因为又引入了loader-balance之类的,其内部也有这两个依赖,覆盖之后就没有问题了。
所以我们手动引入这两个依赖,覆盖即可。引入依赖和spring-cloud-starter内部版本一致即可。
手动覆盖版本,与spring-cloud-starter保持一致
1 2 3 4 5 6 7 8 9 10 11 12 <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-context</artifactId > <version > 3.1.2</version > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-commons</artifactId > <version > 3.1.2</version > </dependency >
4.完整pom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 <?xml version="1.0" encoding="UTF-8" ?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <parent > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-parent</artifactId > <version > 2.6.8</version > <relativePath /> </parent > <groupId > com.example.mall</groupId > <artifactId > mall-search</artifactId > <version > 0.0.1-SNAPSHOT</version > <name > mall-search</name > <description > mall检索服务</description > <properties > <java.version > 1.8</java.version > <elasticsearch.version > 7.4.2</elasticsearch.version > </properties > <dependencies > <dependency > <groupId > com.example.mall</groupId > <artifactId > mall-common</artifactId > <version > 0.0.1-SNAPSHOT</version > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-context</artifactId > <version > 3.1.2</version > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-commons</artifactId > <version > 3.1.2</version > </dependency > <dependency > <groupId > org.elasticsearch.client</groupId > <artifactId > elasticsearch-rest-high-level-client</artifactId > <version > 7.4.2</version > </dependency > </dependencies > <build > <plugins > <plugin > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-maven-plugin</artifactId > </plugin > </plugins > </build > </project >
5.配置bootstrap文件
1 2 spring.cloud.nacos.config.server-addr =127.0.0.1:8848 spring.application.name =mall-search
6.编写ES配置文件
建立config包。于config包下创建ElasticSearchConfig类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 @Configuration public class ElasticSearchConfig { public static final RequestOptions COMMON_OPTIONS; static { RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); COMMON_OPTIONS = builder.build(); } @Bean public RestHighLevelClient esRestClient () { return new RestHighLevelClient ( RestClient.builder(new HttpHost ("127.0.0.1" , 9200 , "http" )) ); } }
7. 测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 @SpringBootTest class ElaTests { @Autowired private RestHighLevelClient client; @SneakyThrows @Test void indexData () { IndexRequest indexRequest = new IndexRequest ("users" ); indexRequest.id("1" ); User user = new User ("zhangsan" , "男" , 18 ); String s = JSONObject.toJSONString(user); indexRequest.source(s, XContentType.JSON); IndexResponse index = client.index(indexRequest, ElasticSearchConfig.COMMON_OPTIONS); System.out.println(index); } @Data @AllArgsConstructor @NoArgsConstructor class User { private String userName; private String gender; private Integer age; } @SneakyThrows @Test void searchData () { SearchRequest searchRequest = new SearchRequest (); searchRequest.indices("bank" ); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder (); sourceBuilder.query(QueryBuilders.matchQuery("address" , "mill" )); TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg" ).field("age" ).size(10 ); sourceBuilder.aggregation(ageAgg); searchRequest.source(sourceBuilder); AvgAggregationBuilder balanceAvg = AggregationBuilders.avg("balanceAvg" ).field("balance" ); sourceBuilder.aggregation(balanceAvg); System.out.println(sourceBuilder.toString()); SearchResponse search = client.search(searchRequest, ElasticSearchConfig.COMMON_OPTIONS); System.out.println(search.toString()); SearchHits hits = search.getHits(); SearchHit[] searchHits = hits.getHits(); for (SearchHit hit : searchHits) { String sourceAsString = hit.getSourceAsString(); Account account = JSONObject.parseObject(sourceAsString, Account.class); System.out.println("account: " + account); } Aggregations aggregations = search.getAggregations(); Terms ageAgg1 = aggregations.get("ageAgg" ); for (Terms.Bucket bucket : ageAgg1.getBuckets()) { System.out.println("年龄:" + bucket.getKeyAsString() + " 总数" + bucket.getDocCount()); } Avg balanceAvg1 = aggregations.get("balanceAvg" ); System.out.println("平均薪资:" + balanceAvg1.getValueAsString()); } @Test void contextLoads () { System.out.println(client); } }