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 13 kB

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