Search in sources :

Example 71 with GeoPoint

use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.

the class GeoDistanceQueryBuilder method fromXContent.

public static GeoDistanceQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    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 (parseContext.isDeprecatedSetting(currentFieldName)) {
        // skip
        } 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)) {
                if (token == XContentParser.Token.VALUE_STRING) {
                    // a String
                    vDistance = parser.text();
                } else {
                    // a Number
                    vDistance = parser.numberValue();
                }
            } else if (UNIT_FIELD.match(currentFieldName)) {
                unit = DistanceUnit.fromString(parser.text());
            } else if (DISTANCE_TYPE_FIELD.match(currentFieldName)) {
                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)) {
                queryName = parser.text();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName)) {
                ignoreUnmapped = parser.booleanValue();
            } else if (VALIDATION_METHOD_FIELD.match(currentFieldName)) {
                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.elasticsearch.common.geo.GeoPoint) ParsingException(org.elasticsearch.common.ParsingException) GeoDistance(org.elasticsearch.common.geo.GeoDistance) DistanceUnit(org.elasticsearch.common.unit.DistanceUnit) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 72 with GeoPoint

use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.

the class GeoPolygonQueryBuilder method doWriteTo.

@Override
protected void doWriteTo(StreamOutput out) throws IOException {
    out.writeString(fieldName);
    out.writeVInt(shell.size());
    for (GeoPoint point : shell) {
        out.writeGeoPoint(point);
    }
    validationMethod.writeTo(out);
    out.writeBoolean(ignoreUnmapped);
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint)

Example 73 with GeoPoint

use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.

the class GeoContextMapping method parseContext.

@Override
public Set<CharSequence> parseContext(Document document) {
    final Set<CharSequence> geohashes = new HashSet<>();
    if (fieldName != null) {
        IndexableField[] fields = document.getFields(fieldName);
        GeoPoint spare = new GeoPoint();
        if (fields.length == 0) {
            IndexableField[] lonFields = document.getFields(fieldName + ".lon");
            IndexableField[] latFields = document.getFields(fieldName + ".lat");
            if (lonFields.length > 0 && latFields.length > 0) {
                for (int i = 0; i < lonFields.length; i++) {
                    IndexableField lonField = lonFields[i];
                    IndexableField latField = latFields[i];
                    assert lonField.fieldType().docValuesType() == latField.fieldType().docValuesType();
                    // we write doc values fields differently: one field for all values, so we need to only care about indexed fields
                    if (lonField.fieldType().docValuesType() == DocValuesType.NONE) {
                        spare.reset(latField.numericValue().doubleValue(), lonField.numericValue().doubleValue());
                        geohashes.add(stringEncode(spare.getLon(), spare.getLat(), precision));
                    }
                }
            }
        } else {
            for (IndexableField field : fields) {
                if (field instanceof StringField) {
                    spare.resetFromString(field.stringValue());
                } else {
                    // todo return this to .stringValue() once LatLonPoint implements it
                    spare.resetFromIndexableField(field);
                }
                geohashes.add(spare.geohash());
            }
        }
    }
    Set<CharSequence> locations = new HashSet<>();
    for (CharSequence geohash : geohashes) {
        int precision = Math.min(this.precision, geohash.length());
        CharSequence truncatedGeohash = geohash.subSequence(0, precision);
        locations.add(truncatedGeohash);
    }
    return locations;
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) GeoPoint(org.elasticsearch.common.geo.GeoPoint) StringField(org.apache.lucene.document.StringField) GeoPoint(org.elasticsearch.common.geo.GeoPoint) HashSet(java.util.HashSet)

Example 74 with GeoPoint

use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.

the class QueryDSLDocumentationTests method testGeoPolygon.

public void testGeoPolygon() {
    List<GeoPoint> points = new ArrayList<GeoPoint>();
    points.add(new GeoPoint(40, -70));
    points.add(new GeoPoint(30, -80));
    points.add(new GeoPoint(20, -90));
    geoPolygonQuery("pin.location", points);
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) ArrayList(java.util.ArrayList)

Example 75 with GeoPoint

use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.

the class GeoDistanceSortBuilderTests method randomGeoDistanceSortBuilder.

public static GeoDistanceSortBuilder randomGeoDistanceSortBuilder() {
    String fieldName = randomAsciiOfLengthBetween(1, 10);
    GeoDistanceSortBuilder result = null;
    int id = randomIntBetween(0, 2);
    switch(id) {
        case 0:
            int count = randomIntBetween(1, 10);
            String[] geohashes = new String[count];
            for (int i = 0; i < count; i++) {
                geohashes[i] = RandomGeoGenerator.randomPoint(random()).geohash();
            }
            result = new GeoDistanceSortBuilder(fieldName, geohashes);
            break;
        case 1:
            GeoPoint pt = RandomGeoGenerator.randomPoint(random());
            result = new GeoDistanceSortBuilder(fieldName, pt.getLat(), pt.getLon());
            break;
        case 2:
            result = new GeoDistanceSortBuilder(fieldName, points(new GeoPoint[0]));
            break;
        default:
            throw new IllegalStateException("one of three geo initialisation strategies must be used");
    }
    if (randomBoolean()) {
        result.geoDistance(geoDistance(result.geoDistance()));
    }
    if (randomBoolean()) {
        result.unit(randomValueOtherThan(result.unit(), () -> randomFrom(DistanceUnit.values())));
    }
    if (randomBoolean()) {
        result.order(randomFrom(SortOrder.values()));
    }
    if (randomBoolean()) {
        result.sortMode(randomValueOtherThan(SortMode.SUM, () -> randomFrom(SortMode.values())));
    }
    if (randomBoolean()) {
        result.setNestedFilter(new MatchAllQueryBuilder());
    }
    if (randomBoolean()) {
        result.setNestedPath(randomValueOtherThan(result.getNestedPath(), () -> randomAsciiOfLengthBetween(1, 10)));
    }
    if (randomBoolean()) {
        result.validation(randomValueOtherThan(result.validation(), () -> randomFrom(GeoValidationMethod.values())));
    }
    return result;
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) GeoPoint(org.elasticsearch.common.geo.GeoPoint) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Aggregations

GeoPoint (org.elasticsearch.common.geo.GeoPoint)122 SearchResponse (org.elasticsearch.action.search.SearchResponse)40 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)27 ArrayList (java.util.ArrayList)20 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)15 XContentParser (org.elasticsearch.common.xcontent.XContentParser)9 GeoBounds (org.elasticsearch.search.aggregations.metrics.geobounds.GeoBounds)9 Range (org.elasticsearch.search.aggregations.bucket.range.Range)8 GeoCentroid (org.elasticsearch.search.aggregations.metrics.geocentroid.GeoCentroid)8 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)7 Version (org.elasticsearch.Version)7 Bucket (org.elasticsearch.search.aggregations.bucket.range.Range.Bucket)7 Settings (org.elasticsearch.common.settings.Settings)6 MultiGeoPointValues (org.elasticsearch.index.fielddata.MultiGeoPointValues)6 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)5 ParsingException (org.elasticsearch.common.ParsingException)5 SearchHit (org.elasticsearch.search.SearchHit)5 Test (org.testng.annotations.Test)5 HashSet (java.util.HashSet)4 DistanceUnit (org.elasticsearch.common.unit.DistanceUnit)4