From 6f3cba8ba5b4f129a056b2fd45d559e46af5fbe7 Mon Sep 17 00:00:00 2001
From: Aivaras Gotovskis <aivaras.gotovskis@ongr.io>
Date: Fri, 10 Jul 2015 14:05:01 +0300
Subject: [PATCH] Fix top hits aggregation to respect set parameters.

---
 src/Aggregation/TopHitsAggregation.php       |  2 +-
 tests/Aggregation/TopHitsAggregationTest.php | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/src/Aggregation/TopHitsAggregation.php b/src/Aggregation/TopHitsAggregation.php
index 03f525d..0212be3 100644
--- a/src/Aggregation/TopHitsAggregation.php
+++ b/src/Aggregation/TopHitsAggregation.php
@@ -153,6 +153,6 @@ class TopHitsAggregation extends AbstractAggregation
             }
         );
 
-        return $fd;
+        return $this->processArray($fd);
     }
 }
diff --git a/tests/Aggregation/TopHitsAggregationTest.php b/tests/Aggregation/TopHitsAggregationTest.php
index a1975eb..1f01013 100644
--- a/tests/Aggregation/TopHitsAggregationTest.php
+++ b/tests/Aggregation/TopHitsAggregationTest.php
@@ -39,4 +39,23 @@ class TopHitsAggregationTest extends \PHPUnit_Framework_TestCase
 
         $this->assertEquals($expected, $aggregation->toArray());
     }
+
+    /**
+     * Tests if adding parameters has any effect.
+     */
+    public function testParameters()
+    {
+        $topHitsAggregation = new TopHitsAggregation('test');
+        $topHitsAggregation->addParameter('_source', ['include' => ['title']]);
+        $expectedAgg = new \stdClass();
+        $expectedAgg->sort = [];
+        $expectedAgg->_source = ['include' => ['title']];
+        $expected = [
+            'agg_test' => [
+                'top_hits' => $expectedAgg,
+            ],
+        ];
+
+        $this->assertEquals($expected, $topHitsAggregation->toArray());
+    }
 }
-- 
GitLab