diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..2f8ca9e38b1bb0a57737bb7c671f9c7d48cffc23 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +.settings +target +.project +.classpath +bin +logs +.springBeans +*.iml +.idea +~* +*.log +.metadata +*module_*.xml diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..a7b41e7d495b042243317ad697761161fb5fdb44 --- /dev/null +++ b/pom.xml @@ -0,0 +1,56 @@ + + + 4.0.0 + + tech.mhuang + hmtool-ext-elasticsearch-springboot-sample + 1.1.0 + + 1.1.0 + UTF-8 + 1.8 + 2.1.5.RELEASE + 2.9.9 + + + + + tech.mhuang + hmtool-ext-elasticsearch-springboot + ${hmtool.ext.elasticsearch.springboot.version} + + + + org.springframework.boot + spring-boot-starter + ${springboot.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + com.fasterxml.jackson.core + jackson-annotations + ${jackson.version} + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${java.version} + ${java.version} + ${project.build.sourceEncoding} + + + + + + \ No newline at end of file diff --git a/src/main/java/tech/mhuang/ext/elasticsearch/springboot/sample/ESSampleApplication.java b/src/main/java/tech/mhuang/ext/elasticsearch/springboot/sample/ESSampleApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..f7ebd3e75c6af53eca23cea751ddc67f9046c9ff --- /dev/null +++ b/src/main/java/tech/mhuang/ext/elasticsearch/springboot/sample/ESSampleApplication.java @@ -0,0 +1,44 @@ +package tech.mhuang.ext.elasticsearch.springboot.sample; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import tech.mhuang.ext.elasticsearch.admin.external.IESExternal; +import tech.mhuang.ext.elasticsearch.admin.factory.IESFactory; +import tech.mhuang.ext.elasticsearch.server.ESFactory; + +/** + * + * jwt测试 + * + * @author mhuang + * @since 1.0.0 + */ +@SpringBootApplication +public class ESSampleApplication { + + /** + * 自定义ES创建类 + * @return + */ + static class CustomESExternal implements IESExternal { + @Override + public IESFactory create(String key) { + System.out.println("自定义ES生产key"+key); + return new ESFactory(); + } + } + + /** + * 自定义ES + * @return + */ + @Bean + public IESExternal CustomESExternal(){ + return new CustomESExternal(); + } + + public static void main(String[] args) { + SpringApplication.run(ESSampleApplication.class,args); + } +} diff --git a/src/main/java/tech/mhuang/ext/elasticsearch/springboot/sample/TestES.java b/src/main/java/tech/mhuang/ext/elasticsearch/springboot/sample/TestES.java new file mode 100644 index 0000000000000000000000000000000000000000..6d22f04f87923c547c58f05f33ec12569d00c96f --- /dev/null +++ b/src/main/java/tech/mhuang/ext/elasticsearch/springboot/sample/TestES.java @@ -0,0 +1,69 @@ +package tech.mhuang.ext.elasticsearch.springboot.sample; + + +import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.elasticsearch.action.delete.DeleteResponse; +import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.action.support.master.AcknowledgedResponse; +import org.elasticsearch.action.update.UpdateResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; +import tech.mhuang.ext.elasticsearch.admin.ESFramework; +import tech.mhuang.ext.elasticsearch.admin.factory.IESFactory; +import tech.mhuang.ext.elasticsearch.server.annoation.ESTable; + +import java.io.IOException; + +/** + * + * 测试 + * + * @author mhuang + * @since 1.0.0 + */ +@Slf4j +@Component +public class TestES implements ApplicationRunner { + + @Autowired + private ESFramework esFramework; + @Override + public void run(ApplicationArguments args) throws Exception { + try { + IESFactory esFactory = esFramework.getFactory("zhangsan"); + //ES新增 + IndexResponse response = esFactory.insert("{\"age\":22}", "test", "test"); + log.info("新增1:{}", response); + Test test = new Test(); + IndexResponse response2 = esFactory.insert(test); + log.info("新增2:{}", response2); + String id = response2.getId(); + test.setName("你好"); + //ES修改 + UpdateResponse response3 = esFactory.update(test, id); + log.info("修改:{}", response3); + //ES删除 + DeleteResponse response4 = esFactory.delete("test", "test", id); + log.info("删除:{}", response4); + //ES清空索引所有数据 + AcknowledgedResponse response5 = esFactory.delete("test"); + log.info("删除索引:{}", response5); + //获取RestHighLevelClient对象 + RestHighLevelClient client = esFactory.getClient(); + log.info("打印信息{}", client.info(RequestOptions.DEFAULT)); + System.exit(0); + } catch (IOException e) { + e.printStackTrace(); + } + } + @Data + @ESTable(index = "test", type = "test") + static class Test { + private String name = "张三"; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000000000000000000000000000000000000..0fdfbadcd95302a66ab010bde46045383c2f1091 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,3 @@ +mhuang.elasticsearch.enable=true +mhuang.elasticsearch.bean-map.zhangsan.enable=true +mhuang.elasticsearch.bean-map.zhangsan.ip=192.168.1.210 \ No newline at end of file