diff --git a/src/Aggregation/GlobalAggregation.php b/src/Aggregation/GlobalAggregation.php index 41c3d627f4..611309594c 100644 --- a/src/Aggregation/GlobalAggregation.php +++ b/src/Aggregation/GlobalAggregation.php @@ -9,6 +9,15 @@ */ class GlobalAggregation extends AbstractAggregation { + public function toArray(): array + { + $array = parent::toArray(); + // Force json encoding to object + $array[$this->_getBaseName()] = new \ArrayObject(); + + return $array; + } + protected function _getBaseName() { return 'global'; diff --git a/tests/Aggregation/GlobalAggregationTest.php b/tests/Aggregation/GlobalAggregationTest.php index d8b14fe247..595724ce9c 100644 --- a/tests/Aggregation/GlobalAggregationTest.php +++ b/tests/Aggregation/GlobalAggregationTest.php @@ -16,7 +16,7 @@ class GlobalAggregationTest extends BaseAggregationTest public function testToArray(): void { $expected = [ - 'global' => [], + 'global' => new \ArrayObject(), 'aggs' => [ 'avg_price' => ['avg' => ['field' => 'price']], ], @@ -27,5 +27,9 @@ public function testToArray(): void $avg->setField('price'); $agg->addAggregation($avg); $this->assertEquals($expected, $agg->toArray()); + $this->assertSame( + '{"global":{},"aggs":{"avg_price":{"avg":{"field":"price"}}}}', + \json_encode($agg->toArray()) + ); } }