From bbd84c2a6770a275d2337bf3568414ce78778b18 Mon Sep 17 00:00:00 2001 From: Mantas <marc.mantas@gmail.com> Date: Mon, 7 Mar 2016 17:00:50 +0200 Subject: [PATCH] added Completion suggester and its test --- src/Suggest/CompletionSuggest.php | 78 +++++++++++++++++++++++++ tests/Suggest/CompletionSuggestTest.php | 69 ++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 src/Suggest/CompletionSuggest.php create mode 100644 tests/Suggest/CompletionSuggestTest.php diff --git a/src/Suggest/CompletionSuggest.php b/src/Suggest/CompletionSuggest.php new file mode 100644 index 0000000..74d9828 --- /dev/null +++ b/src/Suggest/CompletionSuggest.php @@ -0,0 +1,78 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchDSL\Suggest; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +class CompletionSuggest implements BuilderInterface +{ + use ParametersTrait; + + const DEFAULT_SIZE = 3; + + /** + * @var string + */ + private $name; + + /** + * @var string + */ + private $text; + + public function __construct($name, $text, $parameters = []) + { + $this->name = $name; + $this->text = $text; + $this->setParameters($parameters); + } + + /** + * Returns element type. + * + * @return string + */ + public function getType() + { + return 'completion_suggest'; + } + + /** + * Returns suggest name + * + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $this->addParameter('field', 'suggest'); + + if (!$this->hasParameter('size')) { + $this->addParameter('size', self::DEFAULT_SIZE); + } + + $output = [$this->name => [ + 'text' => $this->text, + 'completion' => $this->getParameters() + ]]; + + return $output; + } +} diff --git a/tests/Suggest/CompletionSuggestTest.php b/tests/Suggest/CompletionSuggestTest.php new file mode 100644 index 0000000..322a233 --- /dev/null +++ b/tests/Suggest/CompletionSuggestTest.php @@ -0,0 +1,69 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchDSL\Tests\Suggest; + +use ONGR\ElasticsearchDSL\Suggest\CompletionSuggest; + +class CompletionSuggestTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests getType method. + */ + public function testSuggestGetType() + { + $suggest = new CompletionSuggest('foo', 'bar'); + $result = $suggest->getType(); + $this->assertEquals('completion_suggest', $result); + } + + /** + * Tests toArray() method. + */ + public function testSuggestWithoutFieldAndSize() + { + // Case #1 suggest without field and size params. + $suggest = new CompletionSuggest('foo', 'bar'); + $expected = ['foo' => [ + 'text' => 'bar', + 'completion' => [ + 'field' => 'suggest', + 'size' => 3, + ], + ]]; + $this->assertEquals($expected, $suggest->toArray()); + } + + /** + * Tests toArray() method. + */ + public function testToArray() + { + $suggest = new CompletionSuggest( + 'foo', + 'bar', + [ + 'size' => 5, + 'field' => 'title', + 'fuzzy' => ['fuzziness' => 2] + ] + ); + $expected = ['foo' => [ + 'text' => 'bar', + 'completion' => [ + 'field' => 'suggest', + 'size' => 5, + 'fuzzy' => ['fuzziness' => 2] + ], + ]]; + $this->assertEquals($expected, $suggest->toArray()); + } +} -- GitLab