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:
parent
166b5a92f6
commit
89c0e1f566
|
@ -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 }
|
||||||
|
|
|
@ -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];
|
||||||
|
|
|
@ -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));
|
|
||||||
} else if (type instanceof NumberFieldMapper.NumberFieldType) {
|
|
||||||
boolean comp = false;
|
|
||||||
switch (type.typeName()) {
|
|
||||||
case "byte":
|
|
||||||
case "short":
|
|
||||||
case "integer":
|
|
||||||
comp = true;
|
|
||||||
sortFields.add(new SortedNumericSortField(type.name(), SortField.Type.INT, false));
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "long":
|
|
||||||
sortFields.add(new SortedNumericSortField(type.name(), SortField.Type.LONG, false));
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "float":
|
|
||||||
case "double":
|
|
||||||
comp = true;
|
|
||||||
sortFields.add(new SortedNumericSortField(type.name(), SortField.Type.DOUBLE, false));
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (comp == false) {
|
sortFields.add(sortField);
|
||||||
|
if (sortField instanceof SortedNumericSortField && ((SortedNumericSortField) sortField).getType() == SortField.Type.LONG) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
while (remainingFieldTypes.size() > 0 && randomBoolean()) {
|
||||||
|
// Add extra unused sorts
|
||||||
|
List<String> fields = new ArrayList<>(remainingFieldTypes.keySet());
|
||||||
|
Collections.sort(fields);
|
||||||
|
String field = fields.get(between(0, fields.size() - 1));
|
||||||
|
SortField sortField = sortFieldFrom(remainingFieldTypes.remove(field));
|
||||||
|
if (sortField != null) {
|
||||||
|
sortFields.add(sortField);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue