diff --git a/src/main/java/com/yelp/nrtsearch/server/field/AtomFieldDef.java b/src/main/java/com/yelp/nrtsearch/server/field/AtomFieldDef.java index 10e8e5b31..8da4656fd 100644 --- a/src/main/java/com/yelp/nrtsearch/server/field/AtomFieldDef.java +++ b/src/main/java/com/yelp/nrtsearch/server/field/AtomFieldDef.java @@ -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()) { diff --git a/src/main/java/com/yelp/nrtsearch/server/field/DateTimeFieldDef.java b/src/main/java/com/yelp/nrtsearch/server/field/DateTimeFieldDef.java index bd803154c..1a498a8c9 100644 --- a/src/main/java/com/yelp/nrtsearch/server/field/DateTimeFieldDef.java +++ b/src/main/java/com/yelp/nrtsearch/server/field/DateTimeFieldDef.java @@ -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 @@ -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; diff --git a/src/main/java/com/yelp/nrtsearch/server/field/LatLonFieldDef.java b/src/main/java/com/yelp/nrtsearch/server/field/LatLonFieldDef.java index 868170753..023f4e571 100644 --- a/src/main/java/com/yelp/nrtsearch/server/field/LatLonFieldDef.java +++ b/src/main/java/com/yelp/nrtsearch/server/field/LatLonFieldDef.java @@ -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()); diff --git a/src/main/java/com/yelp/nrtsearch/server/field/NumberFieldDef.java b/src/main/java/com/yelp/nrtsearch/server/field/NumberFieldDef.java index 41a56dbb2..97db78e19 100644 --- a/src/main/java/com/yelp/nrtsearch/server/field/NumberFieldDef.java +++ b/src/main/java/com/yelp/nrtsearch/server/field/NumberFieldDef.java @@ -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 =