Search in sources :

Example 21 with GeoPoint

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

the class GeoPolygonIT method testSimpleUnclosedPolygon.

public void testSimpleUnclosedPolygon() throws Exception {
    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));
    SearchResponse searchResponse = // from NY
    client().prepareSearch("test").setQuery(boolQuery().must(geoPolygonQuery("location", points))).get();
    assertHitCount(searchResponse, 4);
    assertThat(searchResponse.getHits().getHits().length, equalTo(4));
    for (SearchHit hit : searchResponse.getHits()) {
        assertThat(hit.getId(), anyOf(equalTo("1"), equalTo("3"), equalTo("4"), equalTo("5")));
    }
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) SearchHit(org.opensearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 22 with GeoPoint

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

the class GeoCentroidIT method testSingleValueFieldAsSubAggToGeohashGrid.

public void testSingleValueFieldAsSubAggToGeohashGrid() throws Exception {
    SearchResponse response = client().prepareSearch(HIGH_CARD_IDX_NAME).addAggregation(geohashGrid("geoGrid").field(SINGLE_VALUED_FIELD_NAME).subAggregation(geoCentroid(aggName).field(SINGLE_VALUED_FIELD_NAME))).get();
    assertSearchResponse(response);
    GeoGrid grid = response.getAggregations().get("geoGrid");
    assertThat(grid, notNullValue());
    assertThat(grid.getName(), equalTo("geoGrid"));
    List<? extends GeoGrid.Bucket> buckets = grid.getBuckets();
    for (GeoGrid.Bucket cell : buckets) {
        String geohash = cell.getKeyAsString();
        GeoPoint expectedCentroid = expectedCentroidsForGeoHash.get(geohash);
        GeoCentroid centroidAgg = cell.getAggregations().get(aggName);
        assertThat("Geohash " + geohash + " has wrong centroid latitude ", expectedCentroid.lat(), closeTo(centroidAgg.centroid().lat(), GEOHASH_TOLERANCE));
        assertThat("Geohash " + geohash + " has wrong centroid longitude", expectedCentroid.lon(), closeTo(centroidAgg.centroid().lon(), GEOHASH_TOLERANCE));
    }
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) GeoGrid(org.opensearch.search.aggregations.bucket.geogrid.GeoGrid) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 23 with GeoPoint

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

the class GeoCentroidIT method testUnmapped.

public void testUnmapped() throws Exception {
    SearchResponse response = client().prepareSearch(UNMAPPED_IDX_NAME).addAggregation(geoCentroid(aggName).field(SINGLE_VALUED_FIELD_NAME)).get();
    assertSearchResponse(response);
    GeoCentroid geoCentroid = response.getAggregations().get(aggName);
    assertThat(geoCentroid, notNullValue());
    assertThat(geoCentroid.getName(), equalTo(aggName));
    GeoPoint centroid = geoCentroid.centroid();
    assertThat(centroid, equalTo(null));
    assertEquals(0, geoCentroid.count());
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 24 with GeoPoint

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

the class GeoCentroidIT method testMultiValuedField.

public void testMultiValuedField() throws Exception {
    SearchResponse searchResponse = client().prepareSearch(IDX_NAME).setQuery(matchAllQuery()).addAggregation(geoCentroid(aggName).field(MULTI_VALUED_FIELD_NAME)).get();
    assertSearchResponse(searchResponse);
    GeoCentroid geoCentroid = searchResponse.getAggregations().get(aggName);
    assertThat(geoCentroid, notNullValue());
    assertThat(geoCentroid.getName(), equalTo(aggName));
    GeoPoint centroid = geoCentroid.centroid();
    assertThat(centroid.lat(), closeTo(multiCentroid.lat(), GEOHASH_TOLERANCE));
    assertThat(centroid.lon(), closeTo(multiCentroid.lon(), GEOHASH_TOLERANCE));
    assertEquals(2 * numDocs, geoCentroid.count());
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 25 with GeoPoint

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

the class GeoCentroidIT method testSingleValueFieldGetProperty.

public void testSingleValueFieldGetProperty() throws Exception {
    SearchResponse response = client().prepareSearch(IDX_NAME).setQuery(matchAllQuery()).addAggregation(global("global").subAggregation(geoCentroid(aggName).field(SINGLE_VALUED_FIELD_NAME))).get();
    assertSearchResponse(response);
    Global global = response.getAggregations().get("global");
    assertThat(global, notNullValue());
    assertThat(global.getName(), equalTo("global"));
    assertThat(global.getDocCount(), equalTo((long) numDocs));
    assertThat(global.getAggregations(), notNullValue());
    assertThat(global.getAggregations().asMap().size(), equalTo(1));
    GeoCentroid geoCentroid = global.getAggregations().get(aggName);
    assertThat(geoCentroid, notNullValue());
    assertThat(geoCentroid.getName(), equalTo(aggName));
    assertThat((GeoCentroid) ((InternalAggregation) global).getProperty(aggName), sameInstance(geoCentroid));
    GeoPoint centroid = geoCentroid.centroid();
    assertThat(centroid.lat(), closeTo(singleCentroid.lat(), GEOHASH_TOLERANCE));
    assertThat(centroid.lon(), closeTo(singleCentroid.lon(), GEOHASH_TOLERANCE));
    assertThat(((GeoPoint) ((InternalAggregation) global).getProperty(aggName + ".value")).lat(), closeTo(singleCentroid.lat(), GEOHASH_TOLERANCE));
    assertThat(((GeoPoint) ((InternalAggregation) global).getProperty(aggName + ".value")).lon(), closeTo(singleCentroid.lon(), GEOHASH_TOLERANCE));
    assertThat((double) ((InternalAggregation) global).getProperty(aggName + ".lat"), closeTo(singleCentroid.lat(), GEOHASH_TOLERANCE));
    assertThat((double) ((InternalAggregation) global).getProperty(aggName + ".lon"), closeTo(singleCentroid.lon(), GEOHASH_TOLERANCE));
    assertEquals(numDocs, (long) ((InternalAggregation) global).getProperty(aggName + ".count"));
}
Also used : InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) GeoPoint(org.opensearch.common.geo.GeoPoint) Global(org.opensearch.search.aggregations.bucket.global.Global) 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