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));
}
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));
}
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));
}
}
}
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());
}
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);
}
Aggregations