From ea00d4ce20e7d6ebb220bafa891488b633599899 Mon Sep 17 00:00:00 2001 From: fxbin Date: Tue, 17 Sep 2019 14:46:54 +0800 Subject: [PATCH] =?UTF-8?q?Elasticsearcch=207.X=20elasticsearch-rest-high-?= =?UTF-8?q?level-client=20=E5=9F=BA=E6=9C=ACCURD=20=E6=93=8D=E4=BD=9Cdemo?= =?UTF-8?q?=20=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.gitignore | 25 + .../README.md | 473 ++++++++++++++++++ .../pom.xml | 22 +- .../ElasticsearchApplication.java | 2 +- .../ElasticsearchAutoConfiguration.java | 15 +- .../ElasticsearchProperties.java | 34 +- .../contants/DataTypeTransfer.java | 2 +- .../contants/ElasticsearchConstant.java | 2 +- .../exception/ElasticsearchException.java | 4 +- .../elasticsearch/model/Person.java | 2 +- .../elasticsearch/model/Result.java | 2 +- .../elasticsearch/model/ResultCode.java | 2 +- .../service/BaseElasticsearchService.java | 170 +++++++ .../elasticsearch/service/PersonService.java | 68 +++ .../service/impl/PersonServiceImpl.java | 91 ++++ .../elasticsearch/util/BeanUtils.java | 41 +- .../elasticsearch/util/MapUtils.java | 2 +- .../service/BaseElasticsearchService.java | 88 ---- .../elasticsearch/service/PersonService.java | 11 - .../service/impl/PersonServiceImpl.java | 14 - .../main/resources/META-INF/spring.factories | 3 + .../src/main/resources/application.yml | 6 + .../ElasticsearchApplicationTests.java | 17 - .../ElasticsearchApplicationTests.java | 64 +++ 24 files changed, 995 insertions(+), 165 deletions(-) create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/.gitignore create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/README.md rename spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/{xlcoding => xkcoding}/elasticsearch/ElasticsearchApplication.java (91%) rename spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/{xlcoding => xkcoding}/elasticsearch/autoconfigure/ElasticsearchAutoConfiguration.java (81%) rename spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/{xlcoding => xkcoding}/elasticsearch/autoconfigure/ElasticsearchProperties.java (74%) rename spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/{xlcoding => xkcoding}/elasticsearch/contants/DataTypeTransfer.java (73%) rename spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/{xlcoding => xkcoding}/elasticsearch/contants/ElasticsearchConstant.java (82%) rename spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/{xlcoding => xkcoding}/elasticsearch/exception/ElasticsearchException.java (91%) rename spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/{xlcoding => xkcoding}/elasticsearch/model/Person.java (94%) rename spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/{xlcoding => xkcoding}/elasticsearch/model/Result.java (97%) rename spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/{xlcoding => xkcoding}/elasticsearch/model/ResultCode.java (91%) create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/service/BaseElasticsearchService.java create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/service/PersonService.java create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/service/impl/PersonServiceImpl.java rename spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/{xlcoding => xkcoding}/elasticsearch/util/BeanUtils.java (55%) rename spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/{xlcoding => xkcoding}/elasticsearch/util/MapUtils.java (98%) delete mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/BaseElasticsearchService.java delete mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/PersonService.java delete mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/impl/PersonServiceImpl.java create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/main/resources/META-INF/spring.factories delete mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/test/java/com/xdcoding/elasticsearch/ElasticsearchApplicationTests.java create mode 100644 spring-boot-demo-elasticsearch-rest-high-level-client/src/test/java/com/xkcoding/elasticsearch/ElasticsearchApplicationTests.java diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/.gitignore b/spring-boot-demo-elasticsearch-rest-high-level-client/.gitignore new file mode 100644 index 0000000..82eca33 --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/.gitignore @@ -0,0 +1,25 @@ +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ \ No newline at end of file diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/README.md b/spring-boot-demo-elasticsearch-rest-high-level-client/README.md new file mode 100644 index 0000000..f8dd2a4 --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/README.md @@ -0,0 +1,473 @@ +# spring-boot-demo-elasticsearch-rest-high-level-client + +> 此 demo 主要演示了 Spring Boot 如何集成 `elasticsearch-rest-high-level-client` 完成对 ElasticSearch 的基本CURD 操作 + +## elasticsearch 升级 + +先升级到 6.8,索引创建,设置 mapping 等操作加参数:include_type_name=true,然后滚动升级到 7,旧索引可以用 type 访问。具体可以参考: + +https://www.elastic.co/cn/blog/moving-from-types-to-typeless-apis-in-elasticsearch-7-0 + +https://www.elastic.co/guide/en/elasticsearch/reference/7.0/removal-of-types.html + +## 注意 + +作者编写本demo时,ElasticSearch版本为 `7.3.0`,使用 docker 运行,下面是所有步骤: + +1. 下载镜像:`docker pull elasticsearch:7.3.0` + +2. 下载安装 `docker-compose` +``` +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 +参考文档: https://docs.docker.com/compose/install/ +``` + +2. 编写docker-compose 文件 +```yaml +version: "3" + +services: + es7: + hostname: es7 + container_name: es7 + image: elasticsearch:7.3.0 + volumes: + - "/data/es7/logs:/usr/share/es7/logs:rw" + - "/data/es7/data:/usr/share/es7/data:rw" + restart: on-failure + ports: + - "9200:9200" + - "9300:9300" + environment: + cluster.name: elasticsearch + discovery.type: single-node + logging: + driver: "json-file" + options: + max-size: "50m" + +``` +3. 启动: `docker-compose -f elasticsearch.yaml up -d` + + +## pom.xml + +```xml + + + 4.0.0 + + spring-boot-demo + com.xkcoding + 1.0.0-SNAPSHOT + + + spring-boot-demo-elasticsearch-rest-high-level-client + spring-boot-demo-elasticsearch-rest-high-level-client + Demo project for Spring Boot + + + UTF-8 + UTF-8 + 1.8 + + + + + + org.springframework.boot + spring-boot-starter + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + org.hibernate.validator + hibernate-validator + compile + + + + + org.springframework.boot + spring-boot-configuration-processor + + + + + cn.hutool + hutool-all + 4.6.6 + + + + + org.elasticsearch + elasticsearch + 7.3.0 + + + + + org.elasticsearch.client + elasticsearch-rest-client + 7.3.0 + + + + + org.elasticsearch.client + elasticsearch-rest-high-level-client + 7.3.0 + + + org.elasticsearch.client + elasticsearch-rest-client + + + org.elasticsearch + elasticsearch + + + + + + + org.projectlombok + lombok + true + + + + + + +``` + +## Person.java + +> 实体类 +> + +```java +package com.xkcoding.elasticsearch.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.util.Date; + +/** + * Person + * + * @author fxbin + * @version v1.0 + * @since 2019/9/15 23:04 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class Person implements Serializable { + + private static final long serialVersionUID = 8510634155374943623L; + + /** + * 主键 + */ + private Long id; + + /** + * 名字 + */ + private String name; + + /** + * 国家 + */ + private String country; + + /** + * 年龄 + */ + private Integer age; + + /** + * 生日 + */ + private Date birthday; + + /** + * 介绍 + */ + private String remark; + +} + +``` + +## PersonService.java + +```java +package com.xkcoding.elasticsearch.service; + +import com.xkcoding.elasticsearch.model.Person; +import org.springframework.lang.Nullable; + +import java.util.List; + +/** + * PersonService + * + * @author fxbin + * @version v1.0 + * @since 2019/9/15 23:07 + */ +public interface PersonService { + + /** + * create Index + * + * @author fxbin + * @param index elasticsearch index name + */ + void createIndex(String index); + + /** + * delete Index + * + * @author fxbin + * @param index elasticsearch index name + */ + void deleteIndex(String index); + + /** + * insert document source + * + * @author fxbin + * @param index elasticsearch index name + * @param list data source + */ + void insert(String index, List list); + + /** + * update document source + * + * @author fxbin + * @param index elasticsearch index name + * @param list data source + */ + void update(String index, List list); + + /** + * delete document source + * + * @author fxbin + * @param person delete data source and allow null object + */ + void delete(String index, @Nullable Person person); + + /** + * search all doc records + * + * @author fxbin + * @param index elasticsearch index name + * @return person list + */ + List searchList(String index); + +} + +``` + +## PersonServiceImpl.java + +> service 实现类型,基本CURD操作 + +```java +package com.xkcoding.elasticsearch.service.impl; + +import cn.hutool.core.bean.BeanUtil; +import com.xkcoding.elasticsearch.model.Person; +import com.xkcoding.elasticsearch.service.BaseElasticsearchService; +import com.xkcoding.elasticsearch.service.PersonService; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.search.SearchHit; +import org.springframework.stereotype.Service; +import org.springframework.util.ObjectUtils; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +/** + * PersonServiceImpl + * + * @author fxbin + * @version v1.0 + * @since 2019/9/15 23:08 + */ +@Service +public class PersonServiceImpl extends BaseElasticsearchService implements PersonService { + + + @Override + public void createIndex(String index) { + createIndexRequest(index); + } + + @Override + public void deleteIndex(String index) { + deleteIndexRequest(index); + } + + @Override + public void insert(String index, List list) { + + try { + list.forEach(person -> { + IndexRequest request = buildIndexRequest(index, String.valueOf(person.getId()), person); + try { + client.index(request, COMMON_OPTIONS); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } finally { + try { + client.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + @Override + public void update(String index, List list) { + list.forEach(person -> { + updateRequest(index, String.valueOf(person.getId()), person); + }); + } + + @Override + public void delete(String index, Person person) { + if (ObjectUtils.isEmpty(person)) { + // 如果person 对象为空,则删除全量 + searchList(index).forEach(p -> { + deleteRequest(index, String.valueOf(p.getId())); + }); + } + deleteRequest(index, String.valueOf(person.getId())); + } + + @Override + public List searchList(String index) { + SearchResponse searchResponse = search(index); + SearchHit[] hits = searchResponse.getHits().getHits(); + List personList = new ArrayList<>(); + Arrays.stream(hits).forEach(hit -> { + Map sourceAsMap = hit.getSourceAsMap(); + Person person = BeanUtil.mapToBean(sourceAsMap, Person.class, true); + personList.add(person); + }); + return personList; + } +} + +``` + + +## ElasticsearchApplicationTests.java + +> 主要功能测试,参见service 注释说明 + +```java +package com.xkcoding.elasticsearch; + +import com.xkcoding.elasticsearch.contants.ElasticsearchConstant; +import com.xkcoding.elasticsearch.model.Person; +import com.xkcoding.elasticsearch.service.PersonService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ElasticsearchApplicationTests { + + @Resource + private PersonService personService; + + @Test + public void deleteIndexTest() { + personService.deleteIndex(ElasticsearchConstant.INDEX_NAME); + } + + @Test + public void createIndexTest() { + personService.createIndex(ElasticsearchConstant.INDEX_NAME); + } + + @Test + public void insertTest() { + + List list = new ArrayList<>(); + list.add(Person.builder().age(11).birthday(new Date()).country("CN").id(1L).name("哈哈").remark("test1").build()); + list.add(Person.builder().age(22).birthday(new Date()).country("US").id(2L).name("hiahia").remark("test2").build()); + list.add(Person.builder().age(33).birthday(new Date()).country("ID").id(3L).name("呵呵").remark("test3").build()); + + personService.insert(ElasticsearchConstant.INDEX_NAME, list); + } + + @Test + public void updateTest() { + Person person = Person.builder().age(33).birthday(new Date()).country("ID_update").id(3L).name("呵呵update").remark("test3_update").build(); + List list = new ArrayList<>(); + list.add(person); + personService.update(ElasticsearchConstant.INDEX_NAME, list); + } + + @Test + public void deleteTest() { + personService.delete(ElasticsearchConstant.INDEX_NAME, Person.builder().id(1L).build()); + } + + @Test + public void searchListTest() { + List personList = personService.searchList(ElasticsearchConstant.INDEX_NAME); + System.out.println(personList); + } + + +} + +``` + +## 参考 + +1. ElasticSearch 官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html +2. Java High Level REST Client:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.3/java-rest-high.html + diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/pom.xml b/spring-boot-demo-elasticsearch-rest-high-level-client/pom.xml index 8b19069..7a4a9d4 100644 --- a/spring-boot-demo-elasticsearch-rest-high-level-client/pom.xml +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/pom.xml @@ -49,6 +49,13 @@ spring-boot-configuration-processor + + + cn.hutool + hutool-all + 4.6.6 + + org.elasticsearch @@ -56,16 +63,23 @@ 7.3.0 + + + org.elasticsearch.client + elasticsearch-rest-client + 7.3.0 + + org.elasticsearch.client elasticsearch-rest-high-level-client 7.3.0 - - - - + + org.elasticsearch.client + elasticsearch-rest-client + org.elasticsearch elasticsearch diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/ElasticsearchApplication.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/ElasticsearchApplication.java similarity index 91% rename from spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/ElasticsearchApplication.java rename to spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/ElasticsearchApplication.java index 11ba5b3..2b860f4 100644 --- a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/ElasticsearchApplication.java +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/ElasticsearchApplication.java @@ -1,4 +1,4 @@ -package com.xlcoding.elasticsearch; +package com.xkcoding.elasticsearch; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/autoconfigure/ElasticsearchAutoConfiguration.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/autoconfigure/ElasticsearchAutoConfiguration.java similarity index 81% rename from spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/autoconfigure/ElasticsearchAutoConfiguration.java rename to spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/autoconfigure/ElasticsearchAutoConfiguration.java index 4952524..a64349d 100644 --- a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/autoconfigure/ElasticsearchAutoConfiguration.java +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/autoconfigure/ElasticsearchAutoConfiguration.java @@ -1,6 +1,10 @@ -package com.xlcoding.elasticsearch.autoconfigure; +package com.xkcoding.elasticsearch.autoconfigure; import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.impl.client.BasicCredentialsProvider; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; @@ -78,6 +82,15 @@ public class ElasticsearchAutoConfiguration { httpClientBuilder.setMaxConnPerRoute(elasticsearchProperties.getMaxConnectPerRoute()); return httpClientBuilder; }); + + // Callback used the basic credential auth + ElasticsearchProperties.Account account = elasticsearchProperties.getAccount(); + if (!StringUtils.isEmpty(account.getUsername()) && !StringUtils.isEmpty(account.getUsername())) { + final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + + credentialsProvider.setCredentials(AuthScope.ANY, + new UsernamePasswordCredentials(account.getUsername(), account.getPassword())); + } return new RestHighLevelClient(builder); } diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/autoconfigure/ElasticsearchProperties.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/autoconfigure/ElasticsearchProperties.java similarity index 74% rename from spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/autoconfigure/ElasticsearchProperties.java rename to spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/autoconfigure/ElasticsearchProperties.java index e1ba233..8a04b3b 100644 --- a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/autoconfigure/ElasticsearchProperties.java +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/autoconfigure/ElasticsearchProperties.java @@ -1,9 +1,6 @@ -package com.xlcoding.elasticsearch.autoconfigure; +package com.xkcoding.elasticsearch.autoconfigure; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.*; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @@ -70,21 +67,40 @@ public class ElasticsearchProperties { /** * 索引配置信息 */ - private Index index; + private Index index = new Index(); + + /** + * 认证账户 + */ + private Account account = new Account(); @Data - @Builder public static class Index { /** * 分片数量 */ - protected Integer numberOfShards = 3; + private Integer numberOfShards = 3; /** * 副本数量 */ - protected Integer numberOfReplicas = 2; + private Integer numberOfReplicas = 2; + + } + + @Data + public static class Account { + + /** + * 认证用户 + */ + private String username; + + /** + * 认证密码 + */ + private String password; } diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/contants/DataTypeTransfer.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/contants/DataTypeTransfer.java similarity index 73% rename from spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/contants/DataTypeTransfer.java rename to spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/contants/DataTypeTransfer.java index 2453032..7ff2e11 100644 --- a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/contants/DataTypeTransfer.java +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/contants/DataTypeTransfer.java @@ -1,4 +1,4 @@ -package com.xlcoding.elasticsearch.contants; +package com.xkcoding.elasticsearch.contants; /** * DataTypeTransfer diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/contants/ElasticsearchConstant.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/contants/ElasticsearchConstant.java similarity index 82% rename from spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/contants/ElasticsearchConstant.java rename to spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/contants/ElasticsearchConstant.java index 5d1b349..4a989e3 100644 --- a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/contants/ElasticsearchConstant.java +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/contants/ElasticsearchConstant.java @@ -1,4 +1,4 @@ -package com.xlcoding.elasticsearch.contants; +package com.xkcoding.elasticsearch.contants; /** * ElasticsearchConstant diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/exception/ElasticsearchException.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/exception/ElasticsearchException.java similarity index 91% rename from spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/exception/ElasticsearchException.java rename to spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/exception/ElasticsearchException.java index 97c4ce6..621e0bf 100644 --- a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/exception/ElasticsearchException.java +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/exception/ElasticsearchException.java @@ -1,6 +1,6 @@ -package com.xlcoding.elasticsearch.exception; +package com.xkcoding.elasticsearch.exception; -import com.xlcoding.elasticsearch.model.ResultCode; +import com.xkcoding.elasticsearch.model.ResultCode; import lombok.Getter; /** diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/Person.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/model/Person.java similarity index 94% rename from spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/Person.java rename to spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/model/Person.java index dbb79bc..d6c59cb 100644 --- a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/Person.java +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/model/Person.java @@ -1,4 +1,4 @@ -package com.xlcoding.elasticsearch.model; +package com.xkcoding.elasticsearch.model; import lombok.AllArgsConstructor; import lombok.Builder; diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/Result.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/model/Result.java similarity index 97% rename from spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/Result.java rename to spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/model/Result.java index 4737d77..730b774 100644 --- a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/Result.java +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/model/Result.java @@ -1,4 +1,4 @@ -package com.xlcoding.elasticsearch.model; +package com.xkcoding.elasticsearch.model; import lombok.Data; import org.springframework.lang.Nullable; diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/ResultCode.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/model/ResultCode.java similarity index 91% rename from spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/ResultCode.java rename to spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/model/ResultCode.java index 0d03013..7958241 100644 --- a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/model/ResultCode.java +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/model/ResultCode.java @@ -1,4 +1,4 @@ -package com.xlcoding.elasticsearch.model; +package com.xkcoding.elasticsearch.model; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/service/BaseElasticsearchService.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/service/BaseElasticsearchService.java new file mode 100644 index 0000000..1e64f01 --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/service/BaseElasticsearchService.java @@ -0,0 +1,170 @@ +package com.xkcoding.elasticsearch.service; + +import com.xkcoding.elasticsearch.exception.ElasticsearchException; +import com.xkcoding.elasticsearch.autoconfigure.ElasticsearchProperties; +import com.xkcoding.elasticsearch.util.BeanUtils; +import lombok.extern.slf4j.Slf4j; +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; +import org.elasticsearch.action.delete.DeleteRequest; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.update.UpdateRequest; +import org.elasticsearch.action.update.UpdateResponse; +import org.elasticsearch.client.HttpAsyncResponseConsumerFactory; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.client.indices.CreateIndexRequest; +import org.elasticsearch.client.indices.CreateIndexResponse; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.index.engine.Engine; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.search.builder.SearchSourceBuilder; + +import javax.annotation.Resource; +import java.io.IOException; + +/** + * BaseElasticsearchService + * + * @author fxbin + * @version 1.0v + * @since 2019/9/16 15:44 + */ +@Slf4j +public abstract class BaseElasticsearchService { + + @Resource + protected RestHighLevelClient client; + + @Resource + private ElasticsearchProperties elasticsearchProperties; + + protected static final RequestOptions COMMON_OPTIONS; + + static { + RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); + + // 默认缓冲限制为100MB,此处修改为30MB。 + builder.setHttpAsyncResponseConsumerFactory( + new HttpAsyncResponseConsumerFactory + .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024)); + COMMON_OPTIONS = builder.build(); + } + + /** + * create elasticsearch index (asyc) + * + * @author fxbin + * @param index elasticsearch index + */ + protected void createIndexRequest(String index) { + try { + CreateIndexRequest request = new CreateIndexRequest(index); + // Settings for this index + request.settings(Settings.builder() + .put("index.number_of_shards", elasticsearchProperties.getIndex().getNumberOfShards()) + .put("index.number_of_replicas", elasticsearchProperties.getIndex().getNumberOfReplicas())); + + CreateIndexResponse createIndexResponse = client.indices().create(request, COMMON_OPTIONS); + + log.info(" whether all of the nodes have acknowledged the request : {}", createIndexResponse.isAcknowledged()); + log.info(" Indicates whether the requisite number of shard copies were started for each shard in the index before timing out :{}", + createIndexResponse.isShardsAcknowledged()); + } catch (IOException e) { + throw new ElasticsearchException("创建索引 {" + index + "} 失败"); + } + } + + /** + * delete elasticsearch index + * + * @author fxbin + * @param index elasticsearch index name + */ + protected void deleteIndexRequest(String index) { + DeleteIndexRequest deleteIndexRequest = buildDeleteIndexRequest(index); + try { + client.indices().delete(deleteIndexRequest, COMMON_OPTIONS); + } catch (IOException e) { + throw new ElasticsearchException("删除索引 {" + index + "} 失败"); + } + } + + /** + * build DeleteIndexRequest + * + * @author fxbin + * @param index elasticsearch index name + */ + private static DeleteIndexRequest buildDeleteIndexRequest (String index) { + return new DeleteIndexRequest(index); + } + + /** + * build IndexRequest + * + * @author fxbin + * @param index elasticsearch index name + * @param id request object id + * @param object request object + * @return {@link org.elasticsearch.action.index.IndexRequest} + */ + protected static IndexRequest buildIndexRequest(String index, String id, Object object) { + return new IndexRequest(index).id(id).source(BeanUtils.toMap(object), XContentType.JSON); + } + + /** + * exec updateRequest + * + * @author fxbin + * @param index elasticsearch index name + * @param id Document id + * @param object request object + */ + protected void updateRequest(String index, String id, Object object) { + try { + UpdateRequest updateRequest = new UpdateRequest(index, id).doc(BeanUtils.toMap(object), XContentType.JSON); + client.update(updateRequest, COMMON_OPTIONS); + } catch (IOException e) { + throw new ElasticsearchException("更新索引 {" + index + "} 数据 {" + object + "} 失败"); + } + } + + /** + * exec deleteRequest + * + * @author fxbin + * @param index elasticsearch index name + * @param id Document id + */ + protected void deleteRequest(String index, String id) { + try { + DeleteRequest deleteRequest = new DeleteRequest(index, id); + client.delete(deleteRequest, COMMON_OPTIONS); + } catch (IOException e) { + throw new ElasticsearchException("删除索引 {" + index + "} 数据id {" + id + "} 失败"); + } + } + + /** + * search all + * @param index elasticsearch index name + * @return {@link SearchResponse} + */ + protected SearchResponse search(String index) { + SearchRequest searchRequest = new SearchRequest(index); + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); + searchSourceBuilder.query(QueryBuilders.matchAllQuery()); + searchRequest.source(searchSourceBuilder); + SearchResponse searchResponse = null; + try { + searchResponse = client.search(searchRequest, COMMON_OPTIONS); + } catch (IOException e) { + e.printStackTrace(); + } + return searchResponse; + } +} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/service/PersonService.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/service/PersonService.java new file mode 100644 index 0000000..0ffba43 --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/service/PersonService.java @@ -0,0 +1,68 @@ +package com.xkcoding.elasticsearch.service; + +import com.xkcoding.elasticsearch.model.Person; +import org.springframework.lang.Nullable; + +import java.util.List; + +/** + * PersonService + * + * @author fxbin + * @version v1.0 + * @since 2019/9/15 23:07 + */ +public interface PersonService { + + /** + * create Index + * + * @author fxbin + * @param index elasticsearch index name + */ + void createIndex(String index); + + /** + * delete Index + * + * @author fxbin + * @param index elasticsearch index name + */ + void deleteIndex(String index); + + /** + * insert document source + * + * @author fxbin + * @param index elasticsearch index name + * @param list data source + */ + void insert(String index, List list); + + /** + * update document source + * + * @author fxbin + * @param index elasticsearch index name + * @param list data source + */ + void update(String index, List list); + + /** + * delete document source + * + * @author fxbin + * @param person delete data source and allow null object + */ + void delete(String index, @Nullable Person person); + + /** + * search all doc records + * + * @author fxbin + * @param index elasticsearch index name + * @return person list + */ + List searchList(String index); + +} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/service/impl/PersonServiceImpl.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/service/impl/PersonServiceImpl.java new file mode 100644 index 0000000..e72c182 --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/service/impl/PersonServiceImpl.java @@ -0,0 +1,91 @@ +package com.xkcoding.elasticsearch.service.impl; + +import cn.hutool.core.bean.BeanUtil; +import com.xkcoding.elasticsearch.model.Person; +import com.xkcoding.elasticsearch.service.BaseElasticsearchService; +import com.xkcoding.elasticsearch.service.PersonService; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.search.SearchHit; +import org.springframework.stereotype.Service; +import org.springframework.util.ObjectUtils; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +/** + * PersonServiceImpl + * + * @author fxbin + * @version v1.0 + * @since 2019/9/15 23:08 + */ +@Service +public class PersonServiceImpl extends BaseElasticsearchService implements PersonService { + + + @Override + public void createIndex(String index) { + createIndexRequest(index); + } + + @Override + public void deleteIndex(String index) { + deleteIndexRequest(index); + } + + @Override + public void insert(String index, List list) { + + try { + list.forEach(person -> { + IndexRequest request = buildIndexRequest(index, String.valueOf(person.getId()), person); + try { + client.index(request, COMMON_OPTIONS); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } finally { + try { + client.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + @Override + public void update(String index, List list) { + list.forEach(person -> { + updateRequest(index, String.valueOf(person.getId()), person); + }); + } + + @Override + public void delete(String index, Person person) { + if (ObjectUtils.isEmpty(person)) { + // 如果person 对象为空,则删除全量 + searchList(index).forEach(p -> { + deleteRequest(index, String.valueOf(p.getId())); + }); + } + deleteRequest(index, String.valueOf(person.getId())); + } + + @Override + public List searchList(String index) { + SearchResponse searchResponse = search(index); + SearchHit[] hits = searchResponse.getHits().getHits(); + List personList = new ArrayList<>(); + Arrays.stream(hits).forEach(hit -> { + Map sourceAsMap = hit.getSourceAsMap(); + Person person = BeanUtil.mapToBean(sourceAsMap, Person.class, true); + personList.add(person); + }); + return personList; + } +} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/util/BeanUtils.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/util/BeanUtils.java similarity index 55% rename from spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/util/BeanUtils.java rename to spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/util/BeanUtils.java index fe3126e..ffc1bdb 100644 --- a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/util/BeanUtils.java +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/util/BeanUtils.java @@ -1,12 +1,10 @@ -package com.xlcoding.elasticsearch.util; +package com.xkcoding.elasticsearch.util; -import com.xlcoding.elasticsearch.model.Person; +import com.xkcoding.elasticsearch.model.Person; import java.beans.BeanInfo; -import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Map; @@ -50,15 +48,34 @@ public class BeanUtils { return map; } + /** + * Java Bean to Map + * + * @author fxbin + * @param object Object + * @return Map + */ + public static Map toMap(Object object){ + Map map = MapUtils.newHashMap(); + try { + // 获取javaBean的BeanInfo对象 + BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass(),Object.class); - public static void main(String[] args) { - - Person person = new Person(); - - Map stringObjectMap = toFieldNameAndFieldTypeMap(person); - - System.out.println(stringObjectMap); - + // 获取属性描述器 + PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); + for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { + // 获取属性名 + String key = propertyDescriptor.getName(); + // 获取该属性的值 + Method readMethod = propertyDescriptor.getReadMethod(); + // 通过反射来调用javaBean定义的getName()方法 + Object value = readMethod.invoke(object); + map.put(key, value); + } + } catch (Exception e) { + e.printStackTrace(); + } + return map; } } diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/util/MapUtils.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/util/MapUtils.java similarity index 98% rename from spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/util/MapUtils.java rename to spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/util/MapUtils.java index b8b492b..a58fec9 100644 --- a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/util/MapUtils.java +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/util/MapUtils.java @@ -1,4 +1,4 @@ -package com.xlcoding.elasticsearch.util; +package com.xkcoding.elasticsearch.util; import java.util.HashMap; import java.util.LinkedHashMap; diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/BaseElasticsearchService.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/BaseElasticsearchService.java deleted file mode 100644 index a7b691b..0000000 --- a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/BaseElasticsearchService.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.xlcoding.elasticsearch.service; - -import com.xlcoding.elasticsearch.autoconfigure.ElasticsearchProperties; -import com.xlcoding.elasticsearch.exception.ElasticsearchException; -import com.xlcoding.elasticsearch.model.Person; -import com.xlcoding.elasticsearch.util.BeanUtils; -import com.xlcoding.elasticsearch.util.MapUtils; -import org.elasticsearch.action.index.IndexRequest; -import org.elasticsearch.client.indices.CreateIndexRequest; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; - -import javax.annotation.Resource; -import java.io.IOException; -import java.util.Map; - -/** - * BaseElasticsearchService - * - * @author fxbin - * @version 1.0v - * @since 2019/9/16 15:44 - */ -public abstract class BaseElasticsearchService { - - @Resource - private ElasticsearchProperties elasticsearchProperties; - - /** - * create elasticsearch index - * - * @author fxbin - * @param index elasticsearch index - */ - public void createIndex(String index, Person person) { - - try { - CreateIndexRequest request = new CreateIndexRequest(index); - // Settings for this index - request.settings(Settings.builder() - .put("index.number_of_shards", elasticsearchProperties.getIndex().getNumberOfShards()) - .put("index.number_of_replicas", elasticsearchProperties.getIndex().getNumberOfReplicas())); - XContentBuilder builder = XContentFactory.jsonBuilder(); - builder.startObject(); - { - builder.startObject("properties"); - { - builder.startObject("message"); - { - Map map = - BeanUtils.toMap(person); - - map.keySet().forEach(key -> { - - try { - builder.field(key, "text"); - } catch (IOException e) { - e.printStackTrace(); - } - }); - - } - builder.endObject(); - } - builder.endObject(); - } - builder.endObject(); - request.mapping(builder); - - } catch (IOException e) { - throw new ElasticsearchException("创建Elasticsearch索引 {" + index + "} 失败"); - } - - } - - /** - * build IndexRequest - * - * @author fxbin - * @param index elasticsearch index name - * @return {@link org.elasticsearch.action.index.IndexRequest} - */ - public IndexRequest buildIndexRequest(String index) { - return new IndexRequest(index); - } - -} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/PersonService.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/PersonService.java deleted file mode 100644 index 258ed4e..0000000 --- a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/PersonService.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.xlcoding.elasticsearch.service; - -/** - * PersonService - * - * @author fxbin - * @version v1.0 - * @since 2019/9/15 23:07 - */ -public interface PersonService { -} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/impl/PersonServiceImpl.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/impl/PersonServiceImpl.java deleted file mode 100644 index 63de2ae..0000000 --- a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/java/com/xlcoding/elasticsearch/service/impl/PersonServiceImpl.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.xlcoding.elasticsearch.service.impl; - -import com.xlcoding.elasticsearch.service.BaseElasticsearchService; -import com.xlcoding.elasticsearch.service.PersonService; - -/** - * PersonServiceImpl - * - * @author fxbin - * @version v1.0 - * @since 2019/9/15 23:08 - */ -public class PersonServiceImpl extends BaseElasticsearchService implements PersonService { -} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/resources/META-INF/spring.factories b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..91028fe --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/resources/META-INF/spring.factories @@ -0,0 +1,3 @@ +# Auto Configure +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +com.xkcoding.elasticsearch.autoconfigure.ElasticsearchAutoConfiguration diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/resources/application.yml b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/resources/application.yml index da858a6..05aacad 100644 --- a/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/resources/application.yml +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/main/resources/application.yml @@ -3,3 +3,9 @@ demo: elasticsearch: cluster-name: elasticsearch cluster-nodes: 20.20.0.27:9201 + index: + number-of-replicas: 0 + number-of-shards: 3 + account: + username: + password: diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/test/java/com/xdcoding/elasticsearch/ElasticsearchApplicationTests.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/test/java/com/xdcoding/elasticsearch/ElasticsearchApplicationTests.java deleted file mode 100644 index f9c426e..0000000 --- a/spring-boot-demo-elasticsearch-rest-high-level-client/src/test/java/com/xdcoding/elasticsearch/ElasticsearchApplicationTests.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.xdcoding.elasticsearch; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@SpringBootTest -public class ElasticsearchApplicationTests { - - @Test - public void contextLoads() { - } - - -} diff --git a/spring-boot-demo-elasticsearch-rest-high-level-client/src/test/java/com/xkcoding/elasticsearch/ElasticsearchApplicationTests.java b/spring-boot-demo-elasticsearch-rest-high-level-client/src/test/java/com/xkcoding/elasticsearch/ElasticsearchApplicationTests.java new file mode 100644 index 0000000..21f901c --- /dev/null +++ b/spring-boot-demo-elasticsearch-rest-high-level-client/src/test/java/com/xkcoding/elasticsearch/ElasticsearchApplicationTests.java @@ -0,0 +1,64 @@ +package com.xkcoding.elasticsearch; + +import com.xkcoding.elasticsearch.contants.ElasticsearchConstant; +import com.xkcoding.elasticsearch.model.Person; +import com.xkcoding.elasticsearch.service.PersonService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ElasticsearchApplicationTests { + + @Resource + private PersonService personService; + + @Test + public void deleteIndexTest() { + personService.deleteIndex(ElasticsearchConstant.INDEX_NAME); + } + + @Test + public void createIndexTest() { + personService.createIndex(ElasticsearchConstant.INDEX_NAME); + } + + @Test + public void insertTest() { + + List list = new ArrayList<>(); + list.add(Person.builder().age(11).birthday(new Date()).country("CN").id(1L).name("哈哈").remark("test1").build()); + list.add(Person.builder().age(22).birthday(new Date()).country("US").id(2L).name("hiahia").remark("test2").build()); + list.add(Person.builder().age(33).birthday(new Date()).country("ID").id(3L).name("呵呵").remark("test3").build()); + + personService.insert(ElasticsearchConstant.INDEX_NAME, list); + } + + @Test + public void updateTest() { + Person person = Person.builder().age(33).birthday(new Date()).country("ID_update").id(3L).name("呵呵update").remark("test3_update").build(); + List list = new ArrayList<>(); + list.add(person); + personService.update(ElasticsearchConstant.INDEX_NAME, list); + } + + @Test + public void deleteTest() { + personService.delete(ElasticsearchConstant.INDEX_NAME, Person.builder().id(1L).build()); + } + + @Test + public void searchListTest() { + List personList = personService.searchList(ElasticsearchConstant.INDEX_NAME); + System.out.println(personList); + } + + +}