Search in sources :

Example 91 with GeoPoint

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

the class MissingValueIT method testGeoCentroid.

public void testGeoCentroid() {
    SearchResponse response = client().prepareSearch("idx").addAggregation(geoCentroid("centroid").field("location").missing("2,1")).get();
    assertSearchResponse(response);
    GeoCentroid centroid = response.getAggregations().get("centroid");
    GeoPoint point = new GeoPoint(1.5, 1.5);
    assertThat(point.lat(), closeTo(centroid.centroid().lat(), 1E-5));
    assertThat(point.lon(), closeTo(centroid.centroid().lon(), 1E-5));
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) GeoCentroid(org.opensearch.search.aggregations.metrics.GeoCentroid) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 92 with GeoPoint

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

the class GeoDistanceIT method testUnmapped.

public void testUnmapped() throws Exception {
    client().admin().cluster().prepareHealth("idx_unmapped").setWaitForYellowStatus().get();
    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)).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(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.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 93 with GeoPoint

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

the class GeoFilterIT method testBulk.

public void testBulk() throws Exception {
    byte[] bulkAction = unZipData("/org/opensearch/search/geo/gzippedmap.gz");
    Version version = VersionUtils.randomIndexCompatibleVersion(random());
    Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version).build();
    XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("country").startObject("properties").startObject("pin").field("type", "geo_point");
    xContentBuilder.field("store", true).endObject().startObject("location").field("type", "geo_shape").field("ignore_malformed", true).endObject().endObject().endObject().endObject();
    client().admin().indices().prepareCreate("countries").setSettings(settings).addMapping("country", xContentBuilder).get();
    BulkResponse bulk = client().prepareBulk().add(bulkAction, 0, bulkAction.length, null, xContentBuilder.contentType()).get();
    for (BulkItemResponse item : bulk.getItems()) {
        assertFalse("unable to index data", item.isFailed());
    }
    client().admin().indices().prepareRefresh().get();
    String key = "DE";
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchQuery("_id", key)).get();
    assertHitCount(searchResponse, 1);
    for (SearchHit hit : searchResponse.getHits()) {
        assertThat(hit.getId(), equalTo(key));
    }
    SearchResponse world = client().prepareSearch().addStoredField("pin").setQuery(geoBoundingBoxQuery("pin").setCorners(90, -179.99999, -90, 179.99999)).get();
    assertHitCount(world, 53);
    SearchResponse distance = client().prepareSearch().addStoredField("pin").setQuery(geoDistanceQuery("pin").distance("425km").point(51.11, 9.851)).get();
    assertHitCount(distance, 5);
    GeoPoint point = new GeoPoint();
    for (SearchHit hit : distance.getHits()) {
        String name = hit.getId();
        point.resetFromString(hit.getFields().get("pin").getValue());
        double dist = distance(point.getLat(), point.getLon(), 51.11, 9.851);
        assertThat("distance to '" + name + "'", dist, lessThanOrEqualTo(425000d));
        assertThat(name, anyOf(equalTo("CZ"), equalTo("DE"), equalTo("BE"), equalTo("NL"), equalTo("LU")));
        if (key.equals(name)) {
            assertThat(dist, closeTo(0d, 0.1d));
        }
    }
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) SearchHit(org.opensearch.search.SearchHit) Version(org.opensearch.Version) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) BulkResponse(org.opensearch.action.bulk.BulkResponse) Settings(org.opensearch.common.settings.Settings) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 94 with GeoPoint

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

the class GeoDistanceIT method testGeoDistanceAggregation.

public void testGeoDistanceAggregation() throws IOException {
    client().prepareIndex("test").setId("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()).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.opensearch.common.geo.GeoPoint) SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) Aggregations(org.opensearch.search.aggregations.Aggregations) InternalGeoDistance(org.opensearch.search.aggregations.bucket.range.InternalGeoDistance) Range(org.opensearch.search.aggregations.bucket.range.Range) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 95 with GeoPoint

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

the class GeoPolygonIT method testFieldAlias.

public void testFieldAlias() {
    List<GeoPoint> points = new ArrayList<>();
    points.add(new GeoPoint(40.7, -74.0));
    points.add(new GeoPoint(40.7, -74.1));
    points.add(new GeoPoint(40.8, -74.1));
    points.add(new GeoPoint(40.8, -74.0));
    points.add(new GeoPoint(40.7, -74.0));
    SearchResponse searchResponse = // from NY
    client().prepareSearch("test").setQuery(boolQuery().must(geoPolygonQuery("alias", points))).get();
    assertHitCount(searchResponse, 4);
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) ArrayList(java.util.ArrayList) 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