Search in sources :

Example 1 with DistanceUnit

use of org.opensearch.common.unit.DistanceUnit in project OpenSearch by opensearch-project.

the class GeoDistanceQueryBuilder method fromXContent.

public static GeoDistanceQueryBuilder fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String queryName = null;
    String currentFieldName = null;
    GeoPoint point = new GeoPoint(Double.NaN, Double.NaN);
    String fieldName = null;
    Object vDistance = null;
    DistanceUnit unit = GeoDistanceQueryBuilder.DEFAULT_DISTANCE_UNIT;
    GeoDistance geoDistance = GeoDistanceQueryBuilder.DEFAULT_GEO_DISTANCE;
    GeoValidationMethod validationMethod = null;
    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_ARRAY) {
            fieldName = currentFieldName;
            GeoUtils.parseGeoPoint(parser, point);
        } else if (token == XContentParser.Token.START_OBJECT) {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
            // the json in the format of -> field : { lat : 30, lon : 12 }
            String currentName = parser.currentName();
            fieldName = currentFieldName;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentName = parser.currentName();
                } else if (token.isValue()) {
                    if (currentName.equals("lat")) {
                        point.resetLat(parser.doubleValue());
                    } else if (currentName.equals("lon")) {
                        point.resetLon(parser.doubleValue());
                    } else if (currentName.equals("geohash")) {
                        point.resetFromGeoHash(parser.text());
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "[geo_distance] query does not support [" + currentFieldName + "]");
                    }
                }
            }
        } else if (token.isValue()) {
            if (DISTANCE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                if (token == XContentParser.Token.VALUE_STRING) {
                    // a String
                    vDistance = parser.text();
                } else {
                    // a Number
                    vDistance = parser.numberValue();
                }
            } else if (UNIT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                unit = DistanceUnit.fromString(parser.text());
            } else if (DISTANCE_TYPE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                geoDistance = GeoDistance.fromString(parser.text());
            } else if (currentFieldName.endsWith(".lat")) {
                point.resetLat(parser.doubleValue());
                fieldName = currentFieldName.substring(0, currentFieldName.length() - ".lat".length());
            } else if (currentFieldName.endsWith(".lon")) {
                point.resetLon(parser.doubleValue());
                fieldName = currentFieldName.substring(0, currentFieldName.length() - ".lon".length());
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                queryName = parser.text();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                boost = parser.floatValue();
            } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                ignoreUnmapped = parser.booleanValue();
            } else if (VALIDATION_METHOD_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                validationMethod = GeoValidationMethod.fromString(parser.text());
            } else {
                if (fieldName == null) {
                    point.resetFromString(parser.text());
                    fieldName = currentFieldName;
                } else {
                    throw new ParsingException(parser.getTokenLocation(), "failed to parse [{}] query. unexpected field [{}]", NAME, currentFieldName);
                }
            }
        }
    }
    if (vDistance == null) {
        throw new ParsingException(parser.getTokenLocation(), "geo_distance requires 'distance' to be specified");
    }
    GeoDistanceQueryBuilder qb = new GeoDistanceQueryBuilder(fieldName);
    if (vDistance instanceof Number) {
        qb.distance(((Number) vDistance).doubleValue(), unit);
    } else {
        qb.distance((String) vDistance, unit);
    }
    qb.point(point);
    if (validationMethod != null) {
        qb.setValidationMethod(validationMethod);
    }
    qb.geoDistance(geoDistance);
    qb.boost(boost);
    qb.queryName(queryName);
    qb.ignoreUnmapped(ignoreUnmapped);
    return qb;
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) ParsingException(org.opensearch.common.ParsingException) GeoDistance(org.opensearch.common.geo.GeoDistance) DistanceUnit(org.opensearch.common.unit.DistanceUnit) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 2 with DistanceUnit

use of org.opensearch.common.unit.DistanceUnit in project OpenSearch by opensearch-project.

the class CircleBuilderTests method mutate.

static CircleBuilder mutate(CircleBuilder original) throws IOException {
    CircleBuilder mutation = copyShape(original);
    double radius = original.radius();
    DistanceUnit unit = original.unit();
    if (randomBoolean()) {
        if (original.center().x > 0.0 || original.center().y > 0.0) {
            mutation.center(new Coordinate(original.center().x / 2, original.center().y / 2));
        } else {
            // original center was 0.0, 0.0
            mutation.center(randomDouble() + 0.1, randomDouble() + 0.1);
        }
    } else if (randomBoolean()) {
        if (radius > 0) {
            radius = radius / 2;
        } else {
            radius = randomDouble() + 0.1;
        }
    } else {
        DistanceUnit newRandom = unit;
        while (newRandom == unit) {
            newRandom = randomFrom(DistanceUnit.values());
        }
        unit = newRandom;
    }
    return mutation.radius(radius, unit);
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) DistanceUnit(org.opensearch.common.unit.DistanceUnit)

Example 3 with DistanceUnit

use of org.opensearch.common.unit.DistanceUnit in project OpenSearch by opensearch-project.

the class GeoDistanceSortBuilder method fromXContent.

/**
 * Creates a new {@link GeoDistanceSortBuilder} from the query held by the {@link XContentParser} in
 * {@link org.opensearch.common.xcontent.XContent} format.
 *
 * @param parser the input parser. The state on the parser contained in this context will be changed as a
 *                side effect of this method call
 * @param elementName in some sort syntax variations the field name precedes the xContent object that specifies
 *                    further parameters, e.g. in '{ "foo": { "order" : "asc"} }'. When parsing the inner object,
 *                    the field name can be passed in via this argument
 */
public static GeoDistanceSortBuilder fromXContent(XContentParser parser, String elementName) throws IOException {
    String fieldName = null;
    List<GeoPoint> geoPoints = new ArrayList<>();
    DistanceUnit unit = DistanceUnit.DEFAULT;
    GeoDistance geoDistance = GeoDistance.ARC;
    SortOrder order = SortOrder.ASC;
    SortMode sortMode = null;
    QueryBuilder nestedFilter = null;
    String nestedPath = null;
    NestedSortBuilder nestedSort = null;
    GeoValidationMethod validation = null;
    boolean ignoreUnmapped = false;
    XContentParser.Token token;
    String currentName = parser.currentName();
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentName = parser.currentName();
        } else if (token == XContentParser.Token.START_ARRAY) {
            parseGeoPoints(parser, geoPoints);
            fieldName = currentName;
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (NESTED_FILTER_FIELD.match(currentName, parser.getDeprecationHandler())) {
                deprecationLogger.deprecate("geo_distance_nested_filter", "[nested_filter] has been deprecated in favour of the [nested] parameter");
                nestedFilter = parseInnerQueryBuilder(parser);
            } else if (NESTED_FIELD.match(currentName, parser.getDeprecationHandler())) {
                nestedSort = NestedSortBuilder.fromXContent(parser);
            } else {
                // the json in the format of -> field : { lat : 30, lon : 12 }
                if (fieldName != null && fieldName.equals(currentName) == false) {
                    throw new ParsingException(parser.getTokenLocation(), "Trying to reset fieldName to [{}], already set to [{}].", currentName, fieldName);
                }
                fieldName = currentName;
                GeoPoint point = new GeoPoint();
                GeoUtils.parseGeoPoint(parser, point);
                geoPoints.add(point);
            }
        } else if (token.isValue()) {
            if (ORDER_FIELD.match(currentName, parser.getDeprecationHandler())) {
                order = SortOrder.fromString(parser.text());
            } else if (UNIT_FIELD.match(currentName, parser.getDeprecationHandler())) {
                unit = DistanceUnit.fromString(parser.text());
            } else if (DISTANCE_TYPE_FIELD.match(currentName, parser.getDeprecationHandler())) {
                geoDistance = GeoDistance.fromString(parser.text());
            } else if (VALIDATION_METHOD_FIELD.match(currentName, parser.getDeprecationHandler())) {
                validation = GeoValidationMethod.fromString(parser.text());
            } else if (SORTMODE_FIELD.match(currentName, parser.getDeprecationHandler())) {
                sortMode = SortMode.fromString(parser.text());
            } else if (NESTED_PATH_FIELD.match(currentName, parser.getDeprecationHandler())) {
                deprecationLogger.deprecate("geo_distance_nested_path", "[nested_path] has been deprecated in favour of the [nested] parameter");
                nestedPath = parser.text();
            } else if (IGNORE_UNMAPPED.match(currentName, parser.getDeprecationHandler())) {
                ignoreUnmapped = parser.booleanValue();
            } else if (token == Token.VALUE_STRING) {
                if (fieldName != null && fieldName.equals(currentName) == false) {
                    throw new ParsingException(parser.getTokenLocation(), "Trying to reset fieldName to [{}], already set to [{}].", currentName, fieldName);
                }
                GeoPoint point = new GeoPoint();
                point.resetFromString(parser.text());
                geoPoints.add(point);
                fieldName = currentName;
            } else if (fieldName.equals(currentName)) {
                throw new ParsingException(parser.getTokenLocation(), "Only geohashes of type string supported for field [{}]", currentName);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[{}] does not support [{}]", NAME, currentName);
            }
        }
    }
    GeoDistanceSortBuilder result = new GeoDistanceSortBuilder(fieldName, geoPoints.toArray(new GeoPoint[geoPoints.size()]));
    result.geoDistance(geoDistance);
    result.unit(unit);
    result.order(order);
    if (sortMode != null) {
        result.sortMode(sortMode);
    }
    if (nestedFilter != null) {
        result.setNestedFilter(nestedFilter);
    }
    result.setNestedPath(nestedPath);
    if (nestedSort != null) {
        result.setNestedSort(nestedSort);
    }
    if (validation != null) {
        result.validation(validation);
    }
    result.ignoreUnmapped(ignoreUnmapped);
    return result;
}
Also used : GeoValidationMethod(org.opensearch.index.query.GeoValidationMethod) ArrayList(java.util.ArrayList) AbstractQueryBuilder.parseInnerQueryBuilder(org.opensearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) GeoPoint(org.opensearch.common.geo.GeoPoint) Token(org.opensearch.common.xcontent.XContentParser.Token) ParsingException(org.opensearch.common.ParsingException) GeoDistance(org.opensearch.common.geo.GeoDistance) DistanceUnit(org.opensearch.common.unit.DistanceUnit) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 4 with DistanceUnit

use of org.opensearch.common.unit.DistanceUnit in project OpenSearch by opensearch-project.

the class GeometryIO method readCircle.

private static Circle readCircle(StreamInput in) throws IOException {
    double lon = in.readDouble();
    double lat = in.readDouble();
    double alt = readAlt(in);
    double radius = in.readDouble();
    DistanceUnit distanceUnit = DistanceUnit.readFromStream(in);
    return new Circle(lon, lat, alt, distanceUnit.toMeters(radius));
}
Also used : Circle(org.opensearch.geometry.Circle) DistanceUnit(org.opensearch.common.unit.DistanceUnit)

Example 5 with DistanceUnit

use of org.opensearch.common.unit.DistanceUnit in project OpenSearch by opensearch-project.

the class GeoDistanceQueryBuilderTests method doCreateTestQueryBuilder.

@Override
protected GeoDistanceQueryBuilder doCreateTestQueryBuilder() {
    String fieldName = randomFrom(GEO_POINT_FIELD_NAME, GEO_POINT_ALIAS_FIELD_NAME);
    GeoDistanceQueryBuilder qb = new GeoDistanceQueryBuilder(fieldName);
    String distance = "" + randomDouble();
    if (randomBoolean()) {
        DistanceUnit unit = randomFrom(DistanceUnit.values());
        distance = distance + unit.toString();
    }
    int selector = randomIntBetween(0, 2);
    switch(selector) {
        case 0:
            qb.distance(randomDouble(), randomFrom(DistanceUnit.values()));
            break;
        case 1:
            qb.distance(distance, randomFrom(DistanceUnit.values()));
            break;
        case 2:
            qb.distance(distance);
            break;
    }
    Point p = RandomShapeGenerator.xRandomPoint(random());
    qb.point(new GeoPoint(p.getY(), p.getX()));
    if (randomBoolean()) {
        qb.setValidationMethod(randomFrom(GeoValidationMethod.values()));
    }
    if (randomBoolean()) {
        qb.geoDistance(randomFrom(GeoDistance.values()));
    }
    if (randomBoolean()) {
        qb.ignoreUnmapped(randomBoolean());
    }
    return qb;
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) LatLonPoint(org.apache.lucene.document.LatLonPoint) Point(org.locationtech.spatial4j.shape.Point) GeoPoint(org.opensearch.common.geo.GeoPoint) DistanceUnit(org.opensearch.common.unit.DistanceUnit) LatLonPoint(org.apache.lucene.document.LatLonPoint) Point(org.locationtech.spatial4j.shape.Point) GeoPoint(org.opensearch.common.geo.GeoPoint)

Aggregations

DistanceUnit (org.opensearch.common.unit.DistanceUnit)6 GeoPoint (org.opensearch.common.geo.GeoPoint)3 XContentParser (org.opensearch.common.xcontent.XContentParser)3 ParsingException (org.opensearch.common.ParsingException)2 GeoDistance (org.opensearch.common.geo.GeoDistance)2 ArrayList (java.util.ArrayList)1 LatLonPoint (org.apache.lucene.document.LatLonPoint)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Coordinate (org.locationtech.jts.geom.Coordinate)1 Point (org.locationtech.spatial4j.shape.Point)1 Token (org.opensearch.common.xcontent.XContentParser.Token)1 Circle (org.opensearch.geometry.Circle)1 AbstractQueryBuilder.parseInnerQueryBuilder (org.opensearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder)1 GeoValidationMethod (org.opensearch.index.query.GeoValidationMethod)1 QueryBuilder (org.opensearch.index.query.QueryBuilder)1