Fix composite agg sort bug (backport of #53296) (#53337)

When an composite aggregation is run against an index with a sort that
*starts* with the "source" fields from the composite but has additional
fields it'd blow up in while trying to decide if it could use the sort.
This changes it to decide that it *can* use the sort.

Closes #52480
This commit is contained in:
Nik Everett 2020-03-10 11:32:46 -04:00 committed by GitHub
parent 166b5a92f6
commit 89c0e1f566
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 130 additions and 32 deletions

View File

@ -885,3 +885,88 @@ setup:
- length: { aggregations.test.buckets: 1 } - length: { aggregations.test.buckets: 1 }
- match: { aggregations.test.buckets.0.key.date: "2017-10-21T00:00:00.000-02:00" } - match: { aggregations.test.buckets.0.key.date: "2017-10-21T00:00:00.000-02:00" }
- match: { aggregations.test.buckets.0.doc_count: 2 } - match: { aggregations.test.buckets.0.doc_count: 2 }
---
"Terms source from sorted":
- do:
indices.create:
index: sorted_test
body:
settings:
sort.field: keyword
mappings:
properties:
keyword:
type: keyword
long:
type: long
- do:
index:
index: sorted_test
id: 2
refresh: true
body: { "keyword": "foo", "long": 1 }
- do:
search:
index: sorted_test
rest_total_hits_as_int: true
body:
aggregations:
test:
composite:
sources:
- keyword:
terms:
field: keyword
- match: {hits.total: 1}
- length: { aggregations.test.buckets: 1 }
- match: { aggregations.test.buckets.0.key.keyword: "foo" }
- match: { aggregations.test.buckets.0.doc_count: 1 }
---
"Terms source from part of sorted":
- skip:
version: " - 7.6.99"
reason: fixed in 7.7.0
- do:
indices.create:
index: sorted_test
body:
settings:
sort.field: [keyword, long]
mappings:
properties:
keyword:
type: keyword
long:
type: long
- do:
index:
index: sorted_test
id: 2
refresh: true
body: { "keyword": "foo", "long": 1 }
- do:
search:
index: sorted_test
body:
aggregations:
test:
composite:
sources:
- keyword:
terms:
field: keyword
- match: {hits.total.value: 1}
- length: { aggregations.test.buckets: 1 }
- match: { aggregations.test.buckets.0.key.keyword: "foo" }
- match: { aggregations.test.buckets.0.doc_count: 1 }

View File

@ -202,7 +202,8 @@ final class CompositeAggregator extends BucketsAggregator {
return null; return null;
} }
List<SortField> sortFields = new ArrayList<>(); List<SortField> sortFields = new ArrayList<>();
for (int i = 0; i < indexSort.getSort().length; i++) { int end = Math.min(indexSort.getSort().length, sourceConfigs.length);
for (int i = 0; i < end; i++) {
CompositeValuesSourceConfig sourceConfig = sourceConfigs[i]; CompositeValuesSourceConfig sourceConfig = sourceConfigs[i];
SingleDimensionValuesSource<?> source = sources[i]; SingleDimensionValuesSource<?> source = sources[i];
SortField indexSortField = indexSort.getSort()[i]; SortField indexSortField = indexSort.getSort()[i];

View File

@ -2077,40 +2077,52 @@ public class CompositeAggregatorTests extends AggregatorTestCase {
private static Sort buildIndexSort(List<CompositeValuesSourceBuilder<?>> sources, Map<String, MappedFieldType> fieldTypes) { private static Sort buildIndexSort(List<CompositeValuesSourceBuilder<?>> sources, Map<String, MappedFieldType> fieldTypes) {
List<SortField> sortFields = new ArrayList<>(); List<SortField> sortFields = new ArrayList<>();
Map<String, MappedFieldType> remainingFieldTypes = new HashMap<>(fieldTypes);
for (CompositeValuesSourceBuilder<?> source : sources) { for (CompositeValuesSourceBuilder<?> source : sources) {
MappedFieldType type = fieldTypes.get(source.field()); MappedFieldType type = fieldTypes.remove(source.field());
if (type instanceof KeywordFieldMapper.KeywordFieldType) { remainingFieldTypes.remove(source.field());
sortFields.add(new SortedSetSortField(type.name(), false)); SortField sortField = sortFieldFrom(type);
} else if (type instanceof DateFieldMapper.DateFieldType) { if (sortField == null) {
sortFields.add(new SortedNumericSortField(type.name(), SortField.Type.LONG, false)); break;
} else if (type instanceof NumberFieldMapper.NumberFieldType) { }
boolean comp = false; sortFields.add(sortField);
switch (type.typeName()) { if (sortField instanceof SortedNumericSortField && ((SortedNumericSortField) sortField).getType() == SortField.Type.LONG) {
case "byte": break;
case "short": }
case "integer": }
comp = true; while (remainingFieldTypes.size() > 0 && randomBoolean()) {
sortFields.add(new SortedNumericSortField(type.name(), SortField.Type.INT, false)); // Add extra unused sorts
break; List<String> fields = new ArrayList<>(remainingFieldTypes.keySet());
Collections.sort(fields);
case "long": String field = fields.get(between(0, fields.size() - 1));
sortFields.add(new SortedNumericSortField(type.name(), SortField.Type.LONG, false)); SortField sortField = sortFieldFrom(remainingFieldTypes.remove(field));
break; if (sortField != null) {
sortFields.add(sortField);
case "float":
case "double":
comp = true;
sortFields.add(new SortedNumericSortField(type.name(), SortField.Type.DOUBLE, false));
break;
default:
break;
}
if (comp == false) {
break;
}
} }
} }
return sortFields.size() > 0 ? new Sort(sortFields.toArray(new SortField[0])) : null; return sortFields.size() > 0 ? new Sort(sortFields.toArray(new SortField[0])) : null;
} }
private static SortField sortFieldFrom(MappedFieldType type) {
if (type instanceof KeywordFieldMapper.KeywordFieldType) {
return new SortedSetSortField(type.name(), false);
} else if (type instanceof DateFieldMapper.DateFieldType) {
return new SortedNumericSortField(type.name(), SortField.Type.LONG, false);
} else if (type instanceof NumberFieldMapper.NumberFieldType) {
switch (type.typeName()) {
case "byte":
case "short":
case "integer":
return new SortedNumericSortField(type.name(), SortField.Type.INT, false);
case "long":
return new SortedNumericSortField(type.name(), SortField.Type.LONG, false);
case "float":
case "double":
return new SortedNumericSortField(type.name(), SortField.Type.DOUBLE, false);
default:
return null;
}
}
return null;
}
} }