1
1
# spring-boot-demo-elasticsearch-rest-high-level-client
2
2
3
- > 此 demo 主要演示了 Spring Boot 如何集成 ` elasticsearch-rest-high-level-client ` 完成对 ElasticSearch 的基本CURD 操作
3
+ > 此 demo 主要演示了 Spring Boot 如何集成 ` elasticsearch-rest-high-level-client ` 完成对 ` ElasticSearch 7.x ` 版本的基本 CURD 操作
4
4
5
- ## elasticsearch 升级
5
+ ## Elasticsearch 升级
6
6
7
7
先升级到 6.8,索引创建,设置 mapping 等操作加参数:include_type_name=true,然后滚动升级到 7,旧索引可以用 type 访问。具体可以参考:
8
8
@@ -12,17 +12,18 @@ https://www.elastic.co/guide/en/elasticsearch/reference/7.0/removal-of-types.htm
12
12
13
13
## 注意
14
14
15
- 作者编写本demo时,ElasticSearch版本为 ` 7.3.0 ` ,使用 docker 运行,下面是所有步骤:
15
+ 作者编写本 demo 时,ElasticSearch 版本为 ` 7.3.0 ` ,使用 docker 运行,下面是所有步骤:
16
16
17
- 1 . 下载镜像:` docker pull elasticsearch:7.3.0 `
17
+ 1.下载镜像:` docker pull elasticsearch:7.3.0 `
18
18
19
- 2 . 下载安装 ` docker-compose `
20
- ```
19
+ 2.下载安装 ` docker-compose ` ,参考文档: https://docs.docker.com/compose/install/
20
+
21
+ ``` bash
21
22
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
22
- 参考文档: https://docs.docker.com/compose/install/
23
23
```
24
24
25
- 2 . 编写docker-compose 文件
25
+ 3.编写docker-compose 文件
26
+
26
27
``` yaml
27
28
version : " 3"
28
29
@@ -47,8 +48,7 @@ services:
47
48
max-size : " 50m"
48
49
49
50
```
50
- 3 . 启动: ` docker-compose -f elasticsearch.yaml up -d `
51
-
51
+ 4.启动: ` docker-compose -f elasticsearch.yaml up -d `
52
52
53
53
## pom.xml
54
54
@@ -108,7 +108,6 @@ services:
108
108
<dependency >
109
109
<groupId >cn.hutool</groupId >
110
110
<artifactId >hutool-all</artifactId >
111
- <version >4.6.6</version >
112
111
</dependency >
113
112
114
113
<!-- elasticsearch -->
@@ -151,8 +150,17 @@ services:
151
150
152
151
</dependencies >
153
152
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 >
155
162
163
+ </project >
156
164
```
157
165
158
166
## Person.java
@@ -217,7 +225,6 @@ public class Person implements Serializable {
217
225
private String remark;
218
226
219
227
}
220
-
221
228
```
222
229
223
230
## PersonService.java
@@ -291,7 +298,6 @@ public interface PersonService {
291
298
List<Person > searchList (String index );
292
299
293
300
}
294
-
295
301
```
296
302
297
303
## PersonServiceImpl.java
@@ -303,7 +309,7 @@ package com.xkcoding.elasticsearch.service.impl;
303
309
304
310
import cn.hutool.core.bean.BeanUtil ;
305
311
import com.xkcoding.elasticsearch.model.Person ;
306
- import com.xkcoding.elasticsearch.service.BaseElasticsearchService ;
312
+ import com.xkcoding.elasticsearch.service.base. BaseElasticsearchService ;
307
313
import com.xkcoding.elasticsearch.service.PersonService ;
308
314
import org.elasticsearch.action.index.IndexRequest ;
309
315
import org.elasticsearch.action.search.SearchResponse ;
@@ -327,7 +333,6 @@ import java.util.Map;
327
333
@Service
328
334
public class PersonServiceImpl extends BaseElasticsearchService implements PersonService {
329
335
330
-
331
336
@Override
332
337
public void createIndex (String index ) {
333
338
createIndexRequest(index);
@@ -390,7 +395,6 @@ public class PersonServiceImpl extends BaseElasticsearchService implements Perso
390
395
return personList;
391
396
}
392
397
}
393
-
394
398
```
395
399
396
400
@@ -406,10 +410,10 @@ import com.xkcoding.elasticsearch.model.Person;
406
410
import com.xkcoding.elasticsearch.service.PersonService ;
407
411
import org.junit.Test ;
408
412
import org.junit.runner.RunWith ;
413
+ import org.springframework.beans.factory.annotation.Autowired ;
409
414
import org.springframework.boot.test.context.SpringBootTest ;
410
415
import org.springframework.test.context.junit4.SpringRunner ;
411
416
412
- import javax.annotation.Resource ;
413
417
import java.util.ArrayList ;
414
418
import java.util.Date ;
415
419
import java.util.List ;
@@ -418,22 +422,30 @@ import java.util.List;
418
422
@SpringBootTest
419
423
public class ElasticsearchApplicationTests {
420
424
421
- @Resource
425
+ @Autowired
422
426
private PersonService personService;
423
427
428
+ /**
429
+ * 测试删除索引
430
+ */
424
431
@Test
425
432
public void deleteIndexTest () {
426
433
personService. deleteIndex(ElasticsearchConstant . INDEX_NAME );
427
434
}
428
435
436
+ /**
437
+ * 测试创建索引
438
+ */
429
439
@Test
430
440
public void createIndexTest () {
431
441
personService. createIndex(ElasticsearchConstant . INDEX_NAME );
432
442
}
433
443
444
+ /**
445
+ * 测试新增
446
+ */
434
447
@Test
435
448
public void insertTest () {
436
-
437
449
List<Person > list = new ArrayList<> ();
438
450
list. add(Person . builder(). age(11 ). birthday(new Date ()). country(" CN" ). id(1L ). name(" 哈哈" ). remark(" test1" ). build());
439
451
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 {
442
454
personService. insert(ElasticsearchConstant . INDEX_NAME , list);
443
455
}
444
456
457
+ /**
458
+ * 测试更新
459
+ */
445
460
@Test
446
461
public void updateTest () {
447
462
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 {
450
465
personService. update(ElasticsearchConstant . INDEX_NAME , list);
451
466
}
452
467
468
+ /**
469
+ * 测试删除
470
+ */
453
471
@Test
454
472
public void deleteTest () {
455
473
personService. delete(ElasticsearchConstant . INDEX_NAME , Person . builder(). id(1L ). build());
456
474
}
457
475
476
+ /**
477
+ * 测试查询
478
+ */
458
479
@Test
459
480
public void searchListTest () {
460
481
List<Person > personList = personService. searchList(ElasticsearchConstant . INDEX_NAME );
461
482
System . out. println(personList);
462
483
}
463
484
464
-
465
485
}
466
-
467
486
```
468
487
469
488
## 参考
470
489
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
473
493
0 commit comments