Search in sources :

Example 1 with GeoPointFieldMapper

use of org.elasticsearch.index.mapper.GeoPointFieldMapper in project elasticsearch by elastic.

the class GeoContextMapping method parseContext.

/**
     * Parse a set of {@link CharSequence} contexts at index-time.
     * Acceptable formats:
     *
     *  <ul>
     *     <li>Array: <pre>[<i>&lt;GEO POINT&gt;</i>, ..]</pre></li>
     *     <li>String/Object/Array: <pre>&quot;GEO POINT&quot;</pre></li>
     *  </ul>
     *
     * see {@link GeoUtils#parseGeoPoint(String, GeoPoint)} for GEO POINT
     */
@Override
public Set<CharSequence> parseContext(ParseContext parseContext, XContentParser parser) throws IOException, ElasticsearchParseException {
    if (fieldName != null) {
        FieldMapper mapper = parseContext.docMapper().mappers().getMapper(fieldName);
        if (!(mapper instanceof GeoPointFieldMapper)) {
            throw new ElasticsearchParseException("referenced field must be mapped to geo_point");
        }
    }
    final Set<CharSequence> contexts = new HashSet<>();
    Token token = parser.currentToken();
    if (token == Token.START_ARRAY) {
        token = parser.nextToken();
        // Test if value is a single point in <code>[lon, lat]</code> format
        if (token == Token.VALUE_NUMBER) {
            double lon = parser.doubleValue();
            if (parser.nextToken() == Token.VALUE_NUMBER) {
                double lat = parser.doubleValue();
                if (parser.nextToken() == Token.END_ARRAY) {
                    contexts.add(stringEncode(lon, lat, precision));
                } else {
                    throw new ElasticsearchParseException("only two values [lon, lat] expected");
                }
            } else {
                throw new ElasticsearchParseException("latitude must be a numeric value");
            }
        } else {
            while (token != Token.END_ARRAY) {
                GeoPoint point = GeoUtils.parseGeoPoint(parser);
                contexts.add(stringEncode(point.getLon(), point.getLat(), precision));
                token = parser.nextToken();
            }
        }
    } else if (token == Token.VALUE_STRING) {
        final String geoHash = parser.text();
        final CharSequence truncatedGeoHash = geoHash.subSequence(0, Math.min(geoHash.length(), precision));
        contexts.add(truncatedGeoHash);
    } else {
        // or a single location
        GeoPoint point = GeoUtils.parseGeoPoint(parser);
        contexts.add(stringEncode(point.getLon(), point.getLat(), precision));
    }
    return contexts;
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) GeoPointFieldMapper(org.elasticsearch.index.mapper.GeoPointFieldMapper) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) Token(org.elasticsearch.common.xcontent.XContentParser.Token) FieldMapper(org.elasticsearch.index.mapper.FieldMapper) GeoPointFieldMapper(org.elasticsearch.index.mapper.GeoPointFieldMapper) HashSet(java.util.HashSet)

Aggregations

HashSet (java.util.HashSet)1 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)1 GeoPoint (org.elasticsearch.common.geo.GeoPoint)1 Token (org.elasticsearch.common.xcontent.XContentParser.Token)1 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)1 GeoPointFieldMapper (org.elasticsearch.index.mapper.GeoPointFieldMapper)1