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

Improve error when sorting on incompatible types #88399

Merged
merged 3 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions docs/changelog/88399.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 88399
summary: Improve error when sorting on incompatible types
area: Search
type: enhancement
issues:
- 73146
Original file line number Diff line number Diff line change
Expand Up @@ -2122,4 +2122,43 @@ public void testLongSortOptimizationCorrectResults() {
}
}

public void testSortMixedFieldTypes() {
assertAcked(prepareCreate("index_long").setMapping("foo", "type=long").get());
assertAcked(prepareCreate("index_integer").setMapping("foo", "type=integer").get());
assertAcked(prepareCreate("index_double").setMapping("foo", "type=double").get());
assertAcked(prepareCreate("index_keyword").setMapping("foo", "type=keyword").get());

client().prepareIndex("index_long").setId("1").setSource("foo", "123").get();
client().prepareIndex("index_integer").setId("1").setSource("foo", "123").get();
client().prepareIndex("index_double").setId("1").setSource("foo", "123").get();
client().prepareIndex("index_keyword").setId("1").setSource("foo", "123").get();
refresh();

{ // mixing long and integer types is ok, as we convert integer sort to long sort
SearchResponse searchResponse = client().prepareSearch("index_long", "index_integer")
.addSort(new FieldSortBuilder("foo"))
.setSize(10)
.get();
assertSearchResponse(searchResponse);
}

String errMsg = "Can't sort on field [foo]; the field has incompatible sort types";

{ // mixing long and double types is not allowed
SearchPhaseExecutionException exc = expectThrows(
SearchPhaseExecutionException.class,
() -> client().prepareSearch("index_long", "index_double").addSort(new FieldSortBuilder("foo")).setSize(10).get()
);
assertThat(exc.getCause().toString(), containsString(errMsg));
}

{ // mixing long and keyword types is not allowed
SearchPhaseExecutionException exc = expectThrows(
SearchPhaseExecutionException.class,
() -> client().prepareSearch("index_long", "index_keyword").addSort(new FieldSortBuilder("foo")).setSize(10).get()
);
assertThat(exc.getCause().toString(), containsString(errMsg));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.SortedNumericSortField;
import org.apache.lucene.search.SortedSetSortField;
import org.apache.lucene.search.TermStatistics;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TopFieldDocs;
Expand Down Expand Up @@ -197,6 +199,7 @@ static TopDocs mergeTopDocs(Collection<TopDocs> results, int topN, int from) {
final TopFieldGroups[] shardTopDocs = results.toArray(new TopFieldGroups[numShards]);
mergedTopDocs = TopFieldGroups.merge(sort, from, topN, shardTopDocs, false);
} else if (topDocs instanceof TopFieldDocs firstTopDocs) {
assertSameSortTypes(results, firstTopDocs.fields);
final Sort sort = new Sort(firstTopDocs.fields);
final TopFieldDocs[] shardTopDocs = results.toArray(new TopFieldDocs[numShards]);
mergedTopDocs = TopDocs.merge(sort, from, topN, shardTopDocs);
Expand All @@ -207,6 +210,56 @@ static TopDocs mergeTopDocs(Collection<TopDocs> results, int topN, int from) {
return mergedTopDocs;
}

private static void assertSameSortTypes(Collection<TopDocs> results, SortField[] firstSortFields) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we call this 'check' rather than 'assert'? Assert implies that it would be disabled if assertions are turned off.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@romseygeek Thanks for the review and feedback!

if (results.size() < 2) return;

SortField.Type[] firstTypes = new SortField.Type[firstSortFields.length];
boolean isFirstResult = true;
for (TopDocs topDocs : results) {
SortField[] curSortFields = ((TopFieldDocs) topDocs).fields;
if (isFirstResult) {
for (int i = 0; i < curSortFields.length; i++) {
firstTypes[i] = getType(firstSortFields[i]);
if (firstTypes[i] == SortField.Type.CUSTOM) {
// for custom types that we can't resolve, we can't do the check
return;
}
}
isFirstResult = false;
} else {
for (int i = 0; i < curSortFields.length; i++) {
SortField.Type curType = getType(curSortFields[i]);
if (curType != firstTypes[i]) {
if (curType == SortField.Type.CUSTOM) {
// for custom types that we can't resolve, we can't do the check
return;
}
throw new IllegalArgumentException(
"Can't sort on field ["
+ curSortFields[i].getField()
+ "]; the field has incompatible sort types: ["
+ firstTypes[i]
+ "] and ["
+ curType
+ "] across shards!"
);
}
}
}
}
}

private static SortField.Type getType(SortField sortField) {
if (sortField instanceof SortedNumericSortField) {
return ((SortedNumericSortField) sortField).getNumericType();
}
if (sortField instanceof SortedSetSortField) {
return SortField.Type.STRING;
} else {
return sortField.getType();
}
}

static void setShardIndex(TopDocs topDocs, int shardIndex) {
assert topDocs.scoreDocs.length == 0 || topDocs.scoreDocs[0].shardIndex == -1 : "shardIndex is already set";
for (ScoreDoc doc : topDocs.scoreDocs) {
Expand Down