
官网地址简介mavenes集群整合
application.propertiesBBossESStarter *** 作类功能测试
dsl的xml业务代码 添加-修改文档
entity业务代码 搜索文档
dsl的xml业务代码
官网地址https://esdoc.bbossgroups.com/#/
简介bboss es融合了es官方提供的两种方式(restful和transprortclient)的功能,涵盖两方面能力,提供了(orm和restful,直接使用query dsl),是一个综合型的es客户端。
bboss elasticsearch是一套基于query dsl语法 *** 作和访问分布式搜索引擎elasticsearch的o/r mapping高性能开发库,底层基于es restfulapi。基于bboss elasticsearch,可以快速编写出访问和 *** 作elasticsearch的程序代码,简单、高效、可靠、安全。
es集群整合 application.propertiescom.bbossgroups.plugins bboss-elasticsearch-rest-jdbc6.3.6 com.bbossgroups.plugins bboss-elasticsearch-spring-boot-starter6.3.6
##ES集群配置,支持x-pack和searchguard spring.elasticsearch.bboss.elasticUser=账号 spring.elasticsearch.bboss.elasticPassword=密码 spring.elasticsearch.bboss.elasticsearch.rest.hostNames=ip:9200,ip:9200,ip: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 = ## 数据库数据源配置,使用db-es数据导入功能时需要配置 #spring.elasticsearch.bboss.db.name = test #spring.elasticsearch.bboss.db.user = root #spring.elasticsearch.bboss.db.password = 123456 #spring.elasticsearch.bboss.db.driver = com.mysql.jdbc.Driver #spring.elasticsearch.bboss.db.url = jdbc:mysql://localhost:3306/bboss #spring.elasticsearch.bboss.db.usePool = false #spring.elasticsearch.bboss.db.validateSQL = select 1BBossESStarter *** 作类
@Autowired
private BBossESStarter bbossESStarter;
//Create a client tool to load dsl xml configuration files(获取加载读取dsl xml配置文件的api接口实例,可以在代码里面直接通过dsl配置名称引用dsl即可), single instance multithreaded security
ClientInterface configClientUtil = bbossESStarter.getConfigRestClient("esmapper/gateway.xml");
//Build a create/modify/get/delete document client object(获取不需要读取dsl xml配置文件的api接口实例,可以在代码里面直接编写dsl), single instance multi-thread security
ClientInterface clientUtil = bbossESStarter.getRestClient();
功能测试
删除-创建-获取索引
dsl的xml
业务代码
@Service
public class documentCRUD7 {
private Logger logger = LoggerFactory.getLogger(documentCRUD7.class);
@Autowired
private BBossESStarter bbossESStarter;
//DSL config file path
private String mappath = "esmapper/demo7.xml";
public void dropAndCreateAndGetIndice(){
//Create a client tool to load configuration files, single instance multithreaded security
ClientInterface clientUtil = bbossESStarter.getConfigRestClient(mappath);
try {
//To determine whether the indice demo exists, it returns true if it exists and false if it does not
boolean exist = clientUtil.existIndice("demo");
//Delete mapping if the indice demo already exists
if(exist) {
String r = clientUtil.dropIndice("demo");
logger.debug("clientUtil.dropIndice("demo") response:"+r);
}
//Create index demo
clientUtil.createIndiceMapping("demo",//The indice name
"createDemoIndice");//Index mapping DSL script name, defined createDemoIndice in esmapper/demo.xml
String demoIndice = clientUtil.getIndice("demo");//Gets the newly created indice structure
logger.info("after createIndiceMapping clientUtil.getIndice("demo") response:"+demoIndice);
} catch (ElasticSearchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
添加-修改文档
entity
package com.wh.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.frameworkset.orm.annotation.Column;
import com.frameworkset.orm.annotation.ESId;
import org.frameworkset.elasticsearch.entity.ESbaseData;
import java.util.Date;
public class Demo extends ESbaseData {
private Object dynamicPriceTemplate;
//设置文档标识字段
//@ESId 用于标识实体对象中作为_id的属性,该注解只有一个persistent 布尔值属性,用于控制被本注解标注的字段属性是否作为普通文档属性保存,默认为true-保存,false不保存,字段名称为属性名称。ESId可用于添加和修改文档
@ESId(readSet = true,persistent = false)
private Long demoId;
private String contentbody;
protected Date agentStarttime;
public Date getAgentStarttimezh() {
return agentStarttimezh;
}
public void setAgentStarttimezh(Date agentStarttimezh) {
this.agentStarttimezh = agentStarttimezh;
}
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Column(dataformat = "yyyy-MM-dd HH:mm:ss")
private Date agentStarttimezh;
private String applicationName;
private String orderId;
// @JsonProperty(value="contrast_status",access= JsonProperty.Access.WRITE_ONLY)
private int contrastStatus;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
public String getContentbody() {
return contentbody;
}
public void setContentbody(String contentbody) {
this.contentbody = contentbody;
}
public Date getAgentStarttime() {
return agentStarttime;
}
public void setAgentStarttime(Date agentStarttime) {
this.agentStarttime = agentStarttime;
}
public String getApplicationName() {
return applicationName;
}
public void setApplicationName(String applicationName) {
this.applicationName = applicationName;
}
public Long getDemoId() {
return demoId;
}
public void setDemoId(Long demoId) {
this.demoId = demoId;
}
public Object getDynamicPriceTemplate() {
return dynamicPriceTemplate;
}
public void setDynamicPriceTemplate(Object dynamicPriceTemplate) {
this.dynamicPriceTemplate = dynamicPriceTemplate;
}
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public int getContrastStatus() {
return contrastStatus;
}
public void setContrastStatus(int contrastStatus) {
this.contrastStatus = contrastStatus;
}
@Override
public String toString() {
return "Demo{" +
"dynamicPriceTemplate=" + dynamicPriceTemplate +
", demoId=" + demoId +
", contentbody='" + contentbody + ''' +
", agentStarttime=" + agentStarttime +
", agentStarttimezh=" + agentStarttimezh +
", applicationName='" + applicationName + ''' +
", orderId='" + orderId + ''' +
", contrastStatus=" + contrastStatus +
", name='" + name + ''' +
'}';
}
}
业务代码
public void addAndUpdatedocument() {
//构建创建/修改/获取/删除文档客户端对象,单实例多线程安全
ClientInterface clientUtil = bbossESStarter.getRestClient();
//将对象构建为索引文档
Demo demo = new Demo();
//指定文档id、唯一标识和@ESId注释标记。如果demoId已经存在,修改单据;否则,请添加文档
demo.setDemoId(2l);
demo.setAgentStarttime(new Date());
demo.setAgentStarttimezh(new Date());
demo.setApplicationName("blackcatdemo2");
demo.setContentbody("this is content body2");
demo.setName("liudehua");
demo.setOrderId("NFZF15045871807281445364228");
demo.setContrastStatus(2);
//添加文档并强制刷新
String response = clientUtil.adddocument("demo",//索引名称
demo,"refresh=true");
logger.info("Demo id=2:adddocument-------------------------");
logger.info(response);
demo = new Demo();
//指定文档id、唯一标识和@ESId注释标记。如果demoId已经存在,修改单据;否则,请添加文档
demo.setDemoId(3l);
demo.setAgentStarttime(new Date());
demo.setApplicationName("blackcatdemo3");
demo.setContentbody("this is content body3");
demo.setName("zhangxueyou");
demo.setOrderId("NFZF15045871807281445364228");
demo.setContrastStatus(3);
demo.setAgentStarttime(new Date());
demo.setAgentStarttimezh(new Date());
///添加文档并强制刷新
response = clientUtil.adddocument("demo",//索引名
demo,"refresh=true");
//根据文档id获取文档对象,并返回演示对象
demo = clientUtil.getdocument("demo",//索引名
"2",//document id
Demo.class);
logger.info("Demo id=2:getdocument-------------------------");
logger.info(demo.toString());
//update document
demo = new Demo();
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
demo.setAgentStarttime(new Date());
demo.setApplicationName("blackcatdemo2");
demo.setContentbody("this is modify content body2");
demo.setName("刘德华modifyt");
demo.setOrderId("NFZF15045871807281445364228");
demo.setContrastStatus(2);
demo.setAgentStarttimezh(new Date());
//Execute update and force refresh
response = clientUtil.adddocument("demo",//index name
demo,"refresh=true");
logger.info("Demo id=2:updatedocument-------------------------");
logger.info(response);
//Get the modified document object according to the document id and return the json message string
response = clientUtil.getdocument("demo",//indice name
"2");//document id
logger.info("document id:getdocument-------------------------");
logger.info(response);
logger.info("Print the modified result:getdocument-------------------------");
logger.info(response);
}
搜索文档
dsl的xml
searchDatas和wh-searchDatas均可以
业务代码
public DemoSearchResult search() {
//创建加载DSL文件客户端实例以检索文档,单实例多线程安全
ClientInterface clientUtil = bbossESStarter.getConfigRestClient(mappath);
//设置查询条件,通过map传递变量参数值,在DSL中输入变量名称
//DSL中有四个变量:
//applicationName1
//applicationName2
//startTime
//endTime
Map params = new HashMap();
//设置applicationName1和applicationName2变量的值
params.put("applicationName1", "blackcatdemo2");
params.put("applicationName2", "blackcatdemo3");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//设置时间范围,并接受long值作为时间参数
try {
params.put("startTime", dateFormat.parse("2017-09-02 00:00:00").getTime());
} catch (ParseException e) {
e.printStackTrace();
}
params.put("endTime", new Date().getTime());
//执行查询
//ESDatas包含由DSL中的size属性指定的当前检索记录的集合,最多1000条记录
ESDatas esDatas = clientUtil.searchList("demo/_search",//demo 作为索引, search作为搜索 *** 作
"searchDatas",//在esmapper/demo.xml中定义的DSL语句名称
params,//查询参数
Demo.class); //返回的数据对象类型Demo
// ESDatas esDatas = clientUtil.searchList("demo/_search",//demo 作为索引, search作为搜索 *** 作
// "wh-searchDatas",//自己写的一个dsl语句
// params,//查询参数
// Demo.class); //返回的数据对象类型Demo
//获取结果对象的列表,并返回最多1000条记录(在DSL中指定)
List demos = esDatas.getDatas();
// String json = clientUtil.executeRequest("demo/_search",//demo as the index table, _search as the search action
// "searchDatas",//DSL statement name defined in esmapper/demo.xml
// params);//Query parameters
// String json = com.frameworkset.util.SimpleStringUtil.object2json(demos);
//获取记录的总数
long totalSize = esDatas.getTotalSize();
DemoSearchResult demoSearchResult = new DemoSearchResult();
demoSearchResult.setDemos(demos);
demoSearchResult.setTotalSize(totalSize);
return demoSearchResult;
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)