();
85 | Rule rule = new Rule();
86 | rule.setRuleCount(100);
87 | rule.setRuleExist(true);
88 | rule.setRuleId("asdfasd$fasdf");
89 | ruleList.add(rule);
90 |
91 | rule = new Rule();
92 | rule.setRuleCount(101);
93 | rule.setRuleExist(false);
94 | rule.setRuleId("bbbb");
95 | ruleList.add(rule);
96 |
97 | rule = new Rule();
98 | rule.setRuleCount(103);
99 | rule.setRuleExist(true);
100 | rule.setRuleId(null);
101 | ruleList.add(rule);
102 | dynamicPriceTemplate.setRules(ruleList);
103 |
104 |
105 | //为id为2的文档增加last和nick两个属性
106 | params.put("last","gaudre$au");
107 | params.put("nick","hockey");
108 | params.put("id",3);
109 | params.put("dynamicPriceTemplate",dynamicPriceTemplate);
110 | //通过script脚本为id为2的文档增加last和nick两个属性,为了演示效果强制refresh,实际环境慎用
111 | clientInterface.updateByPath("demo/_update_by_query?refresh","scriptDslByQuery",params);
112 | //获取更新后的文档,会看到新加的2个字段属性
113 | String doc = clientInterface.getDocument("demo","3");
114 | System.out.println(doc);
115 |
116 | }
117 |
118 |
119 | }
120 |
--------------------------------------------------------------------------------
/src/main/resources/application-multi-datasource.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | elasticsearch:
3 | bboss:
4 | default:
5 | name: default
6 | elasticPassword: changeme
7 | elasticUser: elastic
8 | elasticsearch:
9 | dateFormat: yyyy.MM.dd
10 | discoverHost: false
11 | rest:
12 | hostNames: 192.168.137.1:9200
13 | scrollBlockedWaitTimeout: 0
14 | scrollThreadCount: 200
15 | scrollThreadQueue: 200
16 | showTemplate: true
17 | sliceScrollBlockedWaitTimeout: 0
18 | sliceScrollThreadCount: 100
19 | sliceScrollThreadQueue: 100
20 | timeZone: Asia/Shanghai
21 | http:
22 | connectionRequestTimeout: 50000
23 | customHttpRequestRetryHandler: org.frameworkset.spi.remote.http.ConnectionResetHttpRequestRetryHandler
24 | defaultMaxPerRoute: 200
25 | hostnameVerifier: ''
26 | keyPassword: ''
27 | keystore: ''
28 | maxHeaderCount: 200
29 | maxLineLength: -1
30 | maxTotal: 400
31 | retryTime: 1
32 | retryInterval: 10000
33 | staleConnectionCheckEnabled: false
34 | timeoutConnection: 50000
35 | timeoutSocket: 50000
36 | validateAfterInactivity: 200000
37 | dslfile:
38 | refreshInterval: -1
39 | logs:
40 | name: logs
41 | elasticPassword: changeme
42 | elasticUser: elastic
43 | elasticsearch:
44 | dateFormat: yyyy.MM.dd
45 | discoverHost: false
46 | rest:
47 | hostNames: 192.168.137.1:9200
48 | scrollBlockedWaitTimeout: 0
49 | scrollThreadCount: 200
50 | scrollThreadQueue: 200
51 | showTemplate: true
52 | sliceScrollBlockedWaitTimeout: 0
53 | sliceScrollThreadCount: 100
54 | sliceScrollThreadQueue: 100
55 | timeZone: Asia/Shanghai
56 | http:
57 | connectionRequestTimeout: 50000
58 | customHttpRequestRetryHandler: org.frameworkset.spi.remote.http.ConnectionResetHttpRequestRetryHandler
59 | defaultMaxPerRoute: 200
60 | hostnameVerifier: ''
61 | keyPassword: ''
62 | keystore: ''
63 | maxHeaderCount: 200
64 | maxLineLength: -1
65 | maxTotal: 400
66 | retryTime: 1
67 | staleConnectionCheckEnabled: false
68 | timeoutConnection: 50000
69 | timeoutSocket: 50000
70 | validateAfterInactivity: 200000
71 |
72 |
--------------------------------------------------------------------------------
/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | elasticsearch:
3 | bboss:
4 | elasticPassword: changeme
5 | elasticUser: elastic
6 | elasticsearch:
7 | dateFormat: yyyy.MM.dd
8 | discoverHost: false
9 | rest:
10 | hostNames: 192.168.137.1:9200
11 | scrollBlockedWaitTimeout: 0
12 | scrollThreadCount: 200
13 | scrollThreadQueue: 200
14 | showTemplate: true
15 | sliceScrollBlockedWaitTimeout: 0
16 | sliceScrollThreadCount: 100
17 | sliceScrollThreadQueue: 100
18 | http:
19 | connectionRequestTimeout: 50000
20 | customHttpRequestRetryHandler: org.frameworkset.spi.remote.http.ConnectionResetHttpRequestRetryHandler
21 | defaultMaxPerRoute: 200
22 | hostnameVerifier:
23 | keepAlive: 3600000
24 | keyPassword:
25 | keystore:
26 | maxHeaderCount: 200
27 | maxLineLength: -1
28 | maxTotal: 400
29 | retryTime: 1
30 | retryInterval: 1000
31 | soKeepAlive: false
32 | soReuseAddress: false
33 | staleConnectionCheckEnabled: false
34 | timeToLive: 3600000
35 | timeoutConnection: 50000
36 | timeoutSocket: 50000
37 | validateAfterInactivity: 50000
38 | db:
39 | driver: com.mysql.jdbc.Driver
40 | name: test
41 | password: 123456
42 | url: jdbc:mysql://localhost:3306/bboss
43 | usePool: false
44 | user: root
45 | validateSQL: select 1
46 | dslfile:
47 | refreshInterval: -1
48 | ##dslMappingDir: D:/workdir/es
49 | propertiesInterceptor: org.bboss.elasticsearchtest.springboot.TestPropertiesInterceptor
50 | # 添加 log4j2 配置
51 | logging:
52 | config: classpath:log4j2.yaml
--------------------------------------------------------------------------------
/src/main/resources/config/application-multi-datasource.properties.example:
--------------------------------------------------------------------------------
1 | ##多集群配置样例,如果需要做多集群配置,请将参照本文内容修改application.properties文件内容
2 | spring.elasticsearch.bboss.default.name = default
3 | ##default集群配配置
4 | spring.elasticsearch.bboss.default.elasticUser=elastic
5 | spring.elasticsearch.bboss.default.elasticPassword=changeme
6 |
7 | spring.elasticsearch.bboss.default.elasticsearch.rest.hostNames=10.21.20.168:9200
8 | #spring.elasticsearch.bboss.default.elasticsearch.rest.hostNames=10.180.211.27:9280,10.180.211.27:9281,10.180.211.27:9282
9 | ##https配置
10 | #spring.elasticsearch.bboss.default.elasticsearch.rest.hostNames=https://10.180.211.27:9280,https://10.180.211.27:9281,https://10.180.211.27:9282
11 | spring.elasticsearch.bboss.default.elasticsearch.dateFormat=yyyy.MM.dd
12 | spring.elasticsearch.bboss.default.elasticsearch.timeZone=Asia/Shanghai
13 | #在控制台输出脚本调试开关showTemplate,false关闭,true打开,同时log4j至少是info级别
14 | spring.elasticsearch.bboss.default.elasticsearch.showTemplate=true
15 | spring.elasticsearch.bboss.default.elasticsearch.discoverHost=false
16 |
17 | #设置slice scroll查询对应的线程数和等待队列数
18 | spring.elasticsearch.bboss.default.elasticsearch.sliceScrollThreadCount=100
19 | spring.elasticsearch.bboss.default.elasticsearch.sliceScrollThreadQueue=100
20 | spring.elasticsearch.bboss.default.elasticsearch.sliceScrollBlockedWaitTimeout=0
21 |
22 | #设置scroll查询对应的线程数和等待队列数
23 | spring.elasticsearch.bboss.default.elasticsearch.scrollThreadCount=200
24 | spring.elasticsearch.bboss.default.elasticsearch.scrollThreadQueue=200
25 | spring.elasticsearch.bboss.default.elasticsearch.scrollBlockedWaitTimeout=0
26 |
27 | ##default连接池配置
28 | spring.elasticsearch.bboss.default.http.timeoutConnection = 5000
29 | spring.elasticsearch.bboss.default.http.timeoutSocket = 5000
30 | spring.elasticsearch.bboss.default.http.connectionRequestTimeout=5000
31 | spring.elasticsearch.bboss.default.http.retryTime = 1
32 | spring.elasticsearch.bboss.default.http.retryInterval = 1000
33 | spring.elasticsearch.bboss.default.http.maxLineLength = -1
34 | spring.elasticsearch.bboss.default.http.maxHeaderCount = 200
35 | spring.elasticsearch.bboss.default.http.maxTotal = 400
36 | spring.elasticsearch.bboss.default.http.defaultMaxPerRoute = 200
37 | spring.elasticsearch.bboss.default.http.keystore =
38 | spring.elasticsearch.bboss.default.http.keyPassword =
39 | # ssl 主机名称校验,是否采用default配置,
40 | # 如果指定为default,就采用DefaultHostnameVerifier,否则采用 SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER
41 | spring.elasticsearch.bboss.default.http.hostnameVerifier =
42 | #每隔多少毫秒校验空闲connection,自动释放无效链接
43 | # -1 或者0不检查
44 | spring.elasticsearch.bboss.default.http.validateAfterInactivity=2000
45 | # 每次获取connection时校验连接,true,校验,false不校验,有性能开销,推荐采用
46 | # validateAfterInactivity来控制连接是否有效
47 | # 默认值false
48 | spring.elasticsearch.bboss.default.http.staleConnectionCheckEnabled=false
49 | #* 自定义重试控制接口,必须实现接口方法
50 | #* public interface CustomHttpRequestRetryHandler {
51 | #* public boolean retryRequest(IOException exception, int executionCount, HttpContext context,ClientConfiguration configuration);
52 | #* }
53 | #* 方法返回true,进行重试,false不重试
54 | spring.elasticsearch.bboss.default.http.customHttpRequestRetryHandler=org.frameworkset.spi.remote.http.ConnectionResetHttpRequestRetryHandler
55 |
56 |
57 | ##logs集群配置
58 | spring.elasticsearch.bboss.logs.name = logs
59 | spring.elasticsearch.bboss.logs.elasticUser=elastic
60 | spring.elasticsearch.bboss.logs.elasticPassword=changeme
61 |
62 | #elasticsearch.rest.hostNames=10.1.236.88:9200
63 | spring.elasticsearch.bboss.logs.elasticsearch.rest.hostNames=10.1.236.88:9200
64 | #elasticsearch.rest.hostNames=10.21.20.168:9200
65 | #elasticsearch.rest.hostNames=10.180.211.27:9280,10.180.211.27:9281,10.180.211.27:9282
66 | spring.elasticsearch.bboss.logs.elasticsearch.dateFormat=yyyy.MM.dd
67 | spring.elasticsearch.bboss.logs.elasticsearch.timeZone=Asia/Shanghai
68 | #在控制台输出脚本调试开关showTemplate,false关闭,true打开,同时log4j至少是info级别
69 | spring.elasticsearch.bboss.logs.elasticsearch.showTemplate=true
70 | spring.elasticsearch.bboss.logs.elasticsearch.discoverHost=false
71 |
72 | #设置slice scroll查询对应的线程数和等待队列数
73 | spring.elasticsearch.bboss.logs.elasticsearch.sliceScrollThreadCount=100
74 | spring.elasticsearch.bboss.logs.elasticsearch.sliceScrollThreadQueue=100
75 | spring.elasticsearch.bboss.logs.elasticsearch.sliceScrollBlockedWaitTimeout=0
76 |
77 | #设置scroll查询对应的线程数和等待队列数
78 | spring.elasticsearch.bboss.logs.elasticsearch.scrollThreadCount=200
79 | spring.elasticsearch.bboss.logs.elasticsearch.scrollThreadQueue=200
80 | spring.elasticsearch.bboss.logs.elasticsearch.scrollBlockedWaitTimeout=0
81 |
82 | ##logs集群对应的连接池配置
83 | spring.elasticsearch.bboss.logs.http.timeoutConnection = 5000
84 | spring.elasticsearch.bboss.logs.http.timeoutSocket = 5000
85 | spring.elasticsearch.bboss.logs.http.connectionRequestTimeout=5000
86 | spring.elasticsearch.bboss.logs.http.retryTime = 1
87 | spring.elasticsearch.bboss.logs.http.retryInterval = 1000
88 | spring.elasticsearch.bboss.logs.http.maxLineLength = -1
89 | spring.elasticsearch.bboss.logs.http.maxHeaderCount = 200
90 | spring.elasticsearch.bboss.logs.http.maxTotal = 400
91 | spring.elasticsearch.bboss.logs.http.defaultMaxPerRoute = 200
92 | # https证书配置
93 | spring.elasticsearch.bboss.logs.http.keystore =
94 | spring.elasticsearch.bboss.logs.http.keyPassword =
95 | # ssl 主机名称校验,是否采用default配置,
96 | # 如果指定为default,就采用DefaultHostnameVerifier,否则采用 SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER
97 | spring.elasticsearch.bboss.logs.http.hostnameVerifier =
98 | #每隔多少毫秒校验空闲connection,自动释放无效链接
99 | # -1 或者0不检查
100 | spring.elasticsearch.bboss.logs.http.validateAfterInactivity=2000
101 | # 每次获取connection时校验连接,true,校验,false不校验,有性能开销,推荐采用
102 | # validateAfterInactivity来控制连接是否有效
103 | # 默认值false
104 | spring.elasticsearch.bboss.logs.http.staleConnectionCheckEnabled=false
105 | #* 自定义重试控制接口,必须实现接口方法
106 | #* public interface CustomHttpRequestRetryHandler {
107 | #* public boolean retryRequest(IOException exception, int executionCount, HttpContext context,ClientConfiguration configuration);
108 | #* }
109 | #* 方法返回true,进行重试,false不重试
110 | spring.elasticsearch.bboss.logs.http.customHttpRequestRetryHandler=org.frameworkset.spi.remote.http.ConnectionResetHttpRequestRetryHandler
111 |
112 |
113 | # dsl配置文件热加载扫描时间间隔,毫秒为单位,默认5秒扫描一次,<= 0时关闭扫描机制
114 | spring.elasticsearch.bboss.dslfile.refreshInterval = -1
--------------------------------------------------------------------------------
/src/main/resources/config/application.properties.example:
--------------------------------------------------------------------------------
1 | ##ES集群配置,支持x-pack和searchguard
2 | spring.elasticsearch.bboss.elasticUser=elastic
3 | spring.elasticsearch.bboss.elasticPassword=changeme
4 |
5 |
6 | spring.elasticsearch.bboss.elasticsearch.rest.hostNames=127.0.0.1:9200
7 | #spring.elasticsearch.bboss.elasticsearch.rest.hostNames=10.180.211.27:9280,10.180.211.27:9281,10.180.211.27:9282
8 | ##https配置,添加https://协议头
9 | #spring.elasticsearch.bboss.default.elasticsearch.rest.hostNames=https://10.180.211.27:9280,https://10.180.211.27:9281,https://10.180.211.27:9282
10 | spring.elasticsearch.bboss.elasticsearch.dateFormat=yyyy.MM.dd
11 | spring.elasticsearch.bboss.elasticsearch.timeZone=Asia/Shanghai
12 | #在控制台输出脚本调试开关showTemplate,false关闭,true打开,同时log4j至少是info级别
13 | spring.elasticsearch.bboss.elasticsearch.showTemplate=true
14 | spring.elasticsearch.bboss.elasticsearch.discoverHost=false
15 | # dsl配置文件热加载扫描时间间隔,毫秒为单位,默认5秒扫描一次,<= 0时关闭扫描机制
16 | spring.elasticsearch.bboss.dslfile.refreshInterval = -1
17 |
18 |
19 | #设置slice scroll查询对应的线程数和等待队列数
20 | spring.elasticsearch.bboss.elasticsearch.sliceScrollThreadCount=100
21 | spring.elasticsearch.bboss.elasticsearch.sliceScrollThreadQueue=100
22 | spring.elasticsearch.bboss.elasticsearch.sliceScrollBlockedWaitTimeout=0
23 |
24 | #设置scroll查询对应的线程数和等待队列数
25 | spring.elasticsearch.bboss.elasticsearch.scrollThreadCount=200
26 | spring.elasticsearch.bboss.elasticsearch.scrollThreadQueue=200
27 | spring.elasticsearch.bboss.elasticsearch.scrollBlockedWaitTimeout=0
28 |
29 | ##es client http连接池配置
30 | spring.elasticsearch.bboss.http.timeoutConnection = 5000
31 | spring.elasticsearch.bboss.http.timeoutSocket = 5000
32 | spring.elasticsearch.bboss.http.connectionRequestTimeout=5000
33 | spring.elasticsearch.bboss.http.retryTime = 1
34 | spring.elasticsearch.bboss.http.retryInterval = 1000
35 | spring.elasticsearch.bboss.http.maxLineLength = -1
36 | spring.elasticsearch.bboss.http.maxHeaderCount = 200
37 | spring.elasticsearch.bboss.http.maxTotal = 400
38 | spring.elasticsearch.bboss.http.defaultMaxPerRoute = 200
39 | spring.elasticsearch.bboss.http.soReuseAddress = false
40 | spring.elasticsearch.bboss.http.soKeepAlive = false
41 | spring.elasticsearch.bboss.http.timeToLive = 3600000
42 | spring.elasticsearch.bboss.http.keepAlive = 3600000
43 | spring.elasticsearch.bboss.http.keystore =
44 | spring.elasticsearch.bboss.http.keyPassword =
45 | # ssl 主机名称校验,是否采用default配置,
46 | # 如果指定为default,就采用DefaultHostnameVerifier,否则采用 SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER
47 | spring.elasticsearch.bboss.http.hostnameVerifier =
48 |
49 | #每隔多少毫秒校验空闲connection,自动释放无效链接
50 | # -1 或者0不检查
51 | spring.elasticsearch.bboss.http.validateAfterInactivity=2000
52 | # 每次获取connection时校验连接,true,校验,false不校验,有性能开销,推荐采用
53 | # validateAfterInactivity来控制连接是否有效
54 | # 默认值false
55 | spring.elasticsearch.bboss.http.staleConnectionCheckEnabled=false
56 | #* 自定义重试控制接口,必须实现接口方法
57 | #* public interface CustomHttpRequestRetryHandler {
58 | #* public boolean retryRequest(IOException exception, int executionCount, HttpContext context,ClientConfiguration configuration);
59 | #* }
60 | #* 方法返回true,进行重试,false不重试
61 | spring.elasticsearch.bboss.http.customHttpRequestRetryHandler=org.frameworkset.spi.remote.http.ConnectionResetHttpRequestRetryHandler
62 |
63 |
64 | # 数据库数据源配置
65 | spring.elasticsearch.bboss.db.name = test
66 | spring.elasticsearch.bboss.db.user = root
67 | spring.elasticsearch.bboss.db.password = 123456
68 | spring.elasticsearch.bboss.db.driver = com.mysql.jdbc.Driver
69 | spring.elasticsearch.bboss.db.url = jdbc:mysql://localhost:3306/bboss
70 | spring.elasticsearch.bboss.db.usePool = false
71 | spring.elasticsearch.bboss.db.validateSQL = select 1
72 |
73 |
--------------------------------------------------------------------------------
/src/main/resources/esmapper/scroll.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
15 |
16 |
19 |
20 |
33 |
34 |
--------------------------------------------------------------------------------
/src/main/resources/log4j2.yaml:
--------------------------------------------------------------------------------
1 | Configuration:
2 | status: WARN
3 | Appenders:
4 | Console:
5 | name: Console
6 | target: SYSTEM_OUT
7 | PatternLayout:
8 | pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
9 | RollingFile:
10 | name: RollingFile
11 | fileName: eshelloword-spring-boot-starter.log
12 | filePattern: "eshelloword-spring-boot-starter-%d{yyyy-MM-dd}-%i.log"
13 | PatternLayout:
14 | pattern: "[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%p] - %l - %m%n"
15 | Policies:
16 | # TimeBasedTriggeringPolicy:
17 | SizeBasedTriggeringPolicy:
18 | size: 10 MB
19 | DefaultRolloverStrategy:
20 | max: 5
21 | Loggers:
22 | Root:
23 | level: INFO
24 | AppenderRef:
25 | - ref: Console
26 | - ref: RollingFile
27 |
--------------------------------------------------------------------------------
/src/test/java/org/bboss/elasticsearchtest/springboot/BBossES7StarterTestCase.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 1999-2018 Alibaba Group Holding Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.bboss.elasticsearchtest.springboot;
17 |
18 |
19 | import org.bboss.elasticsearchtest.springboot.crud.DocumentCRUD7;
20 | import org.junit.Test;
21 | import org.junit.runner.RunWith;
22 | import org.springframework.beans.factory.annotation.Autowired;
23 | import org.springframework.boot.test.context.SpringBootTest;
24 | import org.springframework.test.context.junit4.SpringRunner;
25 |
26 | /**
27 | * 单集群演示功能测试用例,spring boot配置项以spring.elasticsearch.bboss开头
28 | * 对应的配置文件为application.properties文件
29 | * @author yinbp [122054810@qq.com]
30 | */
31 | @RunWith(SpringRunner.class)
32 | @SpringBootTest
33 | public class BBossES7StarterTestCase {
34 |
35 | @Autowired
36 | DocumentCRUD7 documentCRUD;
37 |
38 | @Test
39 | public void testBbossESStarter() throws Exception {
40 | // System.out.println(bbossESStarter);
41 |
42 | //验证环境,获取es状态
43 | // String response = serviceApiUtil.restClient().executeHttp("_cluster/state?pretty",ClientInterface.HTTP_GET);
44 |
45 | // System.out.println(response);
46 | //判断索引类型是否存在,false表示不存在,正常返回true表示存在
47 | // boolean exist = bbossESStarter.getRestClient().existIndiceType("twitter","_doc");
48 |
49 | //判读索引是否存在,false表示不存在,正常返回true表示存在
50 | documentCRUD.testBbossESStarter();
51 |
52 | }
53 | /** should install plugin in elasticsearch to run follow method :
54 | * https://github.com/NLPchina/elasticsearch-sql
55 | @Test
56 | public void testESSQLTranslate(){
57 | ClientInterface clientUtil = bbossESStarter.getRestClient();
58 | String dsl = //将sql转换为dsl
59 | clientUtil.executeHttp("/_sql/_explain",//sql请求
60 | "select operModule.keyword from dbdemo group by operModule.keyword ",ClientInterface.HTTP_POST);//返回的文档封装对象类型
61 |
62 | //获取总记录数
63 | System.out.println(dsl);
64 | }
65 | */
66 | @Test
67 | public void testCRUD() throws Exception {
68 |
69 | //删除/创建文档索引表
70 | documentCRUD.testCreateIndice();
71 | //添加/修改单个文档
72 |
73 | documentCRUD.testAddAndUpdateDocument();
74 | //批量添加文档
75 | documentCRUD.testBulkAddDocument();
76 | //检索文档
77 | documentCRUD.testSearch();
78 | //批量修改文档
79 | documentCRUD.testBulkUpdateDocument();
80 |
81 | //检索批量修改后的文档
82 | documentCRUD.testSearch();
83 | //带list复杂参数的文档检索操作
84 | documentCRUD.testSearchArray();
85 | //带from/size分页操作的文档检索操作
86 | documentCRUD.testPagineSearch();
87 | //带sourcefilter的文档检索操作
88 | documentCRUD.testSearchSourceFilter();
89 |
90 | documentCRUD.updateDemoIndice();
91 | documentCRUD.testBulkAddDocuments();
92 | }
93 |
94 | @Test
95 | public void testPerformaceCRUD() throws Exception {
96 |
97 | //删除/创建文档索引表
98 | documentCRUD.testCreateIndice();
99 |
100 | documentCRUD.testBulkAddDocuments();
101 | }
102 |
103 | }
104 |
--------------------------------------------------------------------------------
/src/test/java/org/bboss/elasticsearchtest/springboot/BBossESStarterTestCase.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 1999-2018 Alibaba Group Holding Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.bboss.elasticsearchtest.springboot;
17 |
18 |
19 | import org.bboss.elasticsearchtest.springboot.crud.DocumentCRUD;
20 | import org.frameworkset.elasticsearch.boot.BBossESStarter;
21 | import org.frameworkset.elasticsearch.client.ClientInterface;
22 | import org.junit.Test;
23 | import org.junit.runner.RunWith;
24 | import org.springframework.beans.factory.annotation.Autowired;
25 | import org.springframework.boot.test.context.SpringBootTest;
26 | import org.springframework.test.context.junit4.SpringRunner;
27 |
28 | /**
29 | * 单集群演示功能测试用例,spring boot配置项以spring.elasticsearch.bboss开头
30 | * 对应的配置文件为application.properties文件
31 | * @author yinbp [122054810@qq.com]
32 | */
33 | @RunWith(SpringRunner.class)
34 | @SpringBootTest
35 | public class BBossESStarterTestCase {
36 | @Autowired
37 | private BBossESStarter bbossESStarter;
38 | @Autowired
39 | DocumentCRUD documentCRUD;
40 |
41 | @Test
42 | public void testBbossESStarter() throws Exception {
43 | // System.out.println(bbossESStarter);
44 |
45 | //验证环境,获取es状态
46 | // String response = serviceApiUtil.restClient().executeHttp("_cluster/state?pretty",ClientInterface.HTTP_GET);
47 |
48 | // System.out.println(response);
49 | //判断索引类型是否存在,false表示不存在,正常返回true表示存在
50 | boolean exist = bbossESStarter.getRestClient().existIndiceType("twitter","tweet");
51 |
52 | //判读索引是否存在,false表示不存在,正常返回true表示存在
53 | exist = bbossESStarter.getRestClient().existIndice("twitter");
54 |
55 | exist = bbossESStarter.getRestClient().existIndice("agentinfo");
56 |
57 | }
58 |
59 | @Test
60 | public void testESSQLTranslate(){
61 | ClientInterface clientUtil = bbossESStarter.getRestClient();
62 | String dsl = //将sql转换为dsl
63 | clientUtil.executeHttp("/_sql/_explain",//sql请求
64 | "select operModule.keyword from dbdemo group by operModule.keyword ",ClientInterface.HTTP_POST);//返回的文档封装对象类型
65 |
66 | //获取总记录数
67 | System.out.println(dsl);
68 | }
69 |
70 | @Test
71 | public void testCRUD() throws Exception {
72 |
73 | //删除/创建文档索引表
74 | documentCRUD.testCreateIndice();
75 | //添加/修改单个文档
76 |
77 | documentCRUD.testAddAndUpdateDocument();
78 | //批量添加文档
79 | documentCRUD.testBulkAddDocument();
80 | //检索文档
81 | documentCRUD.testSearch();
82 | //批量修改文档
83 | documentCRUD.testBulkUpdateDocument();
84 |
85 | //检索批量修改后的文档
86 | documentCRUD.testSearch();
87 | //带list复杂参数的文档检索操作
88 | documentCRUD.testSearchArray();
89 | //带from/size分页操作的文档检索操作
90 | documentCRUD.testPagineSearch();
91 | //带sourcefilter的文档检索操作
92 | documentCRUD.testSearchSourceFilter();
93 |
94 | documentCRUD.updateDemoIndice();
95 | documentCRUD.testBulkAddDocuments();
96 | }
97 |
98 | @Test
99 | public void testPerformaceCRUD() throws Exception {
100 |
101 | //删除/创建文档索引表
102 | documentCRUD.testCreateIndice();
103 |
104 | documentCRUD.testBulkAddDocuments();
105 | }
106 |
107 | }
108 |
--------------------------------------------------------------------------------
/src/test/java/org/bboss/elasticsearchtest/springboot/BulkProcessor7Test.java:
--------------------------------------------------------------------------------
1 | package org.bboss.elasticsearchtest.springboot;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import org.bboss.elasticsearchtest.springboot.bulk.TestBulkProcessor7x;
19 | import org.frameworkset.elasticsearch.boot.BBossESStarter;
20 | import org.junit.Test;
21 | import org.junit.runner.RunWith;
22 | import org.slf4j.Logger;
23 | import org.slf4j.LoggerFactory;
24 | import org.springframework.beans.factory.annotation.Autowired;
25 | import org.springframework.boot.test.context.SpringBootTest;
26 | import org.springframework.test.context.junit4.SpringRunner;
27 |
28 | import java.util.ArrayList;
29 |
30 | import static java.lang.Thread.sleep;
31 |
32 | /**
33 | *
Description:
34 | *
35 | * Copyright (c) 2018
36 | * @Date 2019/9/18 10:27
37 | * @author biaoping.yin
38 | * @version 1.0
39 | */
40 | @RunWith(SpringRunner.class)
41 | @SpringBootTest
42 | public class BulkProcessor7Test {
43 | private Logger logger = LoggerFactory.getLogger(BulkProcessor7Test.class);
44 | @Autowired
45 | private BBossESStarter bbossESStarter;
46 | @Autowired
47 | private TestBulkProcessor7x testBulkProcessor7x;
48 | @Test
49 | public void testBulkProcessor7x(){
50 |
51 | testBulkProcessor7x.buildBulkProcessor();
52 | do {
53 |
54 | testBulkProcessor7x.testBulkDatas();
55 | try {
56 | sleep(1000l);
57 | } catch (InterruptedException e) {
58 | e.printStackTrace();
59 | }
60 | }while(true);
61 |
62 |
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/test/java/org/bboss/elasticsearchtest/springboot/BulkProcessorTest.java:
--------------------------------------------------------------------------------
1 | package org.bboss.elasticsearchtest.springboot;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import org.bboss.elasticsearchtest.springboot.bulk.TestBulkProcessor;
19 | import org.frameworkset.elasticsearch.boot.BBossESStarter;
20 | import org.junit.Test;
21 | import org.junit.runner.RunWith;
22 | import org.slf4j.Logger;
23 | import org.slf4j.LoggerFactory;
24 | import org.springframework.beans.factory.annotation.Autowired;
25 | import org.springframework.boot.test.context.SpringBootTest;
26 | import org.springframework.test.context.junit4.SpringRunner;
27 |
28 | /**
29 | *
Description:
30 | *
31 | * Copyright (c) 2018
32 | * @Date 2019/9/18 10:27
33 | * @author biaoping.yin
34 | * @version 1.0
35 | */
36 | @RunWith(SpringRunner.class)
37 | @SpringBootTest
38 | public class BulkProcessorTest {
39 | private Logger logger = LoggerFactory.getLogger(BulkProcessorTest.class);
40 | @Autowired
41 | private BBossESStarter bbossESStarter;
42 | @Autowired
43 | private TestBulkProcessor testBulkProcessor;
44 | @Test
45 | public void testBulkProcessor7x(){
46 | testBulkProcessor.buildBulkProcessor();
47 | testBulkProcessor.testBulkDatas();
48 |
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/test/java/org/bboss/elasticsearchtest/springboot/MultiBBossES7StartersTestCase.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 1999-2018 Alibaba Group Holding Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.bboss.elasticsearchtest.springboot;
17 |
18 |
19 | import org.bboss.elasticsearchtest.springboot.crud.MultiES7DocumentCRUD;
20 | import org.frameworkset.elasticsearch.boot.BBossESStarter;
21 | import org.junit.Test;
22 | import org.junit.runner.RunWith;
23 | import org.springframework.beans.factory.annotation.Autowired;
24 | import org.springframework.beans.factory.annotation.Qualifier;
25 | import org.springframework.boot.test.context.SpringBootTest;
26 | import org.springframework.test.context.ActiveProfiles;
27 | import org.springframework.test.context.junit4.SpringRunner;
28 |
29 | /**
30 | * 多集群演示功能测试用例,spring boot配置项以spring.elasticsearch.bboss.集群名称开头,例如:
31 | * spring.elasticsearch.bboss.default 默认es集群
32 | * spring.elasticsearch.bboss.logs logs es集群
33 | * 两个集群通过 org.bboss.elasticsearchtest.springboot.MultiESSTartConfigurer加载
34 | * 对应的配置文件为application-multi-datasource.properties文件
35 | * @author yinbp [122054810@qq.com]
36 | */
37 | @RunWith(SpringRunner.class)
38 | @SpringBootTest
39 | @ActiveProfiles("multi-datasource")
40 | public class MultiBBossES7StartersTestCase {
41 | @Autowired
42 | @Qualifier("bbossESStarterDefault")
43 | private BBossESStarter bbossESStarterDefault;
44 | @Autowired
45 | @Qualifier("bbossESStarterLogs")
46 | private BBossESStarter bbossESStarterLogs;
47 |
48 | @Autowired
49 | MultiES7DocumentCRUD multiESDocumentCRUD;
50 | @Test
51 | public void testMultiBBossESStarters() throws Exception {
52 |
53 |
54 |
55 | //获取logs对应的Elasticsearch集群客户端,判读索引是否存在,false表示不存在,正常返回true表示存在
56 | boolean exist = bbossESStarterDefault.getRestClient().existIndice("twitter");
57 | System.out.println("logs twitter:"+exist);
58 | //获取logs对应的Elasticsearch集群客户端,判断索引是否定义
59 | exist = bbossESStarterDefault.getRestClient("logs").existIndice("agentinfo");
60 | System.out.println("logs agentinfo:"+exist);
61 | }
62 |
63 | @Test
64 | public void testCRUD() throws Exception {
65 |
66 | //删除/创建文档索引表
67 | multiESDocumentCRUD.testCreateIndice();
68 | //添加/修改单个文档
69 |
70 | multiESDocumentCRUD.testAddAndUpdateDocument();
71 | //批量添加文档
72 | multiESDocumentCRUD.testBulkAddDocument();
73 | //检索文档,测试指定数据源api,指定数据源default
74 | multiESDocumentCRUD.testSearch("default");
75 | //批量修改文档
76 | multiESDocumentCRUD.testBulkUpdateDocument();
77 |
78 | //检索批量修改后的文档,测试指定数据源api,指定数据源log
79 | multiESDocumentCRUD.testSearch("logs");
80 | //带list复杂参数的文档检索操作
81 | multiESDocumentCRUD.testSearchArray();
82 | //带from/size分页操作的文档检索操作
83 | multiESDocumentCRUD.testPagineSearch();
84 | //带sourcefilter的文档检索操作
85 | multiESDocumentCRUD.testSearchSourceFilter();
86 |
87 | multiESDocumentCRUD.updateDemoIndice();
88 | multiESDocumentCRUD.testBulkAddDocuments();
89 | }
90 |
91 | @Test
92 | public void testPerformaceCRUD() throws Exception {
93 |
94 | //删除/创建文档索引表
95 | multiESDocumentCRUD.testCreateIndice();
96 |
97 | multiESDocumentCRUD.testBulkAddDocuments();
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/src/test/java/org/bboss/elasticsearchtest/springboot/MultiBBossESStartersTestCase.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 1999-2018 Alibaba Group Holding Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.bboss.elasticsearchtest.springboot;
17 |
18 |
19 | import org.bboss.elasticsearchtest.springboot.crud.MultiESDocumentCRUD;
20 | import org.frameworkset.elasticsearch.boot.BBossESStarter;
21 | import org.junit.Test;
22 | import org.junit.runner.RunWith;
23 | import org.springframework.beans.factory.annotation.Autowired;
24 | import org.springframework.beans.factory.annotation.Qualifier;
25 | import org.springframework.boot.test.context.SpringBootTest;
26 | import org.springframework.test.context.ActiveProfiles;
27 | import org.springframework.test.context.junit4.SpringRunner;
28 |
29 | /**
30 | * 多集群演示功能测试用例,spring boot配置项以spring.elasticsearch.bboss.集群名称开头,例如:
31 | * spring.elasticsearch.bboss.default 默认es集群
32 | * spring.elasticsearch.bboss.logs logs es集群
33 | * 两个集群通过 org.bboss.elasticsearchtest.springboot.MultiESSTartConfigurer加载
34 | * 对应的配置文件为application-multi-datasource.properties文件
35 | * @author yinbp [122054810@qq.com]
36 | */
37 | @RunWith(SpringRunner.class)
38 | @SpringBootTest
39 | @ActiveProfiles("multi-datasource")
40 | public class MultiBBossESStartersTestCase {
41 | @Autowired
42 | @Qualifier("bbossESStarterDefault")
43 | private BBossESStarter bbossESStarterDefault;
44 | @Autowired
45 | @Qualifier("bbossESStarterLogs")
46 | private BBossESStarter bbossESStarterLogs;
47 | @Autowired
48 | MultiESDocumentCRUD multiESDocumentCRUD;
49 | @Test
50 | public void testMultiBBossESStarters() throws Exception {
51 |
52 | //验证环境,获取es状态
53 | // String response = bbossESStarterDefault.getRestClient().executeHttp("_cluster/state?pretty",ClientInterface.HTTP_GET);
54 | // System.out.println(response);
55 |
56 |
57 | //判断索引类型是否存在,false表示不存在,正常返回true表示存在
58 | boolean exist = bbossESStarterDefault.getRestClient().existIndiceType("twitter","tweet");
59 | System.out.println("default twitter/tweet:"+exist);
60 | //获取logs对应的Elasticsearch集群客户端,并进行existIndiceType操作
61 | exist = bbossESStarterDefault.getRestClient("logs").existIndiceType("twitter","tweet");
62 | System.out.println("logs twitter/tweet:"+exist);
63 | //获取logs对应的Elasticsearch集群客户端,判读索引是否存在,false表示不存在,正常返回true表示存在
64 | exist = bbossESStarterDefault.getRestClient("logs").existIndice("twitter");
65 | System.out.println("logs twitter:"+exist);
66 | //获取logs对应的Elasticsearch集群客户端,判断索引是否定义
67 | exist = bbossESStarterDefault.getRestClient("logs").existIndice("agentinfo");
68 | System.out.println("logs agentinfo:"+exist);
69 | }
70 |
71 | @Test
72 | public void testCRUD() throws Exception {
73 |
74 | //删除/创建文档索引表
75 | multiESDocumentCRUD.testCreateIndice();
76 | //添加/修改单个文档
77 |
78 | multiESDocumentCRUD.testAddAndUpdateDocument();
79 | //批量添加文档
80 | multiESDocumentCRUD.testBulkAddDocument();
81 | //检索文档
82 | multiESDocumentCRUD.testSearch();
83 | //批量修改文档
84 | multiESDocumentCRUD.testBulkUpdateDocument();
85 |
86 | //检索批量修改后的文档
87 | multiESDocumentCRUD.testSearch();
88 | //带list复杂参数的文档检索操作
89 | multiESDocumentCRUD.testSearchArray();
90 | //带from/size分页操作的文档检索操作
91 | multiESDocumentCRUD.testPagineSearch();
92 | //带sourcefilter的文档检索操作
93 | multiESDocumentCRUD.testSearchSourceFilter();
94 |
95 | multiESDocumentCRUD.updateDemoIndice();
96 | multiESDocumentCRUD.testBulkAddDocuments();
97 | }
98 |
99 | @Test
100 | public void testPerformaceCRUD() throws Exception {
101 |
102 | //删除/创建文档索引表
103 | multiESDocumentCRUD.testCreateIndice();
104 |
105 | multiESDocumentCRUD.testBulkAddDocuments();
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/src/test/java/org/bboss/elasticsearchtest/springboot/MultiES7RestClientTest.java:
--------------------------------------------------------------------------------
1 | package org.bboss.elasticsearchtest.springboot;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import com.example.esbboss.entity.Demo;
19 | import com.example.esbboss.entity.DemoSearchResult;
20 | import org.frameworkset.elasticsearch.ElasticSearchException;
21 | import org.frameworkset.elasticsearch.boot.BBossESStarter;
22 | import org.frameworkset.elasticsearch.client.ClientInterface;
23 | import org.frameworkset.elasticsearch.entity.ESDatas;
24 | import org.junit.Test;
25 | import org.junit.runner.RunWith;
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 | import org.springframework.beans.factory.annotation.Autowired;
29 | import org.springframework.boot.test.context.SpringBootTest;
30 | import org.springframework.test.context.ActiveProfiles;
31 | import org.springframework.test.context.junit4.SpringRunner;
32 |
33 | import java.text.DateFormat;
34 | import java.text.ParseException;
35 | import java.text.SimpleDateFormat;
36 | import java.util.Date;
37 | import java.util.HashMap;
38 | import java.util.List;
39 | import java.util.Map;
40 |
41 | /**
42 | * 多集群演示功能测试用例,spring boot配置项以spring.elasticsearch.bboss.集群名称开头,例如:
43 | * spring.elasticsearch.bboss.default 默认es集群
44 | * spring.elasticsearch.bboss.logs logs es集群
45 | * 两个集群通过 org.bboss.elasticsearchtest.springboot.MultiESSTartConfigurer加载
46 | * 对应的配置文件为application-multi-datasource.properties文件
47 | */
48 | @RunWith(SpringRunner.class)
49 | @SpringBootTest
50 | @ActiveProfiles("multi-datasource")
51 | public class MultiES7RestClientTest {
52 | private final Logger logger = LoggerFactory.getLogger(MultiES7RestClientTest.class);
53 | @Autowired
54 | private BBossESStarter bbossESStarterDefault;
55 | //DSL config file path
56 | private final String mappath = "esmapper/demo7.xml";
57 | @Test
58 | public void testMultiBBossESStarters() throws Exception {
59 |
60 | //验证环境,获取es状态
61 | // String response = bbossESStarterDefaultDefault.getRestClient().executeHttp("_cluster/state?pretty",ClientInterface.HTTP_GET);
62 | // System.out.println(response);
63 |
64 |
65 | //判断索引类型是否存在,false表示不存在,正常返回true表示存在
66 | boolean exist = bbossESStarterDefault.getRestClient().existIndiceType("twitter","tweet");
67 | System.out.println("default twitter/tweet:"+exist);
68 | //获取logs对应的Elasticsearch集群客户端,并进行existIndiceType操作
69 | exist = bbossESStarterDefault.getRestClient("logs").existIndiceType("twitter","tweet");
70 | System.out.println("logs twitter/tweet:"+exist);
71 | //获取logs对应的Elasticsearch集群客户端,判读索引是否存在,false表示不存在,正常返回true表示存在
72 | exist = bbossESStarterDefault.getRestClient("logs").existIndice("twitter");
73 | System.out.println("logs twitter:"+exist);
74 | //获取logs对应的Elasticsearch集群客户端,判断索引是否定义
75 | exist = bbossESStarterDefault.getRestClient("logs").existIndice("agentinfo");
76 | System.out.println("logs agentinfo:"+exist);
77 | }
78 | @Test
79 | public void test(){
80 | this.dropAndCreateAndGetIndice();
81 | addAndUpdateDocument();
82 | searchAllPararrel();
83 | this.search();
84 | this.deleteDocuments();
85 | }
86 | public void dropAndCreateAndGetIndice(){
87 | //Create a client tool to load configuration files, single instance multithreaded security
88 | ClientInterface clientUtil = bbossESStarterDefault.getConfigRestClient("default",mappath);
89 | try {
90 | //To determine whether the indice demo exists, it returns true if it exists and false if it does not
91 | boolean exist = clientUtil.existIndice("demo");
92 |
93 | //Delete mapping if the indice demo already exists
94 | if(exist) {
95 | String r = clientUtil.dropIndice("demo");
96 | logger.debug("clientUtil.dropIndice(\"demo\") response:"+r);
97 |
98 | }
99 | //Create index demo
100 | clientUtil.createIndiceMapping("demo",//The indice name
101 | "createDemoIndice");//Index mapping DSL script name, defined createDemoIndice in esmapper/demo.xml
102 |
103 | String demoIndice = clientUtil.getIndice("demo");//Gets the newly created indice structure
104 | logger.info("after createIndiceMapping clientUtil.getIndice(\"demo\") response:"+demoIndice);
105 | } catch (ElasticSearchException e) {
106 | // TODO Auto-generated catch block
107 | e.printStackTrace();
108 | }
109 |
110 | }
111 |
112 |
113 |
114 | public void addAndUpdateDocument() {
115 | //Build a create/modify/get/delete document client object, single instance multi-thread security
116 | ClientInterface clientUtil = bbossESStarterDefault.getRestClient();
117 | //Build an object as index document
118 | Demo demo = new Demo();
119 | demo.setDemoId(2l);//Specify the document id, the unique identity, and mark with the @ESId annotation. If the demoId already exists, modify the document; otherwise, add the document
120 | demo.setAgentStarttime(new Date());
121 | demo.setApplicationName("blackcatdemo2");
122 | demo.setContentbody("this is content body2");
123 | demo.setName("liudehua");
124 | demo.setOrderId("NFZF15045871807281445364228");
125 | demo.setContrastStatus(2);
126 |
127 |
128 | //Add the document and force refresh
129 | String response = clientUtil.addDocument("demo",//indice name
130 | demo,"refresh=true");
131 |
132 |
133 |
134 | logger.debug("Print the result:addDocument-------------------------");
135 | logger.debug(response);
136 |
137 | demo = new Demo();
138 | demo.setDemoId(3l);//Specify the document id, the unique identity, and mark with the @ESId annotation. If the demoId already exists, modify the document; otherwise, add the document
139 | demo.setAgentStarttime(new Date());
140 | demo.setApplicationName("blackcatdemo3");
141 | demo.setContentbody("this is content body3");
142 | demo.setName("zhangxueyou");
143 | demo.setOrderId("NFZF15045871807281445364228");
144 | demo.setContrastStatus(3);
145 | demo.setAgentStarttime(new Date());
146 |
147 | //Add the document and force refresh
148 | response = clientUtil.addDocument("demo",//indice name
149 | demo,"refresh=true");
150 |
151 | //Get the document object according to the document id, and return the Demo object
152 | demo = clientUtil.getDocument("demo",//indice name
153 | "2",//document id
154 | Demo.class);
155 |
156 | //update document
157 | demo = new Demo();
158 | demo.setDemoId(2l);//Specify the document id, the unique identity, and mark with the @ESId annotation. If the demoId already exists, modify the document; otherwise, add the document
159 | demo.setAgentStarttime(new Date());
160 | demo.setApplicationName("blackcatdemo2");
161 | demo.setContentbody("this is modify content body2");
162 | demo.setName("刘德华modify\t");
163 | demo.setOrderId("NFZF15045871807281445364228");
164 | demo.setContrastStatus(2);
165 | //Execute update and force refresh
166 | response = clientUtil.addDocument("demo",//index name
167 | demo,"refresh=true");
168 |
169 |
170 | //Get the modified document object according to the document id and return the json message string
171 | response = clientUtil.getDocument("demo",//indice name
172 | "2");//document id
173 | logger.debug("Print the modified result:getDocument-------------------------");
174 | logger.debug(response);
175 |
176 |
177 |
178 |
179 | logger.debug("Print the modified result:getDocument-------------------------");
180 | logger.debug(response);
181 |
182 |
183 | }
184 |
185 | public void deleteDocuments(){
186 | //Build a create/modify/get/delete document client object, single instance multi-thread security
187 | ClientInterface clientUtil = bbossESStarterDefault.getRestClient();
188 | //Batch delete documents
189 | clientUtil.deleteDocuments("demo",//indice name
190 | new String[]{"2","3"});//Batch delete document ids
191 | }
192 |
193 | /**
194 | * Use slice parallel scoll query all documents of indice demo by 2 thread tasks. DEFAULT_FETCHSIZE is 5000
195 | */
196 | public void searchAllPararrel(){
197 | ClientInterface clientUtil = bbossESStarterDefault.getRestClient();
198 | ESDatas esDatas = clientUtil.searchAllParallel("demo", Demo.class,2);
199 | }
200 |
201 |
202 |
203 | /**
204 | * Search the documents
205 | */
206 | public DemoSearchResult search() {
207 | //Create a load DSL file client instance to retrieve documents, single instance multithread security
208 | ClientInterface clientUtil = bbossESStarterDefault.getConfigRestClient(mappath);
209 | //Set query conditions, pass variable parameter values via map,key for variable names in DSL
210 | //There are four variables in the DSL:
211 | // applicationName1
212 | // applicationName2
213 | // startTime
214 | // endTime
215 | Map params = new HashMap();
216 | //Set the values of applicationName1 and applicationName2 variables
217 | params.put("applicationName1","blackcatdemo2");
218 | params.put("applicationName2","blackcatdemo3");
219 | DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
220 | //Set the time range, and accept the long value as the time parameter
221 | try {
222 | params.put("startTime",dateFormat.parse("2017-09-02 00:00:00").getTime());
223 | } catch (ParseException e) {
224 | e.printStackTrace();
225 | }
226 | params.put("endTime",new Date().getTime());
227 |
228 |
229 | //Execute the query
230 | ESDatas esDatas = //ESDatas contains a collection of currently retrieved records, up to 1000 records, specified by the size attribute in the DSL
231 | clientUtil.searchList("demo/_search",//demo as the indice, _search as the search action
232 | "searchDatas",//DSL statement name defined in esmapper/demo.xml
233 | params,//Query parameters
234 | Demo.class);//Data object type Demo returned
235 |
236 |
237 | //Gets a list of result objects and returns max up to 1000 records (specified in DSL)
238 | List demos = esDatas.getDatas();
239 |
240 | // String json = clientUtil.executeRequest("demo/_search",//demo as the index table, _search as the search action
241 | // "searchDatas",//DSL statement name defined in esmapper/demo.xml
242 | // params);//Query parameters
243 |
244 | // String json = com.frameworkset.util.SimpleStringUtil.object2json(demos);
245 | //Gets the total number of records
246 | long totalSize = esDatas.getTotalSize();
247 | DemoSearchResult demoSearchResult = new DemoSearchResult();
248 | demoSearchResult.setDemos(demos);
249 | demoSearchResult.setTotalSize(totalSize);
250 | return demoSearchResult;
251 | }
252 | }
253 |
--------------------------------------------------------------------------------
/src/test/java/org/bboss/elasticsearchtest/springboot/MultiESRestClientTest.java:
--------------------------------------------------------------------------------
1 | package org.bboss.elasticsearchtest.springboot;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import com.example.esbboss.entity.Demo;
19 | import com.example.esbboss.entity.DemoSearchResult;
20 | import org.frameworkset.elasticsearch.ElasticSearchException;
21 | import org.frameworkset.elasticsearch.boot.BBossESStarter;
22 | import org.frameworkset.elasticsearch.client.ClientInterface;
23 | import org.frameworkset.elasticsearch.entity.ESDatas;
24 | import org.junit.Test;
25 | import org.junit.runner.RunWith;
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 | import org.springframework.beans.factory.annotation.Autowired;
29 | import org.springframework.boot.test.context.SpringBootTest;
30 | import org.springframework.test.context.ActiveProfiles;
31 | import org.springframework.test.context.junit4.SpringRunner;
32 |
33 | import java.text.DateFormat;
34 | import java.text.ParseException;
35 | import java.text.SimpleDateFormat;
36 | import java.util.Date;
37 | import java.util.HashMap;
38 | import java.util.List;
39 | import java.util.Map;
40 |
41 | /**
42 | * 多集群演示功能测试用例,spring boot配置项以spring.elasticsearch.bboss.集群名称开头,例如:
43 | * spring.elasticsearch.bboss.default 默认es集群
44 | * spring.elasticsearch.bboss.logs logs es集群
45 | * 两个集群通过 org.bboss.elasticsearchtest.springboot.MultiESSTartConfigurer加载
46 | * 对应的配置文件为application-multi-datasource.properties文件
47 | */
48 | @RunWith(SpringRunner.class)
49 | @SpringBootTest
50 | @ActiveProfiles("multi-datasource")
51 | public class MultiESRestClientTest {
52 | private Logger logger = LoggerFactory.getLogger(MultiESRestClientTest.class);
53 | @Autowired
54 | private BBossESStarter bbossESStarterDefault;
55 | //DSL config file path
56 | private String mappath = "esmapper/demo.xml";
57 | @Test
58 | public void testMultiBBossESStarters() throws Exception {
59 |
60 | //验证环境,获取es状态
61 | // String response = bbossESStarterDefaultDefault.getRestClient().executeHttp("_cluster/state?pretty",ClientInterface.HTTP_GET);
62 | // System.out.println(response);
63 |
64 |
65 | //判断索引类型是否存在,false表示不存在,正常返回true表示存在
66 | boolean exist = bbossESStarterDefault.getRestClient().existIndiceType("twitter","tweet");
67 | System.out.println("default twitter/tweet:"+exist);
68 | //获取logs对应的Elasticsearch集群客户端,并进行existIndiceType操作
69 | exist = bbossESStarterDefault.getRestClient("logs").existIndiceType("twitter","tweet");
70 | System.out.println("logs twitter/tweet:"+exist);
71 | //获取logs对应的Elasticsearch集群客户端,判读索引是否存在,false表示不存在,正常返回true表示存在
72 | exist = bbossESStarterDefault.getRestClient("logs").existIndice("twitter");
73 | System.out.println("logs twitter:"+exist);
74 | //获取logs对应的Elasticsearch集群客户端,判断索引是否定义
75 | exist = bbossESStarterDefault.getRestClient("logs").existIndice("agentinfo");
76 | System.out.println("logs agentinfo:"+exist);
77 | }
78 | @Test
79 | public void test(){
80 | this.dropAndCreateAndGetIndice();
81 | addAndUpdateDocument();
82 | searchAllPararrel();
83 | this.search();
84 | this.deleteDocuments();
85 | }
86 | public void dropAndCreateAndGetIndice(){
87 | //Create a client tool to load configuration files, single instance multithreaded security
88 | ClientInterface clientUtil = bbossESStarterDefault.getConfigRestClient(mappath);
89 | try {
90 | //To determine whether the indice demo exists, it returns true if it exists and false if it does not
91 | boolean exist = clientUtil.existIndice("demo");
92 |
93 | //Delete mapping if the indice demo already exists
94 | if(exist) {
95 | String r = clientUtil.dropIndice("demo");
96 | logger.debug("clientUtil.dropIndice(\"demo\") response:"+r);
97 |
98 | }
99 | //Create index demo
100 | clientUtil.createIndiceMapping("demo",//The indice name
101 | "createDemoIndice");//Index mapping DSL script name, defined createDemoIndice in esmapper/demo.xml
102 |
103 | String demoIndice = clientUtil.getIndice("demo");//Gets the newly created indice structure
104 | logger.info("after createIndiceMapping clientUtil.getIndice(\"demo\") response:"+demoIndice);
105 | } catch (ElasticSearchException e) {
106 | // TODO Auto-generated catch block
107 | e.printStackTrace();
108 | }
109 |
110 | }
111 |
112 |
113 |
114 | public void addAndUpdateDocument() {
115 | //Build a create/modify/get/delete document client object, single instance multi-thread security
116 | ClientInterface clientUtil = bbossESStarterDefault.getRestClient();
117 | //Build an object as index document
118 | Demo demo = new Demo();
119 | demo.setDemoId(2l);//Specify the document id, the unique identity, and mark with the @ESId annotation. If the demoId already exists, modify the document; otherwise, add the document
120 | demo.setAgentStarttime(new Date());
121 | demo.setAgentStarttimezh(new Date());
122 | demo.setApplicationName("blackcatdemo2");
123 | demo.setContentbody("this is content body2");
124 | demo.setName("liudehua");
125 | demo.setOrderId("NFZF15045871807281445364228");
126 | demo.setContrastStatus(2);
127 |
128 |
129 | //Add the document and force refresh
130 | String response = clientUtil.addDocument("demo",//indice name
131 | "demo",//idnex type
132 | demo,"refresh=true");
133 |
134 |
135 |
136 | logger.debug("Print the result:addDocument-------------------------");
137 | logger.debug(response);
138 |
139 | demo = new Demo();
140 | demo.setDemoId(3l);//Specify the document id, the unique identity, and mark with the @ESId annotation. If the demoId already exists, modify the document; otherwise, add the document
141 | demo.setAgentStarttime(new Date());
142 | demo.setApplicationName("blackcatdemo3");
143 | demo.setContentbody("this is content body3");
144 | demo.setName("zhangxueyou");
145 | demo.setOrderId("NFZF15045871807281445364228");
146 | demo.setContrastStatus(3);
147 | demo.setAgentStarttime(new Date());
148 | demo.setAgentStarttimezh(new Date());
149 |
150 | //Add the document and force refresh
151 | response = clientUtil.addDocument("demo",//indice name
152 | "demo",//idnex type
153 | demo,"refresh=true");
154 |
155 | //Get the document object according to the document id, and return the Demo object
156 | demo = clientUtil.getDocument("demo",//indice name
157 | "demo",//idnex type
158 | "2",//document id
159 | Demo.class);
160 |
161 | //update document
162 | demo = new Demo();
163 | demo.setDemoId(2l);//Specify the document id, the unique identity, and mark with the @ESId annotation. If the demoId already exists, modify the document; otherwise, add the document
164 | demo.setAgentStarttime(new Date());
165 | demo.setApplicationName("blackcatdemo2");
166 | demo.setContentbody("this is modify content body2");
167 | demo.setName("刘德华modify\t");
168 | demo.setOrderId("NFZF15045871807281445364228");
169 | demo.setContrastStatus(2);
170 | demo.setAgentStarttimezh(new Date());
171 | //Execute update and force refresh
172 | response = clientUtil.addDocument("demo",//index name
173 | "demo",//idnex type
174 | demo,"refresh=true");
175 |
176 |
177 | //Get the modified document object according to the document id and return the json message string
178 | response = clientUtil.getDocument("demo",//indice name
179 | "demo",//idnex type
180 | "2");//document id
181 | logger.debug("Print the modified result:getDocument-------------------------");
182 | logger.debug(response);
183 |
184 |
185 |
186 |
187 | logger.debug("Print the modified result:getDocument-------------------------");
188 | logger.debug(response);
189 |
190 |
191 | }
192 |
193 | public void deleteDocuments(){
194 | //Build a create/modify/get/delete document client object, single instance multi-thread security
195 | ClientInterface clientUtil = bbossESStarterDefault.getRestClient();
196 | //Batch delete documents
197 | clientUtil.deleteDocuments("demo",//indice name
198 | "demo",//idnex type
199 | new String[]{"2","3"});//Batch delete document ids
200 | }
201 |
202 | /**
203 | * Use slice parallel scoll query all documents of indice demo by 2 thread tasks. DEFAULT_FETCHSIZE is 5000
204 | */
205 | public void searchAllPararrel(){
206 | ClientInterface clientUtil = bbossESStarterDefault.getRestClient();
207 | ESDatas esDatas = clientUtil.searchAllParallel("demo", Demo.class,2);
208 | }
209 |
210 |
211 |
212 | /**
213 | * Search the documents
214 | */
215 | public DemoSearchResult search() {
216 | //Create a load DSL file client instance to retrieve documents, single instance multithread security
217 | ClientInterface clientUtil = bbossESStarterDefault.getConfigRestClient(mappath);
218 | //Set query conditions, pass variable parameter values via map,key for variable names in DSL
219 | //There are four variables in the DSL:
220 | // applicationName1
221 | // applicationName2
222 | // startTime
223 | // endTime
224 | Map params = new HashMap();
225 | //Set the values of applicationName1 and applicationName2 variables
226 | params.put("applicationName1","blackcatdemo2");
227 | params.put("applicationName2","blackcatdemo3");
228 | DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
229 | //Set the time range, and accept the long value as the time parameter
230 | try {
231 | params.put("startTime",dateFormat.parse("2017-09-02 00:00:00").getTime());
232 | } catch (ParseException e) {
233 | e.printStackTrace();
234 | }
235 | params.put("endTime",new Date().getTime());
236 |
237 |
238 | //Execute the query
239 | ESDatas esDatas = //ESDatas contains a collection of currently retrieved records, up to 1000 records, specified by the size attribute in the DSL
240 | clientUtil.searchList("demo/_search",//demo as the indice, _search as the search action
241 | "searchDatas",//DSL statement name defined in esmapper/demo.xml
242 | params,//Query parameters
243 | Demo.class);//Data object type Demo returned
244 |
245 |
246 | //Gets a list of result objects and returns max up to 1000 records (specified in DSL)
247 | List demos = esDatas.getDatas();
248 |
249 | // String json = clientUtil.executeRequest("demo/_search",//demo as the index table, _search as the search action
250 | // "searchDatas",//DSL statement name defined in esmapper/demo.xml
251 | // params);//Query parameters
252 |
253 | // String json = com.frameworkset.util.SimpleStringUtil.object2json(demos);
254 | //Gets the total number of records
255 | long totalSize = esDatas.getTotalSize();
256 | DemoSearchResult demoSearchResult = new DemoSearchResult();
257 | demoSearchResult.setDemos(demos);
258 | demoSearchResult.setTotalSize(totalSize);
259 | return demoSearchResult;
260 | }
261 | }
262 |
--------------------------------------------------------------------------------
/src/test/java/org/bboss/elasticsearchtest/springboot/RestClient7Test.java:
--------------------------------------------------------------------------------
1 | package org.bboss.elasticsearchtest.springboot;
2 | /**
3 | * Copyright 2008 biaoping.yin
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import com.example.esbboss.entity.Demo;
19 | import com.example.esbboss.entity.DemoSearchResult;
20 | import org.frameworkset.elasticsearch.ElasticSearchException;
21 | import org.frameworkset.elasticsearch.boot.BBossESStarter;
22 | import org.frameworkset.elasticsearch.client.ClientInterface;
23 | import org.frameworkset.elasticsearch.entity.ESDatas;
24 | import org.frameworkset.elasticsearch.scroll.HandlerInfo;
25 | import org.frameworkset.elasticsearch.scroll.ScrollHandler;
26 | import org.junit.Test;
27 | import org.junit.runner.RunWith;
28 | import org.slf4j.Logger;
29 | import org.slf4j.LoggerFactory;
30 | import org.springframework.beans.factory.annotation.Autowired;
31 | import org.springframework.boot.test.context.SpringBootTest;
32 | import org.springframework.test.context.junit4.SpringRunner;
33 |
34 | import java.text.DateFormat;
35 | import java.text.ParseException;
36 | import java.text.SimpleDateFormat;
37 | import java.util.Date;
38 | import java.util.HashMap;
39 | import java.util.List;
40 | import java.util.Map;
41 |
42 | /**
43 | *
Description:
44 | *
45 | * Copyright (c) 2018
46 | * @Date 2019/9/18 10:27
47 | * @author biaoping.yin
48 | * @version 1.0
49 | */
50 | @RunWith(SpringRunner.class)
51 | @SpringBootTest
52 | public class RestClient7Test {
53 | private Logger logger = LoggerFactory.getLogger(RestClient7Test.class);
54 | @Autowired
55 | private BBossESStarter bbossESStarter;
56 | //DSL config file path
57 | private String mappath = "esmapper/demo7.xml";
58 | @Test
59 | public void testException(){
60 | ClientInterface clientUtil = bbossESStarter.getRestClient();
61 | try {
62 | clientUtil.getDocument("demo", (String) null);
63 | }
64 | catch (Exception e){
65 | e.printStackTrace();
66 | }
67 | }
68 |
69 | @Test
70 | public void testException1(){
71 | ClientInterface clientUtil = bbossESStarter.getRestClient();
72 |
73 | try {
74 | clientUtil.getDocument("demo","");
75 | }
76 | catch (Exception e){
77 | e.printStackTrace();
78 | }
79 | }
80 | /**
81 | * 并行方式执行slice scroll操作:将一个es的数据导入另外一个es数据,需要在application.properties文件中定义default和es233的两个集群
82 | */
83 | @Test
84 | public void testSimpleSliceScrollApiParralHandlerExportDsl() {
85 | ClientInterface clientUtil522 = bbossESStarter.getConfigRestClient("default","esmapper/scroll.xml");//定义一个对应源集群default的客户端组件实例,并且加载配置了scrollSliceQuery dsl的xml配置文件
86 |
87 | final ClientInterface clientUtil234 = bbossESStarter.getRestClient("es233"); //定义一个对应目标集群es233的客户端组件实例
88 | //scroll slice分页检索,max对应并行度,与源表shards数一致即可
89 | int max = 6;
90 | Map params = new HashMap();
91 | params.put("sliceMax", max);//最多6个slice,不能大于share数,必须使用sliceMax作为变量名称
92 | params.put("size", 5000);//每批5000条记录
93 | //采用自定义handler函数处理每个slice scroll的结果集后,sliceResponse中只会包含总记录数,不会包含记录集合
94 | //scroll上下文有效期1分钟,从源集群索引demo中查询数据
95 | ESDatas