Java Code Examples for org.elasticsearch.index.mapper.geo.GeoPointFieldMapper#GeoPointFieldType

The following examples show how to use org.elasticsearch.index.mapper.geo.GeoPointFieldMapper#GeoPointFieldType . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DecayFunctionParser.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private AbstractDistanceScoreFunction parseVariable(String fieldName, XContentParser parser, QueryParseContext parseContext, MultiValueMode mode) throws IOException {

        // now, the field must exist, else we cannot read the value for
        // the doc later
        MappedFieldType fieldType = parseContext.fieldMapper(fieldName);
        if (fieldType == null) {
            throw new QueryParsingException(parseContext, "unknown field [{}]", fieldName);
        }

        // dates and time need special handling
        parser.nextToken();
        if (fieldType instanceof DateFieldMapper.DateFieldType) {
            return parseDateVariable(fieldName, parser, parseContext, (DateFieldMapper.DateFieldType) fieldType, mode);
        } else if (fieldType instanceof GeoPointFieldMapper.GeoPointFieldType) {
            return parseGeoVariable(fieldName, parser, parseContext, (GeoPointFieldMapper.GeoPointFieldType) fieldType, mode);
        } else if (fieldType instanceof NumberFieldMapper.NumberFieldType) {
            return parseNumberVariable(fieldName, parser, parseContext, (NumberFieldMapper.NumberFieldType) fieldType, mode);
        } else {
            throw new QueryParsingException(parseContext, "field [{}] is of type [{}], but only numeric types are supported.", fieldName, fieldType);
        }
    }
 
Example 2
Source File: LuceneQueryBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private Query getQuery(Function inner, Context context) {
    RefLiteralPair innerPair = new RefLiteralPair(inner);
    if (!innerPair.isValid()) {
        return null;
    }
    if (innerPair.reference().valueType().equals(DataTypes.GEO_SHAPE)) {
        // we have within('POINT(0 0)', shape_column)
        return genericFunctionFilter(inner, context);
    }
    GeoPointFieldMapper.GeoPointFieldType geoPointFieldType = getGeoPointFieldType(
            innerPair.reference().ident().columnIdent().fqn(),
            context.mapperService);

    Map<String, Object> geoJSON = (Map<String, Object>) innerPair.input().value();
    Shape shape = GeoJSONUtils.map2Shape(geoJSON);
    Geometry geometry = JtsSpatialContext.GEO.getGeometryFrom(shape);
    IndexGeoPointFieldData fieldData = context.fieldDataService.getForField(geoPointFieldType);
    if (geometry.isRectangle()) {
        Rectangle boundingBox = shape.getBoundingBox();
        return new InMemoryGeoBoundingBoxQuery(
                new GeoPoint(boundingBox.getMaxY(), boundingBox.getMinX()),
                new GeoPoint(boundingBox.getMinY(), boundingBox.getMaxX()),
                fieldData
        );
    } else {
        Coordinate[] coordinates = geometry.getCoordinates();
        GeoPoint[] points = new GeoPoint[coordinates.length];
        for (int i = 0; i < coordinates.length; i++) {
            Coordinate coordinate = coordinates[i];
            points[i] = new GeoPoint(coordinate.y, coordinate.x);
        }
        return new GeoPolygonQuery(fieldData, points);
    }
}
 
Example 3
Source File: LuceneQueryBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private static GeoPointFieldMapper.GeoPointFieldType getGeoPointFieldType(String fieldName, MapperService mapperService) {
    MappedFieldType fieldType =  mapperService.smartNameFieldType(fieldName);
    if (fieldType == null) {
        throw new IllegalArgumentException(String.format("column \"%s\" doesn't exist", fieldName));
    }
    if (!(fieldType instanceof GeoPointFieldMapper.GeoPointFieldType)) {
        throw new IllegalArgumentException(String.format("column \"%s\" isn't of type geo_point", fieldName));
    }
    return (GeoPointFieldMapper.GeoPointFieldType) fieldType;
}
 
Example 4
Source File: DecayFunctionParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private AbstractDistanceScoreFunction parseGeoVariable(String fieldName, XContentParser parser, QueryParseContext parseContext,
        GeoPointFieldMapper.GeoPointFieldType fieldType, MultiValueMode mode) throws IOException {
    XContentParser.Token token;
    String parameterName = null;
    GeoPoint origin = new GeoPoint();
    String scaleString = null;
    String offsetString = "0km";
    double decay = 0.5;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            parameterName = parser.currentName();
        } else if (parameterName.equals(DecayFunctionBuilder.SCALE)) {
            scaleString = parser.text();
        } else if (parameterName.equals(DecayFunctionBuilder.ORIGIN)) {
            origin = GeoUtils.parseGeoPoint(parser);
        } else if (parameterName.equals(DecayFunctionBuilder.DECAY)) {
            decay = parser.doubleValue();
        } else if (parameterName.equals(DecayFunctionBuilder.OFFSET)) {
            offsetString = parser.text();
        } else {
            throw new ElasticsearchParseException("parameter [{}] not supported!", parameterName);
        }
    }
    if (origin == null || scaleString == null) {
        throw new ElasticsearchParseException("[{}] and [{}] must be set for geo fields.", DecayFunctionBuilder.ORIGIN, DecayFunctionBuilder.SCALE);
    }
    double scale = DistanceUnit.DEFAULT.parse(scaleString, DistanceUnit.DEFAULT);
    double offset = DistanceUnit.DEFAULT.parse(offsetString, DistanceUnit.DEFAULT);
    IndexGeoPointFieldData indexFieldData = parseContext.getForField(fieldType);
    return new GeoFieldDataScoreFunction(origin, scale, decay, offset, getDecayFunction(), indexFieldData, mode);

}