use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class GeoDistanceIT method testSimple.
public void testSimple() throws Exception {
SearchResponse response = client().prepareSearch("idx").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(buckets.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));
}
use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class GeoDistanceIT method testWithSubAggregation.
public void testWithSubAggregation() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(geoDistance("amsterdam_rings", new GeoPoint(52.3760, 4.894)).field("location").unit(DistanceUnit.KILOMETERS).addUnboundedTo(500).addRange(500, 1000).addUnboundedFrom(1000).subAggregation(terms("cities").field("city").collectMode(randomFrom(SubAggCollectionMode.values())))).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));
assertThat(((InternalAggregation) geoDist).getProperty("_bucket_count"), equalTo(3));
Object[] propertiesKeys = (Object[]) ((InternalAggregation) geoDist).getProperty("_key");
Object[] propertiesDocCounts = (Object[]) ((InternalAggregation) geoDist).getProperty("_count");
Object[] propertiesCities = (Object[]) ((InternalAggregation) geoDist).getProperty("cities");
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));
assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
Terms cities = bucket.getAggregations().get("cities");
assertThat(cities, Matchers.notNullValue());
Set<String> names = new HashSet<>();
for (Terms.Bucket city : cities.getBuckets()) {
names.add(city.getKeyAsString());
}
assertThat(names.contains("utrecht") && names.contains("haarlem"), is(true));
assertThat((String) propertiesKeys[0], equalTo("*-500.0"));
assertThat((long) propertiesDocCounts[0], equalTo(2L));
assertThat((Terms) propertiesCities[0], sameInstance(cities));
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));
assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
cities = bucket.getAggregations().get("cities");
assertThat(cities, Matchers.notNullValue());
names = new HashSet<>();
for (Terms.Bucket city : cities.getBuckets()) {
names.add(city.getKeyAsString());
}
assertThat(names.contains("berlin") && names.contains("prague"), is(true));
assertThat((String) propertiesKeys[1], equalTo("500.0-1000.0"));
assertThat((long) propertiesDocCounts[1], equalTo(2L));
assertThat((Terms) propertiesCities[1], sameInstance(cities));
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));
assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
cities = bucket.getAggregations().get("cities");
assertThat(cities, Matchers.notNullValue());
names = new HashSet<>();
for (Terms.Bucket city : cities.getBuckets()) {
names.add(city.getKeyAsString());
}
assertThat(names.contains("tel-aviv"), is(true));
assertThat((String) propertiesKeys[2], equalTo("1000.0-*"));
assertThat((long) propertiesDocCounts[2], equalTo(1L));
assertThat((Terms) propertiesCities[2], sameInstance(cities));
}
use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class GeoDistanceIT method testMultiValues.
public void testMultiValues() throws Exception {
SearchResponse response = client().prepareSearch("idx-multi").addAggregation(geoDistance("amsterdam_rings", new GeoPoint(52.3760, 4.894)).field("location").unit(DistanceUnit.KILOMETERS).distanceType(org.opensearch.common.geo.GeoDistance.ARC).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));
}
use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class MultiFieldsIntegrationIT method testGeoPointMultiField.
@SuppressWarnings("unchecked")
public void testGeoPointMultiField() throws Exception {
assertAcked(client().admin().indices().prepareCreate("my-index").addMapping("my-type", createMappingSource("geo_point")));
GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("my-index").get();
MappingMetadata mappingMetadata = getMappingsResponse.mappings().get("my-index");
assertThat(mappingMetadata, not(nullValue()));
Map<String, Object> mappingSource = mappingMetadata.sourceAsMap();
Map<String, Object> aField = ((Map<String, Object>) XContentMapValues.extractValue("properties.a", mappingSource));
logger.info("Keys: {}", aField.keySet());
assertThat(aField.size(), equalTo(2));
assertThat(aField.get("type").toString(), equalTo("geo_point"));
assertThat(aField.get("fields"), notNullValue());
Map<String, Object> bField = ((Map<String, Object>) XContentMapValues.extractValue("properties.a.fields.b", mappingSource));
assertThat(bField.size(), equalTo(1));
assertThat(bField.get("type").toString(), equalTo("keyword"));
GeoPoint point = new GeoPoint(51, 19);
client().prepareIndex("my-index").setId("1").setSource("a", point.toString()).setRefreshPolicy(IMMEDIATE).get();
SearchResponse countResponse = client().prepareSearch("my-index").setSize(0).setQuery(constantScoreQuery(geoDistanceQuery("a").point(51, 19).distance(50, DistanceUnit.KILOMETERS))).get();
assertThat(countResponse.getHits().getTotalHits().value, equalTo(1L));
countResponse = client().prepareSearch("my-index").setSize(0).setQuery(matchQuery("a.b", point.geohash())).get();
assertThat(countResponse.getHits().getTotalHits().value, equalTo(1L));
}
use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class GeoHashGridIT method testSimple.
public void testSimple() throws Exception {
for (int precision = 1; precision <= PRECISION; precision++) {
SearchResponse response = client().prepareSearch("idx").addAggregation(geohashGrid("geohashgrid").field("location").precision(precision)).get();
assertSearchResponse(response);
GeoGrid geoGrid = response.getAggregations().get("geohashgrid");
List<? extends Bucket> buckets = geoGrid.getBuckets();
Object[] propertiesKeys = (Object[]) ((InternalAggregation) geoGrid).getProperty("_key");
Object[] propertiesDocCounts = (Object[]) ((InternalAggregation) geoGrid).getProperty("_count");
for (int i = 0; i < buckets.size(); i++) {
GeoGrid.Bucket cell = buckets.get(i);
String geohash = cell.getKeyAsString();
long bucketCount = cell.getDocCount();
int expectedBucketCount = expectedDocCountsForGeoHash.get(geohash);
assertNotSame(bucketCount, 0);
assertEquals("Geohash " + geohash + " has wrong doc count ", expectedBucketCount, bucketCount);
GeoPoint geoPoint = (GeoPoint) propertiesKeys[i];
assertThat(stringEncode(geoPoint.lon(), geoPoint.lat(), precision), equalTo(geohash));
assertThat((long) propertiesDocCounts[i], equalTo(bucketCount));
}
}
}
Aggregations