You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. # spring-boot-demo-elasticsearch-rest-high-level-client
  2. > 此 demo 主要演示了 Spring Boot 如何集成 `elasticsearch-rest-high-level-client` 完成对 ElasticSearch 的基本CURD 操作
  3. ## elasticsearch 升级
  4. 先升级到 6.8,索引创建,设置 mapping 等操作加参数:include_type_name=true,然后滚动升级到 7,旧索引可以用 type 访问。具体可以参考:
  5. https://www.elastic.co/cn/blog/moving-from-types-to-typeless-apis-in-elasticsearch-7-0
  6. https://www.elastic.co/guide/en/elasticsearch/reference/7.0/removal-of-types.html
  7. ## 注意
  8. 作者编写本demo时,ElasticSearch版本为 `7.3.0`,使用 docker 运行,下面是所有步骤:
  9. 1. 下载镜像:`docker pull elasticsearch:7.3.0`
  10. 2. 下载安装 `docker-compose`
  11. ```
  12. sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  13. 参考文档: https://docs.docker.com/compose/install/
  14. ```
  15. 2. 编写docker-compose 文件
  16. ```yaml
  17. version: "3"
  18. services:
  19. es7:
  20. hostname: es7
  21. container_name: es7
  22. image: elasticsearch:7.3.0
  23. volumes:
  24. - "/data/es7/logs:/usr/share/es7/logs:rw"
  25. - "/data/es7/data:/usr/share/es7/data:rw"
  26. restart: on-failure
  27. ports:
  28. - "9200:9200"
  29. - "9300:9300"
  30. environment:
  31. cluster.name: elasticsearch
  32. discovery.type: single-node
  33. logging:
  34. driver: "json-file"
  35. options:
  36. max-size: "50m"
  37. ```
  38. 3. 启动: `docker-compose -f elasticsearch.yaml up -d`
  39. ## pom.xml
  40. ```xml
  41. <?xml version="1.0" encoding="UTF-8"?>
  42. <project xmlns="http://maven.apache.org/POM/4.0.0"
  43. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  45. <modelVersion>4.0.0</modelVersion>
  46. <parent>
  47. <artifactId>spring-boot-demo</artifactId>
  48. <groupId>com.xkcoding</groupId>
  49. <version>1.0.0-SNAPSHOT</version>
  50. </parent>
  51. <artifactId>spring-boot-demo-elasticsearch-rest-high-level-client</artifactId>
  52. <name>spring-boot-demo-elasticsearch-rest-high-level-client</name>
  53. <description>Demo project for Spring Boot</description>
  54. <properties>
  55. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  56. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  57. <java.version>1.8</java.version>
  58. </properties>
  59. <dependencies>
  60. <dependency>
  61. <groupId>org.springframework.boot</groupId>
  62. <artifactId>spring-boot-starter</artifactId>
  63. </dependency>
  64. <!-- test -->
  65. <dependency>
  66. <groupId>org.springframework.boot</groupId>
  67. <artifactId>spring-boot-starter-test</artifactId>
  68. <scope>test</scope>
  69. </dependency>
  70. <!-- validator -->
  71. <dependency>
  72. <groupId>org.hibernate.validator</groupId>
  73. <artifactId>hibernate-validator</artifactId>
  74. <scope>compile</scope>
  75. </dependency>
  76. <!--
  77. You can easily generate your own configuration metadata file from items annotated with
  78. @ConfigurationProperties by using the spring-boot-configuration-processor jar.
  79. -->
  80. <dependency>
  81. <groupId>org.springframework.boot</groupId>
  82. <artifactId>spring-boot-configuration-processor</artifactId>
  83. </dependency>
  84. <!-- 工具类 -->
  85. <dependency>
  86. <groupId>cn.hutool</groupId>
  87. <artifactId>hutool-all</artifactId>
  88. <version>4.6.6</version>
  89. </dependency>
  90. <!-- elasticsearch -->
  91. <dependency>
  92. <groupId>org.elasticsearch</groupId>
  93. <artifactId>elasticsearch</artifactId>
  94. <version>7.3.0</version>
  95. </dependency>
  96. <!-- elasticsearch-rest-client -->
  97. <dependency>
  98. <groupId>org.elasticsearch.client</groupId>
  99. <artifactId>elasticsearch-rest-client</artifactId>
  100. <version>7.3.0</version>
  101. </dependency>
  102. <!-- elasticsearch-rest-high-level-client -->
  103. <dependency>
  104. <groupId>org.elasticsearch.client</groupId>
  105. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  106. <version>7.3.0</version>
  107. <exclusions>
  108. <exclusion>
  109. <groupId>org.elasticsearch.client</groupId>
  110. <artifactId>elasticsearch-rest-client</artifactId>
  111. </exclusion>
  112. <exclusion>
  113. <groupId>org.elasticsearch</groupId>
  114. <artifactId>elasticsearch</artifactId>
  115. </exclusion>
  116. </exclusions>
  117. </dependency>
  118. <!-- lombok -->
  119. <dependency>
  120. <groupId>org.projectlombok</groupId>
  121. <artifactId>lombok</artifactId>
  122. <optional>true</optional>
  123. </dependency>
  124. </dependencies>
  125. </project>
  126. ```
  127. ## Person.java
  128. > 实体类
  129. >
  130. ```java
  131. package com.xkcoding.elasticsearch.model;
  132. import lombok.AllArgsConstructor;
  133. import lombok.Builder;
  134. import lombok.Data;
  135. import lombok.NoArgsConstructor;
  136. import java.io.Serializable;
  137. import java.util.Date;
  138. /**
  139. * Person
  140. *
  141. * @author fxbin
  142. * @version v1.0
  143. * @since 2019/9/15 23:04
  144. */
  145. @Data
  146. @Builder
  147. @NoArgsConstructor
  148. @AllArgsConstructor
  149. public class Person implements Serializable {
  150. private static final long serialVersionUID = 8510634155374943623L;
  151. /**
  152. * 主键
  153. */
  154. private Long id;
  155. /**
  156. * 名字
  157. */
  158. private String name;
  159. /**
  160. * 国家
  161. */
  162. private String country;
  163. /**
  164. * 年龄
  165. */
  166. private Integer age;
  167. /**
  168. * 生日
  169. */
  170. private Date birthday;
  171. /**
  172. * 介绍
  173. */
  174. private String remark;
  175. }
  176. ```
  177. ## PersonService.java
  178. ```java
  179. package com.xkcoding.elasticsearch.service;
  180. import com.xkcoding.elasticsearch.model.Person;
  181. import org.springframework.lang.Nullable;
  182. import java.util.List;
  183. /**
  184. * PersonService
  185. *
  186. * @author fxbin
  187. * @version v1.0
  188. * @since 2019/9/15 23:07
  189. */
  190. public interface PersonService {
  191. /**
  192. * create Index
  193. *
  194. * @author fxbin
  195. * @param index elasticsearch index name
  196. */
  197. void createIndex(String index);
  198. /**
  199. * delete Index
  200. *
  201. * @author fxbin
  202. * @param index elasticsearch index name
  203. */
  204. void deleteIndex(String index);
  205. /**
  206. * insert document source
  207. *
  208. * @author fxbin
  209. * @param index elasticsearch index name
  210. * @param list data source
  211. */
  212. void insert(String index, List<Person> list);
  213. /**
  214. * update document source
  215. *
  216. * @author fxbin
  217. * @param index elasticsearch index name
  218. * @param list data source
  219. */
  220. void update(String index, List<Person> list);
  221. /**
  222. * delete document source
  223. *
  224. * @author fxbin
  225. * @param person delete data source and allow null object
  226. */
  227. void delete(String index, @Nullable Person person);
  228. /**
  229. * search all doc records
  230. *
  231. * @author fxbin
  232. * @param index elasticsearch index name
  233. * @return person list
  234. */
  235. List<Person> searchList(String index);
  236. }
  237. ```
  238. ## PersonServiceImpl.java
  239. > service 实现类型,基本CURD操作
  240. ```java
  241. package com.xkcoding.elasticsearch.service.impl;
  242. import cn.hutool.core.bean.BeanUtil;
  243. import com.xkcoding.elasticsearch.model.Person;
  244. import com.xkcoding.elasticsearch.service.BaseElasticsearchService;
  245. import com.xkcoding.elasticsearch.service.PersonService;
  246. import org.elasticsearch.action.index.IndexRequest;
  247. import org.elasticsearch.action.search.SearchResponse;
  248. import org.elasticsearch.search.SearchHit;
  249. import org.springframework.stereotype.Service;
  250. import org.springframework.util.ObjectUtils;
  251. import java.io.IOException;
  252. import java.util.ArrayList;
  253. import java.util.Arrays;
  254. import java.util.List;
  255. import java.util.Map;
  256. /**
  257. * PersonServiceImpl
  258. *
  259. * @author fxbin
  260. * @version v1.0
  261. * @since 2019/9/15 23:08
  262. */
  263. @Service
  264. public class PersonServiceImpl extends BaseElasticsearchService implements PersonService {
  265. @Override
  266. public void createIndex(String index) {
  267. createIndexRequest(index);
  268. }
  269. @Override
  270. public void deleteIndex(String index) {
  271. deleteIndexRequest(index);
  272. }
  273. @Override
  274. public void insert(String index, List<Person> list) {
  275. try {
  276. list.forEach(person -> {
  277. IndexRequest request = buildIndexRequest(index, String.valueOf(person.getId()), person);
  278. try {
  279. client.index(request, COMMON_OPTIONS);
  280. } catch (IOException e) {
  281. e.printStackTrace();
  282. }
  283. });
  284. } finally {
  285. try {
  286. client.close();
  287. } catch (IOException e) {
  288. e.printStackTrace();
  289. }
  290. }
  291. }
  292. @Override
  293. public void update(String index, List<Person> list) {
  294. list.forEach(person -> {
  295. updateRequest(index, String.valueOf(person.getId()), person);
  296. });
  297. }
  298. @Override
  299. public void delete(String index, Person person) {
  300. if (ObjectUtils.isEmpty(person)) {
  301. // 如果person 对象为空,则删除全量
  302. searchList(index).forEach(p -> {
  303. deleteRequest(index, String.valueOf(p.getId()));
  304. });
  305. }
  306. deleteRequest(index, String.valueOf(person.getId()));
  307. }
  308. @Override
  309. public List<Person> searchList(String index) {
  310. SearchResponse searchResponse = search(index);
  311. SearchHit[] hits = searchResponse.getHits().getHits();
  312. List<Person> personList = new ArrayList<>();
  313. Arrays.stream(hits).forEach(hit -> {
  314. Map<String, Object> sourceAsMap = hit.getSourceAsMap();
  315. Person person = BeanUtil.mapToBean(sourceAsMap, Person.class, true);
  316. personList.add(person);
  317. });
  318. return personList;
  319. }
  320. }
  321. ```
  322. ## ElasticsearchApplicationTests.java
  323. > 主要功能测试,参见service 注释说明
  324. ```java
  325. package com.xkcoding.elasticsearch;
  326. import com.xkcoding.elasticsearch.contants.ElasticsearchConstant;
  327. import com.xkcoding.elasticsearch.model.Person;
  328. import com.xkcoding.elasticsearch.service.PersonService;
  329. import org.junit.Test;
  330. import org.junit.runner.RunWith;
  331. import org.springframework.boot.test.context.SpringBootTest;
  332. import org.springframework.test.context.junit4.SpringRunner;
  333. import javax.annotation.Resource;
  334. import java.util.ArrayList;
  335. import java.util.Date;
  336. import java.util.List;
  337. @RunWith(SpringRunner.class)
  338. @SpringBootTest
  339. public class ElasticsearchApplicationTests {
  340. @Resource
  341. private PersonService personService;
  342. @Test
  343. public void deleteIndexTest() {
  344. personService.deleteIndex(ElasticsearchConstant.INDEX_NAME);
  345. }
  346. @Test
  347. public void createIndexTest() {
  348. personService.createIndex(ElasticsearchConstant.INDEX_NAME);
  349. }
  350. @Test
  351. public void insertTest() {
  352. List<Person> list = new ArrayList<>();
  353. list.add(Person.builder().age(11).birthday(new Date()).country("CN").id(1L).name("哈哈").remark("test1").build());
  354. list.add(Person.builder().age(22).birthday(new Date()).country("US").id(2L).name("hiahia").remark("test2").build());
  355. list.add(Person.builder().age(33).birthday(new Date()).country("ID").id(3L).name("呵呵").remark("test3").build());
  356. personService.insert(ElasticsearchConstant.INDEX_NAME, list);
  357. }
  358. @Test
  359. public void updateTest() {
  360. Person person = Person.builder().age(33).birthday(new Date()).country("ID_update").id(3L).name("呵呵update").remark("test3_update").build();
  361. List<Person> list = new ArrayList<>();
  362. list.add(person);
  363. personService.update(ElasticsearchConstant.INDEX_NAME, list);
  364. }
  365. @Test
  366. public void deleteTest() {
  367. personService.delete(ElasticsearchConstant.INDEX_NAME, Person.builder().id(1L).build());
  368. }
  369. @Test
  370. public void searchListTest() {
  371. List<Person> personList = personService.searchList(ElasticsearchConstant.INDEX_NAME);
  372. System.out.println(personList);
  373. }
  374. }
  375. ```
  376. ## 参考
  377. 1. ElasticSearch 官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
  378. 2. Java High Level REST Client:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.3/java-rest-high.html

一个用来深度学习并实战 spring boot 的项目,目前总共包含 66 个集成demo,已经完成 55 个。