Search in sources :

Example 6 with GeoPoint

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));
}
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 7 with GeoPoint

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));
}
Also used : Bucket(org.opensearch.search.aggregations.bucket.range.Range.Bucket) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) Range(org.opensearch.search.aggregations.bucket.range.Range) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) GeoPoint(org.opensearch.common.geo.GeoPoint) HashSet(java.util.HashSet)

Example 8 with GeoPoint

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));
}
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 9 with GeoPoint

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));
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) GetMappingsResponse(org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 10 with GeoPoint

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));
        }
    }
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) Bucket(org.opensearch.search.aggregations.bucket.geogrid.GeoGrid.Bucket) GeoGrid(org.opensearch.search.aggregations.bucket.geogrid.GeoGrid) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoPoint(org.opensearch.common.geo.GeoPoint) 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