当前位置:   article > 正文

springboot集成bboss操作elasticsearch索引,类型,文档(单集群)_bboss-elasticsearch-spring-boot-starter

bboss-elasticsearch-spring-boot-starter

    bboss是一套基于query dsl语法操作和访问分布式搜索引擎elasticsearch的o/r mapping高性能开发库,底层基于es restful api。基于bboss elasticsearch,可以快速编写出访问和操作elasticsearch的程序代码,简单、高效、可靠、安全。

1.导依赖

  1. <dependency>
  2. <groupId>com.bbossgroups.plugins</groupId>
  3. <artifactId>bboss-elasticsearch-rest-jdbc</artifactId>
  4. <version>6.1.8</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.bbossgroups.plugins</groupId>
  8. <artifactId>bboss-elasticsearch-spring-boot-starter</artifactId>
  9. <version>6.1.8</version>
  10. </dependency>

2.配置

  1. ##ES集群配置,支持x-pack和searchguard
  2. #spring.elasticsearch.bboss.elasticUser=elastic
  3. #spring.elasticsearch.bboss.elasticPassword=changeme
  4. spring.elasticsearch.bboss.elasticsearch.rest.hostNames=192.168.57.128:9200
  5. #spring.elasticsearch.bboss.elasticsearch.rest.hostNames=10.180.211.27:9280,10.180.211.27:9281,10.180.211.27:9282
  6. ##https配置,添加https://协议头
  7. #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
  8. spring.elasticsearch.bboss.elasticsearch.dateFormat=yyyy.MM.dd
  9. spring.elasticsearch.bboss.elasticsearch.timeZone=Asia/Shanghai
  10. spring.elasticsearch.bboss.elasticsearch.ttl=2d
  11. #在控制台输出脚本调试开关showTemplate,false关闭,true打开,同时log4j至少是info级别
  12. spring.elasticsearch.bboss.elasticsearch.showTemplate=true
  13. spring.elasticsearch.bboss.elasticsearch.discoverHost=false
  14. # dsl配置文件热加载扫描时间间隔,毫秒为单位,默认5秒扫描一次,<= 0时关闭扫描机制
  15. spring.elasticsearch.bboss.dslfile.refreshInterval = -1
  16. ##es client http连接池配置
  17. spring.elasticsearch.bboss.http.timeoutConnection = 50000
  18. spring.elasticsearch.bboss.http.timeoutSocket = 50000
  19. spring.elasticsearch.bboss.http.connectionRequestTimeout=50000
  20. spring.elasticsearch.bboss.http.retryTime = 1
  21. spring.elasticsearch.bboss.http.maxLineLength = -1
  22. spring.elasticsearch.bboss.http.maxHeaderCount = 200
  23. spring.elasticsearch.bboss.http.maxTotal = 400
  24. spring.elasticsearch.bboss.http.defaultMaxPerRoute = 200
  25. spring.elasticsearch.bboss.http.soReuseAddress = false
  26. spring.elasticsearch.bboss.http.soKeepAlive = false
  27. spring.elasticsearch.bboss.http.timeToLive = 3600000
  28. spring.elasticsearch.bboss.http.keepAlive = 3600000
  29. spring.elasticsearch.bboss.http.keystore =
  30. spring.elasticsearch.bboss.http.keyPassword =
  31. # ssl 主机名称校验,是否采用default配置,
  32. # 如果指定为default,就采用DefaultHostnameVerifier,否则采用 SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER
  33. spring.elasticsearch.bboss.http.hostnameVerifier =

4.开始使用api(这里我使用的测试类)

由于操作api需要创建客户端实例,这里有两种方式:

  1. @Autowired
  2. private BBossESStarter bbossESStarter;
  3. //这个是通过dsl脚本配置来创建(在需要自定义mapping时需要)
  4. ClientInterface configClientUtil = bbossESStarter.getConfigRestClient(mappath);
  5. //这个则相反,不需要dsl
  6. ClientInterface clientUtil = bbossESStarter.getRestClient();

4.1 创建索引(创建索引有两种方式,一种是普通的创建,即不指定mapping,对字段使用默认的数据类型。而另一种则是自定义mapping,每个字段的数据类型由我们自己指定):

4.1.1 普通创建:

  1. @SpringBootTest
  2. class EsBbossTest {
  3. @Autowired
  4. private BBossESStarter bbossESStarter;
  5. /**
  6. *创建索引
  7. */
  8. @Test
  9. public void createIndex(){
  10. String result = bbossESStarter.getRestClient().createIndiceMapping("blog","");
  11. System.out.println(result);
  12. }
  13. }

以下是控制台打印结果:

  1. 2020-07-04 17:30:42.913 INFO 7276 --- [ main] o.f.e.client.ElasticSearchRestClient : ElasticSearch http request action:blog,request body:
  2. {"acknowledged":true,"shards_acknowledged":true,"index":"blog"}

4.1.2 自定义mapping:

先创建一个dsl脚本文件,在里面自定义mapping规则:

  1. <propertys>
  2. <property name="create51jobIndex">//每个property中的name属性表示当前mapping的命名,一个xml文件里可设置多个dsl脚本
  3. <![CDATA[{
  4. "settings": {
  5. "number_of_shards": 1,
  6. "number_of_replicas": 1,
  7. "index.refresh_interval": "5s"
  8. },
  9. "mappings": {
  10. "properties": {
  11. "job":{
  12. "type":"text"
  13. },
  14. "company": {
  15. "type": "text"
  16. },
  17. "place": {
  18. "type": "text"
  19. },
  20. "salar": {
  21. "type": "text"
  22. },
  23. "data": {
  24. "type": "date",
  25. "format":"yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd'T'HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss||epoch_millis"
  26. }
  27. }
  28. }
  29. }]]>
  30. </property>
  31. </propertys>

然后创建客户端,并且调用创建索引api:

  1. @SpringBootTest
  2. class EsBbossTest {
  3. @Autowired
  4. private BBossESStarter bbossESStarter;
  5. /**
  6. *创建索引
  7. */
  8. @Test
  9. public void createIndex(){
  10. ClientInterface clientUtil = bbossESStarter.getConfigRestClient("esmapper/job.xml");
  11. String result = clientUtil.createIndiceMapping("51job","create51jobIndex");
  12. System.out.println(result);
  13. }
  14. }

以下是控制台打印结果:

  1. 2020-07-04 17:10:35.903 INFO 6172 --- [ main] o.f.e.client.ElasticSearchRestClient : ElasticSearch http request action:51job,request body:
  2. {
  3. "settings": {
  4. "number_of_shards": 1,
  5. "number_of_replicas": 1,
  6. "index.refresh_interval": "5s"
  7. },
  8. "mappings": {
  9. "properties": {
  10. "job":{
  11. "type":"text"
  12. },
  13. "company": {
  14. "type": "text"
  15. },
  16. "place": {
  17. "type": "text"
  18. },
  19. "salar": {
  20. "type": "text"
  21. },
  22. "data": {
  23. "type": "date",
  24. "format":"yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd'T'HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss||epoch_millis"
  25. }
  26. }
  27. }
  28. }
  29. {"acknowledged":true,"shards_acknowledged":true,"index":"51job"}
  30. 2020-07-04 17:10:36.113 INFO 6172 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
  31. 2020-07-04 17:10:37.110 INFO 6172 --- [ Thread-153] o.f.elasticsearch.ElasticSearch : ElasticSearch client stopping
  32. Process finished with exit code 0

4.2 删除索引

  1. @SpringBootTest
  2. class EsBbossTest {
  3. @Autowired
  4. private BBossESStarter bbossESStarter;
  5. /**
  6. *删除索引
  7. */
  8. @Test
  9. public void dropIndex(){
  10. String result = bbossESStarter.getRestClient().dropIndice("blog");
  11. System.out.println(result);
  12. }
  13. }

打印结果:

  1. 2020-07-04 17:37:12.133 INFO 9384 --- [ main] o.f.e.client.ElasticSearchRestClient : ElasticSearch http request action:blog?pretty
  2. {
  3. "acknowledged" : true
  4. }

4.3 判断索引是否存在

  1. @SpringBootTest
  2. class EsBbossTest {
  3. @Autowired
  4. private BBossESStarter bbossESStarter;
  5. /**
  6. *判断索引是否存在
  7. */
  8. @Test
  9. public void indexIsExists(){
  10. boolean exist = bbossESStarter.getRestClient().existIndice("51job");
  11. System.out.println(exist);
  12. }
  13. }

打印结果:

  1. 2020-07-04 17:40:03.011 INFO 14156 --- [ main] o.f.e.client.ElasticSearchRestClient : ElasticSearch http request action:51job
  2. true

4.4 判断索引类型是否存在

  1. @SpringBootTest
  2. class EsBbossTest {
  3. @Autowired
  4. private BBossESStarter bbossESStarter;
  5. /**
  6. *判断索引类型是否存在
  7. */
  8. @Test
  9. public void typeIsExists(){
  10. boolean exist = bbossESStarter.getRestClient().existIndiceType("51job","_doc");
  11. System.out.println(exist);
  12. }
  13. }

打印结果:

  1. 2020-07-04 17:42:24.657 INFO 9168 --- [ main] o.f.e.client.ElasticSearchRestClient : ElasticSearch http request action:51job/_mapping/_doc
  2. true

4.5 添加单个文档

  1. @SpringBootTest
  2. class EsBbossTest {
  3. @Autowired
  4. private BBossESStarter bbossESStarter;
  5. /**
  6. *指定索引添加单个文档
  7. */
  8. @Test
  9. public void addDocument(){
  10. Qcpage qcpage=new Qcpage();
  11. qcpage.setCompany("安软科技股份有限公司").setData("2020-06-03 21:18:12").setJob("Java开发工程师").setPlace("深圳南山").setSalar("13000/月");
  12. String result = bbossESStarter.getRestClient().addDocument("51job",qcpage);
  13. System.out.println(result);
  14. }
  15. }

打印结果:

  1. 2020-07-04 17:53:00.735 INFO 13100 --- [ main] o.f.e.client.ElasticSearchRestClient : ElasticSearch http request action:51job/_doc,request body:
  2. {"id":null,"job":"java开发工程师","company":"安软科技股份有限公司","place":"深圳南山","salar":"13000/月","data":"2020-06-03 21:18:12"}
  3. {"_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 批量添加文档

  1. @SpringBootTest
  2. class EsBbossTest {
  3. @Autowired
  4. private BBossESStarter bbossESStarter;
  5. @Autowired
  6. QcpageService qcpageService;
  7. /**
  8. *批量添加文档(此处从mysql取出来1300条数据测试)
  9. */
  10. @Test
  11. public void batchAddDocument(){
  12. List<Qcpage> list=qcpageService.list();
  13. long startTime = System.currentTimeMillis();
  14. String result = bbossESStarter.getRestClient().addDocuments("51job",list);
  15. long endTime = System.currentTimeMillis();
  16. System.out.println("添加"+list.size()+"条数据耗时:" + (endTime - startTime)/1000 + "s");
  17. System.out.println(result);
  18. }
  19. }

打印结果(耗时0.3s):

添加1341条数据耗时:333ms

4.7 查询单个文档

  1. @SpringBootTest
  2. class EsBbossTest {
  3. @Autowired
  4. private BBossESStarter bbossESStarter;
  5. /**
  6. *查询一个文档
  7. */
  8. @Test
  9. public void getDocument(){
  10. String result = bbossESStarter.getRestClient().getDocument("51job","7pJsGXMBBreT5daW4X1w");
  11. System.out.println(result);
  12. }
  13. }

打印结果:

{"_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自定义条件查询,内容多就不写啦!!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/500512
推荐阅读
相关标签
  

闽ICP备14008679号