Elasticsearch添加修改删除索引文档

bboss

本文介绍如何采用bboss es添加/修改/查询/删除/批量删除elasticsearch索引文档,直接看代码。

增删改查操作

  1. 添加/修改文档:
  2. TAgentInfo agentInfo = new TAgentInfo() ;
  3. agentInfo.setIp("192.168.137.1");//ip属性作为文档唯一标识,根据ip值对应的索引文档存在与否来决定添加或者修改操作
  4. //设置地理位置坐标
  5. agentInfo.setLocation("28.292781,117.238963");
  6. //设置其他属性
  7. 。。。。
  8. ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
  9. clientUtil.addDocument("agentinfo",//索引名称
  10. "agentinfo",//索引类型
  11. agentInfo);//索引数据对象
  12. //执行查询操作
  13. ESDatas<TAgentInfo> data //ESDatas为查询结果集对象,封装了返回的当前查询的List<TAgentInfo>结果集和符合条件的总记录数totalSize
  14. = clientUtil.searchList("trace-*/_search",//查询操作,查询indices trace-*中符合条件的数据
  15. "queryServiceByCondition",//通过名称引用配置文件中的query dsl语句
  16. traceExtraCriteria,//查询条件封装对象
  17. TAgentInfo.class);//指定返回的po对象类型,po对象中的属性与indices表中的文档filed名称保持一致
  18. TAgentInfo的结构如下:
  19. public class TAgentInfo implements java.io.Serializable{
  20. private String hostname;
  21. @ESId //ip属性作为文档唯一标识,根据ip值对应的索引文档存在与否来决定添加或者修改操作
  22. private String ip;
  23. private String ports;
  24. private String agentId;
  25. private String location;
  26. private String applicationName;
  27. private int serviceType;
  28. private int pid;
  29. private String agentVersion;
  30. private String vmVersion;
  31. //日期类型
  32. private Date startTimestampDate;
  33. private Date endTimestampDate;
  34. private long startTimestamp;
  35. private long endTimestamp;
  36. private int endStatus;
  37. private String serverMetaData;
  38. private String jvmInfo;
  39. }
  40. //删除索引文档
  41. clientUtil.deleteDocument("agentinfo",//索引表
  42. "agentinfo",//索引类型
  43. "192.168.137.1");//文档id
  44. //批量删除索引文档
  45. clientUtil.deleteDocuments("agentinfo",//索引表
  46. "agentinfo",//索引类型
  47. "192.168.137.1","192.168.137.2","192.168.137.3");//文档ids

执行多表查询操作

  1. //执行多表查询操作
  2. ESDatas<TAgentInfo> data //ESDatas为查询结果集对象,封装了返回的当前查询的List<TAgentInfo>结果集和符合条件的总记录数totalSize
  3. = clientUtil.searchList("trace1,trace2/_search",//查询操作,同时查询trace1,trace2中符合条件的数据
  4. "queryServiceByCondition",//通过名称引用配置文件中的query dsl语句
  5. traceExtraCriteria,//查询条件封装对象
  6. TAgentInfo.class);//指定返回的po对象类型,po对象中的属性与indices表中的文档filed名称保持一致