Elasticsearch Mget、GetDocSource、局部更新索引案例

1.准备工作

参考文档《高性能elasticsearch ORM开发库使用介绍》导入和配置es客户端bboss

2.mget操作

简单而直观的多文档获取案例

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

dsl定义-esmapper/estrace/mget.xml

  1. <!--
  2. GET /_mget
  3. {
  4. "docs" : [
  5. {
  6. "_index" : "agentinfo",
  7. "_type" : "agentinfo",
  8. "_id" : "10.21.20.168"
  9. },
  10. {
  11. "_index" : "agentinfo",
  12. "_type" : "agentinfo",
  13. "_id" : "192.168.0.143"
  14. }
  15. ]
  16. -->
  17. <property name="testMget">
  18. <![CDATA[
  19. {
  20. "docs" : [
  21. #foreach($id in $ids)
  22. #if($velocityCount > 0),#end
  23. {
  24. "_index" : "agentinfo",
  25. "_type" : "agentinfo",
  26. "_id" : "$id"
  27. }
  28. #end
  29. ]
  30. }
  31. ]]>
  32. </property>

3.更新索引文档部分信息案例

简单api案例

  1. ClientInterface configRestClientUtil =
  2. ElasticSearchHelper.getConfigRestClientUtil("esmapper/agentstat.xml");
  3. Map params = new HashMap();
  4. Date date = new Date();
  5. params.put("eventTimestamp",date.getTime());
  6. params.put("eventTimestampDate",date);
  7. params.put("location","28.292781,117.238963");
  8. /**
  9. * 采用dsl更新索引部分内容,dsl定义和路径api可以参考文档:
  10. * https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html
  11. */
  12. StringBuilder path = new StringBuilder();
  13. String docid = "pdpagent";
  14. String parentId = "parentId";
  15. path.append("agentinfo/agentinfo/").append(docid ).append("/_update?refresh");//自行拼接rest api地址
  16. //path.append("&routing=").append(parentId );//如果需要指定routing,则使用routing参数
  17. configRestClientUtil.updateByPath(path.toString(),
  18. "updateAgentInfoEndtime",//更新文档内容的dsl配置名称
  19. params);

dsl文件定义-esmapper/agentstat.xml

  1. <properties>
  2. <!--
  3. {
  4. "name" : "new_name"
  5. }
  6. }
  7. -->
  8. <property name="updateAgentInfoEndtime">
  9. <![CDATA[
  10. {
  11. "doc" : {
  12. "endTimestamp" : #[eventTimestamp],
  13. "endTimestampDate" : #[eventTimestampDate],
  14. "location":#[location]
  15. }
  16. }
  17. ]]>
  18. </property>
  19. </properties>

4.GetDocSource案例

5.几种经典的获取文档数据案例

根据文档id获取

  1. //根据文档id获取文档对象,返回json报文字符串
  2. String response = clientUtil.getDocument("demo",//索引表
  3. "demo",//索引类型
  4. "2");//w
  5. System.out.println("打印结果:getDocument-------------------------");
  6. System.out.println(response);
  7. //根据文档id获取文档对象,返回Demo对象
  8. demo = clientUtil.getDocument("demo",//索引表
  9. "demo",//索引类型
  10. "2",//文档id
  11. Demo.class);
  1. ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
  2. String response = clientUtil.getDocumentByPath("agentinfo/agentinfo/10.21.20.168");
  3. System.out.println(response);
  4. Map data = clientUtil.getDocumentByPath("agentinfo/agentinfo/10.21.20.168",Map.class);
  5. System.out.println(data);
  6. //请求地址格式说明:
  7. // index/indexType/docId
  8. // 实例如下:
  9. // "agentinfo/agentinfo/10.21.20.168"