Search in sources :

Example 1 with InternalGeoDistance

use of org.elasticsearch.search.aggregations.bucket.range.geodistance.InternalGeoDistance in project elasticsearch by elastic.

the class GeoDistanceIT method testGeoDistanceAggregation.

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));
    // no hits please
    search.setSize(0);
    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());
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Aggregations(org.elasticsearch.search.aggregations.Aggregations) InternalGeoDistance(org.elasticsearch.search.aggregations.bucket.range.geodistance.InternalGeoDistance) Range(org.elasticsearch.search.aggregations.bucket.range.Range) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 GeoPoint (org.elasticsearch.common.geo.GeoPoint)1 Aggregations (org.elasticsearch.search.aggregations.Aggregations)1 Range (org.elasticsearch.search.aggregations.bucket.range.Range)1 InternalGeoDistance (org.elasticsearch.search.aggregations.bucket.range.geodistance.InternalGeoDistance)1