Skip to content

Commit

Permalink
Clean up sort field validation (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
aprudhomme authored Jan 8, 2025
1 parent bb431d0 commit fd2415a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ protected Analyzer parseSearchAnalyzer(Field requestField) {

@Override
public SortField getSortField(SortType type) {
if (!hasDocValues()) {
throw new IllegalStateException("Doc values are required for sorted fields");
verifyDocValues("Sort field");
if (docValuesType != DocValuesType.SORTED && docValuesType != DocValuesType.SORTED_SET) {
throw new IllegalStateException(
"Sort field requires SORTED or SORTED_SET doc values: " + getName());
}
SortField sortField;
if (isMultiValue()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.SortedNumericSortField;

/** Field class for 'DATE_TIME' field type. */
public class DateTimeFieldDef extends IndexableFieldDef<Instant>
Expand Down Expand Up @@ -88,13 +89,19 @@ public DateTimeFieldDef(

@Override
public SortField getSortField(SortType type) {
if (!hasDocValues()) {
throw new IllegalStateException("Doc values are required for sorted fields");
}
verifyDocValues("Sort field");
SortField sortField;
if (isMultiValue()) {
throw new IllegalStateException("DATE_TIME does not support sort for multi value field");
sortField =
new SortedNumericSortField(
getName(),
SortField.Type.LONG,
type.getReverse(),
NUMERIC_TYPE_PARSER.apply(type.getSelector()));
} else {
sortField = new SortField(getName(), SortField.Type.LONG, type.getReverse());
}
SortField sortField = new SortField(getName(), SortField.Type.LONG, type.getReverse());

boolean missingLast = type.getMissingLast();
sortField.setMissingValue(missingLast ? Long.MAX_VALUE : Long.MIN_VALUE);
return sortField;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ public String getType() {

@Override
public SortField getSortField(SortType type) {
if (!hasDocValues()) {
throw new IllegalStateException("Doc values are required for sorted fields");
}
verifyDocValues("Sort field");
Point origin = type.getOrigin();
return LatLonDocValuesField.newDistanceSort(
getName(), origin.getLatitude(), origin.getLongitude());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,7 @@ public DoubleValuesSource getExpressionBinding(String property) {

@Override
public SortField getSortField(SortType type) {
if (!hasDocValues()) {
throw new IllegalStateException("Doc values are required for sorted fields");
}
verifyDocValues("Sort field");
SortField sortField;
if (isMultiValue()) {
sortField =
Expand Down

0 comments on commit fd2415a

Please sign in to comment.