Java Code Examples for org.elasticsearch.common.xcontent.XContentParser.Token#VALUE_STRING

The following examples show how to use org.elasticsearch.common.xcontent.XContentParser.Token#VALUE_STRING . 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: GeohashCellQuery.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();

    String fieldName = null;
    String geohash = null;
    int levels = -1;
    boolean neighbors = false;


    XContentParser.Token token;
    if ((token = parser.currentToken()) != Token.START_OBJECT) {
        throw new ElasticsearchParseException("failed to parse [{}] query. expected an object but found [{}] instead", NAME, token);
    }

    while ((token = parser.nextToken()) != Token.END_OBJECT) {
        if (token == Token.FIELD_NAME) {
            String field = parser.currentName();

            if (parseContext.isDeprecatedSetting(field)) {
                // skip
            } else if (PRECISION.equals(field)) {
                token = parser.nextToken();
                if(token == Token.VALUE_NUMBER) {
                    levels = parser.intValue();
                } else if(token == Token.VALUE_STRING) {
                    double meters = DistanceUnit.parse(parser.text(), DistanceUnit.DEFAULT, DistanceUnit.METERS);
                    levels = GeoUtils.geoHashLevelsForPrecision(meters);
                }
            } else if (NEIGHBORS.equals(field)) {
                parser.nextToken();
                neighbors = parser.booleanValue();
            } else {
                fieldName = field;
                token = parser.nextToken();
                if(token == Token.VALUE_STRING) {
                    // A string indicates either a gehash or a lat/lon string
                    String location = parser.text();
                    if(location.indexOf(",")>0) {
                        geohash = GeoUtils.parseGeoPoint(parser).geohash();
                    } else {
                        geohash = location;
                    }
                } else {
                    geohash = GeoUtils.parseGeoPoint(parser).geohash();
                }
            }
        } else {
            throw new ElasticsearchParseException("failed to parse [{}] query. unexpected token [{}]", NAME, token);
        }
    }

    if (geohash == null) {
        throw new QueryParsingException(parseContext, "failed to parse [{}] query. missing geohash value", NAME);
    }

    MappedFieldType fieldType = parseContext.fieldMapper(fieldName);
    if (fieldType == null) {
        throw new QueryParsingException(parseContext, "failed to parse [{}] query. missing [{}] field [{}]", NAME, BaseGeoPointFieldMapper.CONTENT_TYPE, fieldName);
    }

    if (!(fieldType instanceof BaseGeoPointFieldMapper.GeoPointFieldType)) {
        throw new QueryParsingException(parseContext, "failed to parse [{}] query. field [{}] is not a geo_point field", NAME, fieldName);
    }

    BaseGeoPointFieldMapper.GeoPointFieldType geoFieldType = ((BaseGeoPointFieldMapper.GeoPointFieldType) fieldType);
    if (!geoFieldType.isGeoHashPrefixEnabled()) {
        throw new QueryParsingException(parseContext, "failed to parse [{}] query. [geohash_prefix] is not enabled for field [{}]", NAME, fieldName);
    }

    if(levels > 0) {
        int len = Math.min(levels, geohash.length());
        geohash = geohash.substring(0, len);
    }

    Query filter;
    if (neighbors) {
        filter = create(parseContext, geoFieldType, geohash, GeoHashUtils.addNeighbors(geohash, new ArrayList<CharSequence>(8)));
    } else {
        filter = create(parseContext, geoFieldType, geohash, null);
    }

    return filter;
}
 
Example 2
Source File: GeoUtils.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Parse a {@link GeoPoint} with a {@link XContentParser}. A geopoint has one of the following forms:
 * 
 * <ul>
 *     <li>Object: <pre>{&quot;lat&quot;: <i>&lt;latitude&gt;</i>, &quot;lon&quot;: <i>&lt;longitude&gt;</i>}</pre></li>
 *     <li>String: <pre>&quot;<i>&lt;latitude&gt;</i>,<i>&lt;longitude&gt;</i>&quot;</pre></li>
 *     <li>Geohash: <pre>&quot;<i>&lt;geohash&gt;</i>&quot;</pre></li>
 *     <li>Array: <pre>[<i>&lt;longitude&gt;</i>,<i>&lt;latitude&gt;</i>]</pre></li>
 * </ul>
 * 
 * @param parser {@link XContentParser} to parse the value from
 * @param point A {@link GeoPoint} that will be reset by the values parsed
 * @return new {@link GeoPoint} parsed from the parse
 */
public static GeoPoint parseGeoPoint(XContentParser parser, GeoPoint point) throws IOException, ElasticsearchParseException {
    double lat = Double.NaN;
    double lon = Double.NaN;
    String geohash = null;
    
    if(parser.currentToken() == Token.START_OBJECT) {
        while(parser.nextToken() != Token.END_OBJECT) {
            if(parser.currentToken() == Token.FIELD_NAME) {
                String field = parser.currentName();
                if(LATITUDE.equals(field)) {
                    parser.nextToken();
                    switch (parser.currentToken()) {
                        case VALUE_NUMBER:
                        case VALUE_STRING:
                            lat = parser.doubleValue(true);
                            break;
                        default:
                            throw new ElasticsearchParseException("latitude must be a number");
                    }
                } else if (LONGITUDE.equals(field)) {
                    parser.nextToken();
                    switch (parser.currentToken()) {
                        case VALUE_NUMBER:
                        case VALUE_STRING:
                            lon = parser.doubleValue(true);
                            break;
                        default:
                            throw new ElasticsearchParseException("longitude must be a number");
                    }
                } else if (GEOHASH.equals(field)) {
                    if(parser.nextToken() == Token.VALUE_STRING) {
                        geohash = parser.text();
                    } else {
                        throw new ElasticsearchParseException("geohash must be a string");
                    }
                } else {
                    throw new ElasticsearchParseException("field must be either [{}], [{}] or [{}]", LATITUDE, LONGITUDE, GEOHASH);
                }
            } else {
                throw new ElasticsearchParseException("token [{}] not allowed", parser.currentToken());
            }
        }

        if (geohash != null) {
            if(!Double.isNaN(lat) || !Double.isNaN(lon)) {
                throw new ElasticsearchParseException("field must be either lat/lon or geohash");
            } else {
                return point.resetFromGeoHash(geohash);
            }
        } else if (Double.isNaN(lat)) {
            throw new ElasticsearchParseException("field [{}] missing", LATITUDE);
        } else if (Double.isNaN(lon)) {
            throw new ElasticsearchParseException("field [{}] missing", LONGITUDE);
        } else {
            return point.reset(lat, lon);
        }
        
    } else if(parser.currentToken() == Token.START_ARRAY) {
        int element = 0;
        while(parser.nextToken() != Token.END_ARRAY) {
            if(parser.currentToken() == Token.VALUE_NUMBER) {
                element++;
                if(element == 1) {
                    lon = parser.doubleValue();
                } else if(element == 2) {
                    lat = parser.doubleValue();
                } else {
                    throw new ElasticsearchParseException("only two values allowed");
                }
            } else {
                throw new ElasticsearchParseException("numeric value expected");
            }
        }
        return point.reset(lat, lon);
    } else if(parser.currentToken() == Token.VALUE_STRING) {
        String data = parser.text();
        return parseGeoPoint(data, point);
    } else {
        throw new ElasticsearchParseException("geo_point expected");
    }
}
 
Example 3
Source File: NumberFieldMapper.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
    XContentParser parser = context.parser();
    Object value;
    Number numericValue = null;
    if (context.externalValueSet()) {
        value = context.externalValue();
    } else if (parser.currentToken() == Token.VALUE_NULL) {
        value = null;
    } else if (coerce.value()
            && parser.currentToken() == Token.VALUE_STRING
            && parser.textLength() == 0) {
        value = null;
    } else {
        try {
            numericValue = fieldType().type.parse(parser, coerce.value());
        } catch (IllegalArgumentException e) {
            if (ignoreMalformed.value()) {
                context.addIgnoredField(fieldType.name());
                return;
            } else {
                throw e;
            }
        }
        value = numericValue;
    }

    if (value == null) {
        value = fieldType().nullValue();
    }

    if (value == null) {
        return;
    }

    if (numericValue == null) {
        numericValue = fieldType().type.parse(value, coerce.value());
    }

    boolean indexed = fieldType().indexOptions() != IndexOptions.NONE;
    boolean docValued = fieldType().hasDocValues();
    boolean stored = fieldType().stored();
    fields.addAll(fieldType().type.createFields(fieldType().name(), numericValue, indexed, docValued, stored));
    if (docValued == false && (stored || indexed)) {
        createFieldNamesField(context, fields);
    }
}