Search in sources :

Example 96 with GeoPoint

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

the class GeoPolygonIT method testSimplePolygon.

public void testSimplePolygon() 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));
    points.add(new GeoPoint(40.7, -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 97 with GeoPoint

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

the class GeoPolygonQueryBuilder method fromXContent.

public static GeoPolygonQueryBuilder fromXContent(XContentParser parser) throws IOException {
    String fieldName = null;
    List<GeoPoint> shell = null;
    Float boost = null;
    GeoValidationMethod validationMethod = null;
    String queryName = null;
    String currentFieldName = null;
    XContentParser.Token token;
    boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
            fieldName = currentFieldName;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if (token == XContentParser.Token.START_ARRAY) {
                    if (POINTS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        shell = new ArrayList<>();
                        while ((token = parser.nextToken()) != Token.END_ARRAY) {
                            shell.add(GeoUtils.parseGeoPoint(parser));
                        }
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "[geo_polygon] query does not support [" + currentFieldName + "]");
                    }
                } else {
                    throw new ParsingException(parser.getTokenLocation(), "[geo_polygon] query does not support token type [" + token.name() + "] under [" + currentFieldName + "]");
                }
            }
        } else if (token.isValue()) {
            if ("_name".equals(currentFieldName)) {
                queryName = parser.text();
            } else if ("boost".equals(currentFieldName)) {
                boost = parser.floatValue();
            } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                ignoreUnmapped = parser.booleanValue();
            } else if (VALIDATION_METHOD.match(currentFieldName, parser.getDeprecationHandler())) {
                validationMethod = GeoValidationMethod.fromString(parser.text());
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[geo_polygon] query does not support [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[geo_polygon] unexpected token type [" + token.name() + "]");
        }
    }
    GeoPolygonQueryBuilder builder = new GeoPolygonQueryBuilder(fieldName, shell);
    if (validationMethod != null) {
        // if GeoValidationMethod was explicitly set ignore deprecated coerce and ignoreMalformed settings
        builder.setValidationMethod(validationMethod);
    }
    if (queryName != null) {
        builder.queryName(queryName);
    }
    if (boost != null) {
        builder.boost(boost);
    }
    builder.ignoreUnmapped(ignoreUnmapped);
    return builder;
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) Token(org.opensearch.common.xcontent.XContentParser.Token) ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 98 with GeoPoint

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

the class GeoPolygonQueryBuilder method doXContent.

@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject(NAME);
    builder.startObject(fieldName);
    builder.startArray(POINTS_FIELD.getPreferredName());
    for (GeoPoint point : shell) {
        builder.startArray().value(point.lon()).value(point.lat()).endArray();
    }
    builder.endArray();
    builder.endObject();
    builder.field(VALIDATION_METHOD.getPreferredName(), validationMethod);
    builder.field(IGNORE_UNMAPPED_FIELD.getPreferredName(), ignoreUnmapped);
    printBoostAndQueryName(builder);
    builder.endObject();
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint)

Example 99 with GeoPoint

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

the class GeoBoundingBoxQueryBuilder method doToQuery.

@Override
public Query doToQuery(QueryShardContext context) {
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new QueryShardException(context, "failed to find geo_point field [" + fieldName + "]");
        }
    }
    if (!(fieldType instanceof GeoPointFieldType)) {
        throw new QueryShardException(context, "field [" + fieldName + "] is not a geo_point field");
    }
    QueryValidationException exception = checkLatLon();
    if (exception != null) {
        throw new QueryShardException(context, "couldn't validate latitude/ longitude values", exception);
    }
    GeoPoint luceneTopLeft = new GeoPoint(geoBoundingBox.topLeft());
    GeoPoint luceneBottomRight = new GeoPoint(geoBoundingBox.bottomRight());
    if (GeoValidationMethod.isCoerce(validationMethod)) {
        // Special case: if the difference between the left and right is 360 and the right is greater than the left, we are asking for
        // the complete longitude range so need to set longitude to the complete longitude range
        double right = luceneBottomRight.getLon();
        double left = luceneTopLeft.getLon();
        boolean completeLonRange = ((right - left) % 360 == 0 && right > left);
        GeoUtils.normalizePoint(luceneTopLeft, true, !completeLonRange);
        GeoUtils.normalizePoint(luceneBottomRight, true, !completeLonRange);
        if (completeLonRange) {
            luceneTopLeft.resetLon(-180);
            luceneBottomRight.resetLon(180);
        }
    }
    Query query = LatLonPoint.newBoxQuery(fieldType.name(), luceneBottomRight.getLat(), luceneTopLeft.getLat(), luceneTopLeft.getLon(), luceneBottomRight.getLon());
    if (fieldType.hasDocValues()) {
        Query dvQuery = LatLonDocValuesField.newSlowBoxQuery(fieldType.name(), luceneBottomRight.getLat(), luceneTopLeft.getLat(), luceneTopLeft.getLon(), luceneBottomRight.getLon());
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) GeoPointFieldType(org.opensearch.index.mapper.GeoPointFieldMapper.GeoPointFieldType) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery)

Example 100 with GeoPoint

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

the class GeoBoundingBoxQueryBuilder method checkLatLon.

QueryValidationException checkLatLon() {
    if (GeoValidationMethod.isIgnoreMalformed(validationMethod)) {
        return null;
    }
    GeoPoint topLeft = geoBoundingBox.topLeft();
    GeoPoint bottomRight = geoBoundingBox.bottomRight();
    QueryValidationException validationException = null;
    // For everything post 2.0 validate latitude and longitude unless validation was explicitly turned off
    if (GeoUtils.isValidLatitude(topLeft.getLat()) == false) {
        validationException = addValidationError("top latitude is invalid: " + topLeft.getLat(), validationException);
    }
    if (GeoUtils.isValidLongitude(topLeft.getLon()) == false) {
        validationException = addValidationError("left longitude is invalid: " + topLeft.getLon(), validationException);
    }
    if (GeoUtils.isValidLatitude(bottomRight.getLat()) == false) {
        validationException = addValidationError("bottom latitude is invalid: " + bottomRight.getLat(), validationException);
    }
    if (GeoUtils.isValidLongitude(bottomRight.getLon()) == false) {
        validationException = addValidationError("right longitude is invalid: " + bottomRight.getLon(), validationException);
    }
    return validationException;
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint)

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