Search in sources :

Example 1 with GeoPoint

use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.

the class QueryDSLDocumentationTests method testGeoPolygon.

public void testGeoPolygon() {
    // tag::geo_polygon
    // <1>
    List<GeoPoint> points = new ArrayList<>();
    points.add(new GeoPoint(40, -70));
    points.add(new GeoPoint(30, -80));
    points.add(new GeoPoint(20, -90));
    // <2>
    geoPolygonQuery("pin.location", points);
// end::geo_polygon
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) ArrayList(java.util.ArrayList)

Example 2 with GeoPoint

use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.

the class GeoHashTests method testGeohashAsLongRoutines.

public void testGeohashAsLongRoutines() {
    final GeoPoint expected = new GeoPoint();
    final GeoPoint actual = new GeoPoint();
    // String based counterpart
    for (double lat = -90; lat < 90; lat++) {
        for (double lng = -180; lng < 180; lng++) {
            for (int p = 1; p <= 12; p++) {
                long geoAsLong = Geohash.longEncode(lng, lat, p);
                // string encode from geohashlong encoded location
                String geohashFromLong = Geohash.stringEncode(geoAsLong);
                // string encode from full res lat lon
                String geohash = Geohash.stringEncode(lng, lat, p);
                // ensure both strings are the same
                assertEquals(geohash, geohashFromLong);
                // decode from the full-res geohash string
                expected.resetFromGeoHash(geohash);
                // decode from the geohash encoded long
                actual.resetFromGeoHash(geoAsLong);
                assertEquals(expected, actual);
            }
        }
    }
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) GeoPoint(org.opensearch.common.geo.GeoPoint)

Example 3 with GeoPoint

use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.

the class GeoDistanceIT method testSimpleWithCustomKeys.

public void testSimpleWithCustomKeys() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(geoDistance("amsterdam_rings", new GeoPoint(52.3760, 4.894)).field("location").unit(DistanceUnit.KILOMETERS).addUnboundedTo("ring1", 500).addRange("ring2", 500, 1000).addUnboundedFrom("ring3", 1000)).get();
    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("ring1"));
    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("ring2"));
    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("ring3"));
    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.opensearch.common.geo.GeoPoint) Bucket(org.opensearch.search.aggregations.bucket.range.Range.Bucket) Range(org.opensearch.search.aggregations.bucket.range.Range) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with GeoPoint

use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.

the class GeoDistanceIT method testEmptyAggregation.

public void testEmptyAggregation() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("empty_bucket_idx").setQuery(matchAllQuery()).addAggregation(histogram("histo").field("value").interval(1L).minDocCount(0).subAggregation(geoDistance("geo_dist", new GeoPoint(52.3760, 4.894)).field("location").addRange("0-100", 0.0, 100.0))).get();
    assertThat(searchResponse.getHits().getTotalHits().value, equalTo(2L));
    Histogram histo = searchResponse.getAggregations().get("histo");
    assertThat(histo, Matchers.notNullValue());
    Histogram.Bucket bucket = histo.getBuckets().get(1);
    assertThat(bucket, Matchers.notNullValue());
    Range geoDistance = bucket.getAggregations().get("geo_dist");
    // TODO: use diamond once JI-9019884 is fixed
    List<Range.Bucket> buckets = new ArrayList<>(geoDistance.getBuckets());
    assertThat(geoDistance, Matchers.notNullValue());
    assertThat(geoDistance.getName(), equalTo("geo_dist"));
    assertThat(buckets.size(), is(1));
    assertThat((String) buckets.get(0).getKey(), equalTo("0-100"));
    assertThat(((Number) buckets.get(0).getFrom()).doubleValue(), equalTo(0.0));
    assertThat(((Number) buckets.get(0).getTo()).doubleValue(), equalTo(100.0));
    assertThat(buckets.get(0).getFromAsString(), equalTo("0.0"));
    assertThat(buckets.get(0).getToAsString(), equalTo("100.0"));
    assertThat(buckets.get(0).getDocCount(), equalTo(0L));
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) Histogram(org.opensearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.opensearch.search.aggregations.bucket.range.Range.Bucket) ArrayList(java.util.ArrayList) Range(org.opensearch.search.aggregations.bucket.range.Range) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 5 with GeoPoint

use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.

the class GeoDistanceIT method testPartiallyUnmapped.

public void testPartiallyUnmapped() throws Exception {
    SearchResponse response = client().prepareSearch("idx", "idx_unmapped").addAggregation(geoDistance("amsterdam_rings", new GeoPoint(52.3760, 4.894)).field("location").unit(DistanceUnit.KILOMETERS).addUnboundedTo(500).addRange(500, 1000).addUnboundedFrom(1000)).get();
    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(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.opensearch.common.geo.GeoPoint) Bucket(org.opensearch.search.aggregations.bucket.range.Range.Bucket) Range(org.opensearch.search.aggregations.bucket.range.Range) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

GeoPoint (org.opensearch.common.geo.GeoPoint)150 SearchResponse (org.opensearch.action.search.SearchResponse)41 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)27 ArrayList (java.util.ArrayList)23 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)19 Matchers.containsString (org.hamcrest.Matchers.containsString)11 XContentParser (org.opensearch.common.xcontent.XContentParser)10 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)10 LatLonPoint (org.apache.lucene.document.LatLonPoint)8 Version (org.opensearch.Version)8 IOException (java.io.IOException)7 LatLonDocValuesField (org.apache.lucene.document.LatLonDocValuesField)7 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)7 OpenSearchParseException (org.opensearch.OpenSearchParseException)7 Range (org.opensearch.search.aggregations.bucket.range.Range)6 Bucket (org.opensearch.search.aggregations.bucket.range.Range.Bucket)6 Map (java.util.Map)5 Document (org.apache.lucene.document.Document)5 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)5 Query (org.apache.lucene.search.Query)5