Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/unreleased/SOLR-13309-floatRangeField.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
title: Introduce new `FloatRangeField` field type for storing and querying float-based ranges
type: added
authors:
- name: Jason Gerlowski
links:
- name: SOLR-13309
url: https://issues.apache.org/jira/browse/SOLR-13309
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
*
* @see IntRangeField
* @see LongRangeField
* @see FloatRangeField
*/
public abstract class AbstractNumericRangeField extends PrimitiveFieldType {

Expand Down Expand Up @@ -82,9 +83,54 @@ public interface NumericRangeValue {
protected static final Pattern SINGLE_BOUND_PATTERN =
Pattern.compile("^" + COMMA_DELIMITED_NUMS + "$");

/**
* Regex fragment matching a comma-separated list of signed floating-point numbers (integers or
* floating-point literals).
*/
protected static final String COMMA_DELIMITED_FP_NUMS =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[0] All of these regexes are a bit messy.

Ultimately the idea here is that we want the validation for each implementing type (int, float, etc.) to be specific to what that type looks like. IntRangeField needs to be able to reject fp-values, etc.

This validation is done by regex. The regexes live in this base class because some code here that invotes this verification. Individual sub-classes specify the regex pattern they want to use by using overrideable methods getRangePattern and getSingleBoundPattern.

So that's my rationale here. Open to other ways of doing it if folks can see a better approach....

"-?\\d+(?:\\.\\d+)?(?:\\s*,\\s*-?\\d+(?:\\.\\d+)?)*";

private static final String FP_RANGE_PATTERN_STR =
Copy link

@chan-dx chan-dx Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed a small gap in the float validation regex: it doesn't accept scientific notation. I tested FP_RANGE_PATTERN_STR pattern on regex101.com, and an input like [1.1 TO 1.5e10] does not match.

Was the exclusion of scientific notation intentional? Or are values like this expected to be rejected before reaching this validation step?

"\\[\\s*(" + COMMA_DELIMITED_FP_NUMS + ")\\s+TO\\s+(" + COMMA_DELIMITED_FP_NUMS + ")\\s*\\]";

/**
* Pre-compiled pattern matching {@code [min1,min2,... TO max1,max2,...]} range syntax where
* values may be floating-point numbers.
*/
protected static final Pattern FP_RANGE_PATTERN_REGEX = Pattern.compile(FP_RANGE_PATTERN_STR);

/**
* Pre-compiled pattern matching a single (multi-dimensional) floating-point bound, e.g. {@code
* 1.5,2.0,3.14}.
*/
protected static final Pattern FP_SINGLE_BOUND_PATTERN =
Pattern.compile("^" + COMMA_DELIMITED_FP_NUMS + "$");

/** Configured number of dimensions for this field type; defaults to 1. */
protected int numDimensions = 1;

/**
* Returns the regex {@link Pattern} used to match a full range value string of the form {@code
* [min TO max]}. Subclasses may override to use an alternative pattern (e.g. one that accepts
* floating-point numbers).
*
* @return the range pattern for this field type
*/
protected Pattern getRangePattern() {
return RANGE_PATTERN_REGEX;
}

/**
* Returns the regex {@link Pattern} used to match a single multi-dimensional bound (e.g. {@code
* 1,2,3}). Subclasses may override to use an alternative pattern (e.g. one that accepts
* floating-point numbers).
*
* @return the single-bound pattern for this field type
*/
protected Pattern getSingleBoundPattern() {
return SINGLE_BOUND_PATTERN;
}

@Override
protected boolean enableDocValuesByDefault() {
return false; // Range fields do not support docValues
Expand Down Expand Up @@ -287,13 +333,13 @@ public Query getFieldQuery(QParser parser, SchemaField field, String externalVal
String trimmed = externalVal.trim();

// Check if it's the full range syntax: [min1,min2 TO max1,max2]
if (RANGE_PATTERN_REGEX.matcher(trimmed).matches()) {
if (getRangePattern().matcher(trimmed).matches()) {
final var rangeValue = parseRangeValue(trimmed);
return newContainsQuery(field.getName(), rangeValue);
}

// Syntax sugar: also accept a single-bound (i.e pX,pY,pZ)
if (SINGLE_BOUND_PATTERN.matcher(trimmed).matches()) {
if (getSingleBoundPattern().matcher(trimmed).matches()) {
final var singleBoundRange = parseSingleBound(trimmed);

if (singleBoundRange.getDimensions() != numDimensions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.schema.numericrange;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.lucene.document.FloatRange;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.search.Query;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.search.QParser;

/**
* Field type for float ranges with support for 1-4 dimensions.
*
* <p>This field type wraps Lucene's {@link FloatRange} to provide storage and querying of float
* range values. Ranges can be 1-dimensional (simple ranges), 2-dimensional (bounding boxes),
* 3-dimensional (bounding cubes), or 4-dimensional (tesseracts).
*
* <h2>Value Format</h2>
*
* Values are specified using bracket notation with a TO keyword separator:
*
* <ul>
* <li>1D: {@code [1.5 TO 2.5]}
* <li>2D: {@code [1.0,2.0 TO 3.0,4.0]}
* <li>3D: {@code [1.0,2.0,3.0 TO 4.0,5.0,6.0]}
* <li>4D: {@code [1.0,2.0,3.0,4.0 TO 5.0,6.0,7.0,8.0]}
* </ul>
*
* As the name suggests minimum values (those on the left) must always be less than or equal to the
* maximum value for the corresponding dimension. Integer values (e.g. {@code [10 TO 20]}) are also
* accepted and parsed as floats.
*
* <h2>Schema Configuration</h2>
*
* <pre>
* &lt;fieldType name="floatrange" class="org.apache.solr.schema.numericrange.FloatRangeField" numDimensions="1"/&gt;
* &lt;fieldType name="floatrange2d" class="org.apache.solr.schema.numericrange.FloatRangeField" numDimensions="2"/&gt;
* &lt;field name="price_range" type="floatrange" indexed="true" stored="true"/&gt;
* &lt;field name="bbox" type="floatrange2d" indexed="true" stored="true"/&gt;
* </pre>
*
* <h2>Querying</h2>
*
* Use the {@code numericRange} query parser for range queries with support for different query
* types:
*
* <ul>
* <li>Intersects: {@code {!numericRange criteria="intersects" field=price_range}[1.0 TO 2.0]}
* <li>Within: {@code {!numericRange criteria="within" field=price_range}[0.0 TO 3.0]}
* <li>Contains: {@code {!numericRange criteria="contains" field=price_range}[1.5 TO 1.75]}
* <li>Crosses: {@code {!numericRange criteria="crosses" field=price_range}[1.5 TO 2.5]}
* </ul>
*
* <h2>Limitations</h2>
*
* The main limitation of this field type is that it doesn't support docValues or uninversion, and
* therefore can't be used for sorting, faceting, etc.
*
* @see FloatRange
* @see org.apache.solr.search.numericrange.NumericRangeQParserPlugin
*/
public class FloatRangeField extends AbstractNumericRangeField {

@Override
protected Pattern getRangePattern() {
return FP_RANGE_PATTERN_REGEX;
}

@Override
protected Pattern getSingleBoundPattern() {
return FP_SINGLE_BOUND_PATTERN;
}

@Override
public IndexableField createField(SchemaField field, Object value) {
if (!field.indexed() && !field.stored()) {
return null;
}

String valueStr = value.toString();
RangeValue rangeValue = parseRangeValue(valueStr);

return new FloatRange(field.getName(), rangeValue.mins, rangeValue.maxs);
}

/**
* Parse a range value string into a RangeValue object.
*
* @param value the string value in format "[min1,min2,... TO max1,max2,...]"
* @return parsed RangeValue
* @throws SolrException if value format is invalid
*/
@Override
public RangeValue parseRangeValue(String value) {
if (value == null || value.trim().isEmpty()) {
throw new SolrException(ErrorCode.BAD_REQUEST, "Range value cannot be null or empty");
}

Matcher matcher = FP_RANGE_PATTERN_REGEX.matcher(value.trim());
if (!matcher.matches()) {
throw new SolrException(
ErrorCode.BAD_REQUEST,
"Invalid range format. Expected: [min1,min2,... TO max1,max2,...] where min and max values are floats, but got: "
+ value);
}

String minPart = matcher.group(1).trim();
String maxPart = matcher.group(2).trim();

float[] mins = parseFloatArray(minPart, "min values");
float[] maxs = parseFloatArray(maxPart, "max values");

if (mins.length != maxs.length) {
throw new SolrException(
ErrorCode.BAD_REQUEST,
"Min and max dimensions must match. Min dimensions: "
+ mins.length
+ ", max dimensions: "
+ maxs.length);
}

if (mins.length != numDimensions) {
throw new SolrException(
ErrorCode.BAD_REQUEST,
"Range dimensions ("
+ mins.length
+ ") do not match field type numDimensions ("
+ numDimensions
+ ")");
}

// Validate that min <= max for each dimension
for (int i = 0; i < mins.length; i++) {
if (mins[i] > maxs[i]) {
throw new SolrException(
ErrorCode.BAD_REQUEST,
"Min value must be <= max value for dimension "
+ i
+ ". Min: "
+ mins[i]
+ ", Max: "
+ maxs[i]);
}
}

return new RangeValue(mins, maxs);
}

@Override
public NumericRangeValue parseSingleBound(String value) {
final var singleBoundTyped = parseFloatArray(value, "single bound values");
return new RangeValue(singleBoundTyped, singleBoundTyped);
}

/**
* Parse a comma-separated string of floats into an array.
*
* @param str the string to parse
* @param description description for error messages
* @return array of parsed floats
*/
private float[] parseFloatArray(String str, String description) {
String[] parts = str.split(",");
float[] result = new float[parts.length];

for (int i = 0; i < parts.length; i++) {
try {
result[i] = Float.parseFloat(parts[i].trim());
} catch (NumberFormatException e) {
throw new SolrException(
ErrorCode.BAD_REQUEST,
"Invalid float in " + description + ": '" + parts[i].trim() + "'",
e);
}
}

return result;
}

@Override
public Query newContainsQuery(String fieldName, NumericRangeValue rangeValue) {
final var rv = (RangeValue) rangeValue;
return FloatRange.newContainsQuery(fieldName, rv.mins, rv.maxs);
}

@Override
public Query newIntersectsQuery(String fieldName, NumericRangeValue rangeValue) {
final var rv = (RangeValue) rangeValue;
return FloatRange.newIntersectsQuery(fieldName, rv.mins, rv.maxs);
}

@Override
public Query newWithinQuery(String fieldName, NumericRangeValue rangeValue) {
final var rv = (RangeValue) rangeValue;
return FloatRange.newWithinQuery(fieldName, rv.mins, rv.maxs);
}

@Override
public Query newCrossesQuery(String fieldName, NumericRangeValue rangeValue) {
final var rv = (RangeValue) rangeValue;
return FloatRange.newCrossesQuery(fieldName, rv.mins, rv.maxs);
}

@Override
protected Query getSpecializedRangeQuery(
QParser parser,
SchemaField field,
String part1,
String part2,
boolean minInclusive,
boolean maxInclusive) {
// For standard range syntax field:[value TO value], default to contains query
if (part1 == null || part2 == null) {
return super.getSpecializedRangeQuery(
parser, field, part1, part2, minInclusive, maxInclusive);
}

// Parse the range bounds as single-dimensional float values
float min, max;
try {
min = Float.parseFloat(part1.trim());
max = Float.parseFloat(part2.trim());
} catch (NumberFormatException e) {
throw new SolrException(
ErrorCode.BAD_REQUEST,
"Invalid float values in range query: [" + part1 + " TO " + part2 + "]",
e);
}

// For exclusive bounds, step to the next representable float value
if (!minInclusive) {
min = Math.nextUp(min);
}
if (!maxInclusive) {
max = Math.nextDown(max);
}

// Build arrays for the query based on configured dimensions
float[] mins = new float[numDimensions];
float[] maxs = new float[numDimensions];

// For now, only support 1D range syntax with field:[X TO Y]
if (numDimensions == 1) {
mins[0] = min;
maxs[0] = max;
return FloatRange.newContainsQuery(field.getName(), mins, maxs);
} else {
throw new SolrException(
ErrorCode.BAD_REQUEST,
"Standard range query syntax only supports 1D ranges. "
+ "Use {!numericRange ...} for multi-dimensional queries.");
}
}

/** Simple holder class for parsed float range values. */
public static class RangeValue implements AbstractNumericRangeField.NumericRangeValue {
public final float[] mins;
public final float[] maxs;

public RangeValue(float[] mins, float[] maxs) {
this.mins = mins;
this.maxs = maxs;
}

@Override
public int getDimensions() {
return mins.length;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < mins.length; i++) {
if (i > 0) sb.append(",");
sb.append(mins[i]);
}
sb.append(" TO ");
for (int i = 0; i < maxs.length; i++) {
if (i > 0) sb.append(",");
sb.append(maxs[i]);
}
sb.append("]");
return sb.toString();
}
}
}
Loading
Loading