赞
踩
bboss是一套基于query dsl语法操作和访问分布式搜索引擎elasticsearch的o/r mapping高性能开发库,底层基于es restful api。基于bboss elasticsearch,可以快速编写出访问和操作elasticsearch的程序代码,简单、高效、可靠、安全。
1.导依赖
- <dependency>
- <groupId>com.bbossgroups.plugins</groupId>
- <artifactId>bboss-elasticsearch-rest-jdbc</artifactId>
- <version>6.1.8</version>
- </dependency>
- <dependency>
- <groupId>com.bbossgroups.plugins</groupId>
- <artifactId>bboss-elasticsearch-spring-boot-starter</artifactId>
- <version>6.1.8</version>
- </dependency>
2.配置
- ##ES集群配置,支持x-pack和searchguard
- #spring.elasticsearch.bboss.elasticUser=elastic
- #spring.elasticsearch.bboss.elasticPassword=changeme
-
-
- spring.elasticsearch.bboss.elasticsearch.rest.hostNames=192.168.57.128:9200
- #spring.elasticsearch.bboss.elasticsearch.rest.hostNames=10.180.211.27:9280,10.180.211.27:9281,10.180.211.27:9282
- ##https配置,添加https://协议头
- #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
- spring.elasticsearch.bboss.elasticsearch.dateFormat=yyyy.MM.dd
- spring.elasticsearch.bboss.elasticsearch.timeZone=Asia/Shanghai
- spring.elasticsearch.bboss.elasticsearch.ttl=2d
- #在控制台输出脚本调试开关showTemplate,false关闭,true打开,同时log4j至少是info级别
- spring.elasticsearch.bboss.elasticsearch.showTemplate=true
- spring.elasticsearch.bboss.elasticsearch.discoverHost=false
- # dsl配置文件热加载扫描时间间隔,毫秒为单位,默认5秒扫描一次,<= 0时关闭扫描机制
- spring.elasticsearch.bboss.dslfile.refreshInterval = -1
-
- ##es client http连接池配置
- spring.elasticsearch.bboss.http.timeoutConnection = 50000
- spring.elasticsearch.bboss.http.timeoutSocket = 50000
- spring.elasticsearch.bboss.http.connectionRequestTimeout=50000
- spring.elasticsearch.bboss.http.retryTime = 1
- spring.elasticsearch.bboss.http.maxLineLength = -1
- spring.elasticsearch.bboss.http.maxHeaderCount = 200
- spring.elasticsearch.bboss.http.maxTotal = 400
- spring.elasticsearch.bboss.http.defaultMaxPerRoute = 200
- spring.elasticsearch.bboss.http.soReuseAddress = false
- spring.elasticsearch.bboss.http.soKeepAlive = false
- spring.elasticsearch.bboss.http.timeToLive = 3600000
- spring.elasticsearch.bboss.http.keepAlive = 3600000
- spring.elasticsearch.bboss.http.keystore =
- spring.elasticsearch.bboss.http.keyPassword =
- # ssl 主机名称校验,是否采用default配置,
- # 如果指定为default,就采用DefaultHostnameVerifier,否则采用 SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER
- spring.elasticsearch.bboss.http.hostnameVerifier =
4.开始使用api(这里我使用的测试类)
由于操作api需要创建客户端实例,这里有两种方式:
- @Autowired
- private BBossESStarter bbossESStarter;
-
- //这个是通过dsl脚本配置来创建(在需要自定义mapping时需要)
- ClientInterface configClientUtil = bbossESStarter.getConfigRestClient(mappath);
-
- //这个则相反,不需要dsl
- ClientInterface clientUtil = bbossESStarter.getRestClient();
4.1 创建索引(创建索引有两种方式,一种是普通的创建,即不指定mapping,对字段使用默认的数据类型。而另一种则是自定义mapping,每个字段的数据类型由我们自己指定):
4.1.1 普通创建:
- @SpringBootTest
- class EsBbossTest {
- @Autowired
- private BBossESStarter bbossESStarter;
-
- /**
- *创建索引
- */
- @Test
- public void createIndex(){
- String result = bbossESStarter.getRestClient().createIndiceMapping("blog","");
- System.out.println(result);
- }
- }
以下是控制台打印结果:
- 2020-07-04 17:30:42.913 INFO 7276 --- [ main] o.f.e.client.ElasticSearchRestClient : ElasticSearch http request action:blog,request body:
-
- {"acknowledged":true,"shards_acknowledged":true,"index":"blog"}
4.1.2 自定义mapping:
先创建一个dsl脚本文件,在里面自定义mapping规则:
- <propertys>
- <property name="create51jobIndex">//每个property中的name属性表示当前mapping的命名,一个xml文件里可设置多个dsl脚本
- <![CDATA[{
- "settings": {
- "number_of_shards": 1,
- "number_of_replicas": 1,
- "index.refresh_interval": "5s"
- },
- "mappings": {
- "properties": {
- "job":{
- "type":"text"
- },
- "company": {
- "type": "text"
- },
- "place": {
- "type": "text"
- },
- "salar": {
- "type": "text"
- },
- "data": {
- "type": "date",
- "format":"yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd'T'HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss||epoch_millis"
- }
- }
- }
- }]]>
- </property>
- </propertys>
然后创建客户端,并且调用创建索引api:
- @SpringBootTest
- class EsBbossTest {
- @Autowired
- private BBossESStarter bbossESStarter;
-
- /**
- *创建索引
- */
- @Test
- public void createIndex(){
- ClientInterface clientUtil = bbossESStarter.getConfigRestClient("esmapper/job.xml");
- String result = clientUtil.createIndiceMapping("51job","create51jobIndex");
- System.out.println(result);
- }
- }
以下是控制台打印结果:
- 2020-07-04 17:10:35.903 INFO 6172 --- [ main] o.f.e.client.ElasticSearchRestClient : ElasticSearch http request action:51job,request body:
- {
- "settings": {
- "number_of_shards": 1,
- "number_of_replicas": 1,
- "index.refresh_interval": "5s"
- },
- "mappings": {
- "properties": {
- "job":{
- "type":"text"
- },
- "company": {
- "type": "text"
- },
- "place": {
- "type": "text"
- },
- "salar": {
- "type": "text"
- },
- "data": {
- "type": "date",
- "format":"yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd'T'HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss||epoch_millis"
- }
- }
- }
- }
- {"acknowledged":true,"shards_acknowledged":true,"index":"51job"}
-
- 2020-07-04 17:10:36.113 INFO 6172 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
- 2020-07-04 17:10:37.110 INFO 6172 --- [ Thread-153] o.f.elasticsearch.ElasticSearch : ElasticSearch client stopping
-
- Process finished with exit code 0
4.2 删除索引
- @SpringBootTest
- class EsBbossTest {
- @Autowired
- private BBossESStarter bbossESStarter;
-
- /**
- *删除索引
- */
- @Test
- public void dropIndex(){
- String result = bbossESStarter.getRestClient().dropIndice("blog");
- System.out.println(result);
- }
- }
打印结果:
- 2020-07-04 17:37:12.133 INFO 9384 --- [ main] o.f.e.client.ElasticSearchRestClient : ElasticSearch http request action:blog?pretty
- {
- "acknowledged" : true
- }
4.3 判断索引是否存在
- @SpringBootTest
- class EsBbossTest {
- @Autowired
- private BBossESStarter bbossESStarter;
-
- /**
- *判断索引是否存在
- */
- @Test
- public void indexIsExists(){
- boolean exist = bbossESStarter.getRestClient().existIndice("51job");
- System.out.println(exist);
- }
- }
打印结果:
- 2020-07-04 17:40:03.011 INFO 14156 --- [ main] o.f.e.client.ElasticSearchRestClient : ElasticSearch http request action:51job
- true
4.4 判断索引类型是否存在
- @SpringBootTest
- class EsBbossTest {
- @Autowired
- private BBossESStarter bbossESStarter;
-
- /**
- *判断索引类型是否存在
- */
- @Test
- public void typeIsExists(){
- boolean exist = bbossESStarter.getRestClient().existIndiceType("51job","_doc");
- System.out.println(exist);
- }
- }
打印结果:
- 2020-07-04 17:42:24.657 INFO 9168 --- [ main] o.f.e.client.ElasticSearchRestClient : ElasticSearch http request action:51job/_mapping/_doc
- true
4.5 添加单个文档
- @SpringBootTest
- class EsBbossTest {
- @Autowired
- private BBossESStarter bbossESStarter;
-
- /**
- *指定索引添加单个文档
- */
- @Test
- public void addDocument(){
- Qcpage qcpage=new Qcpage();
- qcpage.setCompany("安软科技股份有限公司").setData("2020-06-03 21:18:12").setJob("Java开发工程师").setPlace("深圳南山").setSalar("13000/月");
- String result = bbossESStarter.getRestClient().addDocument("51job",qcpage);
- System.out.println(result);
- }
- }
打印结果:
- 2020-07-04 17:53:00.735 INFO 13100 --- [ main] o.f.e.client.ElasticSearchRestClient : ElasticSearch http request action:51job/_doc,request body:
- {"id":null,"job":"java开发工程师","company":"安软科技股份有限公司","place":"深圳南山","salar":"13000/月","data":"2020-06-03 21:18:12"}
- {"_index":"51job","_type":"_doc","_id":"YZI-GXMBBreT5daWT3ND","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":2,"_primary_term":1}
4.6 批量添加文档
- @SpringBootTest
- class EsBbossTest {
- @Autowired
- private BBossESStarter bbossESStarter;
- @Autowired
- QcpageService qcpageService;
-
- /**
- *批量添加文档(此处从mysql取出来1300条数据测试)
- */
- @Test
- public void batchAddDocument(){
- List<Qcpage> list=qcpageService.list();
- long startTime = System.currentTimeMillis();
- String result = bbossESStarter.getRestClient().addDocuments("51job",list);
- long endTime = System.currentTimeMillis();
- System.out.println("添加"+list.size()+"条数据耗时:" + (endTime - startTime)/1000 + "s");
- System.out.println(result);
- }
- }
打印结果(耗时0.3s):
添加1341条数据耗时:333ms
4.7 查询单个文档
- @SpringBootTest
- class EsBbossTest {
- @Autowired
- private BBossESStarter bbossESStarter;
-
- /**
- *查询一个文档
- */
- @Test
- public void getDocument(){
- String result = bbossESStarter.getRestClient().getDocument("51job","7pJsGXMBBreT5daW4X1w");
- System.out.println(result);
- }
- }
打印结果:
{"_index":"51job","_type":"_doc","_id":"7pJsGXMBBreT5daW4X1w","_version":1,"_seq_no":18,"_primary_term":1,"found":true,"_source":{"id":19,"job":"JAVA开发工程师","company":"木炎(深圳)区块链技术有限公司","place":"深圳-福田区","salar":"1-1.5万/月","data":"12-13"}}
还有很多查询方法,也可以写dsl自定义条件查询,内容多就不写啦!!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。