11# spring-boot-demo-elasticsearch-rest-high-level-client
22
3- > 此 demo 主要演示了 Spring Boot 如何集成 ` elasticsearch-rest-high-level-client ` 完成对 ElasticSearch 的基本CURD 操作
3+ > 此 demo 主要演示了 Spring Boot 如何集成 ` elasticsearch-rest-high-level-client ` 完成对 ` ElasticSearch 7.x ` 版本的基本 CURD 操作
44
5- ## elasticsearch 升级
5+ ## Elasticsearch 升级
66
77先升级到 6.8,索引创建,设置 mapping 等操作加参数:include_type_name=true,然后滚动升级到 7,旧索引可以用 type 访问。具体可以参考:
88
@@ -12,17 +12,18 @@ https://www.elastic.co/guide/en/elasticsearch/reference/7.0/removal-of-types.htm
1212
1313## 注意
1414
15- 作者编写本demo时,ElasticSearch版本为 ` 7.3.0 ` ,使用 docker 运行,下面是所有步骤:
15+ 作者编写本 demo 时,ElasticSearch 版本为 ` 7.3.0 ` ,使用 docker 运行,下面是所有步骤:
1616
17- 1 . 下载镜像:` docker pull elasticsearch:7.3.0 `
17+ 1.下载镜像:` docker pull elasticsearch:7.3.0 `
1818
19- 2 . 下载安装 ` docker-compose `
20- ```
19+ 2.下载安装 ` docker-compose ` ,参考文档: https://docs.docker.com/compose/install/
20+
21+ ``` bash
2122sudo curl -L " https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$( uname -s) -$( uname -m) " -o /usr/local/bin/docker-compose
22- 参考文档: https://docs.docker.com/compose/install/
2323```
2424
25- 2 . 编写docker-compose 文件
25+ 3.编写docker-compose 文件
26+
2627``` yaml
2728version : " 3"
2829
@@ -47,8 +48,7 @@ services:
4748 max-size : " 50m"
4849
4950```
50- 3 . 启动: ` docker-compose -f elasticsearch.yaml up -d `
51-
51+ 4.启动: ` docker-compose -f elasticsearch.yaml up -d `
5252
5353## pom.xml
5454
@@ -108,7 +108,6 @@ services:
108108 <dependency >
109109 <groupId >cn.hutool</groupId >
110110 <artifactId >hutool-all</artifactId >
111- <version >4.6.6</version >
112111 </dependency >
113112
114113 <!-- elasticsearch -->
@@ -151,8 +150,17 @@ services:
151150
152151 </dependencies >
153152
154- </project >
153+ <build >
154+ <finalName >spring-boot-demo-elasticsearch-rest-high-level-client</finalName >
155+ <plugins >
156+ <plugin >
157+ <groupId >org.springframework.boot</groupId >
158+ <artifactId >spring-boot-maven-plugin</artifactId >
159+ </plugin >
160+ </plugins >
161+ </build >
155162
163+ </project >
156164```
157165
158166## Person.java
@@ -217,7 +225,6 @@ public class Person implements Serializable {
217225 private String remark;
218226
219227}
220-
221228```
222229
223230## PersonService.java
@@ -291,7 +298,6 @@ public interface PersonService {
291298 List<Person > searchList (String index );
292299
293300}
294-
295301```
296302
297303## PersonServiceImpl.java
@@ -303,7 +309,7 @@ package com.xkcoding.elasticsearch.service.impl;
303309
304310import cn.hutool.core.bean.BeanUtil ;
305311import com.xkcoding.elasticsearch.model.Person ;
306- import com.xkcoding.elasticsearch.service.BaseElasticsearchService ;
312+ import com.xkcoding.elasticsearch.service.base. BaseElasticsearchService ;
307313import com.xkcoding.elasticsearch.service.PersonService ;
308314import org.elasticsearch.action.index.IndexRequest ;
309315import org.elasticsearch.action.search.SearchResponse ;
@@ -327,7 +333,6 @@ import java.util.Map;
327333@Service
328334public class PersonServiceImpl extends BaseElasticsearchService implements PersonService {
329335
330-
331336 @Override
332337 public void createIndex (String index ) {
333338 createIndexRequest(index);
@@ -390,7 +395,6 @@ public class PersonServiceImpl extends BaseElasticsearchService implements Perso
390395 return personList;
391396 }
392397}
393-
394398```
395399
396400
@@ -406,10 +410,10 @@ import com.xkcoding.elasticsearch.model.Person;
406410import com.xkcoding.elasticsearch.service.PersonService ;
407411import org.junit.Test ;
408412import org.junit.runner.RunWith ;
413+ import org.springframework.beans.factory.annotation.Autowired ;
409414import org.springframework.boot.test.context.SpringBootTest ;
410415import org.springframework.test.context.junit4.SpringRunner ;
411416
412- import javax.annotation.Resource ;
413417import java.util.ArrayList ;
414418import java.util.Date ;
415419import java.util.List ;
@@ -418,22 +422,30 @@ import java.util.List;
418422@SpringBootTest
419423public class ElasticsearchApplicationTests {
420424
421- @Resource
425+ @Autowired
422426 private PersonService personService;
423427
428+ /**
429+ * 测试删除索引
430+ */
424431 @Test
425432 public void deleteIndexTest () {
426433 personService. deleteIndex(ElasticsearchConstant . INDEX_NAME );
427434 }
428435
436+ /**
437+ * 测试创建索引
438+ */
429439 @Test
430440 public void createIndexTest () {
431441 personService. createIndex(ElasticsearchConstant . INDEX_NAME );
432442 }
433443
444+ /**
445+ * 测试新增
446+ */
434447 @Test
435448 public void insertTest () {
436-
437449 List<Person > list = new ArrayList<> ();
438450 list. add(Person . builder(). age(11 ). birthday(new Date ()). country(" CN" ). id(1L ). name(" 哈哈" ). remark(" test1" ). build());
439451 list. add(Person . builder(). age(22 ). birthday(new Date ()). country(" US" ). id(2L ). name(" hiahia" ). remark(" test2" ). build());
@@ -442,6 +454,9 @@ public class ElasticsearchApplicationTests {
442454 personService. insert(ElasticsearchConstant . INDEX_NAME , list);
443455 }
444456
457+ /**
458+ * 测试更新
459+ */
445460 @Test
446461 public void updateTest () {
447462 Person person = Person . builder(). age(33 ). birthday(new Date ()). country(" ID_update" ). id(3L ). name(" 呵呵update" ). remark(" test3_update" ). build();
@@ -450,24 +465,29 @@ public class ElasticsearchApplicationTests {
450465 personService. update(ElasticsearchConstant . INDEX_NAME , list);
451466 }
452467
468+ /**
469+ * 测试删除
470+ */
453471 @Test
454472 public void deleteTest () {
455473 personService. delete(ElasticsearchConstant . INDEX_NAME , Person . builder(). id(1L ). build());
456474 }
457475
476+ /**
477+ * 测试查询
478+ */
458479 @Test
459480 public void searchListTest () {
460481 List<Person > personList = personService. searchList(ElasticsearchConstant . INDEX_NAME );
461482 System . out. println(personList);
462483 }
463484
464-
465485}
466-
467486```
468487
469488## 参考
470489
471- 1 . ElasticSearch 官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
472- 2 . Java High Level REST Client:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.3/java-rest-high.html
490+ - ElasticSearch 官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
491+
492+ - Java High Level REST Client:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.3/java-rest-high.html
473493
0 commit comments