From 049129c7b64eef8c45f9e2f4c9bf165c8724c5a7 Mon Sep 17 00:00:00 2001 From: Jay Deng Date: Thu, 22 Jun 2023 09:46:13 -0700 Subject: [PATCH] Return null for aggregations factory even if empty aggs is present in search source (#8206) * Return null for aggregations factory even if empty aggs is present in search source Signed-off-by: Jay Deng * Update rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/60_empty.yml Co-authored-by: Andriy Redko Signed-off-by: Jay Deng --------- Signed-off-by: Jay Deng Co-authored-by: Andriy Redko --- .../test/search.aggregation/60_empty.yml | 29 +++++++++++++++++++ .../aggregations/AggregatorFactories.java | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/60_empty.yml diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/60_empty.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/60_empty.yml new file mode 100644 index 0000000000000..7b374e3f6a409 --- /dev/null +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/60_empty.yml @@ -0,0 +1,29 @@ +--- +"Empty aggs Body": + - do: + index: + index: test + id: 1 + body: { "double" : 42 } + + - do: + index: + index: test + id: 2 + body: { "double" : 100 } + + - do: + index: + index: test + id: 3 + body: { "double" : 50 } + + - do: + indices.refresh: {} + + - do: + search: + rest_total_hits_as_int: true + body: { "size" : 0, "aggs" : { } } + + - match: { hits.total: 3 } diff --git a/server/src/main/java/org/opensearch/search/aggregations/AggregatorFactories.java b/server/src/main/java/org/opensearch/search/aggregations/AggregatorFactories.java index f760070a9b650..e57776fd78c49 100644 --- a/server/src/main/java/org/opensearch/search/aggregations/AggregatorFactories.java +++ b/server/src/main/java/org/opensearch/search/aggregations/AggregatorFactories.java @@ -235,7 +235,7 @@ private static AggregatorFactories.Builder parseAggregators(XContentParser parse } } - return factories; + return factories.count() > 0 ? factories : null; } public static final AggregatorFactories EMPTY = new AggregatorFactories(new AggregatorFactory[0]);