Search in sources :

Example 46 with GeoPoint

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));
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.range.Range.Bucket) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Range(org.elasticsearch.search.aggregations.bucket.range.Range) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) GeoPoint(org.elasticsearch.common.geo.GeoPoint) HashSet(java.util.HashSet)

Example 47 with GeoPoint

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));
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) Bucket(org.elasticsearch.search.aggregations.bucket.range.Range.Bucket) Range(org.elasticsearch.search.aggregations.bucket.range.Range) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 48 with GeoPoint

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));
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) Bucket(org.elasticsearch.search.aggregations.bucket.range.Range.Bucket) Range(org.elasticsearch.search.aggregations.bucket.range.Range) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 49 with GeoPoint

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);
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) GeoPoint(org.elasticsearch.common.geo.GeoPoint)

Example 50 with GeoPoint

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));
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) GeoBounds(org.elasticsearch.search.aggregations.metrics.geobounds.GeoBounds) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

GeoPoint (org.elasticsearch.common.geo.GeoPoint)122 SearchResponse (org.elasticsearch.action.search.SearchResponse)40 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)27 ArrayList (java.util.ArrayList)20 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)15 XContentParser (org.elasticsearch.common.xcontent.XContentParser)9 GeoBounds (org.elasticsearch.search.aggregations.metrics.geobounds.GeoBounds)9 Range (org.elasticsearch.search.aggregations.bucket.range.Range)8 GeoCentroid (org.elasticsearch.search.aggregations.metrics.geocentroid.GeoCentroid)8 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)7 Version (org.elasticsearch.Version)7 Bucket (org.elasticsearch.search.aggregations.bucket.range.Range.Bucket)7 Settings (org.elasticsearch.common.settings.Settings)6 MultiGeoPointValues (org.elasticsearch.index.fielddata.MultiGeoPointValues)6 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)5 ParsingException (org.elasticsearch.common.ParsingException)5 SearchHit (org.elasticsearch.search.SearchHit)5 Test (org.testng.annotations.Test)5 HashSet (java.util.HashSet)4 DistanceUnit (org.elasticsearch.common.unit.DistanceUnit)4