项目作者: wenzuojing

项目描述 :
easy-elastic4j是基于elasticsearch java client封装的elasticsearch操作库,使用更简单方便
高级语言: Java
项目地址: git://github.com/wenzuojing/easy-elastic4j.git
创建时间: 2016-03-15T09:34:42Z
项目社区:https://github.com/wenzuojing/easy-elastic4j

开源协议:

下载


easy-elastic4j

elasticsearch原生client api相对来说还是比较复杂的,大多数的api在实际的业务场景中可能用不到,easy-elastic4j只是封装了常见的操作,简化了使用难度,对初学者也非常友好.

Example

  1. //create elastic client
  2. ElasticClient elasticClient = ElasticClientFactory.create(R.CLUSTER_NAME, R.SERVER_ADDRESSES);
  3. //index
  4. IndexObject indexObject = new IndexObject("1");
  5. indexObject.field("name", "wens");
  6. indexObject.field("age", 29);
  7. indexObject.field("height", 165.5f);
  8. indexObject.field("comment", "攻城狮");
  9. elasticClient.index(test_index, indexObject);
  10. //bluk index
  11. List<IndexObject> list = new ArrayList<>();
  12. Random r = new Random();
  13. int count = r.nextInt(100);
  14. for (int i = 0; i < count; i++) {
  15. IndexObject indexObject = new IndexObject(String.valueOf(i));
  16. indexObject.field("name", "wens" + i);
  17. indexObject.field("age", r.nextInt(30));
  18. indexObject.field("height", 165 + r.nextFloat());
  19. indexObject.field("comment", "攻城狮" + i);
  20. list.add(indexObject);
  21. }
  22. elasticClient.index(test_index, list);
  23. //query for page
  24. Page page = elasticClient.queryPage(new Query(test_index).eq("group", "coder").limit(0, 5).orderByAsc("name"));
  25. Assert.assertEquals(5, page.getItems().size());
  26. Assert.assertEquals(true, page.isMore());
  27. int id = 0;
  28. for (Map<String, Object> item : page.getItems()) {
  29. Assert.assertEquals(String.format("wens%05d", id++), item.get("name"));
  30. }
  31. //query geo
  32. double lat = 23.131187;
  33. double lon = 113.257174;
  34. Query query = new Query(query_geo);
  35. query.nearby("location", lat, lon, 1000000);
  36. List<Map<String, Object>> list = elasticClient.queryList(query);
  37. double last = 0;
  38. for (Map<String, Object> item : list) {
  39. String location = (String) item.get("location");
  40. String[] point = location.split(",");
  41. double distance = GeoUtils.getDistance(lat, lon, Double.parseDouble(point[0]), Double.parseDouble(point[1]));
  42. Assert.assertEquals(true, last <= distance);
  43. System.out.println(item.get("name") + " " + location + " " + item.get("num") + " " + distance);
  44. }
  45. ......

Query Condition

  • FieldEqual 相等
  • FieldNotEqual 不相等
  • FieldExist 字段存在
  • Nearby 地理位置附近
  • NearbyRange 地理位置范围
  • NoneInField not in匹配
  • OneInField in匹配
  • Prefix 前缀匹配
  • QueryString 原生查询字符串
  • Range 范围比较
  • And and组合
  • Or or组合
  • Not 取反