Quick Start

bboss

Introduce Elasticsearch Bboss:

  1. A highlevel rest client.
  2. A high performence o/r mapping rest client.
  3. A dsl and sql rest client.

First add the maven dependency of BBoss to your pom.xml:

  1. <dependency>
  2. <groupId>com.bbossgroups.plugins</groupId>
  3. <artifactId>bboss-elasticsearch-rest-jdbc</artifactId>
  4. <version>5.5.3</version>
  5. </dependency>

If it’s a spring boot project, you can replace the Maven coordinate above with the following Maven coordinate:

  1. <dependency>
  2. <groupId>com.bbossgroups.plugins</groupId>
  3. <artifactId>bboss-elasticsearch-spring-boot-starter</artifactId>
  4. <version>5.5.3</version>
  5. </dependency>

Next, add the Elasticsearch addresses to the application.properties file under the project resource directory, and create a new one if the file does not exist:

  1. elasticsearch.rest.hostNames=10.21.20.168:9200
  2. #Cluster addresses are separated by commas
  3. #elasticsearch.rest.hostNames=10.180.211.27:9200,10.180.211.28:9200,10.180.211.29:9200

If the HTTPS protocol is on, add the https protocol header to the elasticsearch address:

  1. elasticsearch.rest.hostNames=https://10.180.211.27:9280,https://10.180.211.27:9281,https://10.180.211.27:9282

If x-pack or searchguard security authentication is enabled, configure the account and password with the following two properties in application.properties:

  1. # x-pack or searchguard security authentication and password configuration
  2. elasticUser=elastic
  3. elasticPassword=changeme

And last create a jsp file named testElasticsearch.jsp :

  1. <%@ page import="org.frameworkset.elasticsearch.ElasticSearchHelper" %>
  2. <%@ page import="org.frameworkset.elasticsearch.client.ClientInterface" %>
  3. <%@ page import="org.frameworkset.elasticsearch.entity.ESDatas" %>
  4. <%@ page import="org.frameworkset.elasticsearch.scroll.ScrollHandler" %>
  5. <%@ page import="java.util.List" %>
  6. <%@ page import="java.util.Map" %>
  7. <%@ page import="com.frameworkset.common.poolman.SQLExecutor" %>
  8. <%@ page language="java" pageEncoding="UTF-8"%>
  9. <%
  10. ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
  11. //get elasticsearch cluster state
  12. String result = clientUtil.executeHttp("_cluster/state?pretty",ClientInterface.HTTP_GET);
  13. //check indice twitter and index type tweet exist or not.
  14. boolean exist1 = clientUtil.existIndiceType("twitter","tweet");
  15. out.println("twitter tweet type exist:"+exist1);
  16. //check indice twitter exist or not
  17. exist1 = clientUtil.existIndice("twitter");
  18. out.println("twitter exist:"+exist1);
  19. //count documents in indice twitter
  20. long count = clientUtil.countAll("twitter");
  21. out.println(count);
  22. //Get All documents of indice twitter,DEFAULT_FETCHSIZE is 5000
  23. ESDatas<Map> esDatas = clientUtil.searchAll("twitter", Map.class);
  24. //Get All documents of indice twitter,Set fetchsize to 10000, Using ScrollHandler to process each batch of datas.
  25. clientUtil.searchAll("twitter",10000,new ScrollHandler<Map>() {
  26. public void handle(ESDatas<Map> esDatas) throws Exception {
  27. List<Map> dataList = esDatas.getDatas();
  28. System.out.println("TotalSize:"+esDatas.getTotalSize());
  29. if(dataList != null) {
  30. System.out.println("dataList.size:" + dataList.size());
  31. }
  32. else
  33. {
  34. System.out.println("dataList.size:0");
  35. }
  36. //do something other such as do a db query.
  37. //SQLExecutor.queryList(Map.class,"select * from td_sm_user");
  38. }
  39. },Map.class);
  40. //Use slice parallel scoll query all documents of indice twitter by 2 thread tasks. DEFAULT_FETCHSIZE is 5000
  41. //You can also use ScrollHandler to process each batch of datas on your own.
  42. esDatas = clientUtil.searchAllParallel("twitter", Map.class,2);
  43. out.println("searchAllParallel:ok");
  44. %>

Put the jsp file into your web project , run it in browser, then see the execution result of bboss .

The Web demo github url:

https://github.com/bbossgroups/es_bboss_web

bboss elasticsearch document:

Development Document