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.
This commit is contained in:
brandonkearby 2016-10-11 15:56:56 -05:00 committed by Nik Everett
parent 298cf1cf21
commit a3908c4c40
2 changed files with 57 additions and 9 deletions

View File

@ -119,4 +119,9 @@ public class InternalGeoDistance extends InternalRange<InternalGeoDistance.Bucke
public InternalRange.Factory<Bucket, InternalGeoDistance> getFactory() { public InternalRange.Factory<Bucket, InternalGeoDistance> getFactory() {
return FACTORY; return FACTORY;
} }
}
@Override
public String getWriteableName() {
return GeoDistanceAggregationBuilder.NAME;
}
}

View File

@ -20,26 +20,32 @@
package org.elasticsearch.search.geo; package org.elasticsearch.search.geo;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.geo.GeoHashUtils; import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoUtils; import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.fielddata.ScriptDocValues; import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.script.MockScriptPlugin; import org.elasticsearch.script.MockScriptPlugin;
import org.elasticsearch.script.Script; 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.ESIntegTestCase;
import org.elasticsearch.test.InternalSettingsPlugin; import org.elasticsearch.test.InternalSettingsPlugin;
import org.elasticsearch.test.VersionUtils; import org.elasticsearch.test.VersionUtils;
import org.junit.Before;
import java.util.Arrays; import java.io.IOException;
import java.util.Collection; import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
@ -63,7 +69,6 @@ public class GeoDistanceIT extends ESIntegTestCase {
public static class CustomScriptPlugin extends MockScriptPlugin { public static class CustomScriptPlugin extends MockScriptPlugin {
@Override @Override
@SuppressWarnings("unchecked")
protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() { protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>(); Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
@ -83,15 +88,14 @@ public class GeoDistanceIT extends ESIntegTestCase {
return scripts; return scripts;
} }
@SuppressWarnings("unchecked")
static Double distanceScript(Map<String, Object> vars, Function<ScriptDocValues.GeoPoints, Double> distance) { static Double distanceScript(Map<String, Object> vars, Function<ScriptDocValues.GeoPoints, Double> distance) {
Map<?, ?> doc = (Map) vars.get("doc"); Map<?, ?> doc = (Map) vars.get("doc");
return distance.apply((ScriptDocValues.GeoPoints) doc.get("location")); 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); Version version = VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.CURRENT);
Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build(); Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("type1") XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("type1")
@ -102,7 +106,9 @@ public class GeoDistanceIT extends ESIntegTestCase {
xContentBuilder.endObject().endObject().endObject().endObject(); xContentBuilder.endObject().endObject().endObject().endObject();
assertAcked(prepareCreate("test").setSettings(settings).addMapping("type1", xContentBuilder)); assertAcked(prepareCreate("test").setSettings(settings).addMapping("type1", xContentBuilder));
ensureGreen(); ensureGreen();
}
public void testDistanceScript() throws Exception {
client().prepareIndex("test", "type1", "1") client().prepareIndex("test", "type1", "1")
.setSource(jsonBuilder().startObject() .setSource(jsonBuilder().startObject()
.field("name", "TestPosition") .field("name", "TestPosition")
@ -156,4 +162,41 @@ public class GeoDistanceIT extends ESIntegTestCase {
assertThat(resultArcDistance6, assertThat(resultArcDistance6,
closeTo(GeoUtils.arcDistance(src_lat, src_lon, tgt_lat, tgt_lon)/1000d, 0.01d)); 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<? extends Range.Bucket> 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());
}
} }