Skip to content
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 @@ -493,18 +493,7 @@ public BucketedSort newBucketedSort(

@Override
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
if (format != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
}

return new SourceValueFetcher(name(), context) {
@Override
@SuppressWarnings("unchecked")
protected Object parseSourceValue(Object value) {
Map<String, Double> metrics = (Map<String, Double>) value;
return metrics.get(defaultMetric.name());
}
};
return SourceValueFetcher.identity(name(), context, format);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,17 @@ public void testRangeQuery() {

public void testFetchSourceValueWithOneMetric() throws IOException {
final MappedFieldType fieldType = createDefaultFieldType("field", Collections.emptyMap(), Metric.min);
final double defaultValue = 45.8;
final Map<String, Object> metric = Collections.singletonMap("min", defaultValue);
assertEquals(List.of(defaultValue), fetchSourceValue(fieldType, metric));
final double min = 45.8;
final Map<String, Object> metric = Collections.singletonMap("min", min);
assertEquals(List.of(metric), fetchSourceValue(fieldType, metric));
}

public void testFetchSourceValueWithMultipleMetrics() throws IOException {
final MappedFieldType fieldType = createDefaultFieldType("field", Collections.emptyMap(), Metric.max);
final double defaultValue = 45.8;
final Map<String, Object> metric = Map.of("min", 14.2, "max", defaultValue);
assertEquals(List.of(defaultValue), fetchSourceValue(fieldType, metric));
final double max = 45.8;
final double min = 14.2;
final Map<String, Object> metric = Map.of("min", min, "max", max);
assertEquals(List.of(metric), fetchSourceValue(fieldType, metric));
}

/** Tests that aggregate_metric_double uses the default_metric subfield's doc-values as values in scripts */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,63 @@
index: test
body:
sort: [ { metric.min: desc } ]

---
"Test fields api":
- skip:
version: "- 8.3.99"
reason: "Breaking change introduced in 8.4.0"
- do:
indices.create:
index: test
body:
mappings:
properties:
metric:
type: aggregate_metric_double
metrics: [min, max, sum, value_count]
default_metric: sum

- do:
index:
index: test
id: "1"
body:
metric:
min: 10
max: 100
sum: 200
value_count: 5
refresh: true

- do:
index:
index: test
id: "2"
body:
metric:
min: 50
max: 1000
sum: 5000
value_count: 10
refresh: true

- do:
search:
index: test
body:
_source: false
fields: [ metric ]
query:
match_all: {}

- match: { hits.total.value: 2 }
- length: { hits.hits: 2 }
- match: { hits.hits.0.fields.metric.0.min: 10 }
- match: { hits.hits.0.fields.metric.0.max: 100 }
- match: { hits.hits.0.fields.metric.0.sum: 200 }
- match: { hits.hits.0.fields.metric.0.value_count: 5 }
- match: { hits.hits.1.fields.metric.0.min: 50 }
- match: { hits.hits.1.fields.metric.0.max: 1000 }
- match: { hits.hits.1.fields.metric.0.sum: 5000 }
- match: { hits.hits.1.fields.metric.0.value_count: 10 }