Elasticsearch Mget、GetDocSource、索引部分更新案例分享

[size=large][b]1.前期准备[/b][/size]
参考文档《[url=https://my.oschina.net/bboss/blog/1556866]高性能elasticsearch ORM开发库使用介绍[/url]》导入和配置es客户端bboss

[size=large][b]2.mget操作[/b][/size]
简单而直观的多文档获取案例
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
//获取json报文
String response = clientUtil.mgetDocuments("agentinfo",//索引表
"agentinfo",//索引表类型
"10.21.20.168","192.168.0.143");//文档id清单
System.out.println(response);
//获取封装成对象的文档列表,此处是Map对象,还可以是其他用户定义的对象类型
List<Map> docs = clientUtil.mgetDocuments("agentinfo",//索引表
"agentinfo",//索引表类型
Map.class,//返回文档对象类型
"10.21.20.168","192.168.0.143");//文档id清单
System.out.println(docs);


通过执行dsl获取多个文档的内容案例
ClientInterface clientUtil = 
ElasticSearchHelper.getConfigRestClientUtil("esmapper/estrace/mget.xml");
//通过执行dsl获取多个文档的内容,具体可以参考文档:
//https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html
List<String> ids = new ArrayList<String>();
ids.add("10.21.20.168");
ids.add("192.168.0.143");
Map params = new HashMap();
params.put("ids",ids);
String response = clientUtil.executeHttp("_mget",
"testMget",//dsl定义名称
params, //存放文档id的参数
ClientUtil.HTTP_POST);
System.out.println(response);
List<Map> docs = clientUtil.mgetDocuments("_mget",
"testMget",//dsl定义名称
params, //存放文档id的参数
Map.class);//返回文档对象类型
System.out.println(docs);

dsl定义-esmapper/estrace/mget.xml

<!--
GET /_mget
{
"docs" : [
{
"_index" : "agentinfo",
"_type" : "agentinfo",
"_id" : "10.21.20.168"
},
{
"_index" : "agentinfo",
"_type" : "agentinfo",
"_id" : "192.168.0.143"
}
]
}
-->
<property name="testMget">
<![CDATA[

{
"docs" : [
#foreach($id in $ids)
#if($velocityCount > 0),#end
{
"_index" : "agentinfo",
"_type" : "agentinfo",
"_id" : "$id"
}
#end
]
}
]]>
</property>

[size=large][b]3.更新索引文档部分信息案例[/b][/size]
简单api案例
Map params = new HashMap();
Date date = new Date();
params.put("eventTimestamp",date.getTime());
params.put("eventTimestampDate",date);
/**
* 更新索引部分内容
*/
ClientInterface restClientUtil = ElasticSearchHelper.getRestClientUtil();
String response = restClientUtil.updateDocument("agentinfo",//索引表名称
"agentinfo",//索引type
"pdpagent",//索引id
params,//待更新的索引字段信息
"refresh");//强制刷新索引
System.out.println(response);

采用dsl案例
ClientInterface configRestClientUtil = 
ElasticSearchHelper.getConfigRestClientUtil("esmapper/agentstat.xml");
Map params = new HashMap();
Date date = new Date();
params.put("eventTimestamp",date.getTime());
params.put("eventTimestampDate",date);
/**
* 采用dsl更新索引部分内容,dsl定义和路径api可以参考文档:
* https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html
*/
StringBuilder path = new StringBuilder();
path.append("agentinfo/agentinfo/pdpagent/_update?refresh");//自行拼接rest api地址
configRestClientUtil.updateByPath(path.toString(),
"updateAgentInfoEndtime",//更新文档内容的dsl配置名称
params);

dsl文件定义-esmapper/agentstat.xml
<properties>
<!--
POST test/_doc/1/_update
{
"doc" : {
"name" : "new_name"
}
}
-->
<property name="updateAgentInfoEndtime">
<![CDATA[
{
"doc" : {
"endTimestamp" : #[eventTimestamp],
"endTimestampDate" : #[eventTimestampDate]
}
}
]]>
</property>
</properties>

[size=large][b]4.GetDocSource案例[/b][/size]
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
//获取json报文索引source,不返回索引元数据
String response = clientUtil.getDocumentSource("agentinfo/agentinfo/10.21.20.168/_source");
System.out.println(response);
//获取对象类型source,此处对象类型是map,可以指定自定义的对象类型,不返回索引元数据
Map data = clientUtil.getDocumentSource("agentinfo/agentinfo/10.21.20.168/_source",Map.class);
System.out.println(data);
//请求地址格式说明:
// index/indexType/docId/_source
// 实例如下:
// "agentinfo/agentinfo/10.21.20.168/_source"

[size=large][b]5.几种经典的获取文档数据案例[/b][/size]
根据文档id获取
//根据文档id获取文档对象,返回json报文字符串
String response = clientUtil.getDocument("demo",//索引表
"demo",//索引类型
"2");//w

System.out.println("打印结果:getDocument-------------------------");
System.out.println(response);
//根据文档id获取文档对象,返回Demo对象
demo = clientUtil.getDocument("demo",//索引表
"demo",//索引类型
"2",//文档id
Demo.class);

根据rest url获取
 ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
String response = clientUtil.getDocumentByPath("agentinfo/agentinfo/10.21.20.168");
System.out.println(response);
Map data = clientUtil.getDocumentByPath("agentinfo/agentinfo/10.21.20.168",Map.class);
System.out.println(data);
//请求地址格式说明:
// index/indexType/docId
// 实例如下:
// "agentinfo/agentinfo/10.21.20.168"

更多bboss 使用文档可以参考:
[url]https://my.oschina.net/bboss/blog/1556866[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值