From 20b1c387b6de5033a0f3dbef569828ba2868cf44 Mon Sep 17 00:00:00 2001 From: smileInLife <38873832+codersfarm@users.noreply.github.com> Date: Mon, 14 Oct 2019 16:48:56 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=97=A0=E7=94=A8=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/elastic/api/Aggregations.java | 103 ------------------ 1 file changed, 103 deletions(-) delete mode 100644 elasticsearch5.x/src/main/java/com/elastic/api/Aggregations.java diff --git a/elasticsearch5.x/src/main/java/com/elastic/api/Aggregations.java b/elasticsearch5.x/src/main/java/com/elastic/api/Aggregations.java deleted file mode 100644 index 02eba1e..0000000 --- a/elasticsearch5.x/src/main/java/com/elastic/api/Aggregations.java +++ /dev/null @@ -1,103 +0,0 @@ -package com.elastic.api; - -import com.elastic.transprotclient.TransportClientBuild; -import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.action.search.SearchType; -import org.elasticsearch.client.Client; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.search.aggregations.AggregationBuilder; -import org.elasticsearch.search.aggregations.AggregationBuilders; -import org.elasticsearch.search.aggregations.bucket.filters.Filters; -import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator; -import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; -import org.elasticsearch.search.aggregations.metrics.stats.Stats; -import org.elasticsearch.search.aggregations.metrics.stats.StatsAggregationBuilder; - -import static org.elasticsearch.index.query.QueryBuilders.termQuery; - -/** - * @Date: 2019/3/22 11:32 - **/ -public class Aggregations { - - - Client client = TransportClientBuild.getClient(); - - /** - * 4.1 Structuring aggregations - */ - public void structAgg() { - SearchResponse sr = client.prepareSearch() - .addAggregation(AggregationBuilders - .terms("by_country") - .field("country") - .subAggregation(AggregationBuilders - .dateHistogram("by_year") - .field("dateOfBirth") - .dateHistogramInterval(DateHistogramInterval.YEAR) - .subAggregation(AggregationBuilders - .avg("avg_children") - .field("children")) - ) - ) - .execute().actionGet(); - - } - - /** - * 4.2 metrics_aggregations - */ - public void metricAgg() { - - StatsAggregationBuilder aggregation = - AggregationBuilders - /*聚合名称*/ - .stats("agg") - .field("height"); - - SearchResponse searchResponse = client.prepareSearch() - .addAggregation(aggregation) - .execute() - .actionGet(); - - Stats agg = searchResponse.getAggregations().get("agg"); - double min = agg.getMin(); - double max = agg.getMax(); - double avg = agg.getAvg(); - double sum = agg.getSum(); - long count = agg.getCount(); - - } - - /** - * 4.3 Bucket aggregations - */ - public void bucketAgg() { - AggregationBuilder aggregation = - AggregationBuilders - .filters("agg", - new FiltersAggregator.KeyedFilter("men", QueryBuilders.termQuery("gender", "male")), - new FiltersAggregator.KeyedFilter("women", QueryBuilders.termQuery("gender", "female"))); - - - SearchResponse searchResponse = client.prepareSearch() - .addAggregation(aggregation) - .execute() - .actionGet(); - - - // searchResponse is here your SearchResponse object - Filters agg = searchResponse.getAggregations().get("agg"); - - // For each entry - for (Filters.Bucket entry : agg.getBuckets()) { - // bucket key - String key = entry.getKeyAsString(); - // Doc count - long docCount = entry.getDocCount(); - System.out.print("Key :" + key + "douCount:" + docCount); - } - } - -} \ No newline at end of file From d3786bcdee54ff647588aaa1012302672cadd52e Mon Sep 17 00:00:00 2001 From: smileInLife <38873832+codersfarm@users.noreply.github.com> Date: Mon, 14 Oct 2019 16:52:00 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=83=A8=E5=88=86API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/elastic/api/Aggregations.java | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 elasticsearch5.x/src/main/java/com/elastic/api/Aggregations.java diff --git a/elasticsearch5.x/src/main/java/com/elastic/api/Aggregations.java b/elasticsearch5.x/src/main/java/com/elastic/api/Aggregations.java new file mode 100644 index 0000000..02eba1e --- /dev/null +++ b/elasticsearch5.x/src/main/java/com/elastic/api/Aggregations.java @@ -0,0 +1,103 @@ +package com.elastic.api; + +import com.elastic.transprotclient.TransportClientBuild; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.search.SearchType; +import org.elasticsearch.client.Client; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.search.aggregations.AggregationBuilder; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.bucket.filters.Filters; +import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator; +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; +import org.elasticsearch.search.aggregations.metrics.stats.Stats; +import org.elasticsearch.search.aggregations.metrics.stats.StatsAggregationBuilder; + +import static org.elasticsearch.index.query.QueryBuilders.termQuery; + +/** + * @Date: 2019/3/22 11:32 + **/ +public class Aggregations { + + + Client client = TransportClientBuild.getClient(); + + /** + * 4.1 Structuring aggregations + */ + public void structAgg() { + SearchResponse sr = client.prepareSearch() + .addAggregation(AggregationBuilders + .terms("by_country") + .field("country") + .subAggregation(AggregationBuilders + .dateHistogram("by_year") + .field("dateOfBirth") + .dateHistogramInterval(DateHistogramInterval.YEAR) + .subAggregation(AggregationBuilders + .avg("avg_children") + .field("children")) + ) + ) + .execute().actionGet(); + + } + + /** + * 4.2 metrics_aggregations + */ + public void metricAgg() { + + StatsAggregationBuilder aggregation = + AggregationBuilders + /*聚合名称*/ + .stats("agg") + .field("height"); + + SearchResponse searchResponse = client.prepareSearch() + .addAggregation(aggregation) + .execute() + .actionGet(); + + Stats agg = searchResponse.getAggregations().get("agg"); + double min = agg.getMin(); + double max = agg.getMax(); + double avg = agg.getAvg(); + double sum = agg.getSum(); + long count = agg.getCount(); + + } + + /** + * 4.3 Bucket aggregations + */ + public void bucketAgg() { + AggregationBuilder aggregation = + AggregationBuilders + .filters("agg", + new FiltersAggregator.KeyedFilter("men", QueryBuilders.termQuery("gender", "male")), + new FiltersAggregator.KeyedFilter("women", QueryBuilders.termQuery("gender", "female"))); + + + SearchResponse searchResponse = client.prepareSearch() + .addAggregation(aggregation) + .execute() + .actionGet(); + + + // searchResponse is here your SearchResponse object + Filters agg = searchResponse.getAggregations().get("agg"); + + // For each entry + for (Filters.Bucket entry : agg.getBuckets()) { + // bucket key + String key = entry.getKeyAsString(); + // Doc count + long docCount = entry.getDocCount(); + System.out.print("Key :" + key + "douCount:" + docCount); + } + } + +} \ No newline at end of file From e127d1919d6730c78d620faabc40500d7c9f6b7d Mon Sep 17 00:00:00 2001 From: smileInLife <38873832+codersfarm@users.noreply.github.com> Date: Tue, 10 Dec 2019 16:40:14 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/elastic/utils/ConfigUtils.java | 126 +++++++++--------- 1 file changed, 62 insertions(+), 64 deletions(-) diff --git a/elasticsearch6.x/src/main/java/com/elastic/utils/ConfigUtils.java b/elasticsearch6.x/src/main/java/com/elastic/utils/ConfigUtils.java index 79d4c24..810e870 100644 --- a/elasticsearch6.x/src/main/java/com/elastic/utils/ConfigUtils.java +++ b/elasticsearch6.x/src/main/java/com/elastic/utils/ConfigUtils.java @@ -1,9 +1,6 @@ package com.elastic.utils; -import com.elastic.exception.ESIoException; -import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils; - import java.io.IOException; import java.io.InputStream; import java.util.Properties; @@ -13,65 +10,66 @@ **/ public class ConfigUtils { - /** - * 配置文件 - * */ - private static String esConfigFileName = "elasticsearch.properties"; - - /** - * es集群名 - * */ - private static String esClusterName; - /** - * es集群ip地址 - * */ - private static String esClusterDiscoverHostName; - /** - * es集群是否加入嗅探功能 - * */ - private static String clientTransportSniff; - - private static Properties properties = new Properties(); - static{ - try { - ClassLoader classLoader = ConfigUtils.class.getClassLoader(); - InputStream resourceAsStream = classLoader.getResourceAsStream(esConfigFileName); - properties.load(resourceAsStream); - init(); - }catch (IOException e){ - e.printStackTrace(); - } - - } - - private static void init() { - - esClusterName = properties.getProperty("elastic.cluster.name"); - esClusterDiscoverHostName = properties.getProperty("elastic.cluster.discover.hostname"); - clientTransportSniff = properties.getProperty("elastic.cluster.clientTransportSniff"); - - if ("".equals(esClusterName)||"".equals(esClusterName)||"".equals(clientTransportSniff)){ - throw new RuntimeException("elasticsearch 集群参数为空异常"); - } - } - - public static String getEsClusterName() { - return esClusterName; - } - - public static String getEsClusterDiscoverHostName() { - return esClusterDiscoverHostName; - } - - public static void setEsClusterDiscoverHostName(String esClusterDiscoverHostName) { - ConfigUtils.esClusterDiscoverHostName = esClusterDiscoverHostName; - } - - public static String getClientTransportSniff() { - return clientTransportSniff; - } - - public static void setClientTransportSniff(String clientTransportSniff) { - ConfigUtils.clientTransportSniff = clientTransportSniff; - } + /** + * 配置文件 + */ + private static String esConfigFileName = "elasticsearch.properties"; + + /** + * es集群名 + */ + private static String esClusterName; + /** + * es集群ip地址 + */ + private static String esClusterDiscoverHostName; + /** + * es集群是否加入嗅探功能 + */ + private static String clientTransportSniff; + + private static Properties properties = new Properties(); + + static { + try { + ClassLoader classLoader = ConfigUtils.class.getClassLoader(); + InputStream resourceAsStream = classLoader.getResourceAsStream(esConfigFileName); + properties.load(resourceAsStream); + init(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + private static void init() { + + esClusterName = properties.getProperty("elastic.cluster.name"); + esClusterDiscoverHostName = properties.getProperty("elastic.cluster.discover.hostname"); + clientTransportSniff = properties.getProperty("elastic.cluster.clientTransportSniff"); + + if ("".equals(esClusterName) || "".equals(esClusterName) || "".equals(clientTransportSniff)) { + throw new RuntimeException("elasticsearch 集群参数为空异常"); + } + } + + public static String getEsClusterName() { + return esClusterName; + } + + public static String getEsClusterDiscoverHostName() { + return esClusterDiscoverHostName; + } + + public static void setEsClusterDiscoverHostName(String esClusterDiscoverHostName) { + ConfigUtils.esClusterDiscoverHostName = esClusterDiscoverHostName; + } + + public static String getClientTransportSniff() { + return clientTransportSniff; + } + + public static void setClientTransportSniff(String clientTransportSniff) { + ConfigUtils.clientTransportSniff = clientTransportSniff; + } } From dd7c83db8fa54a4ec5c4a702f27e5fc1f82fac4e Mon Sep 17 00:00:00 2001 From: smileInLife <38873832+codersfarm@users.noreply.github.com> Date: Thu, 19 Dec 2019 10:06:32 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springdataclient/SpringDataClientBuild.java | 4 ++-- .../src/test/java/com/elastic/MultiThreadTest.java | 5 +---- .../main/java/com/elastic/client/ClientBuilders.java | 11 ++++++----- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/elasticsearch5.x/src/main/java/com/elastic/springdataclient/SpringDataClientBuild.java b/elasticsearch5.x/src/main/java/com/elastic/springdataclient/SpringDataClientBuild.java index 10bd354..8954df5 100644 --- a/elasticsearch5.x/src/main/java/com/elastic/springdataclient/SpringDataClientBuild.java +++ b/elasticsearch5.x/src/main/java/com/elastic/springdataclient/SpringDataClientBuild.java @@ -34,9 +34,9 @@ public class SpringDataClientBuild { private String CLUSTER_HOSTNAME_PORT; /** - * 在Spring中,bean可以被定义为两种模式:prototype(多例)和singleton(单例) + * 在Spring中,bean可以被定义为两种模式:prototype(原型)和singleton(单例) * singleton(单例):只有一个共享的实例存在,所有对这个bean的请求都会返回这个唯一的实例。Spring bean 默认是单例模式. - * prototype(多例):对这个bean的每次请求都会创建一个新的bean实例,类似于new。 + * prototype(原型):对这个bean的每次请求都会创建一个新的bean实例,类似于new。 */ @Bean @Scope("prototype") diff --git a/elasticsearch5.x/src/test/java/com/elastic/MultiThreadTest.java b/elasticsearch5.x/src/test/java/com/elastic/MultiThreadTest.java index e9ec54b..005dd99 100644 --- a/elasticsearch5.x/src/test/java/com/elastic/MultiThreadTest.java +++ b/elasticsearch5.x/src/test/java/com/elastic/MultiThreadTest.java @@ -46,7 +46,4 @@ public void run() { } } - - -} - +} \ No newline at end of file diff --git a/elasticsearch6.x/src/main/java/com/elastic/client/ClientBuilders.java b/elasticsearch6.x/src/main/java/com/elastic/client/ClientBuilders.java index 9f76212..4066ce9 100644 --- a/elasticsearch6.x/src/main/java/com/elastic/client/ClientBuilders.java +++ b/elasticsearch6.x/src/main/java/com/elastic/client/ClientBuilders.java @@ -8,7 +8,6 @@ import org.apache.http.message.BasicHeader; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; -import org.elasticsearch.client.RestHighLevelClient; import java.util.List; import java.util.stream.Collectors; @@ -23,15 +22,16 @@ public class ClientBuilders { /** * 构建一个简单的RestClientBuilder方便测试 + * * @return */ - public RestClientBuilder getSimpleClientBuilder(){ - String [] ipHosts = CLUSTER_HOSTNAME_PORT.split(","); + public RestClientBuilder getSimpleClientBuilder() { + String[] ipHosts = CLUSTER_HOSTNAME_PORT.split(","); List httpHostsList = Stream.of(ipHosts) .map(this::createHttpHost) .collect(Collectors.toList()); - HttpHost [] httpHosts = httpHostsList.toArray(new HttpHost[httpHostsList.size()]); + HttpHost[] httpHosts = httpHostsList.toArray(new HttpHost[httpHostsList.size()]); RestClientBuilder builder = RestClient.builder(httpHosts); return builder; @@ -43,12 +43,13 @@ private HttpHost createHttpHost(String ip) { /** * 初始化 clientBuilder的详细说明 + * * @return */ public static RestClientBuilder getClientBulider() { - String [] hostNamesPort = CLUSTER_HOSTNAME_PORT.split(","); + String[] hostNamesPort = CLUSTER_HOSTNAME_PORT.split(","); String host; int port; From 77d1f1acc3f6c74109c893074789f8ed6bcd5d2b Mon Sep 17 00:00:00 2001 From: smileInLife <38873832+codersfarm@users.noreply.github.com> Date: Thu, 19 Dec 2019 15:23:10 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ES=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- elasticsearch5.x/pom.xml | 4 ++++ elasticsearchAction/elasticsearchAction.iml | 9 +++++++++ 2 files changed, 13 insertions(+) create mode 100644 elasticsearchAction/elasticsearchAction.iml diff --git a/elasticsearch5.x/pom.xml b/elasticsearch5.x/pom.xml index eee0ba5..2c96b4b 100644 --- a/elasticsearch5.x/pom.xml +++ b/elasticsearch5.x/pom.xml @@ -6,7 +6,11 @@ com.elastic elasticsearch5.x + pom 1.0-SNAPSHOT + + ../elasticsearchAction + diff --git a/elasticsearchAction/elasticsearchAction.iml b/elasticsearchAction/elasticsearchAction.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/elasticsearchAction/elasticsearchAction.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file From 15858c0c7a7cf3c2b35784ed0e5257d148b07da5 Mon Sep 17 00:00:00 2001 From: smileInLife <38873832+codersfarm@users.noreply.github.com> Date: Thu, 19 Dec 2019 15:24:19 +0800 Subject: [PATCH 6/8] =?UTF-8?q?git=E8=BF=87=E6=BB=A4=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- elasticsearchAction/.gitingore | 41 ++++++++++ elasticsearchAction/elasticsearchAction.iml | 89 ++++++++++++++++++++- 2 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 elasticsearchAction/.gitingore diff --git a/elasticsearchAction/.gitingore b/elasticsearchAction/.gitingore new file mode 100644 index 0000000..d195cbb --- /dev/null +++ b/elasticsearchAction/.gitingore @@ -0,0 +1,41 @@ +#Eclipse +.classpath +.project +.settings/ + +#Intel Idea +.idea +*.iml +*.iws + +#Maven +log +target +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionBackup +pom.xml.next +release.properties +dependcy-reduced-pom.xml +buildNumber.properties + +#java +*.class +*.war +*.ear + +# bak +*.bak +/bin/ + +# sbt +/target/ +/project/target/ +/project/project/target/ +/project/project/project/target/ +/build-sbt/ +local.sbt + + +# spring +*.springBeans diff --git a/elasticsearchAction/elasticsearchAction.iml b/elasticsearchAction/elasticsearchAction.iml index d6ebd48..41d68a6 100644 --- a/elasticsearchAction/elasticsearchAction.iml +++ b/elasticsearchAction/elasticsearchAction.iml @@ -1,9 +1,90 @@ - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From bcfc6cf083d0dc72121421aad8f4bbdb7960e7e0 Mon Sep 17 00:00:00 2001 From: smileInLife <38873832+codersfarm@users.noreply.github.com> Date: Thu, 19 Dec 2019 16:28:12 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=92=8C=E5=AE=9E=E4=BD=93=E7=B1=BB=20?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Maven__com_alibaba_fastjson_1_2_62.xml | 13 ++ ...terxml_jackson_core_jackson_core_2_9_7.xml | 13 ++ .idea/uiDesigner.xml | 124 ++++++++++++++++++ .../SpringDataClientBuild.java | 2 +- elasticsearchAction/elasticsearchAction.iml | 19 +-- elasticsearchAction/pom.xml | 36 +++++ .../com/elastic/annotations/Document.java | 103 +++++++++++++++ .../java/com/elastic/annotations/Field.java | 78 +++++++++++ .../com/elastic/annotations/FieldType.java | 53 ++++++++ .../java/com/elastic/annotations/Mapping.java | 35 +++++ .../java/com/elastic/beans/domain/User.java | 9 ++ .../com/elastic/beans/es/BaseESEnity.java | 29 ++++ .../elastic/config/ElasticSearchConfig.java | 76 +++++++++++ 13 files changed, 580 insertions(+), 10 deletions(-) create mode 100644 .idea/libraries/Maven__com_alibaba_fastjson_1_2_62.xml create mode 100644 .idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_9_7.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 elasticsearchAction/pom.xml create mode 100644 elasticsearchAction/src/main/java/com/elastic/annotations/Document.java create mode 100644 elasticsearchAction/src/main/java/com/elastic/annotations/Field.java create mode 100644 elasticsearchAction/src/main/java/com/elastic/annotations/FieldType.java create mode 100644 elasticsearchAction/src/main/java/com/elastic/annotations/Mapping.java create mode 100644 elasticsearchAction/src/main/java/com/elastic/beans/domain/User.java create mode 100644 elasticsearchAction/src/main/java/com/elastic/beans/es/BaseESEnity.java create mode 100644 elasticsearchAction/src/main/java/com/elastic/config/ElasticSearchConfig.java diff --git a/.idea/libraries/Maven__com_alibaba_fastjson_1_2_62.xml b/.idea/libraries/Maven__com_alibaba_fastjson_1_2_62.xml new file mode 100644 index 0000000..d7a8103 --- /dev/null +++ b/.idea/libraries/Maven__com_alibaba_fastjson_1_2_62.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_9_7.xml b/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_9_7.xml new file mode 100644 index 0000000..02897be --- /dev/null +++ b/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_9_7.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/elasticsearch5.x/src/main/java/com/elastic/springdataclient/SpringDataClientBuild.java b/elasticsearch5.x/src/main/java/com/elastic/springdataclient/SpringDataClientBuild.java index 8954df5..e6ff0fd 100644 --- a/elasticsearch5.x/src/main/java/com/elastic/springdataclient/SpringDataClientBuild.java +++ b/elasticsearch5.x/src/main/java/com/elastic/springdataclient/SpringDataClientBuild.java @@ -78,4 +78,4 @@ public Client getClient() { return client; } -} +} \ No newline at end of file diff --git a/elasticsearchAction/elasticsearchAction.iml b/elasticsearchAction/elasticsearchAction.iml index 41d68a6..fcc9d62 100644 --- a/elasticsearchAction/elasticsearchAction.iml +++ b/elasticsearchAction/elasticsearchAction.iml @@ -16,13 +16,7 @@ - - - - - - - + @@ -44,7 +38,7 @@ - + @@ -52,7 +46,6 @@ - @@ -67,6 +60,13 @@ + + + + + + + @@ -83,6 +83,7 @@ + diff --git a/elasticsearchAction/pom.xml b/elasticsearchAction/pom.xml new file mode 100644 index 0000000..e68902f --- /dev/null +++ b/elasticsearchAction/pom.xml @@ -0,0 +1,36 @@ + + + + elasticsearch5.x + com.elastic + 1.0-SNAPSHOT + ../elasticsearch5.x/pom.xml + + 4.0.0 + + elasticsearchAction + + + + org.elasticsearch.client + transport + 5.6.15 + + + + org.springframework.data + spring-data-elasticsearch + 3.1.0.RELEASE + + + + com.alibaba + fastjson + 1.2.62 + + + + + \ No newline at end of file diff --git a/elasticsearchAction/src/main/java/com/elastic/annotations/Document.java b/elasticsearchAction/src/main/java/com/elastic/annotations/Document.java new file mode 100644 index 0000000..32ce607 --- /dev/null +++ b/elasticsearchAction/src/main/java/com/elastic/annotations/Document.java @@ -0,0 +1,103 @@ +/* + * Copyright 2013-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.elastic.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.elasticsearch.index.VersionType; +import org.springframework.data.annotation.Persistent; + +/** + * Identifies a domain object to be persisted to Elasticsearch. + * + * @author Rizwan Idrees + * @author Mohsin Husen + * @author Mason Chan + * @author Ivan Greene + * @author Mark Paluch + * @author Peter-Josef Meisch + */ +@Persistent +@Inherited +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE }) +public @interface Document { + + /** + * Name of the Elasticsearch index. + *
    + *
  • Lowercase only
  • + *
  • , |, ` ` (space character), ,, #/li> + *
  • Cannot start with -, _, +
  • + *
  • Cannot be . or ..
  • + *
  • Cannot be longer than 255 bytes (note it is bytes, so multi-byte characters will count towards the 255 limit + * faster)
  • + *
+ */ + String indexName(); + + /** + * Mapping type name.
+ * deprecated as Elasticsearch does not support this anymore + * (@see Elastisearch removal of types documentation) and will remove it in + * Elasticsearch 8. + * + * @deprecated since 4.0 + */ + @Deprecated + String type() default ""; + + /** + * Use server-side settings when creating the index. + */ + boolean useServerConfiguration() default false; + + /** + * Number of shards for the index {@link #indexName()}. Used for index creation.
+ * With version 4.0, the default value is changed from 5 to 1 to reflect the change in the default settings of + * Elasticsearch which changed to 1 as well in Elasticsearch 7.0. + */ + short shards() default 1; + + /** + * Number of replicas for the index {@link #indexName()}. Used for index creation. + */ + short replicas() default 1; + + /** + * Refresh interval for the index {@link #indexName()}. Used for index creation. + */ + String refreshInterval() default "1s"; + + /** + * Index storage type for the index {@link #indexName()}. Used for index creation. + */ + String indexStoreType() default "fs"; + + /** + * Configuration whether to create an index on repository bootstrapping. + */ + boolean createIndex() default true; + + /** + * Configuration of version management. + */ + VersionType versionType() default VersionType.EXTERNAL; +} diff --git a/elasticsearchAction/src/main/java/com/elastic/annotations/Field.java b/elasticsearchAction/src/main/java/com/elastic/annotations/Field.java new file mode 100644 index 0000000..9a3fe68 --- /dev/null +++ b/elasticsearchAction/src/main/java/com/elastic/annotations/Field.java @@ -0,0 +1,78 @@ +/* + * Copyright 2013-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.elastic.annotations; + +import org.springframework.core.annotation.AliasFor; +import org.springframework.data.elasticsearch.annotations.DateFormat; +import org.springframework.data.elasticsearch.annotations.FieldType; + +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +@Documented +@Inherited +public @interface Field { + + @AliasFor("name") + String value() default ""; + + @AliasFor("value") + String name() default ""; + + FieldType type() default FieldType.Auto; + + boolean index() default true; + + DateFormat format() default DateFormat.none; + + String pattern() default ""; + + boolean store() default false; + + boolean fielddata() default false; + + String searchAnalyzer() default ""; + + String analyzer() default ""; + + String normalizer() default ""; + + String[] ignoreFields() default {}; + + boolean includeInParent() default false; + + String[] copyTo() default {}; + + int ignoreAbove() default -1; + + boolean coerce() default true; + + boolean docValues() default true; + + boolean ignoreMalformed() default false; + + boolean indexPhrases() default false; + + + boolean norms() default true; + + + String nullValue() default ""; + + + double scalingFactor() default 1; +} diff --git a/elasticsearchAction/src/main/java/com/elastic/annotations/FieldType.java b/elasticsearchAction/src/main/java/com/elastic/annotations/FieldType.java new file mode 100644 index 0000000..2f60bc3 --- /dev/null +++ b/elasticsearchAction/src/main/java/com/elastic/annotations/FieldType.java @@ -0,0 +1,53 @@ +/* + * Copyright 2013-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.elastic.annotations; + +/** + * @author Rizwan Idrees + * @author Mohsin Husen + * @author Artur Konczak + * @author Zeng Zetang + * @author Peter-Josef Meisch + */ +public enum FieldType { + Auto, + Text, + Keyword, + Long, + Integer, + Short, + Byte, + Double, + Float, + Half_Float, + Scaled_Float, + Date, + Date_Nanos, + Boolean, + Binary, + Integer_Range, + Float_Range, + Long_Range, + Double_Range, + Date_Range, + Ip_Range, + Object, + Nested, + Ip, + TokenCount, + Percolator, + Flattened +} diff --git a/elasticsearchAction/src/main/java/com/elastic/annotations/Mapping.java b/elasticsearchAction/src/main/java/com/elastic/annotations/Mapping.java new file mode 100644 index 0000000..627649b --- /dev/null +++ b/elasticsearchAction/src/main/java/com/elastic/annotations/Mapping.java @@ -0,0 +1,35 @@ +/* + * Copyright 2014-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.elastic.annotations; + +import org.springframework.data.annotation.Persistent; + +import java.lang.annotation.*; + +/** + * Elasticsearch Mapping + * + * @author Mohsin Husen + */ +@Persistent +@Inherited +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.FIELD}) +public @interface Mapping { + + String mappingPath() default ""; + +} diff --git a/elasticsearchAction/src/main/java/com/elastic/beans/domain/User.java b/elasticsearchAction/src/main/java/com/elastic/beans/domain/User.java new file mode 100644 index 0000000..efbb09d --- /dev/null +++ b/elasticsearchAction/src/main/java/com/elastic/beans/domain/User.java @@ -0,0 +1,9 @@ +package com.elastic.beans.domain; + +/** + * 用户实体 + * @Date: 2019/12/19 15:28 + **/ +public class User { + +} diff --git a/elasticsearchAction/src/main/java/com/elastic/beans/es/BaseESEnity.java b/elasticsearchAction/src/main/java/com/elastic/beans/es/BaseESEnity.java new file mode 100644 index 0000000..fb716ad --- /dev/null +++ b/elasticsearchAction/src/main/java/com/elastic/beans/es/BaseESEnity.java @@ -0,0 +1,29 @@ +package com.elastic.beans.es; + +import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.Version; +import org.springframework.data.elasticsearch.annotations.Document; +import org.springframework.data.elasticsearch.annotations.Field; + +import static org.springframework.data.elasticsearch.annotations.FieldType.Text; + +/** + * 用户实体对应的ES库实体 + * @Date: 2019/12/19 15:25 + **/ +@Document(indexName = "test-index-uuid-keyed", type = "test-type-uuid-keyed", + shards = 1, replicas = 0, refreshInterval = "-1") +public class BaseESEnity { + + @Id + private String id; + + @Field(type = Text, store = true, fielddata = true) + private String type; + + @Field(type = Text, store = true, fielddata = true) + private String message; + + @Version + private Long version; +} diff --git a/elasticsearchAction/src/main/java/com/elastic/config/ElasticSearchConfig.java b/elasticsearchAction/src/main/java/com/elastic/config/ElasticSearchConfig.java new file mode 100644 index 0000000..815f03b --- /dev/null +++ b/elasticsearchAction/src/main/java/com/elastic/config/ElasticSearchConfig.java @@ -0,0 +1,76 @@ +package com.elastic.config; + +import org.elasticsearch.client.Client; +import org.elasticsearch.client.transport.TransportClient; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.transport.InetSocketTransportAddress; +import org.elasticsearch.transport.client.PreBuiltTransportClient; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +/** + * ES-Client封装工具类 + * @Date: 2019/12/19 15:32 + **/ +@Configuration("classpath:elasticsearch.properties") +public class ElasticSearchConfig { + + @Value("${elastic.cluster.name}") + private String CLUSTER_NAME; + + @Value("${elastic.cluster.clientTransportSniff}") + private String CLUSTER_CLIENT_TRANSPORTSNIFF; + + @Value("${elastic.cluster.discover.hostname}") + private String CLUSTER_HOSTNAME_PORT; + + /** + * 在Spring中,bean可以被定义为两种模式:prototype(原型)和singleton(单例) + * singleton(单例):只有一个共享的实例存在,所有对这个bean的请求都会返回这个唯一的实例。Spring bean 默认是单例模式. + * prototype(原型):对这个bean的每次请求都会创建一个新的bean实例,类似于new。 + */ + @Bean + @Scope("singleton") + public Client getClient() { + + String[] hostNamesPort = CLUSTER_HOSTNAME_PORT.split(","); + + Settings settings = Settings.builder() + /*设置ES实例的名称*/ + .put("cluster.name", CLUSTER_NAME) + /*自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中*/ + .put("client.transport.sniff", CLUSTER_CLIENT_TRANSPORTSNIFF) + /*x-pack设置*/ + /*.put("xpack.security.transport.ssl.enabled", false) + //x-pack用户密码 elastic:changme是默认的用户名:密码 + .put("xpack.security.user", "elastic:changme")*/ + .build(); + + /*初始化client*/ + TransportClient transportClient = new PreBuiltTransportClient(settings); + String host; + int port; + String[] temp; + + if (0 != hostNamesPort.length){ + for (String hostPort : hostNamesPort) { + try { + temp = hostPort.split(":"); + host = temp[0].trim(); + port = Integer.parseInt(temp[1].trim()); + transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port)); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + } + } + + Client client = transportClient; + return client; + } +} \ No newline at end of file From e882862b484fce49406d592e43d48efcea080f59 Mon Sep 17 00:00:00 2001 From: smileInLife <38873832+codersfarm@users.noreply.github.com> Date: Thu, 19 Dec 2019 16:32:25 +0800 Subject: [PATCH 8/8] Update README.md --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 73de094..71f1eb8 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,7 @@ java 连接 elasticsearch5.x 集群及Java API代码 ## package elasticsearch6.x -java 连接 elasticsearch6.x 集群及Java API代码 \ No newline at end of file +java 连接 elasticsearch6.x 集群及Java API代码 + +## package elasticsearchAction +elasticsearch 二次开发,包含增删改查底层操作