diff --git a/src/Search.php b/src/Search.php index 0a524d91..08b69669 100644 --- a/src/Search.php +++ b/src/Search.php @@ -383,11 +383,11 @@ public function getHighlight() /** * Adds suggest into search. * - * @param Suggest $suggest + * @param BuilderInterface $suggest * * @return $this */ - public function addSuggest(Suggest $suggest) + public function addSuggest(BuilderInterface $suggest) { $this->getEndpoint(SuggestEndpoint::NAME)->add($suggest, $suggest->getName()); diff --git a/src/SearchEndpoint/SuggestEndpoint.php b/src/SearchEndpoint/SuggestEndpoint.php index 323bd228..2e2e16ac 100644 --- a/src/SearchEndpoint/SuggestEndpoint.php +++ b/src/SearchEndpoint/SuggestEndpoint.php @@ -11,7 +11,7 @@ namespace ONGR\ElasticsearchDSL\SearchEndpoint; -use ONGR\ElasticsearchDSL\Suggest\Suggest; +use ONGR\ElasticsearchDSL\Suggest\TermSuggest; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; /** @@ -31,9 +31,9 @@ public function normalize(NormalizerInterface $normalizer, $format = null, array { $output = []; if (count($this->getAll()) > 0) { - /** @var Suggest $suggest */ + /** @var TermSuggest $suggest */ foreach ($this->getAll() as $suggest) { - $output[$suggest->getName()] = $suggest->toArray(); + $output = array_merge($output, $suggest->toArray()); } } diff --git a/src/Suggest/Suggest.php b/src/Suggest/TermSuggest.php similarity index 91% rename from src/Suggest/Suggest.php rename to src/Suggest/TermSuggest.php index fe70fb94..d2afa0c2 100644 --- a/src/Suggest/Suggest.php +++ b/src/Suggest/TermSuggest.php @@ -14,7 +14,7 @@ use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\ParametersTrait; -class Suggest implements BuilderInterface +class TermSuggest implements BuilderInterface { use ParametersTrait; @@ -44,7 +44,7 @@ public function __construct($name, $text, $parameters = []) */ public function getType() { - return 'suggest'; + return 'term_suggest'; } /** @@ -70,10 +70,10 @@ public function toArray() $this->addParameter('size', self::DEFAULT_SIZE); } - $output = [ + $output = [$this->name => [ 'text' => $this->text, 'term' => $this->getParameters(), - ]; + ]]; return $output; } diff --git a/tests/SearchEndpoint/SuggestEndpointTest.php b/tests/SearchEndpoint/SuggestEndpointTest.php index c1760b80..7702a56c 100644 --- a/tests/SearchEndpoint/SuggestEndpointTest.php +++ b/tests/SearchEndpoint/SuggestEndpointTest.php @@ -12,7 +12,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; use ONGR\ElasticsearchDSL\SearchEndpoint\SuggestEndpoint; -use ONGR\ElasticsearchDSL\Suggest\Suggest; +use ONGR\ElasticsearchDSL\Suggest\TermSuggest; use PHPUnit_Framework_MockObject_MockObject as MockObject; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; @@ -33,7 +33,7 @@ public function testEndpointGetter() { $suggestName = 'acme_suggest'; $text = 'foo'; - $suggest = new Suggest($suggestName, $text); + $suggest = new TermSuggest($suggestName, $text); $endpoint = new SuggestEndpoint(); $endpoint->add($suggest, $suggestName); $builders = $endpoint->getAll(); @@ -54,11 +54,11 @@ public function testNormalize() 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' ); - $suggest = new Suggest('foo', 'bar'); + $suggest = new TermSuggest('foo', 'bar'); $instance->add($suggest); $this->assertEquals( - ['foo' => $suggest->toArray()], + $suggest->toArray(), $instance->normalize($normalizerInterface) ); } diff --git a/tests/SearchTest.php b/tests/SearchTest.php index fe438aa4..77d7e408 100644 --- a/tests/SearchTest.php +++ b/tests/SearchTest.php @@ -15,7 +15,7 @@ use ONGR\ElasticsearchDSL\Query\TermQuery; use ONGR\ElasticsearchDSL\Search; use ONGR\ElasticsearchDSL\Sort\FieldSort; -use ONGR\ElasticsearchDSL\Suggest\Suggest; +use ONGR\ElasticsearchDSL\Suggest\TermSuggest; /** * Test for Search. @@ -266,7 +266,7 @@ public function getTestToArrayData() ], ], ], - (new Search())->addSuggest(new Suggest('foo', 'bar', ['field' => 'title', 'size' => 2])), + (new Search())->addSuggest(new TermSuggest('foo', 'bar', ['field' => 'title', 'size' => 2])), ]; $cases['multiple_suggests'] = [ @@ -283,8 +283,8 @@ public function getTestToArrayData() ], ], (new Search()) - ->addSuggest(new Suggest('foo', 'bar', ['field' => 'title', 'size' => 2])) - ->addSuggest(new Suggest('bar', 'foo', ['field' => 'title', 'size' => 2])), + ->addSuggest(new TermSuggest('foo', 'bar', ['field' => 'title', 'size' => 2])) + ->addSuggest(new TermSuggest('bar', 'foo', ['field' => 'title', 'size' => 2])), ]; return $cases; diff --git a/tests/Suggest/SuggestTest.php b/tests/Suggest/TermSuggestTest.php similarity index 80% rename from tests/Suggest/SuggestTest.php rename to tests/Suggest/TermSuggestTest.php index a680f476..d20c0bdc 100644 --- a/tests/Suggest/SuggestTest.php +++ b/tests/Suggest/TermSuggestTest.php @@ -11,7 +11,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Suggest; -use ONGR\ElasticsearchDSL\Suggest\Suggest; +use ONGR\ElasticsearchDSL\Suggest\TermSuggest; class SuggestTest extends \PHPUnit_Framework_TestCase { @@ -20,9 +20,9 @@ class SuggestTest extends \PHPUnit_Framework_TestCase */ public function testSuggestGetType() { - $suggest = new Suggest('foo', 'bar'); + $suggest = new TermSuggest('foo', 'bar'); $result = $suggest->getType(); - $this->assertEquals('suggest', $result); + $this->assertEquals('term_suggest', $result); } /** @@ -31,14 +31,14 @@ public function testSuggestGetType() public function testSuggestWithoutFieldAndSize() { // Case #1 suggest without field and size params. - $suggest = new Suggest('foo', 'bar'); - $expected = [ + $suggest = new TermSuggest('foo', 'bar'); + $expected = ['foo' => [ 'text' => 'bar', 'term' => [ 'field' => '_all', 'size' => 3, ], - ]; + ]]; $this->assertEquals($expected, $suggest->toArray()); } @@ -47,7 +47,7 @@ public function testSuggestWithoutFieldAndSize() */ public function testToArray() { - $suggest = new Suggest( + $suggest = new TermSuggest( 'foo', 'bar', [ @@ -56,14 +56,14 @@ public function testToArray() 'analyzer' => 'whitespace', ] ); - $expected = [ + $expected = ['foo' => [ 'text' => 'bar', 'term' => [ 'field' => 'title', 'size' => 5, 'analyzer' => 'whitespace', ], - ]; + ]]; $this->assertEquals($expected, $suggest->toArray()); } }