Skip to content

Commit e67a772

Browse files
committed
✨ spring-boot-demo-elasticsearch-rest-high-level-client 完成
1 parent 7d10952 commit e67a772

File tree

13 files changed

+117
-197
lines changed

13 files changed

+117
-197
lines changed

spring-boot-demo-elasticsearch-rest-high-level-client/README.md

+44-24
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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
2122
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/
2323
```
2424

25-
2. 编写docker-compose 文件
25+
3.编写docker-compose 文件
26+
2627
```yaml
2728
version: "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

304310
import cn.hutool.core.bean.BeanUtil;
305311
import com.xkcoding.elasticsearch.model.Person;
306-
import com.xkcoding.elasticsearch.service.BaseElasticsearchService;
312+
import com.xkcoding.elasticsearch.service.base.BaseElasticsearchService;
307313
import com.xkcoding.elasticsearch.service.PersonService;
308314
import org.elasticsearch.action.index.IndexRequest;
309315
import org.elasticsearch.action.search.SearchResponse;
@@ -327,7 +333,6 @@ import java.util.Map;
327333
@Service
328334
public 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;
406410
import com.xkcoding.elasticsearch.service.PersonService;
407411
import org.junit.Test;
408412
import org.junit.runner.RunWith;
413+
import org.springframework.beans.factory.annotation.Autowired;
409414
import org.springframework.boot.test.context.SpringBootTest;
410415
import org.springframework.test.context.junit4.SpringRunner;
411416

412-
import javax.annotation.Resource;
413417
import java.util.ArrayList;
414418
import java.util.Date;
415419
import java.util.List;
@@ -418,22 +422,30 @@ import java.util.List;
418422
@SpringBootTest
419423
public 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

spring-boot-demo-elasticsearch-rest-high-level-client/pom.xml

+10-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
<dependency>
5454
<groupId>cn.hutool</groupId>
5555
<artifactId>hutool-all</artifactId>
56-
<version>4.6.6</version>
5756
</dependency>
5857

5958
<!-- elasticsearch -->
@@ -96,4 +95,14 @@
9695

9796
</dependencies>
9897

98+
<build>
99+
<finalName>spring-boot-demo-elasticsearch-rest-high-level-client</finalName>
100+
<plugins>
101+
<plugin>
102+
<groupId>org.springframework.boot</groupId>
103+
<artifactId>spring-boot-maven-plugin</artifactId>
104+
</plugin>
105+
</plugins>
106+
</build>
107+
99108
</project>
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.xkcoding.elasticsearch.model;
1+
package com.xkcoding.elasticsearch.common;
22

33
import lombok.Data;
44
import org.springframework.lang.Nullable;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.xkcoding.elasticsearch.model;
1+
package com.xkcoding.elasticsearch.common;
22

33
import lombok.AllArgsConstructor;
44
import lombok.Getter;
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package com.xkcoding.elasticsearch.autoconfigure;
1+
package com.xkcoding.elasticsearch.config;
22

3+
import lombok.RequiredArgsConstructor;
34
import org.apache.http.HttpHost;
45
import org.apache.http.auth.AuthScope;
56
import org.apache.http.auth.UsernamePasswordCredentials;
@@ -8,14 +9,14 @@
89
import org.elasticsearch.client.RestClient;
910
import org.elasticsearch.client.RestClientBuilder;
1011
import org.elasticsearch.client.RestHighLevelClient;
12+
import org.springframework.beans.factory.annotation.Autowired;
1113
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
1214
import org.springframework.boot.context.properties.EnableConfigurationProperties;
1315
import org.springframework.context.annotation.Bean;
16+
import org.springframework.context.annotation.Configuration;
1417
import org.springframework.util.Assert;
1518
import org.springframework.util.StringUtils;
1619

17-
import javax.annotation.Resource;
18-
import javax.validation.constraints.NotNull;
1920
import java.util.ArrayList;
2021
import java.util.List;
2122

@@ -26,13 +27,12 @@
2627
* @version v1.0
2728
* @since 2019/9/15 22:59
2829
*/
30+
@Configuration
31+
@RequiredArgsConstructor(onConstructor_ = @Autowired)
2932
@EnableConfigurationProperties(ElasticsearchProperties.class)
3033
public class ElasticsearchAutoConfiguration {
3134

32-
@SuppressWarnings("NullableProblems")
33-
@NotNull
34-
@Resource
35-
private ElasticsearchProperties elasticsearchProperties;
35+
private final ElasticsearchProperties elasticsearchProperties;
3636

3737
private List<HttpHost> httpHosts = new ArrayList<>();
3838

@@ -48,8 +48,7 @@ public RestHighLevelClient restHighLevelClient() {
4848
Assert.state(parts.length == 2, "Must be defined as 'host:port'");
4949
httpHosts.add(new HttpHost(parts[0], Integer.parseInt(parts[1]), elasticsearchProperties.getSchema()));
5050
} catch (Exception e) {
51-
throw new IllegalStateException(
52-
"Invalid ES nodes " + "property '" + node + "'", e);
51+
throw new IllegalStateException("Invalid ES nodes " + "property '" + node + "'", e);
5352
}
5453
});
5554
RestClientBuilder builder = RestClient.builder(httpHosts.toArray(new HttpHost[0]));
@@ -61,10 +60,10 @@ public RestHighLevelClient restHighLevelClient() {
6160
/**
6261
* get restHistLevelClient
6362
*
64-
* @author fxbin
65-
* @param builder RestClientBuilder
63+
* @param builder RestClientBuilder
6664
* @param elasticsearchProperties elasticsearch default properties
6765
* @return {@link org.elasticsearch.client.RestHighLevelClient}
66+
* @author fxbin
6867
*/
6968
private static RestHighLevelClient getRestHighLevelClient(RestClientBuilder builder, ElasticsearchProperties elasticsearchProperties) {
7069

@@ -88,11 +87,9 @@ private static RestHighLevelClient getRestHighLevelClient(RestClientBuilder buil
8887
if (!StringUtils.isEmpty(account.getUsername()) && !StringUtils.isEmpty(account.getUsername())) {
8988
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
9089

91-
credentialsProvider.setCredentials(AuthScope.ANY,
92-
new UsernamePasswordCredentials(account.getUsername(), account.getPassword()));
90+
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(account.getUsername(), account.getPassword()));
9391
}
9492
return new RestHighLevelClient(builder);
9593
}
9694

97-
9895
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.xkcoding.elasticsearch.autoconfigure;
1+
package com.xkcoding.elasticsearch.config;
22

33
import lombok.AllArgsConstructor;
44
import lombok.Builder;
@@ -77,6 +77,9 @@ public class ElasticsearchProperties {
7777
*/
7878
private Account account = new Account();
7979

80+
/**
81+
* 索引配置信息
82+
*/
8083
@Data
8184
public static class Index {
8285

@@ -92,6 +95,9 @@ public static class Index {
9295

9396
}
9497

98+
/**
99+
* 认证账户
100+
*/
95101
@Data
96102
public static class Account {
97103

spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/exception/ElasticsearchException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.xkcoding.elasticsearch.exception;
22

3-
import com.xkcoding.elasticsearch.model.ResultCode;
3+
import com.xkcoding.elasticsearch.common.ResultCode;
44
import lombok.Getter;
55

66
/**

0 commit comments

Comments
 (0)