public GeoDistanceRangeFilter( double lat, double lon, Double lowerVal, Double upperVal, boolean includeLower, boolean includeUpper, GeoDistance geoDistance, String fieldName, GeoPointFieldMapper mapper, FieldDataCache fieldDataCache, String optimizeBbox) { this.lat = lat; this.lon = lon; this.geoDistance = geoDistance; this.fieldName = fieldName; this.fieldDataCache = fieldDataCache; this.fixedSourceDistance = geoDistance.fixedSourceDistance(lat, lon, DistanceUnit.MILES); if (lowerVal != null) { double f = lowerVal.doubleValue(); long i = NumericUtils.doubleToSortableLong(f); inclusiveLowerPoint = NumericUtils.sortableLongToDouble(includeLower ? i : (i + 1L)); } else { inclusiveLowerPoint = Double.NEGATIVE_INFINITY; } if (upperVal != null) { double f = upperVal.doubleValue(); long i = NumericUtils.doubleToSortableLong(f); inclusiveUpperPoint = NumericUtils.sortableLongToDouble(includeUpper ? i : (i - 1L)); } else { inclusiveUpperPoint = Double.POSITIVE_INFINITY; // we disable bounding box in this case, since the upper point is all and we create bounding // box up to the // upper point it will effectively include all // TODO we can create a bounding box up to from and "not" it optimizeBbox = null; } if (optimizeBbox != null && !"none".equals(optimizeBbox)) { distanceBoundingCheck = GeoDistance.distanceBoundingCheck(lat, lon, inclusiveUpperPoint, DistanceUnit.MILES); if ("memory".equals(optimizeBbox)) { boundingBoxFilter = null; } else if ("indexed".equals(optimizeBbox)) { boundingBoxFilter = IndexedGeoBoundingBoxFilter.create( distanceBoundingCheck.topLeft(), distanceBoundingCheck.bottomRight(), mapper); distanceBoundingCheck = GeoDistance.ALWAYS_INSTANCE; // fine, we do the bounding box check using the filter } else { throw new ElasticSearchIllegalArgumentException( "type [" + optimizeBbox + "] for bounding box optimization not supported"); } } else { distanceBoundingCheck = GeoDistance.ALWAYS_INSTANCE; boundingBoxFilter = null; } }
private void assertLegacyQuery(GeoDistanceRangeQueryBuilder queryBuilder, Query query) throws IOException { assertThat(query, instanceOf(GeoDistanceRangeQuery.class)); GeoDistanceRangeQuery geoQuery = (GeoDistanceRangeQuery) query; assertThat(geoQuery.fieldName(), equalTo(queryBuilder.fieldName())); if (queryBuilder.point() != null) { GeoPoint expectedPoint = new GeoPoint(queryBuilder.point()); if (GeoValidationMethod.isCoerce(queryBuilder.getValidationMethod())) { GeoUtils.normalizePoint(expectedPoint, true, true); } assertThat(geoQuery.lat(), equalTo(expectedPoint.lat())); assertThat(geoQuery.lon(), equalTo(expectedPoint.lon())); } assertThat(geoQuery.geoDistance(), equalTo(queryBuilder.geoDistance())); if (queryBuilder.from() != null && queryBuilder.from() instanceof Number) { double fromValue = ((Number) queryBuilder.from()).doubleValue(); if (queryBuilder.unit() != null) { fromValue = queryBuilder.unit().toMeters(fromValue); } if (queryBuilder.geoDistance() != null) { fromValue = queryBuilder.geoDistance().normalize(fromValue, DistanceUnit.DEFAULT); } double fromSlop = Math.abs(fromValue) / 1000; if (queryBuilder.includeLower() == false) { fromSlop = NumericUtils.sortableLongToDouble( (NumericUtils.doubleToSortableLong(Math.abs(fromValue)) + 1L)) / 1000.0; } assertThat(geoQuery.minInclusiveDistance(), closeTo(fromValue, fromSlop)); } if (queryBuilder.to() != null && queryBuilder.to() instanceof Number) { double toValue = ((Number) queryBuilder.to()).doubleValue(); if (queryBuilder.unit() != null) { toValue = queryBuilder.unit().toMeters(toValue); } if (queryBuilder.geoDistance() != null) { toValue = queryBuilder.geoDistance().normalize(toValue, DistanceUnit.DEFAULT); } double toSlop = Math.abs(toValue) / 1000; if (queryBuilder.includeUpper() == false) { toSlop = NumericUtils.sortableLongToDouble( (NumericUtils.doubleToSortableLong(Math.abs(toValue)) - 1L)) / 1000.0; } assertThat(geoQuery.maxInclusiveDistance(), closeTo(toValue, toSlop)); } }
@Override public BytesRef indexedValueForSearch(Object value) { long longValue = NumericUtils.doubleToSortableLong(parseDoubleValue(value)); BytesRefBuilder bytesRef = new BytesRefBuilder(); NumericUtils.longToPrefixCoded(longValue, 0, bytesRef); // 0 because of exact match return bytesRef.get(); }
private static byte[][] pack(Double value) { if (value == null) { // OK: open ended range return new byte[1][]; } byte[][] result = new byte[][] {new byte[RamUsageEstimator.NUM_BYTES_LONG]}; NumericUtils.longToBytesDirect(NumericUtils.doubleToSortableLong(value), result[0], 0); return result; }
public LongFilter convertToDoubleFilter() { if (isPartitionBased()) { return new PartitionedLongFilter(); } int numValids = includeValues == null ? 0 : includeValues.size(); int numInvalids = excludeValues == null ? 0 : excludeValues.size(); SetBackedLongFilter result = new SetBackedLongFilter(numValids, numInvalids); if (includeValues != null) { for (BytesRef val : includeValues) { double dval = Double.parseDouble(val.utf8ToString()); result.addAccept(NumericUtils.doubleToSortableLong(dval)); } } if (excludeValues != null) { for (BytesRef val : excludeValues) { double dval = Double.parseDouble(val.utf8ToString()); result.addReject(NumericUtils.doubleToSortableLong(dval)); } } return result; }
@Override public String readableToIndexed(String val) { switch (type) { case INTEGER: return NumericUtils.intToPrefixCoded(Integer.parseInt(val)); case FLOAT: return NumericUtils.intToPrefixCoded( NumericUtils.floatToSortableInt(Float.parseFloat(val))); case LONG: return NumericUtils.longToPrefixCoded(Long.parseLong(val)); case DOUBLE: return NumericUtils.longToPrefixCoded( NumericUtils.doubleToSortableLong(Double.parseDouble(val))); case DATE: return NumericUtils.longToPrefixCoded(dateField.parseMath(null, val).getTime()); default: throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + type); } }
/** * Initializes the token stream with the supplied <code>double</code> value. * * @param value the value, for which this TokenStream should enumerate tokens. * @return this instance, because of this you can use it the following way: <code> * new Field(name, new NumericTokenStream(precisionStep).setDoubleValue(value))</code> */ public NumericTokenStream setDoubleValue(final double value) { numericAtt.init( NumericUtils.doubleToSortableLong(value), valSize = 64, precisionStep, -precisionStep); return this; }
@Override protected void innerParseCreateField(ParseContext context, List<Field> fields) throws IOException { double value; float boost = this.boost; if (context.externalValueSet()) { Object externalValue = context.externalValue(); if (externalValue == null) { if (nullValue == null) { return; } value = nullValue; } else if (externalValue instanceof String) { String sExternalValue = (String) externalValue; if (sExternalValue.length() == 0) { if (nullValue == null) { return; } value = nullValue; } else { value = Double.parseDouble(sExternalValue); } } else { value = ((Number) externalValue).doubleValue(); } if (context.includeInAll(includeInAll, this)) { context.allEntries().addText(names.fullName(), Double.toString(value), boost); } } else { XContentParser parser = context.parser(); if (parser.currentToken() == XContentParser.Token.VALUE_NULL || (parser.currentToken() == XContentParser.Token.VALUE_STRING && parser.textLength() == 0)) { if (nullValue == null) { return; } value = nullValue; if (nullValueAsString != null && (context.includeInAll(includeInAll, this))) { context.allEntries().addText(names.fullName(), nullValueAsString, boost); } } else if (parser.currentToken() == XContentParser.Token.START_OBJECT) { XContentParser.Token token; String currentFieldName = null; Double objValue = nullValue; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else { if ("value".equals(currentFieldName) || "_value".equals(currentFieldName)) { if (parser.currentToken() != XContentParser.Token.VALUE_NULL) { objValue = parser.doubleValue(coerce.value()); } } else if ("boost".equals(currentFieldName) || "_boost".equals(currentFieldName)) { boost = parser.floatValue(); } else { throw new ElasticsearchIllegalArgumentException( "unknown property [" + currentFieldName + "]"); } } } if (objValue == null) { // no value return; } value = objValue; } else { value = parser.doubleValue(coerce.value()); if (context.includeInAll(includeInAll, this)) { context.allEntries().addText(names.fullName(), parser.text(), boost); } } } if (fieldType.indexOptions() != IndexOptions.NONE || fieldType.stored()) { CustomDoubleNumericField field = new CustomDoubleNumericField(this, value, fieldType); field.setBoost(boost); fields.add(field); } if (hasDocValues()) { if (useSortedNumericDocValues) { addDocValue(context, fields, NumericUtils.doubleToSortableLong(value)); } else { CustomDoubleNumericDocValuesField field = (CustomDoubleNumericDocValuesField) context.doc().getByKey(names().indexName()); if (field != null) { field.add(value); } else { field = new CustomDoubleNumericDocValuesField(names().indexName(), value); context.doc().addWithKey(names().indexName(), field); } } } }
@Override public String storedToIndexed(Fieldable f) { if (f instanceof NumericField) { final Number val = ((NumericField) f).getNumericValue(); if (val == null) throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, "Invalid field contents: " + f.name()); switch (type) { case INTEGER: return NumericUtils.intToPrefixCoded(val.intValue()); case FLOAT: return NumericUtils.intToPrefixCoded(NumericUtils.floatToSortableInt(val.floatValue())); case LONG: // fallthrough! case DATE: return NumericUtils.longToPrefixCoded(val.longValue()); case DOUBLE: return NumericUtils.longToPrefixCoded( NumericUtils.doubleToSortableLong(val.doubleValue())); default: throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + f.name()); } } else { // the following code is "deprecated" and only to support pre-3.2 indexes using the old // BinaryField encoding: final byte[] arr = f.getBinaryValue(); if (arr == null) throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, "Invalid field contents: " + f.name()); switch (type) { case INTEGER: return NumericUtils.intToPrefixCoded(toInt(arr)); case FLOAT: { // WARNING: Code Duplication! Keep in sync with o.a.l.util.NumericUtils! // copied from NumericUtils to not convert to/from float two times // code in next 2 lines is identical to: int v = // NumericUtils.floatToSortableInt(Float.intBitsToFloat(toInt(arr))); int v = toInt(arr); if (v < 0) v ^= 0x7fffffff; return NumericUtils.intToPrefixCoded(v); } case LONG: // fallthrough! case DATE: return NumericUtils.longToPrefixCoded(toLong(arr)); case DOUBLE: { // WARNING: Code Duplication! Keep in sync with o.a.l.util.NumericUtils! // copied from NumericUtils to not convert to/from double two times // code in next 2 lines is identical to: long v = // NumericUtils.doubleToSortableLong(Double.longBitsToDouble(toLong(arr))); long v = toLong(arr); if (v < 0) v ^= 0x7fffffffffffffffL; return NumericUtils.longToPrefixCoded(v); } default: throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + f.name()); } } }