Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up sort field validation #809

Merged
merged 1 commit into from
Jan 8, 2025
Merged
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
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
Loading