Search in sources :

Example 1 with IndexGeoPointFieldData

use of org.elasticsearch.index.fielddata.IndexGeoPointFieldData in project elasticsearch by elastic.

the class GeoDistanceSortBuilder method build.

@Override
public SortFieldAndFormat build(QueryShardContext context) throws IOException {
    final boolean indexCreatedBeforeV2_0 = context.indexVersionCreated().before(Version.V_2_0_0);
    // validation was not available prior to 2.x, so to support bwc percolation queries we only ignore_malformed
    // on 2.x created indexes
    GeoPoint[] localPoints = points.toArray(new GeoPoint[points.size()]);
    if (!indexCreatedBeforeV2_0 && !GeoValidationMethod.isIgnoreMalformed(validation)) {
        for (GeoPoint point : localPoints) {
            if (GeoUtils.isValidLatitude(point.lat()) == false) {
                throw new ElasticsearchParseException("illegal latitude value [{}] for [GeoDistanceSort] for field [{}].", point.lat(), fieldName);
            }
            if (GeoUtils.isValidLongitude(point.lon()) == false) {
                throw new ElasticsearchParseException("illegal longitude value [{}] for [GeoDistanceSort] for field [{}].", point.lon(), fieldName);
            }
        }
    }
    if (GeoValidationMethod.isCoerce(validation)) {
        for (GeoPoint point : localPoints) {
            GeoUtils.normalizePoint(point, true, true);
        }
    }
    boolean reverse = (order == SortOrder.DESC);
    final MultiValueMode finalSortMode;
    if (sortMode == null) {
        finalSortMode = reverse ? MultiValueMode.MAX : MultiValueMode.MIN;
    } else {
        finalSortMode = MultiValueMode.fromString(sortMode.toString());
    }
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType == null) {
        throw new IllegalArgumentException("failed to find mapper for [" + fieldName + "] for geo distance based sort");
    }
    final IndexGeoPointFieldData geoIndexFieldData = context.getForField(fieldType);
    final Nested nested = resolveNested(context, nestedPath, nestedFilter);
    if (// only works with 5.x geo_point
    geoIndexFieldData.getClass() == LatLonPointDVIndexFieldData.class && nested == null && // LatLonDocValuesField internally picks the closest point
    finalSortMode == MultiValueMode.MIN && unit == DistanceUnit.METERS && reverse == false && localPoints.length == 1) {
        return new SortFieldAndFormat(LatLonDocValuesField.newDistanceSort(fieldName, localPoints[0].lat(), localPoints[0].lon()), DocValueFormat.RAW);
    }
    IndexFieldData.XFieldComparatorSource geoDistanceComparatorSource = new IndexFieldData.XFieldComparatorSource() {

        @Override
        public SortField.Type reducedType() {
            return SortField.Type.DOUBLE;
        }

        @Override
        public FieldComparator<?> newComparator(String fieldname, int numHits, int sortPos, boolean reversed) {
            return new FieldComparator.DoubleComparator(numHits, null, null) {

                @Override
                protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException {
                    final MultiGeoPointValues geoPointValues = geoIndexFieldData.load(context).getGeoPointValues();
                    final SortedNumericDoubleValues distanceValues = GeoUtils.distanceValues(geoDistance, unit, geoPointValues, localPoints);
                    final NumericDoubleValues selectedValues;
                    if (nested == null) {
                        selectedValues = finalSortMode.select(distanceValues, Double.POSITIVE_INFINITY);
                    } else {
                        final BitSet rootDocs = nested.rootDocs(context);
                        final DocIdSetIterator innerDocs = nested.innerDocs(context);
                        selectedValues = finalSortMode.select(distanceValues, Double.POSITIVE_INFINITY, rootDocs, innerDocs, context.reader().maxDoc());
                    }
                    return selectedValues.getRawDoubleValues();
                }
            };
        }
    };
    return new SortFieldAndFormat(new SortField(fieldName, geoDistanceComparatorSource, reverse), DocValueFormat.RAW);
}
Also used : IndexGeoPointFieldData(org.elasticsearch.index.fielddata.IndexGeoPointFieldData) Nested(org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) BitSet(org.apache.lucene.util.BitSet) SortField(org.apache.lucene.search.SortField) NumericDoubleValues(org.elasticsearch.index.fielddata.NumericDoubleValues) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) MultiValueMode(org.elasticsearch.search.MultiValueMode) GeoPoint(org.elasticsearch.common.geo.GeoPoint) LatLonPointDVIndexFieldData(org.elasticsearch.index.fielddata.plain.AbstractLatLonPointDVIndexFieldData.LatLonPointDVIndexFieldData) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) LatLonPointDVIndexFieldData(org.elasticsearch.index.fielddata.plain.AbstractLatLonPointDVIndexFieldData.LatLonPointDVIndexFieldData) IndexFieldData(org.elasticsearch.index.fielddata.IndexFieldData) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) MultiGeoPointValues(org.elasticsearch.index.fielddata.MultiGeoPointValues) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator)

Example 2 with IndexGeoPointFieldData

use of org.elasticsearch.index.fielddata.IndexGeoPointFieldData in project elasticsearch by elastic.

the class DecayFunctionBuilder method parseGeoVariable.

private AbstractDistanceScoreFunction parseGeoVariable(XContentParser parser, QueryShardContext context, MappedFieldType 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 (DecayFunctionBuilder.SCALE.equals(parameterName)) {
            scaleString = parser.text();
        } else if (DecayFunctionBuilder.ORIGIN.equals(parameterName)) {
            origin = GeoUtils.parseGeoPoint(parser);
        } else if (DecayFunctionBuilder.DECAY.equals(parameterName)) {
            decay = parser.doubleValue();
        } else if (DecayFunctionBuilder.OFFSET.equals(parameterName)) {
            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 = context.getForField(fieldType);
    return new GeoFieldDataScoreFunction(origin, scale, decay, offset, getDecayFunction(), indexFieldData, mode);
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) IndexGeoPointFieldData(org.elasticsearch.index.fielddata.IndexGeoPointFieldData) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 3 with IndexGeoPointFieldData

use of org.elasticsearch.index.fielddata.IndexGeoPointFieldData in project geocluster-facet by zenobase.

the class GeoClusterFacetParser method parse.

@Override
public FacetExecutor parse(String facetName, XContentParser parser, SearchContext context) throws IOException {
    String fieldName = null;
    double factor = 0.1;
    String currentName = parser.currentName();
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentName = parser.currentName();
        } else if (token.isValue()) {
            if ("field".equals(currentName)) {
                fieldName = parser.text();
            } else if ("factor".equals(currentName)) {
                factor = parser.doubleValue();
            }
        }
    }
    if (factor < 0.0 || factor > 1.0) {
        throw new FacetPhaseExecutionException(facetName, "value [" + factor + "] is not in range [0.0, 1.0]");
    }
    FieldMapper<?> fieldMapper = context.smartNameFieldMapper(fieldName);
    if (fieldMapper == null) {
        throw new FacetPhaseExecutionException(facetName, "failed to find mapping for [" + fieldName + "]");
    }
    IndexGeoPointFieldData indexFieldData = context.fieldData().getForField(fieldMapper);
    return new GeoClusterFacetExecutor(indexFieldData, factor);
}
Also used : FacetPhaseExecutionException(org.elasticsearch.search.facet.FacetPhaseExecutionException) IndexGeoPointFieldData(org.elasticsearch.index.fielddata.IndexGeoPointFieldData) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 4 with IndexGeoPointFieldData

use of org.elasticsearch.index.fielddata.IndexGeoPointFieldData in project elasticsearch by elastic.

the class ValuesSourceConfig method resolve.

/**
     * Resolve a {@link ValuesSourceConfig} given configuration parameters.
     */
public static <VS extends ValuesSource> ValuesSourceConfig<VS> resolve(QueryShardContext context, ValueType valueType, String field, Script script, Object missing, DateTimeZone timeZone, String format) {
    if (field == null) {
        if (script == null) {
            @SuppressWarnings("unchecked") ValuesSourceConfig<VS> config = new ValuesSourceConfig<>(ValuesSourceType.ANY);
            config.format(resolveFormat(null, valueType));
            return config;
        }
        ValuesSourceType valuesSourceType = valueType != null ? valueType.getValuesSourceType() : ValuesSourceType.ANY;
        if (valuesSourceType == ValuesSourceType.ANY) {
            // the specific value source type is undefined, but for scripts,
            // we need to have a specific value source
            // type to know how to handle the script values, so we fallback
            // on Bytes
            valuesSourceType = ValuesSourceType.BYTES;
        }
        ValuesSourceConfig<VS> config = new ValuesSourceConfig<VS>(valuesSourceType);
        config.missing(missing);
        config.timezone(timeZone);
        config.format(resolveFormat(format, valueType));
        config.script(createScript(script, context));
        config.scriptValueType(valueType);
        return config;
    }
    MappedFieldType fieldType = context.fieldMapper(field);
    if (fieldType == null) {
        ValuesSourceType valuesSourceType = valueType != null ? valueType.getValuesSourceType() : ValuesSourceType.ANY;
        ValuesSourceConfig<VS> config = new ValuesSourceConfig<>(valuesSourceType);
        config.missing(missing);
        config.timezone(timeZone);
        config.format(resolveFormat(format, valueType));
        config.unmapped(true);
        if (valueType != null) {
            // todo do we really need this for unmapped?
            config.scriptValueType(valueType);
        }
        return config;
    }
    IndexFieldData<?> indexFieldData = context.getForField(fieldType);
    ValuesSourceConfig<VS> config;
    if (valueType == null) {
        if (indexFieldData instanceof IndexNumericFieldData) {
            config = new ValuesSourceConfig<>(ValuesSourceType.NUMERIC);
        } else if (indexFieldData instanceof IndexGeoPointFieldData) {
            config = new ValuesSourceConfig<>(ValuesSourceType.GEOPOINT);
        } else {
            config = new ValuesSourceConfig<>(ValuesSourceType.BYTES);
        }
    } else {
        config = new ValuesSourceConfig<>(valueType.getValuesSourceType());
    }
    config.fieldContext(new FieldContext(field, indexFieldData, fieldType));
    config.missing(missing);
    config.timezone(timeZone);
    config.script(createScript(script, context));
    config.format(fieldType.docValueFormat(format, timeZone));
    return config;
}
Also used : IndexGeoPointFieldData(org.elasticsearch.index.fielddata.IndexGeoPointFieldData) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexNumericFieldData(org.elasticsearch.index.fielddata.IndexNumericFieldData)

Example 5 with IndexGeoPointFieldData

use of org.elasticsearch.index.fielddata.IndexGeoPointFieldData in project elasticsearch by elastic.

the class MultiValuesSourceAggregationBuilder method config.

public ValuesSourceConfig<VS> config(SearchContext context, String field, Script script) {
    ValueType valueType = this.valueType != null ? this.valueType : targetValueType;
    if (field == null) {
        if (script == null) {
            ValuesSourceConfig<VS> config = new ValuesSourceConfig<>(ValuesSourceType.ANY);
            return config.format(resolveFormat(null, valueType));
        }
        ValuesSourceType valuesSourceType = valueType != null ? valueType.getValuesSourceType() : this.valuesSourceType;
        if (valuesSourceType == null || valuesSourceType == ValuesSourceType.ANY) {
            // the specific value source type is undefined, but for scripts,
            // we need to have a specific value source
            // type to know how to handle the script values, so we fallback
            // on Bytes
            valuesSourceType = ValuesSourceType.BYTES;
        }
        ValuesSourceConfig<VS> config = new ValuesSourceConfig<>(valuesSourceType);
        config.missing(missingMap.get(field));
        return config.format(resolveFormat(format, valueType));
    }
    MappedFieldType fieldType = context.smartNameFieldType(field);
    if (fieldType == null) {
        ValuesSourceType valuesSourceType = valueType != null ? valueType.getValuesSourceType() : this.valuesSourceType;
        ValuesSourceConfig<VS> config = new ValuesSourceConfig<>(valuesSourceType);
        config.missing(missingMap.get(field));
        config.format(resolveFormat(format, valueType));
        return config.unmapped(true);
    }
    IndexFieldData<?> indexFieldData = context.fieldData().getForField(fieldType);
    ValuesSourceConfig<VS> config;
    if (valuesSourceType == ValuesSourceType.ANY) {
        if (indexFieldData instanceof IndexNumericFieldData) {
            config = new ValuesSourceConfig<>(ValuesSourceType.NUMERIC);
        } else if (indexFieldData instanceof IndexGeoPointFieldData) {
            config = new ValuesSourceConfig<>(ValuesSourceType.GEOPOINT);
        } else {
            config = new ValuesSourceConfig<>(ValuesSourceType.BYTES);
        }
    } else {
        config = new ValuesSourceConfig<>(valuesSourceType);
    }
    config.fieldContext(new FieldContext(field, indexFieldData, fieldType));
    config.missing(missingMap.get(field));
    return config.format(fieldType.docValueFormat(format, null));
}
Also used : IndexGeoPointFieldData(org.elasticsearch.index.fielddata.IndexGeoPointFieldData) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexNumericFieldData(org.elasticsearch.index.fielddata.IndexNumericFieldData)

Aggregations

IndexGeoPointFieldData (org.elasticsearch.index.fielddata.IndexGeoPointFieldData)5 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)3 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)2 GeoPoint (org.elasticsearch.common.geo.GeoPoint)2 XContentParser (org.elasticsearch.common.xcontent.XContentParser)2 IndexNumericFieldData (org.elasticsearch.index.fielddata.IndexNumericFieldData)2 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)1 SortField (org.apache.lucene.search.SortField)1 BitSet (org.apache.lucene.util.BitSet)1 IndexFieldData (org.elasticsearch.index.fielddata.IndexFieldData)1 Nested (org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested)1 MultiGeoPointValues (org.elasticsearch.index.fielddata.MultiGeoPointValues)1 NumericDoubleValues (org.elasticsearch.index.fielddata.NumericDoubleValues)1 SortedNumericDoubleValues (org.elasticsearch.index.fielddata.SortedNumericDoubleValues)1 LatLonPointDVIndexFieldData (org.elasticsearch.index.fielddata.plain.AbstractLatLonPointDVIndexFieldData.LatLonPointDVIndexFieldData)1 MultiValueMode (org.elasticsearch.search.MultiValueMode)1 FacetPhaseExecutionException (org.elasticsearch.search.facet.FacetPhaseExecutionException)1