From ddacb5db81d8647e6086e693f470d92514f9c366 Mon Sep 17 00:00:00 2001
From: Mantas Varatiejus <mantas.varatiejus@nfq.com>
Date: Mon, 1 Feb 2016 11:26:58 +0200
Subject: [PATCH] Revert old sort format

---
 src/Aggregation/TopHitsAggregation.php | 13 +++++--------
 src/Sort/FieldSort.php                 |  2 +-
 tests/SearchTest.php                   | 14 ++++++++++++++
 3 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/src/Aggregation/TopHitsAggregation.php b/src/Aggregation/TopHitsAggregation.php
index d315bcd..e88e996 100644
--- a/src/Aggregation/TopHitsAggregation.php
+++ b/src/Aggregation/TopHitsAggregation.php
@@ -125,15 +125,12 @@ class TopHitsAggregation extends AbstractAggregation
      */
     public function getArray()
     {
-        $output = $this->getSort() ? $this->getSort()->toArray() : [];
         $output = array_filter(
-            array_merge(
-                $output,
-                [
-                    'size' => $this->getSize(),
-                    'from' => $this->getFrom(),
-                ]
-            ),
+            [
+                'sort' => $this->getSort() ? $this->getSort()->toArray() : null,
+                'size' => $this->getSize(),
+                'from' => $this->getFrom(),
+            ],
             function ($val) {
                 return (($val || is_array($val) || ($val || is_numeric($val))));
             }
diff --git a/src/Sort/FieldSort.php b/src/Sort/FieldSort.php
index 2f3eeeb..7389976 100644
--- a/src/Sort/FieldSort.php
+++ b/src/Sort/FieldSort.php
@@ -103,6 +103,6 @@ class FieldSort implements BuilderInterface
             $this->field => empty($fieldValues) ? new \stdClass() : $fieldValues,
         ];
 
-        return [$this->getType() => $output];
+        return $output;
     }
 }
diff --git a/tests/SearchTest.php b/tests/SearchTest.php
index 23ed4c2..17bebd5 100644
--- a/tests/SearchTest.php
+++ b/tests/SearchTest.php
@@ -14,6 +14,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL;
 use ONGR\ElasticsearchDSL\Query\MissingQuery;
 use ONGR\ElasticsearchDSL\Query\TermQuery;
 use ONGR\ElasticsearchDSL\Search;
+use ONGR\ElasticsearchDSL\Sort\FieldSort;
 
 /**
  * Test for Search.
@@ -242,6 +243,19 @@ class SearchTest extends \PHPUnit_Framework_TestCase
             (new Search())->addQuery(new TermQuery('foo', 'bar'))->addFilter(new MissingQuery('baz')),
         ];
 
+        $cases['sort_by_price'] = [
+            [
+                'sort' => [
+                    [
+                        'price' => [
+                            'order' => 'asc',
+                        ],
+                    ],
+                ],
+            ],
+            (new Search())->addSort(new FieldSort('price', 'asc')),
+        ];
+
         return $cases;
     }
 
-- 
GitLab