diff --git a/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.aggregation/440_weighted_avg.yml b/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.aggregation/440_weighted_avg.yml new file mode 100644 index 0000000000000..2ffc7eaf74625 --- /dev/null +++ b/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.aggregation/440_weighted_avg.yml @@ -0,0 +1,199 @@ +setup: + - do: + indices.create: + index: test + body: + settings: + number_of_shards: 1 + number_of_replicas: 0 + mappings: + properties: + double_field: + type: double + long_field: + type: long + integer_field: + type: integer + multi_value_field: + type: integer + weight: + type: integer + + - do: + bulk: + refresh: true + index: test + body: + - '{ "index": {} }' + - '{ "double_field": 1.0, "long_field": 6, "integer_field": 10, "multi_value_field": [1, 2, 3], "weight": 2 }' + - '{ "index": {} }' + - '{ "double_field": 3.0, "long_field": 2, "multi_value_field": [0, 1, 4], "weight": 4 }' + - '{ "index": {} }' + - '{ "double_field": 1.0, "long_field": 1 }' + - '{ "index": {} }' + - '{ "double_field": 5.0, "long_field": 3, "integer_field": 4, "weight": 3 }' + +--- +"Basic test": + - skip: + features: close_to + - do: + search: + body: + aggs: + weighted_long_avg: + weighted_avg: + value: + field: long_field + weight: + field: weight + weighted_double_avg: + weighted_avg: + value: + field: double_field + weight: + field: weight + weighted_integer_avg: + weighted_avg: + value: + field: integer_field + weight: + field: weight + + - match: { hits.total.value: 4 } + - length: { hits.hits: 4 } + - close_to: { aggregations.weighted_long_avg.value: { value: 3.222222, error: 0.000001 } } + - close_to: { aggregations.weighted_double_avg.value: { value: 3.222222, error: 0.000001 } } + - close_to: { aggregations.weighted_integer_avg.value: { value: 6.400000, error: 0.000001 } } + +--- +"Value with explicit missing configuration": + - skip: + features: close_to + - do: + search: + body: + aggs: + weighted_integer_avg: + weighted_avg: + value: + field: integer_field + missing: 2 + weight: + field: weight + + - match: { hits.total.value: 4 } + - length: { hits.hits: 4 } + - close_to: { aggregations.weighted_integer_avg.value: { value: 4.444444, error: 0.000001 } } + +--- +"Weight with explicit missing configuration": + - skip: + features: close_to + - do: + search: + body: + aggs: + weighted_long_avg: + weighted_avg: + value: + field: long_field + weight: + field: weight + missing: 4 + + - match: { hits.total.value: 4 } + - length: { hits.hits: 4 } + - close_to: { aggregations.weighted_long_avg.value: { value: 2.538461, error: 0.000001 } } + +--- +"Missing value results in skipping document": + - skip: + features: close_to + - do: + search: + body: + aggs: + weighted_integer_avg: + weighted_avg: + value: + field: integer_field + weight: + field: weight + missing: 1 + + - match: { hits.total.value: 4 } + - length: { hits.hits: 4 } + - close_to: { aggregations.weighted_integer_avg.value: { value: 6.400000, error: 0.000001 } } + +--- +"Missing weight results in skipping document": + - skip: + features: close_to + - do: + search: + body: + aggs: + weighted_long_avg: + weighted_avg: + value: + field: long_field + weight: + field: weight + + - match: { hits.total.value: 4 } + - length: { hits.hits: 4 } + - close_to: { aggregations.weighted_long_avg.value: { value: 3.222222, error: 0.000001 } } + +--- +"Sum of weights equal to zero results in null weighted average": + - do: + search: + body: + aggs: + weighted_integer_avg: + weighted_avg: + value: + field: integer_field + weight: + field: unknown + missing: 0 + + - match: { hits.total.value: 4 } + - length: { hits.hits: 4 } + - match: { aggregations.weighted_integer_avg.value: null } + +--- +"Multi value field": + - skip: + features: close_to + - do: + search: + body: + aggs: + weighted_multi_value_avg: + weighted_avg: + value: + field: multi_value_field + weight: + field: weight + + - match: { hits.total.value: 4 } + - length: { hits.hits: 4 } + - close_to: { aggregations.weighted_multi_value_avg.value: { value: 1.777777, error: 0.000001 } } + +--- +"Multi weight field not allowed": + - skip: + features: close_to + - do: + catch: request + search: + body: + aggs: + weighted_multi_weight_avg: + weighted_avg: + value: + field: integer_field + weight: + field: multi_value_field