use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class GeoDistanceIT method testWithSubAggregation.
public void testWithSubAggregation() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(geoDistance("amsterdam_rings", new GeoPoint(52.3760, 4.894)).field("location").unit(DistanceUnit.KILOMETERS).addUnboundedTo(500).addRange(500, 1000).addUnboundedFrom(1000).subAggregation(terms("cities").field("city").collectMode(randomFrom(SubAggCollectionMode.values())))).execute().actionGet();
assertSearchResponse(response);
Range geoDist = response.getAggregations().get("amsterdam_rings");
assertThat(geoDist, notNullValue());
assertThat(geoDist.getName(), equalTo("amsterdam_rings"));
List<? extends Bucket> buckets = geoDist.getBuckets();
assertThat(geoDist.getBuckets().size(), equalTo(3));
assertThat(geoDist.getProperty("_bucket_count"), equalTo(3));
Object[] propertiesKeys = (Object[]) geoDist.getProperty("_key");
Object[] propertiesDocCounts = (Object[]) geoDist.getProperty("_count");
Object[] propertiesCities = (Object[]) geoDist.getProperty("cities");
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-500.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(0.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(500.0));
assertThat(bucket.getFromAsString(), equalTo("0.0"));
assertThat(bucket.getToAsString(), equalTo("500.0"));
assertThat(bucket.getDocCount(), equalTo(2L));
assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
Terms cities = bucket.getAggregations().get("cities");
assertThat(cities, Matchers.notNullValue());
Set<String> names = new HashSet<>();
for (Terms.Bucket city : cities.getBuckets()) {
names.add(city.getKeyAsString());
}
assertThat(names.contains("utrecht") && names.contains("haarlem"), is(true));
assertThat((String) propertiesKeys[0], equalTo("*-500.0"));
assertThat((long) propertiesDocCounts[0], equalTo(2L));
assertThat((Terms) propertiesCities[0], sameInstance(cities));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("500.0-1000.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(500.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(1000.0));
assertThat(bucket.getFromAsString(), equalTo("500.0"));
assertThat(bucket.getToAsString(), equalTo("1000.0"));
assertThat(bucket.getDocCount(), equalTo(2L));
assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
cities = bucket.getAggregations().get("cities");
assertThat(cities, Matchers.notNullValue());
names = new HashSet<>();
for (Terms.Bucket city : cities.getBuckets()) {
names.add(city.getKeyAsString());
}
assertThat(names.contains("berlin") && names.contains("prague"), is(true));
assertThat((String) propertiesKeys[1], equalTo("500.0-1000.0"));
assertThat((long) propertiesDocCounts[1], equalTo(2L));
assertThat((Terms) propertiesCities[1], sameInstance(cities));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("1000.0-*"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(1000.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(bucket.getFromAsString(), equalTo("1000.0"));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(1L));
assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
cities = bucket.getAggregations().get("cities");
assertThat(cities, Matchers.notNullValue());
names = new HashSet<>();
for (Terms.Bucket city : cities.getBuckets()) {
names.add(city.getKeyAsString());
}
assertThat(names.contains("tel-aviv"), is(true));
assertThat((String) propertiesKeys[2], equalTo("1000.0-*"));
assertThat((long) propertiesDocCounts[2], equalTo(1L));
assertThat((Terms) propertiesCities[2], sameInstance(cities));
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class GeoDistanceIT method testUnmapped.
public void testUnmapped() throws Exception {
client().admin().cluster().prepareHealth("idx_unmapped").setWaitForYellowStatus().execute().actionGet();
SearchResponse response = client().prepareSearch("idx_unmapped").addAggregation(geoDistance("amsterdam_rings", new GeoPoint(52.3760, 4.894)).field("location").unit(DistanceUnit.KILOMETERS).addUnboundedTo(500).addRange(500, 1000).addUnboundedFrom(1000)).execute().actionGet();
assertSearchResponse(response);
Range geoDist = response.getAggregations().get("amsterdam_rings");
assertThat(geoDist, notNullValue());
assertThat(geoDist.getName(), equalTo("amsterdam_rings"));
List<? extends Bucket> buckets = geoDist.getBuckets();
assertThat(geoDist.getBuckets().size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-500.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(0.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(500.0));
assertThat(bucket.getFromAsString(), equalTo("0.0"));
assertThat(bucket.getToAsString(), equalTo("500.0"));
assertThat(bucket.getDocCount(), equalTo(0L));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("500.0-1000.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(500.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(1000.0));
assertThat(bucket.getFromAsString(), equalTo("500.0"));
assertThat(bucket.getToAsString(), equalTo("1000.0"));
assertThat(bucket.getDocCount(), equalTo(0L));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("1000.0-*"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(1000.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(bucket.getFromAsString(), equalTo("1000.0"));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(0L));
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class GeoDistanceIT method testSimple.
public void testSimple() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(geoDistance("amsterdam_rings", new GeoPoint(52.3760, 4.894)).field("location").unit(DistanceUnit.KILOMETERS).addUnboundedTo(500).addRange(500, 1000).addUnboundedFrom(1000)).execute().actionGet();
assertSearchResponse(response);
Range geoDist = response.getAggregations().get("amsterdam_rings");
assertThat(geoDist, notNullValue());
assertThat(geoDist.getName(), equalTo("amsterdam_rings"));
List<? extends Bucket> buckets = geoDist.getBuckets();
assertThat(buckets.size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("*-500.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(0.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(500.0));
assertThat(bucket.getFromAsString(), equalTo("0.0"));
assertThat(bucket.getToAsString(), equalTo("500.0"));
assertThat(bucket.getDocCount(), equalTo(2L));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("500.0-1000.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(500.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(1000.0));
assertThat(bucket.getFromAsString(), equalTo("500.0"));
assertThat(bucket.getToAsString(), equalTo("1000.0"));
assertThat(bucket.getDocCount(), equalTo(2L));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("1000.0-*"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(1000.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(bucket.getFromAsString(), equalTo("1000.0"));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(1L));
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class AbstractGeoTestCase method updateHashCentroid.
private GeoPoint updateHashCentroid(String hash, final GeoPoint location) {
GeoPoint centroid = expectedCentroidsForGeoHash.getOrDefault(hash, null);
if (centroid == null) {
return new GeoPoint(location.lat(), location.lon());
}
final int docCount = expectedDocCountsForGeoHash.get(hash);
final double newLon = centroid.lon() + (location.lon() - centroid.lon()) / docCount;
final double newLat = centroid.lat() + (location.lat() - centroid.lat()) / docCount;
return centroid.reset(newLat, newLon);
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class GeoBoundsIT method testEmptyAggregation.
public void testEmptyAggregation() throws Exception {
SearchResponse searchResponse = client().prepareSearch(EMPTY_IDX_NAME).setQuery(matchAllQuery()).addAggregation(geoBounds(aggName).field(SINGLE_VALUED_FIELD_NAME).wrapLongitude(false)).execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(0L));
GeoBounds geoBounds = searchResponse.getAggregations().get(aggName);
assertThat(geoBounds, notNullValue());
assertThat(geoBounds.getName(), equalTo(aggName));
GeoPoint topLeft = geoBounds.topLeft();
GeoPoint bottomRight = geoBounds.bottomRight();
assertThat(topLeft, equalTo(null));
assertThat(bottomRight, equalTo(null));
}
Aggregations