org.elasticsearch.index.mapper.AbstractGeometryFieldMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch Show documentation
Show all versions of elasticsearch Show documentation
Elasticsearch subproject :server
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.index.mapper;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.geo.GeoJsonGeometryFormat;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.support.MapXContentParser;
import org.elasticsearch.index.analysis.NamedAnalyzer;
import org.elasticsearch.index.query.QueryShardContext;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
/**
* Base field mapper class for all spatial field types
*/
public abstract class AbstractGeometryFieldMapper extends FieldMapper {
public static Parameter> ignoreMalformedParam(Function> initializer,
boolean ignoreMalformedByDefault) {
return Parameter.explicitBoolParam("ignore_malformed", true, initializer, ignoreMalformedByDefault);
}
public static Parameter> ignoreZValueParam(Function> initializer) {
return Parameter.explicitBoolParam("ignore_z_value", true, initializer, true);
}
/**
* Interface representing an preprocessor in geometry indexing pipeline
*/
public interface Indexer {
Processed prepareForIndexing(Parsed geometry);
Class processedClass();
List indexShape(ParseContext context, Processed shape);
}
/**
* Interface representing parser in geometry indexing pipeline.
*/
public abstract static class Parser {
/**
* Parse the given xContent value to an object of type {@link Parsed}. The value can be
* in any supported format.
*/
public abstract Parsed parse(XContentParser parser) throws IOException, ParseException;
/**
* Given a parsed value and a format string, formats the value into a plain Java object.
*
* Supported formats include 'geojson' and 'wkt'. The different formats are defined
* as subclasses of {@link org.elasticsearch.common.geo.GeometryFormat}.
*/
public abstract Object format(Parsed value, String format);
/**
* Parses the given value, then formats it according to the 'format' string.
*
* By default, this method simply parses the value using {@link Parser#parse}, then formats
* it with {@link Parser#format}. However some {@link Parser} implementations override this
* as they can avoid parsing the value if it is already in the right format.
*/
public Object parseAndFormatObject(Object value, String format) {
Parsed geometry;
try (XContentParser parser = new MapXContentParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE,
Collections.singletonMap("dummy_field", value), XContentType.JSON)) {
parser.nextToken(); // start object
parser.nextToken(); // field name
parser.nextToken(); // field value
geometry = parse(parser);
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (ParseException e) {
throw new RuntimeException(e);
}
return format(geometry, format);
}
}
public abstract static class AbstractGeometryFieldType extends MappedFieldType {
protected final Parser geometryParser;
protected final boolean parsesArrayValue;
protected AbstractGeometryFieldType(String name, boolean indexed, boolean stored, boolean hasDocValues,
boolean parsesArrayValue, Parser geometryParser, Map meta) {
super(name, indexed, stored, hasDocValues, TextSearchInfo.NONE, meta);
this.parsesArrayValue = parsesArrayValue;
this.geometryParser = geometryParser;
}
@Override
public final Query termQuery(Object value, QueryShardContext context) {
throw new IllegalArgumentException("Geometry fields do not support exact searching, use dedicated geometry queries instead: ["
+ name() + "]");
}
@Override
public final ValueFetcher valueFetcher(QueryShardContext context, String format) {
String geoFormat = format != null ? format : GeoJsonGeometryFormat.NAME;
Function