From 2f05d5afaf983afed64c3b19b9e65afcda237e7b Mon Sep 17 00:00:00 2001
From: Denis Urban <urbandenis0@gmail.com>
Date: Thu, 3 Aug 2023 15:44:35 +0200
Subject: [PATCH] fix empty objects being normalized as arrays

---
 src/Aggregation/AbstractAggregation.php                     | 6 +++---
 src/Aggregation/Bucketing/AdjacencyMatrixAggregation.php    | 2 +-
 src/Aggregation/Bucketing/AutoDateHistogramAggregation.php  | 2 +-
 src/Aggregation/Bucketing/ChildrenAggregation.php           | 2 +-
 src/Aggregation/Bucketing/CompositeAggregation.php          | 2 +-
 src/Aggregation/Bucketing/DateHistogramAggregation.php      | 2 +-
 src/Aggregation/Bucketing/DateRangeAggregation.php          | 2 +-
 src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php | 2 +-
 src/Aggregation/Bucketing/FilterAggregation.php             | 2 +-
 src/Aggregation/Bucketing/FiltersAggregation.php            | 2 +-
 src/Aggregation/Bucketing/GeoDistanceAggregation.php        | 2 +-
 src/Aggregation/Bucketing/GeoHashGridAggregation.php        | 2 +-
 src/Aggregation/Bucketing/GlobalAggregation.php             | 4 ++--
 src/Aggregation/Bucketing/HistogramAggregation.php          | 2 +-
 src/Aggregation/Bucketing/Ipv4RangeAggregation.php          | 2 +-
 src/Aggregation/Bucketing/MissingAggregation.php            | 2 +-
 src/Aggregation/Bucketing/NestedAggregation.php             | 2 +-
 src/Aggregation/Bucketing/RangeAggregation.php              | 2 +-
 src/Aggregation/Bucketing/ReverseNestedAggregation.php      | 4 ++--
 src/Aggregation/Bucketing/SamplerAggregation.php            | 2 +-
 src/Aggregation/Bucketing/TermsAggregation.php              | 2 +-
 src/Aggregation/Matrix/MatrixStatsAggregation.php           | 2 +-
 src/Aggregation/Metric/CardinalityAggregation.php           | 2 +-
 src/Aggregation/Metric/ExtendedStatsAggregation.php         | 2 +-
 src/Aggregation/Metric/GeoBoundsAggregation.php             | 2 +-
 src/Aggregation/Metric/GeoCentroidAggregation.php           | 2 +-
 src/Aggregation/Metric/PercentileRanksAggregation.php       | 2 +-
 src/Aggregation/Metric/PercentilesAggregation.php           | 2 +-
 src/Aggregation/Metric/ScriptedMetricAggregation.php        | 2 +-
 src/Aggregation/Metric/StatsAggregation.php                 | 2 +-
 src/Aggregation/Metric/TopHitsAggregation.php               | 4 ++--
 src/Aggregation/Pipeline/AbstractPipelineAggregation.php    | 2 +-
 src/Aggregation/Pipeline/BucketScriptAggregation.php        | 2 +-
 src/Aggregation/Pipeline/BucketSortAggregation.php          | 2 +-
 src/Aggregation/Pipeline/PercentilesBucketAggregation.php   | 2 +-
 src/BuilderBag.php                                          | 2 +-
 src/BuilderInterface.php                                    | 2 +-
 src/Highlight/Highlight.php                                 | 4 ++--
 src/InnerHit/NestedInnerHit.php                             | 4 ++--
 src/Query/Compound/BoolQuery.php                            | 4 ++--
 src/Query/Compound/BoostingQuery.php                        | 2 +-
 src/Query/Compound/ConstantScoreQuery.php                   | 2 +-
 src/Query/Compound/DisMaxQuery.php                          | 2 +-
 src/Query/Compound/FunctionScoreQuery.php                   | 4 ++--
 src/Query/FullText/CommonTermsQuery.php                     | 2 +-
 src/Query/FullText/MatchQuery.php                           | 2 +-
 src/Query/FullText/MultiMatchQuery.php                      | 2 +-
 src/Query/FullText/QueryStringQuery.php                     | 2 +-
 src/Query/FullText/SimpleQueryStringQuery.php               | 2 +-
 src/Query/Geo/GeoBoundingBoxQuery.php                       | 2 +-
 src/Query/Geo/GeoDistanceQuery.php                          | 2 +-
 src/Query/Geo/GeoPolygonQuery.php                           | 2 +-
 src/Query/Geo/GeoShapeQuery.php                             | 2 +-
 src/Query/Joining/HasChildQuery.php                         | 2 +-
 src/Query/Joining/HasParentQuery.php                        | 2 +-
 src/Query/Joining/NestedQuery.php                           | 2 +-
 src/Query/Joining/ParentIdQuery.php                         | 2 +-
 src/Query/MatchAllQuery.php                                 | 4 ++--
 src/Query/Span/FieldMaskingSpanQuery.php                    | 2 +-
 src/Query/Span/SpanContainingQuery.php                      | 2 +-
 src/Query/Span/SpanFirstQuery.php                           | 2 +-
 src/Query/Span/SpanMultiTermQuery.php                       | 2 +-
 src/Query/Span/SpanNearQuery.php                            | 2 +-
 src/Query/Span/SpanNotQuery.php                             | 2 +-
 src/Query/Span/SpanOrQuery.php                              | 2 +-
 src/Query/Specialized/MoreLikeThisQuery.php                 | 2 +-
 src/Query/Specialized/ScriptQuery.php                       | 2 +-
 src/Query/Specialized/TemplateQuery.php                     | 2 +-
 src/Query/TermLevel/ExistsQuery.php                         | 2 +-
 src/Query/TermLevel/FuzzyQuery.php                          | 2 +-
 src/Query/TermLevel/IdsQuery.php                            | 2 +-
 src/Query/TermLevel/PrefixQuery.php                         | 2 +-
 src/Query/TermLevel/RangeQuery.php                          | 2 +-
 src/Query/TermLevel/RegexpQuery.php                         | 2 +-
 src/Query/TermLevel/TermQuery.php                           | 2 +-
 src/Query/TermLevel/TermsQuery.php                          | 2 +-
 src/Query/TermLevel/TermsSetQuery.php                       | 2 +-
 src/Query/TermLevel/TypeQuery.php                           | 2 +-
 src/Query/TermLevel/WildcardQuery.php                       | 2 +-
 src/Search.php                                              | 2 +-
 src/Sort/FieldSort.php                                      | 4 ++--
 src/Sort/NestedSort.php                                     | 2 +-
 src/Suggest/Suggest.php                                     | 2 +-
 tests/Unit/Aggregation/Bucketing/GlobalAggregationTest.php  | 4 ++--
 .../Aggregation/Bucketing/ReverseNestedAggregationTest.php  | 2 +-
 tests/Unit/Highlight/HighlightTest.php                      | 2 +-
 tests/Unit/InnerHit/NestedInnerHitTest.php                  | 2 +-
 tests/Unit/Query/Compound/BoolQueryTest.php                 | 2 +-
 tests/Unit/Query/Compound/FunctionScoreQueryTest.php        | 2 +-
 tests/Unit/Query/MatchAllQueryTest.php                      | 2 +-
 90 files changed, 102 insertions(+), 102 deletions(-)

diff --git a/src/Aggregation/AbstractAggregation.php b/src/Aggregation/AbstractAggregation.php
index a2dd131..d83de82 100644
--- a/src/Aggregation/AbstractAggregation.php
+++ b/src/Aggregation/AbstractAggregation.php
@@ -41,7 +41,7 @@ abstract class AbstractAggregation implements NamedBuilderInterface
     /**
      * @return array|\stdClass
      */
-    abstract public function getArray(): array;
+    abstract public function getArray(): array|\stdClass;
 
     /**
      * Inner aggregations container init.
@@ -122,11 +122,11 @@ abstract class AbstractAggregation implements NamedBuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $array = $this->getArray();
         $result = [
-            $this->getType() => $this->processArray($array),
+            $this->getType() => is_array($array) ? $this->processArray($array) : $array,
         ];
 
         if ($this->supportsNesting()) {
diff --git a/src/Aggregation/Bucketing/AdjacencyMatrixAggregation.php b/src/Aggregation/Bucketing/AdjacencyMatrixAggregation.php
index 9bbcdc8..606c3cc 100644
--- a/src/Aggregation/Bucketing/AdjacencyMatrixAggregation.php
+++ b/src/Aggregation/Bucketing/AdjacencyMatrixAggregation.php
@@ -61,7 +61,7 @@ class AdjacencyMatrixAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         return $this->filters;
     }
diff --git a/src/Aggregation/Bucketing/AutoDateHistogramAggregation.php b/src/Aggregation/Bucketing/AutoDateHistogramAggregation.php
index 7e97b0d..45a30b6 100644
--- a/src/Aggregation/Bucketing/AutoDateHistogramAggregation.php
+++ b/src/Aggregation/Bucketing/AutoDateHistogramAggregation.php
@@ -50,7 +50,7 @@ class AutoDateHistogramAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         return array_filter(
             [
diff --git a/src/Aggregation/Bucketing/ChildrenAggregation.php b/src/Aggregation/Bucketing/ChildrenAggregation.php
index f81a192..c36203e 100644
--- a/src/Aggregation/Bucketing/ChildrenAggregation.php
+++ b/src/Aggregation/Bucketing/ChildrenAggregation.php
@@ -55,7 +55,7 @@ class ChildrenAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         if (count($this->getAggregations()) == 0) {
             throw new \LogicException(sprintf('Children aggregation `%s` has no aggregations added', $this->getName()));
diff --git a/src/Aggregation/Bucketing/CompositeAggregation.php b/src/Aggregation/Bucketing/CompositeAggregation.php
index 01d58b0..ad4df1a 100644
--- a/src/Aggregation/Bucketing/CompositeAggregation.php
+++ b/src/Aggregation/Bucketing/CompositeAggregation.php
@@ -63,7 +63,7 @@ class CompositeAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         $array = [
             'sources' => $this->sources,
diff --git a/src/Aggregation/Bucketing/DateHistogramAggregation.php b/src/Aggregation/Bucketing/DateHistogramAggregation.php
index abea7fc..f78f3eb 100644
--- a/src/Aggregation/Bucketing/DateHistogramAggregation.php
+++ b/src/Aggregation/Bucketing/DateHistogramAggregation.php
@@ -68,7 +68,7 @@ class DateHistogramAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         if (!$this->getField() || !$this->getInterval()) {
             throw new \LogicException('Date histogram aggregation must have field and interval set.');
diff --git a/src/Aggregation/Bucketing/DateRangeAggregation.php b/src/Aggregation/Bucketing/DateRangeAggregation.php
index 9197a9c..77829f1 100644
--- a/src/Aggregation/Bucketing/DateRangeAggregation.php
+++ b/src/Aggregation/Bucketing/DateRangeAggregation.php
@@ -98,7 +98,7 @@ class DateRangeAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         if ($this->getField() && $this->getFormat() && !empty($this->ranges)) {
             return [
diff --git a/src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php b/src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php
index b00793c..461c165 100644
--- a/src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php
+++ b/src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php
@@ -73,7 +73,7 @@ class DiversifiedSamplerAggregation extends AbstractAggregation
     /**
      * @inheritdoc
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         return array_filter(
             [
diff --git a/src/Aggregation/Bucketing/FilterAggregation.php b/src/Aggregation/Bucketing/FilterAggregation.php
index dff3d33..d5f936c 100644
--- a/src/Aggregation/Bucketing/FilterAggregation.php
+++ b/src/Aggregation/Bucketing/FilterAggregation.php
@@ -67,7 +67,7 @@ class FilterAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         if (!$this->filter) {
             throw new \LogicException(sprintf('Filter aggregation `%s` has no filter added', $this->getName()));
diff --git a/src/Aggregation/Bucketing/FiltersAggregation.php b/src/Aggregation/Bucketing/FiltersAggregation.php
index ead56bb..72fee22 100644
--- a/src/Aggregation/Bucketing/FiltersAggregation.php
+++ b/src/Aggregation/Bucketing/FiltersAggregation.php
@@ -78,7 +78,7 @@ class FiltersAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         return $this->filters;
     }
diff --git a/src/Aggregation/Bucketing/GeoDistanceAggregation.php b/src/Aggregation/Bucketing/GeoDistanceAggregation.php
index f9857d8..7432a5e 100644
--- a/src/Aggregation/Bucketing/GeoDistanceAggregation.php
+++ b/src/Aggregation/Bucketing/GeoDistanceAggregation.php
@@ -122,7 +122,7 @@ class GeoDistanceAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         $data = [];
 
diff --git a/src/Aggregation/Bucketing/GeoHashGridAggregation.php b/src/Aggregation/Bucketing/GeoHashGridAggregation.php
index 4199342..675ee62 100644
--- a/src/Aggregation/Bucketing/GeoHashGridAggregation.php
+++ b/src/Aggregation/Bucketing/GeoHashGridAggregation.php
@@ -120,7 +120,7 @@ class GeoHashGridAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         $data = [];
 
diff --git a/src/Aggregation/Bucketing/GlobalAggregation.php b/src/Aggregation/Bucketing/GlobalAggregation.php
index 4e4fac3..92f9221 100644
--- a/src/Aggregation/Bucketing/GlobalAggregation.php
+++ b/src/Aggregation/Bucketing/GlobalAggregation.php
@@ -42,8 +42,8 @@ class GlobalAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
-        return [];
+        return new \stdClass();
     }
 }
diff --git a/src/Aggregation/Bucketing/HistogramAggregation.php b/src/Aggregation/Bucketing/HistogramAggregation.php
index eb84237..2f27e39 100644
--- a/src/Aggregation/Bucketing/HistogramAggregation.php
+++ b/src/Aggregation/Bucketing/HistogramAggregation.php
@@ -218,7 +218,7 @@ class HistogramAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         $out = array_filter(
             [
diff --git a/src/Aggregation/Bucketing/Ipv4RangeAggregation.php b/src/Aggregation/Bucketing/Ipv4RangeAggregation.php
index 7538896..066e82d 100644
--- a/src/Aggregation/Bucketing/Ipv4RangeAggregation.php
+++ b/src/Aggregation/Bucketing/Ipv4RangeAggregation.php
@@ -83,7 +83,7 @@ class Ipv4RangeAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         if ($this->getField() && !empty($this->ranges)) {
             return [
diff --git a/src/Aggregation/Bucketing/MissingAggregation.php b/src/Aggregation/Bucketing/MissingAggregation.php
index c3eb5cc..01af957 100644
--- a/src/Aggregation/Bucketing/MissingAggregation.php
+++ b/src/Aggregation/Bucketing/MissingAggregation.php
@@ -36,7 +36,7 @@ class MissingAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         if ($this->getField()) {
             return ['field' => $this->getField()];
diff --git a/src/Aggregation/Bucketing/NestedAggregation.php b/src/Aggregation/Bucketing/NestedAggregation.php
index e54ea12..fc47fe6 100644
--- a/src/Aggregation/Bucketing/NestedAggregation.php
+++ b/src/Aggregation/Bucketing/NestedAggregation.php
@@ -74,7 +74,7 @@ class NestedAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         return ['path' => $this->getPath()];
     }
diff --git a/src/Aggregation/Bucketing/RangeAggregation.php b/src/Aggregation/Bucketing/RangeAggregation.php
index 12aaf41..a1c87d3 100644
--- a/src/Aggregation/Bucketing/RangeAggregation.php
+++ b/src/Aggregation/Bucketing/RangeAggregation.php
@@ -112,7 +112,7 @@ class RangeAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         $data = [
             'keyed' => $this->keyed,
diff --git a/src/Aggregation/Bucketing/ReverseNestedAggregation.php b/src/Aggregation/Bucketing/ReverseNestedAggregation.php
index c14b0e8..bc66b48 100644
--- a/src/Aggregation/Bucketing/ReverseNestedAggregation.php
+++ b/src/Aggregation/Bucketing/ReverseNestedAggregation.php
@@ -68,9 +68,9 @@ class ReverseNestedAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
-        $output = [];
+        $output = new \stdClass();
         if ($this->getPath()) {
             $output = ['path' => $this->getPath()];
         }
diff --git a/src/Aggregation/Bucketing/SamplerAggregation.php b/src/Aggregation/Bucketing/SamplerAggregation.php
index 3bdc031..73bf0c5 100644
--- a/src/Aggregation/Bucketing/SamplerAggregation.php
+++ b/src/Aggregation/Bucketing/SamplerAggregation.php
@@ -75,7 +75,7 @@ class SamplerAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         return array_filter(
             [
diff --git a/src/Aggregation/Bucketing/TermsAggregation.php b/src/Aggregation/Bucketing/TermsAggregation.php
index 7ada672..d6b6335 100644
--- a/src/Aggregation/Bucketing/TermsAggregation.php
+++ b/src/Aggregation/Bucketing/TermsAggregation.php
@@ -51,7 +51,7 @@ class TermsAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         return array_filter(
             [
diff --git a/src/Aggregation/Matrix/MatrixStatsAggregation.php b/src/Aggregation/Matrix/MatrixStatsAggregation.php
index f9cf983..648df48 100644
--- a/src/Aggregation/Matrix/MatrixStatsAggregation.php
+++ b/src/Aggregation/Matrix/MatrixStatsAggregation.php
@@ -92,7 +92,7 @@ class MatrixStatsAggregation extends AbstractAggregation
         return 'matrix_stats';
     }
 
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         $out = [];
         if ($this->getField()) {
diff --git a/src/Aggregation/Metric/CardinalityAggregation.php b/src/Aggregation/Metric/CardinalityAggregation.php
index 51486d5..ab6706f 100644
--- a/src/Aggregation/Metric/CardinalityAggregation.php
+++ b/src/Aggregation/Metric/CardinalityAggregation.php
@@ -38,7 +38,7 @@ class CardinalityAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         $out = array_filter(
             [
diff --git a/src/Aggregation/Metric/ExtendedStatsAggregation.php b/src/Aggregation/Metric/ExtendedStatsAggregation.php
index 5fdcb9c..b807a97 100644
--- a/src/Aggregation/Metric/ExtendedStatsAggregation.php
+++ b/src/Aggregation/Metric/ExtendedStatsAggregation.php
@@ -78,7 +78,7 @@ class ExtendedStatsAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         return array_filter(
             [
diff --git a/src/Aggregation/Metric/GeoBoundsAggregation.php b/src/Aggregation/Metric/GeoBoundsAggregation.php
index 13c3503..3d363ae 100644
--- a/src/Aggregation/Metric/GeoBoundsAggregation.php
+++ b/src/Aggregation/Metric/GeoBoundsAggregation.php
@@ -54,7 +54,7 @@ class GeoBoundsAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         $data = [];
         if ($this->getField()) {
diff --git a/src/Aggregation/Metric/GeoCentroidAggregation.php b/src/Aggregation/Metric/GeoCentroidAggregation.php
index 3b0529a..33d090c 100644
--- a/src/Aggregation/Metric/GeoCentroidAggregation.php
+++ b/src/Aggregation/Metric/GeoCentroidAggregation.php
@@ -36,7 +36,7 @@ class GeoCentroidAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         $data = [];
         if ($this->getField()) {
diff --git a/src/Aggregation/Metric/PercentileRanksAggregation.php b/src/Aggregation/Metric/PercentileRanksAggregation.php
index 2f7969f..0631d67 100644
--- a/src/Aggregation/Metric/PercentileRanksAggregation.php
+++ b/src/Aggregation/Metric/PercentileRanksAggregation.php
@@ -105,7 +105,7 @@ class PercentileRanksAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         $out = array_filter(
             [
diff --git a/src/Aggregation/Metric/PercentilesAggregation.php b/src/Aggregation/Metric/PercentilesAggregation.php
index 385ef4b..ede8b5e 100644
--- a/src/Aggregation/Metric/PercentilesAggregation.php
+++ b/src/Aggregation/Metric/PercentilesAggregation.php
@@ -105,7 +105,7 @@ class PercentilesAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         $out = array_filter(
             [
diff --git a/src/Aggregation/Metric/ScriptedMetricAggregation.php b/src/Aggregation/Metric/ScriptedMetricAggregation.php
index 903c6e9..653dfce 100644
--- a/src/Aggregation/Metric/ScriptedMetricAggregation.php
+++ b/src/Aggregation/Metric/ScriptedMetricAggregation.php
@@ -151,7 +151,7 @@ class ScriptedMetricAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         return array_filter(
             [
diff --git a/src/Aggregation/Metric/StatsAggregation.php b/src/Aggregation/Metric/StatsAggregation.php
index 111c648..776e927 100644
--- a/src/Aggregation/Metric/StatsAggregation.php
+++ b/src/Aggregation/Metric/StatsAggregation.php
@@ -47,7 +47,7 @@ class StatsAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         $out = [];
         if ($this->getField()) {
diff --git a/src/Aggregation/Metric/TopHitsAggregation.php b/src/Aggregation/Metric/TopHitsAggregation.php
index d81ee3d..aed69bc 100644
--- a/src/Aggregation/Metric/TopHitsAggregation.php
+++ b/src/Aggregation/Metric/TopHitsAggregation.php
@@ -117,7 +117,7 @@ class TopHitsAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         $sortsOutput = null;
         $addedSorts = array_filter($this->getSorts());
@@ -136,6 +136,6 @@ class TopHitsAggregation extends AbstractAggregation
             static fn($val): bool => $val || is_array($val) || ($val || is_numeric($val))
         );
 
-        return empty($output) ? [] : $output;
+        return empty($output) ? new \stdClass() : $output;
     }
 }
diff --git a/src/Aggregation/Pipeline/AbstractPipelineAggregation.php b/src/Aggregation/Pipeline/AbstractPipelineAggregation.php
index b797d0f..c701bac 100644
--- a/src/Aggregation/Pipeline/AbstractPipelineAggregation.php
+++ b/src/Aggregation/Pipeline/AbstractPipelineAggregation.php
@@ -47,7 +47,7 @@ abstract class AbstractPipelineAggregation extends AbstractAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         return ['buckets_path' => $this->getBucketsPath()];
     }
diff --git a/src/Aggregation/Pipeline/BucketScriptAggregation.php b/src/Aggregation/Pipeline/BucketScriptAggregation.php
index a39d775..8cc36fa 100644
--- a/src/Aggregation/Pipeline/BucketScriptAggregation.php
+++ b/src/Aggregation/Pipeline/BucketScriptAggregation.php
@@ -52,7 +52,7 @@ class BucketScriptAggregation extends AbstractPipelineAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         if (!$this->getScript()) {
             throw new \LogicException(
diff --git a/src/Aggregation/Pipeline/BucketSortAggregation.php b/src/Aggregation/Pipeline/BucketSortAggregation.php
index 3c09af1..f6f06a8 100644
--- a/src/Aggregation/Pipeline/BucketSortAggregation.php
+++ b/src/Aggregation/Pipeline/BucketSortAggregation.php
@@ -68,7 +68,7 @@ class BucketSortAggregation extends AbstractPipelineAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         return array_filter(
             [
diff --git a/src/Aggregation/Pipeline/PercentilesBucketAggregation.php b/src/Aggregation/Pipeline/PercentilesBucketAggregation.php
index 83d7c43..f6b4f9b 100644
--- a/src/Aggregation/Pipeline/PercentilesBucketAggregation.php
+++ b/src/Aggregation/Pipeline/PercentilesBucketAggregation.php
@@ -49,7 +49,7 @@ class PercentilesBucketAggregation extends AbstractPipelineAggregation
     /**
      * {@inheritdoc}
      */
-    public function getArray(): array
+    public function getArray(): array|\stdClass
     {
         $data = ['buckets_path' => $this->getBucketsPath()];
 
diff --git a/src/BuilderBag.php b/src/BuilderBag.php
index cdb6ea1..3a23a45 100644
--- a/src/BuilderBag.php
+++ b/src/BuilderBag.php
@@ -106,7 +106,7 @@ class BuilderBag
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $output = [];
         foreach ($this->all() as $builder) {
diff --git a/src/BuilderInterface.php b/src/BuilderInterface.php
index f57e0a0..7e45aa7 100644
--- a/src/BuilderInterface.php
+++ b/src/BuilderInterface.php
@@ -21,7 +21,7 @@ interface BuilderInterface
      *
      * @return array
      */
-    public function toArray(): array;
+    public function toArray(): array|\stdClass;
 
     /**
      * Returns element type.
diff --git a/src/Highlight/Highlight.php b/src/Highlight/Highlight.php
index 8bc04c6..adb74be 100644
--- a/src/Highlight/Highlight.php
+++ b/src/Highlight/Highlight.php
@@ -58,7 +58,7 @@ class Highlight implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $output = [];
 
@@ -69,7 +69,7 @@ class Highlight implements BuilderInterface
         $output = $this->processArray($output);
 
         foreach ($this->fields as $field => $params) {
-            $output['fields'][$field] = count($params) ? $params : [];
+            $output['fields'][$field] = count($params) ? $params : new \stdClass();
         }
 
         return $output;
diff --git a/src/InnerHit/NestedInnerHit.php b/src/InnerHit/NestedInnerHit.php
index 649ebad..467fdf6 100644
--- a/src/InnerHit/NestedInnerHit.php
+++ b/src/InnerHit/NestedInnerHit.php
@@ -77,9 +77,9 @@ class NestedInnerHit implements NamedBuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
-        $out = $this->getSearch() ? $this->getSearch()->toArray() : [];
+        $out = $this->getSearch() ? $this->getSearch()->toArray() : new \stdClass();
 
         return [
             $this->getPathType() => [
diff --git a/src/Query/Compound/BoolQuery.php b/src/Query/Compound/BoolQuery.php
index 3bb46a4..f4647b9 100644
--- a/src/Query/Compound/BoolQuery.php
+++ b/src/Query/Compound/BoolQuery.php
@@ -95,7 +95,7 @@ class BoolQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         if (count($this->container) === 1 && isset($this->container[self::MUST])
                 && count($this->container[self::MUST]) === 1) {
@@ -116,7 +116,7 @@ class BoolQuery implements BuilderInterface
         $output = $this->processArray($output);
 
         if (empty($output)) {
-            $output = [];
+            $output = new \stdClass();
         }
 
         return [$this->getType() => $output];
diff --git a/src/Query/Compound/BoostingQuery.php b/src/Query/Compound/BoostingQuery.php
index 0df5fd2..46830f9 100644
--- a/src/Query/Compound/BoostingQuery.php
+++ b/src/Query/Compound/BoostingQuery.php
@@ -38,7 +38,7 @@ class BoostingQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'positive' => $this->positive->toArray(),
diff --git a/src/Query/Compound/ConstantScoreQuery.php b/src/Query/Compound/ConstantScoreQuery.php
index c1a19da..dd1d372 100644
--- a/src/Query/Compound/ConstantScoreQuery.php
+++ b/src/Query/Compound/ConstantScoreQuery.php
@@ -39,7 +39,7 @@ class ConstantScoreQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'filter' => $this->query->toArray(),
diff --git a/src/Query/Compound/DisMaxQuery.php b/src/Query/Compound/DisMaxQuery.php
index 4bde4c4..bc00c14 100644
--- a/src/Query/Compound/DisMaxQuery.php
+++ b/src/Query/Compound/DisMaxQuery.php
@@ -60,7 +60,7 @@ class DisMaxQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [];
         foreach ($this->queries as $type) {
diff --git a/src/Query/Compound/FunctionScoreQuery.php b/src/Query/Compound/FunctionScoreQuery.php
index 323beb6..bdd93c5 100644
--- a/src/Query/Compound/FunctionScoreQuery.php
+++ b/src/Query/Compound/FunctionScoreQuery.php
@@ -149,7 +149,7 @@ class FunctionScoreQuery implements BuilderInterface
     public function addRandomFunction(mixed $seed = null, BuilderInterface $query = null)
     {
         $function = [
-            'random_score' => $seed ? ['seed' => $seed] : [],
+            'random_score' => $seed ? ['seed' => $seed] : new \stdClass(),
         ];
 
         $this->applyFilter($function, $query);
@@ -210,7 +210,7 @@ class FunctionScoreQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'query' => $this->query->toArray(),
diff --git a/src/Query/FullText/CommonTermsQuery.php b/src/Query/FullText/CommonTermsQuery.php
index 66c19c9..5b365cf 100644
--- a/src/Query/FullText/CommonTermsQuery.php
+++ b/src/Query/FullText/CommonTermsQuery.php
@@ -43,7 +43,7 @@ class CommonTermsQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'query' => $this->query,
diff --git a/src/Query/FullText/MatchQuery.php b/src/Query/FullText/MatchQuery.php
index 7f5398a..e8dd8ee 100644
--- a/src/Query/FullText/MatchQuery.php
+++ b/src/Query/FullText/MatchQuery.php
@@ -43,7 +43,7 @@ class MatchQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'query' => $this->query,
diff --git a/src/Query/FullText/MultiMatchQuery.php b/src/Query/FullText/MultiMatchQuery.php
index 89ee785..c6f538f 100644
--- a/src/Query/FullText/MultiMatchQuery.php
+++ b/src/Query/FullText/MultiMatchQuery.php
@@ -45,7 +45,7 @@ class MultiMatchQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'query' => $this->query,
diff --git a/src/Query/FullText/QueryStringQuery.php b/src/Query/FullText/QueryStringQuery.php
index 9b90f02..cfbf323 100644
--- a/src/Query/FullText/QueryStringQuery.php
+++ b/src/Query/FullText/QueryStringQuery.php
@@ -42,7 +42,7 @@ class QueryStringQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'query' => $this->query,
diff --git a/src/Query/FullText/SimpleQueryStringQuery.php b/src/Query/FullText/SimpleQueryStringQuery.php
index efee2cd..8d48f35 100644
--- a/src/Query/FullText/SimpleQueryStringQuery.php
+++ b/src/Query/FullText/SimpleQueryStringQuery.php
@@ -42,7 +42,7 @@ class SimpleQueryStringQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'query' => $this->query,
diff --git a/src/Query/Geo/GeoBoundingBoxQuery.php b/src/Query/Geo/GeoBoundingBoxQuery.php
index e2db2fc..a5f85a1 100644
--- a/src/Query/Geo/GeoBoundingBoxQuery.php
+++ b/src/Query/Geo/GeoBoundingBoxQuery.php
@@ -43,7 +43,7 @@ class GeoBoundingBoxQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         return [
             $this->getType() => $this->processArray([$this->field => $this->points()])
diff --git a/src/Query/Geo/GeoDistanceQuery.php b/src/Query/Geo/GeoDistanceQuery.php
index df60072..8156b54 100644
--- a/src/Query/Geo/GeoDistanceQuery.php
+++ b/src/Query/Geo/GeoDistanceQuery.php
@@ -43,7 +43,7 @@ class GeoDistanceQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'distance' => $this->distance,
diff --git a/src/Query/Geo/GeoPolygonQuery.php b/src/Query/Geo/GeoPolygonQuery.php
index 656154d..27eeab7 100644
--- a/src/Query/Geo/GeoPolygonQuery.php
+++ b/src/Query/Geo/GeoPolygonQuery.php
@@ -42,7 +42,7 @@ class GeoPolygonQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [$this->field => ['points' => $this->points]];
         $output = $this->processArray($query);
diff --git a/src/Query/Geo/GeoShapeQuery.php b/src/Query/Geo/GeoShapeQuery.php
index 029b233..212cc4a 100644
--- a/src/Query/Geo/GeoShapeQuery.php
+++ b/src/Query/Geo/GeoShapeQuery.php
@@ -124,7 +124,7 @@ class GeoShapeQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $output = $this->processArray($this->fields);
 
diff --git a/src/Query/Joining/HasChildQuery.php b/src/Query/Joining/HasChildQuery.php
index 809d59a..c3f364a 100644
--- a/src/Query/Joining/HasChildQuery.php
+++ b/src/Query/Joining/HasChildQuery.php
@@ -42,7 +42,7 @@ class HasChildQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'type' => $this->type,
diff --git a/src/Query/Joining/HasParentQuery.php b/src/Query/Joining/HasParentQuery.php
index 37fe5e1..55efeb8 100644
--- a/src/Query/Joining/HasParentQuery.php
+++ b/src/Query/Joining/HasParentQuery.php
@@ -42,7 +42,7 @@ class HasParentQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'parent_type' => $this->parentType,
diff --git a/src/Query/Joining/NestedQuery.php b/src/Query/Joining/NestedQuery.php
index c77958c..3147ec1 100644
--- a/src/Query/Joining/NestedQuery.php
+++ b/src/Query/Joining/NestedQuery.php
@@ -42,7 +42,7 @@ class NestedQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         return [
             $this->getType() => $this->processArray(
diff --git a/src/Query/Joining/ParentIdQuery.php b/src/Query/Joining/ParentIdQuery.php
index 8c2be24..3b1426e 100644
--- a/src/Query/Joining/ParentIdQuery.php
+++ b/src/Query/Joining/ParentIdQuery.php
@@ -31,7 +31,7 @@ class ParentIdQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'id' => $this->parentId,
diff --git a/src/Query/MatchAllQuery.php b/src/Query/MatchAllQuery.php
index 0a727ab..870c190 100644
--- a/src/Query/MatchAllQuery.php
+++ b/src/Query/MatchAllQuery.php
@@ -42,9 +42,9 @@ class MatchAllQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $params = $this->getParameters();
-        return [$this->getType() => !empty($params) ? $params : []];
+        return [$this->getType() => !empty($params) ? $params : new \stdClass()];
     }
 }
diff --git a/src/Query/Span/FieldMaskingSpanQuery.php b/src/Query/Span/FieldMaskingSpanQuery.php
index 9cd641f..e14b237 100644
--- a/src/Query/Span/FieldMaskingSpanQuery.php
+++ b/src/Query/Span/FieldMaskingSpanQuery.php
@@ -82,7 +82,7 @@ class FieldMaskingSpanQuery implements SpanQueryInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $output = [
             'query' => $this->getQuery()->toArray(),
diff --git a/src/Query/Span/SpanContainingQuery.php b/src/Query/Span/SpanContainingQuery.php
index ed23bd0..ec26948 100644
--- a/src/Query/Span/SpanContainingQuery.php
+++ b/src/Query/Span/SpanContainingQuery.php
@@ -79,7 +79,7 @@ class SpanContainingQuery implements SpanQueryInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $output = [
             'little' => $this->getLittle()->toArray(),
diff --git a/src/Query/Span/SpanFirstQuery.php b/src/Query/Span/SpanFirstQuery.php
index 61e33df..ca0c896 100644
--- a/src/Query/Span/SpanFirstQuery.php
+++ b/src/Query/Span/SpanFirstQuery.php
@@ -43,7 +43,7 @@ class SpanFirstQuery implements SpanQueryInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [];
         $query['match'] = $this->query->toArray();
diff --git a/src/Query/Span/SpanMultiTermQuery.php b/src/Query/Span/SpanMultiTermQuery.php
index ac02169..5fbb4b4 100644
--- a/src/Query/Span/SpanMultiTermQuery.php
+++ b/src/Query/Span/SpanMultiTermQuery.php
@@ -44,7 +44,7 @@ class SpanMultiTermQuery implements SpanQueryInterface
      *
      * @throws \InvalidArgumentException
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [];
         $query['match'] = $this->query->toArray();
diff --git a/src/Query/Span/SpanNearQuery.php b/src/Query/Span/SpanNearQuery.php
index b0392ac..24a6b07 100644
--- a/src/Query/Span/SpanNearQuery.php
+++ b/src/Query/Span/SpanNearQuery.php
@@ -54,7 +54,7 @@ class SpanNearQuery extends SpanOrQuery implements SpanQueryInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [];
         foreach ($this->getQueries() as $type) {
diff --git a/src/Query/Span/SpanNotQuery.php b/src/Query/Span/SpanNotQuery.php
index 2929b6a..80b8474 100644
--- a/src/Query/Span/SpanNotQuery.php
+++ b/src/Query/Span/SpanNotQuery.php
@@ -38,7 +38,7 @@ class SpanNotQuery implements SpanQueryInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'include' => $this->include->toArray(),
diff --git a/src/Query/Span/SpanOrQuery.php b/src/Query/Span/SpanOrQuery.php
index 3ff25aa..296cab2 100644
--- a/src/Query/Span/SpanOrQuery.php
+++ b/src/Query/Span/SpanOrQuery.php
@@ -64,7 +64,7 @@ class SpanOrQuery implements SpanQueryInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [];
         foreach ($this->queries as $type) {
diff --git a/src/Query/Specialized/MoreLikeThisQuery.php b/src/Query/Specialized/MoreLikeThisQuery.php
index 90d5d19..914c862 100644
--- a/src/Query/Specialized/MoreLikeThisQuery.php
+++ b/src/Query/Specialized/MoreLikeThisQuery.php
@@ -42,7 +42,7 @@ class MoreLikeThisQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [];
 
diff --git a/src/Query/Specialized/ScriptQuery.php b/src/Query/Specialized/ScriptQuery.php
index ce166d3..3dd1a3f 100644
--- a/src/Query/Specialized/ScriptQuery.php
+++ b/src/Query/Specialized/ScriptQuery.php
@@ -43,7 +43,7 @@ class ScriptQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = ['inline' => $this->script];
         $output = $this->processArray($query);
diff --git a/src/Query/Specialized/TemplateQuery.php b/src/Query/Specialized/TemplateQuery.php
index de5c96e..c5ffba2 100644
--- a/src/Query/Specialized/TemplateQuery.php
+++ b/src/Query/Specialized/TemplateQuery.php
@@ -121,7 +121,7 @@ class TemplateQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $output = array_filter(
             [
diff --git a/src/Query/TermLevel/ExistsQuery.php b/src/Query/TermLevel/ExistsQuery.php
index 49a5d32..74953ce 100644
--- a/src/Query/TermLevel/ExistsQuery.php
+++ b/src/Query/TermLevel/ExistsQuery.php
@@ -38,7 +38,7 @@ class ExistsQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         return [
             $this->getType() => [
diff --git a/src/Query/TermLevel/FuzzyQuery.php b/src/Query/TermLevel/FuzzyQuery.php
index 418c919..2638a0d 100644
--- a/src/Query/TermLevel/FuzzyQuery.php
+++ b/src/Query/TermLevel/FuzzyQuery.php
@@ -43,7 +43,7 @@ class FuzzyQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'value' => $this->value,
diff --git a/src/Query/TermLevel/IdsQuery.php b/src/Query/TermLevel/IdsQuery.php
index 749066b..4a173c8 100644
--- a/src/Query/TermLevel/IdsQuery.php
+++ b/src/Query/TermLevel/IdsQuery.php
@@ -39,7 +39,7 @@ class IdsQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'values' => $this->values,
diff --git a/src/Query/TermLevel/PrefixQuery.php b/src/Query/TermLevel/PrefixQuery.php
index 5b0a20b..f1648d8 100644
--- a/src/Query/TermLevel/PrefixQuery.php
+++ b/src/Query/TermLevel/PrefixQuery.php
@@ -44,7 +44,7 @@ class PrefixQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'value' => $this->value,
diff --git a/src/Query/TermLevel/RangeQuery.php b/src/Query/TermLevel/RangeQuery.php
index 338224b..4cca3f1 100644
--- a/src/Query/TermLevel/RangeQuery.php
+++ b/src/Query/TermLevel/RangeQuery.php
@@ -61,7 +61,7 @@ class RangeQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $output = [
             $this->field => $this->getParameters(),
diff --git a/src/Query/TermLevel/RegexpQuery.php b/src/Query/TermLevel/RegexpQuery.php
index 5ffde9e..a4aa518 100644
--- a/src/Query/TermLevel/RegexpQuery.php
+++ b/src/Query/TermLevel/RegexpQuery.php
@@ -43,7 +43,7 @@ class RegexpQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'value' => $this->regexpValue,
diff --git a/src/Query/TermLevel/TermQuery.php b/src/Query/TermLevel/TermQuery.php
index 73afffa..3b5766d 100644
--- a/src/Query/TermLevel/TermQuery.php
+++ b/src/Query/TermLevel/TermQuery.php
@@ -39,7 +39,7 @@ class TermQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = $this->processArray();
 
diff --git a/src/Query/TermLevel/TermsQuery.php b/src/Query/TermLevel/TermsQuery.php
index 2269701..be2da6d 100644
--- a/src/Query/TermLevel/TermsQuery.php
+++ b/src/Query/TermLevel/TermsQuery.php
@@ -46,7 +46,7 @@ class TermsQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             $this->field => $this->terms,
diff --git a/src/Query/TermLevel/TermsSetQuery.php b/src/Query/TermLevel/TermsSetQuery.php
index d15595c..a644f13 100644
--- a/src/Query/TermLevel/TermsSetQuery.php
+++ b/src/Query/TermLevel/TermsSetQuery.php
@@ -51,7 +51,7 @@ class TermsSetQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'terms' => $this->terms,
diff --git a/src/Query/TermLevel/TypeQuery.php b/src/Query/TermLevel/TypeQuery.php
index 533be8f..5f42816 100644
--- a/src/Query/TermLevel/TypeQuery.php
+++ b/src/Query/TermLevel/TypeQuery.php
@@ -40,7 +40,7 @@ class TypeQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         return [
             $this->getType() => [
diff --git a/src/Query/TermLevel/WildcardQuery.php b/src/Query/TermLevel/WildcardQuery.php
index 901dfb0..19dbbcc 100644
--- a/src/Query/TermLevel/WildcardQuery.php
+++ b/src/Query/TermLevel/WildcardQuery.php
@@ -43,7 +43,7 @@ class WildcardQuery implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $query = [
             'value' => $this->value,
diff --git a/src/Search.php b/src/Search.php
index 0d21cbf..e9e99a4 100644
--- a/src/Search.php
+++ b/src/Search.php
@@ -724,7 +724,7 @@ class Search
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $output = array_filter(static::$serializer->normalize($this->endpoints));
 
diff --git a/src/Sort/FieldSort.php b/src/Sort/FieldSort.php
index fe7c1d5..6d343c7 100644
--- a/src/Sort/FieldSort.php
+++ b/src/Sort/FieldSort.php
@@ -83,7 +83,7 @@ class FieldSort implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         if ($this->order) {
             $this->addParameter('order', $this->order);
@@ -94,7 +94,7 @@ class FieldSort implements BuilderInterface
         }
 
         return [
-            $this->field => $this->getParameters() ?: [],
+            $this->field => $this->getParameters() ?: new \stdClass(),
         ];
     }
 }
diff --git a/src/Sort/NestedSort.php b/src/Sort/NestedSort.php
index 355ece6..d61c640 100644
--- a/src/Sort/NestedSort.php
+++ b/src/Sort/NestedSort.php
@@ -48,7 +48,7 @@ class NestedSort implements BuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         $output = [
             'path'   => $this->path,
diff --git a/src/Suggest/Suggest.php b/src/Suggest/Suggest.php
index 95fa710..2054496 100644
--- a/src/Suggest/Suggest.php
+++ b/src/Suggest/Suggest.php
@@ -86,7 +86,7 @@ class Suggest implements NamedBuilderInterface
     /**
      * {@inheritdoc}
      */
-    public function toArray(): array
+    public function toArray(): array|\stdClass
     {
         return [
             $this->getName() => [
diff --git a/tests/Unit/Aggregation/Bucketing/GlobalAggregationTest.php b/tests/Unit/Aggregation/Bucketing/GlobalAggregationTest.php
index 107c2ea..6ab648b 100644
--- a/tests/Unit/Aggregation/Bucketing/GlobalAggregationTest.php
+++ b/tests/Unit/Aggregation/Bucketing/GlobalAggregationTest.php
@@ -28,7 +28,7 @@ class GlobalAggregationTest extends TestCase
         $aggregation = new GlobalAggregation('test_agg');
 
         $result = [
-            'global' => [],
+            'global' => new \stdClass(),
         ];
 
         $out[] = [
@@ -42,7 +42,7 @@ class GlobalAggregationTest extends TestCase
         $aggregation->addAggregation($aggregation2);
 
         $result = [
-            'global' => [],
+            'global' => new \stdClass(),
             'aggregations' => [
                 $aggregation2->getName() => $aggregation2->toArray(),
             ],
diff --git a/tests/Unit/Aggregation/Bucketing/ReverseNestedAggregationTest.php b/tests/Unit/Aggregation/Bucketing/ReverseNestedAggregationTest.php
index 62d711d..3e28739 100644
--- a/tests/Unit/Aggregation/Bucketing/ReverseNestedAggregationTest.php
+++ b/tests/Unit/Aggregation/Bucketing/ReverseNestedAggregationTest.php
@@ -49,7 +49,7 @@ class ReverseNestedAggregationTest extends TestCase
         $aggregation->addAggregation($termAggregation);
 
         $expectedResult = [
-            'reverse_nested' => [],
+            'reverse_nested' => new \stdClass(),
             'aggregations' => [
                 $termAggregation->getName() => $termAggregation->toArray(),
             ],
diff --git a/tests/Unit/Highlight/HighlightTest.php b/tests/Unit/Highlight/HighlightTest.php
index c4761d8..da6df7e 100644
--- a/tests/Unit/Highlight/HighlightTest.php
+++ b/tests/Unit/Highlight/HighlightTest.php
@@ -99,7 +99,7 @@ class HighlightTest extends TestCase
         $result = $highlight->toArray();
         $expectedResult = [
             'fields' => [
-                'ok' => [],
+                'ok' => new \stdClass(),
             ],
             '_source' => [
                 'include' => [
diff --git a/tests/Unit/InnerHit/NestedInnerHitTest.php b/tests/Unit/InnerHit/NestedInnerHitTest.php
index cbddec8..94dc3e2 100644
--- a/tests/Unit/InnerHit/NestedInnerHitTest.php
+++ b/tests/Unit/InnerHit/NestedInnerHitTest.php
@@ -38,7 +38,7 @@ class NestedInnerHitTest extends TestCase
             $emptyInnerHit,
             [
                 'path' => [
-                    'foo' => [],
+                    'foo' => new \stdClass(),
                 ],
             ],
         ];
diff --git a/tests/Unit/Query/Compound/BoolQueryTest.php b/tests/Unit/Query/Compound/BoolQueryTest.php
index 708f016..169196d 100644
--- a/tests/Unit/Query/Compound/BoolQueryTest.php
+++ b/tests/Unit/Query/Compound/BoolQueryTest.php
@@ -136,7 +136,7 @@ class BoolQueryTest extends TestCase
     {
         $bool = new BoolQuery();
 
-        $this->assertEquals(['bool' => []], $bool->toArray());
+        $this->assertEquals(['bool' => new \stdClass()], $bool->toArray());
     }
 
     /**
diff --git a/tests/Unit/Query/Compound/FunctionScoreQueryTest.php b/tests/Unit/Query/Compound/FunctionScoreQueryTest.php
index 8270a41..e95b196 100644
--- a/tests/Unit/Query/Compound/FunctionScoreQueryTest.php
+++ b/tests/Unit/Query/Compound/FunctionScoreQueryTest.php
@@ -36,7 +36,7 @@ class FunctionScoreQueryTest extends TestCase
                     'query' => [],
                     'functions' => [
                         [
-                            'random_score' => [],
+                            'random_score' => new \stdClass(),
                         ],
                     ],
                 ],
diff --git a/tests/Unit/Query/MatchAllQueryTest.php b/tests/Unit/Query/MatchAllQueryTest.php
index a1e040e..240f85d 100644
--- a/tests/Unit/Query/MatchAllQueryTest.php
+++ b/tests/Unit/Query/MatchAllQueryTest.php
@@ -22,7 +22,7 @@ class MatchAllQueryTest extends TestCase
     public function testToArrayWhenThereAreNoParams(): void
     {
         $query = new MatchAllQuery();
-        $this->assertEquals(['match_all' => []], $query->toArray());
+        $this->assertEquals(['match_all' => new \stdClass()], $query->toArray());
     }
 
     /**
-- 
GitLab