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

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

Merged
merged 3 commits into from
Mar 10, 2020
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 @@ -885,3 +885,88 @@ setup:
- 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.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 }
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ private Sort buildIndexSortPrefix(LeafReaderContext context) throws IOException
return null;
}
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];
SingleDimensionValuesSource<?> source = sources[i];
SortField indexSortField = indexSort.getSort()[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2077,40 +2077,52 @@ private static long asLong(String dateTime) {

private static Sort buildIndexSort(List<CompositeValuesSourceBuilder<?>> sources, Map<String, MappedFieldType> fieldTypes) {
List<SortField> sortFields = new ArrayList<>();
Map<String, MappedFieldType> remainingFieldTypes = new HashMap<>(fieldTypes);
for (CompositeValuesSourceBuilder<?> source : sources) {
MappedFieldType type = fieldTypes.get(source.field());
if (type instanceof KeywordFieldMapper.KeywordFieldType) {
sortFields.add(new SortedSetSortField(type.name(), false));
} else if (type instanceof DateFieldMapper.DateFieldType) {
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;
}
if (comp == false) {
break;
}
MappedFieldType type = fieldTypes.remove(source.field());
remainingFieldTypes.remove(source.field());
SortField sortField = sortFieldFrom(type);
if (sortField == null) {
break;
}
sortFields.add(sortField);
if (sortField instanceof SortedNumericSortField && ((SortedNumericSortField) sortField).getType() == SortField.Type.LONG) {
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;
}

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;
}
}