From a3908c4c406e29e38b44dd74a112ba3564898202 Mon Sep 17 00:00:00 2001 From: brandonkearby Date: Tue, 11 Oct 2016 15:56:56 -0500 Subject: [PATCH] Fixed writeable name from range to geo_distance (#20860) * Fixed writeable name from range to geo_distance * Added testGeoDistanceAggregation * Added asserts for correct result in testGeoDistanceAggregation * Setup mapping on test index. --- .../geodistance/InternalGeoDistance.java | 7 ++- .../search/geo/GeoDistanceIT.java | 59 ++++++++++++++++--- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/InternalGeoDistance.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/InternalGeoDistance.java index f01e0233afd..c3dbd842e6d 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/InternalGeoDistance.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/InternalGeoDistance.java @@ -119,4 +119,9 @@ public class InternalGeoDistance extends InternalRange getFactory() { return FACTORY; } -} \ No newline at end of file + + @Override + public String getWriteableName() { + return GeoDistanceAggregationBuilder.NAME; + } +} diff --git a/core/src/test/java/org/elasticsearch/search/geo/GeoDistanceIT.java b/core/src/test/java/org/elasticsearch/search/geo/GeoDistanceIT.java index 4ea143ed7f9..b33ea303562 100644 --- a/core/src/test/java/org/elasticsearch/search/geo/GeoDistanceIT.java +++ b/core/src/test/java/org/elasticsearch/search/geo/GeoDistanceIT.java @@ -20,26 +20,32 @@ package org.elasticsearch.search.geo; import org.elasticsearch.Version; +import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.geo.GeoHashUtils; +import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoUtils; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.fielddata.ScriptDocValues; +import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.script.MockScriptPlugin; import org.elasticsearch.script.Script; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.Aggregations; +import org.elasticsearch.search.aggregations.bucket.range.Range; +import org.elasticsearch.search.aggregations.bucket.range.geodistance.InternalGeoDistance; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.InternalSettingsPlugin; import org.elasticsearch.test.VersionUtils; +import org.junit.Before; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; +import java.io.IOException; +import java.util.*; import java.util.function.Function; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; @@ -63,7 +69,6 @@ public class GeoDistanceIT extends ESIntegTestCase { public static class CustomScriptPlugin extends MockScriptPlugin { @Override - @SuppressWarnings("unchecked") protected Map, Object>> pluginScripts() { Map, Object>> scripts = new HashMap<>(); @@ -83,15 +88,14 @@ public class GeoDistanceIT extends ESIntegTestCase { return scripts; } - @SuppressWarnings("unchecked") static Double distanceScript(Map vars, Function distance) { Map doc = (Map) vars.get("doc"); return distance.apply((ScriptDocValues.GeoPoints) doc.get("location")); } } - public void testDistanceScript() throws Exception { - + @Before + public void setupTestIndex() throws IOException { Version version = VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.CURRENT); Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build(); XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("type1") @@ -102,7 +106,9 @@ public class GeoDistanceIT extends ESIntegTestCase { xContentBuilder.endObject().endObject().endObject().endObject(); assertAcked(prepareCreate("test").setSettings(settings).addMapping("type1", xContentBuilder)); ensureGreen(); + } + public void testDistanceScript() throws Exception { client().prepareIndex("test", "type1", "1") .setSource(jsonBuilder().startObject() .field("name", "TestPosition") @@ -156,4 +162,41 @@ public class GeoDistanceIT extends ESIntegTestCase { assertThat(resultArcDistance6, closeTo(GeoUtils.arcDistance(src_lat, src_lon, tgt_lat, tgt_lon)/1000d, 0.01d)); } + + public void testGeoDistanceAggregation() throws IOException { + client().prepareIndex("test", "type1", "1") + .setSource(jsonBuilder().startObject() + .field("name", "TestPosition") + .startObject("location") + .field("lat", src_lat) + .field("lon", src_lon) + .endObject() + .endObject()) + .get(); + + refresh(); + + SearchRequestBuilder search = client().prepareSearch("test"); + String name = "TestPosition"; + + search.setQuery(QueryBuilders.matchAllQuery()) + .setTypes("type1") + .addAggregation(AggregationBuilders.geoDistance(name, new GeoPoint(tgt_lat, tgt_lon)) + .field("location") + .unit(DistanceUnit.MILES) + .addRange(0, 25000)); + + search.setSize(0); // no hits please + + SearchResponse response = search.get(); + Aggregations aggregations = response.getAggregations(); + assertNotNull(aggregations); + InternalGeoDistance geoDistance = aggregations.get(name); + assertNotNull(geoDistance); + + List buckets = ((Range) geoDistance).getBuckets(); + assertNotNull("Buckets should not be null", buckets); + assertEquals("Unexpected number of buckets", 1, buckets.size()); + assertEquals("Unexpected doc count for geo distance", 1, buckets.get(0).getDocCount()); + } }