GeohashUtil { public static void updateGridAggregationPrecision(Map<String,Map<String,Map<String,Object>>> aggregations, int precision) { aggregations.values().stream().filter(a -> a.containsKey("geohash_grid")).forEach(a -> { Map<String, Object> geohashGrid = a.get("geohash_grid"); if (!geohashGrid.containsKey("precision")) { geohashGrid.put("precision", precision); } }); }  static int computePrecision(Envelope envelope, long size, double threshold); static void updateGridAggregationPrecision(Map<String,Map<String,Map<String,Object>>> aggregations, int precision);  }
@Test public void doNotUpdatePrecisionIfAlreadyDefined() { final Map<String, Object> geohashGridAgg = new HashMap<>(ImmutableMap.of("field", "name", "precision", 3)); final Map<String,Map<String,Map<String,Object>>> aggregations; aggregations = ImmutableMap.of("first",ImmutableMap.of("geohash_grid",geohashGridAgg)); final Map<String,Object> expected = ImmutableMap.of("first", ImmutableMap.of("geohash_grid",ImmutableMap.of("field","name","precision",3))); GeohashUtil.updateGridAggregationPrecision(aggregations, 2); assertEquals(expected, aggregations); }
@Test public void updatePrecisionIfNotDefined() { final Map<String, Object> geohashGridAgg = new HashMap<>(ImmutableMap.of("field", "name")); final Map<String,Map<String,Map<String,Object>>> aggregations; aggregations = ImmutableMap.of("first",ImmutableMap.of("geohash_grid",geohashGridAgg)); final Map<String,Object> expected = ImmutableMap.of("first", ImmutableMap.of("geohash_grid",ImmutableMap.of("field","name","precision",2))); GeohashUtil.updateGridAggregationPrecision(aggregations, 2); assertEquals(expected, aggregations); }