Skip to content

Commit

Permalink
moar
Browse files Browse the repository at this point in the history
  • Loading branch information
original-brownbear committed Sep 19, 2024
1 parent 0c1418b commit c4744ab
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,8 @@ private static boolean shouldSortShards(MinAndMax<?>[] minAndMaxes) {
Class<?> clazz = null;
for (MinAndMax<?> minAndMax : minAndMaxes) {
if (clazz == null) {
clazz = minAndMax == null ? null : minAndMax.minValue().getClass();
} else if (minAndMax != null && clazz != minAndMax.maxValue().getClass()) {
clazz = minAndMax == null ? null : minAndMax.min().getClass();
} else if (minAndMax != null && clazz != minAndMax.max().getClass()) {
// we don't support sort values that mix different types (e.g.: long/double, numeric/keyword).
// TODO: we could fail the request because there is a high probability
// that the merging of topdocs will fail later for the same reason ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ public SearchAfterBuilder() {}
* Read from a stream.
*/
public SearchAfterBuilder(StreamInput in) throws IOException {
int size = in.readVInt();
sortValues = new Object[size];
for (int i = 0; i < size; i++) {
sortValues[i] = in.readGenericValue();
}
sortValues = in.readArray(StreamInput::readGenericValue, Object[]::new);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
/**
* A class that encapsulates a minimum and a maximum, that are of the same type and {@link Comparable}.
*/
public record MinAndMax<T extends Comparable<? super T>>(T minValue, T maxValue) implements Writeable {
public record MinAndMax<T extends Comparable<? super T>>(T min, T max) implements Writeable {

public MinAndMax(T minValue, T maxValue) {
this.minValue = Objects.requireNonNull(minValue);
this.maxValue = Objects.requireNonNull(maxValue);
public MinAndMax(T min, T max) {
this.min = Objects.requireNonNull(min);
this.max = Objects.requireNonNull(max);
}

@SuppressWarnings("unchecked")
Expand All @@ -35,24 +35,24 @@ public static <T extends Comparable<? super T>> MinAndMax<T> readFrom(StreamInpu

@Override
public void writeTo(StreamOutput out) throws IOException {
Lucene.writeSortValue(out, minValue);
Lucene.writeSortValue(out, maxValue);
Lucene.writeSortValue(out, min);
Lucene.writeSortValue(out, max);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private static final Comparator<MinAndMax> ASC_COMPARATOR = (left, right) -> {
if (left == null) {
return right == null ? 0 : -1; // nulls last
}
return right == null ? 1 : left.minValue.compareTo(right.minValue);
return right == null ? 1 : left.min.compareTo(right.min);
};

@SuppressWarnings({ "unchecked", "rawtypes" })
private static final Comparator<MinAndMax> DESC_COMPARATOR = (left, right) -> {
if (left == null) {
return right == null ? 0 : 1; // nulls first
}
return right == null ? -1 : right.maxValue.compareTo(left.maxValue);
return right == null ? -1 : right.max.compareTo(left.max);
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,8 @@ public void testGetMaxNumericSortValue() throws IOException {
assertNull(getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName)));
} else {
assertNull(getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName + "-ni")));
assertEquals(values[numDocs - 1], getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName)).maxValue());
assertEquals(values[0], getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName)).minValue());
assertEquals(values[numDocs - 1], getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName)).max());
assertEquals(values[0], getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName)).min());
}
}
}
Expand All @@ -535,8 +535,8 @@ public void testGetMaxNumericDateValue() throws IOException {
Arrays.sort(values);
try (DirectoryReader reader = writer.getReader()) {
SearchExecutionContext newContext = createMockSearchExecutionContext(new AssertingIndexSearcher(random(), reader));
assertEquals(values[numDocs - 1], getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName)).maxValue());
assertEquals(values[0], getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName)).minValue());
assertEquals(values[numDocs - 1], getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName)).max());
assertEquals(values[0], getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName)).min());
}
}
}
Expand All @@ -560,8 +560,8 @@ public void testGetMaxKeywordValue() throws IOException {
Arrays.sort(values);
try (DirectoryReader reader = writer.getReader()) {
SearchExecutionContext newContext = createMockSearchExecutionContext(new AssertingIndexSearcher(random(), reader));
assertEquals(values[numDocs - 1], getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName)).maxValue());
assertEquals(values[0], getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName)).minValue());
assertEquals(values[numDocs - 1], getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName)).max());
assertEquals(values[0], getMinMaxOrNull(newContext, SortBuilders.fieldSort(fieldName)).min());
}
}
}
Expand Down

0 comments on commit c4744ab

Please sign in to comment.