Skip to content
Snippets Groups Projects
Commit e2d99b3d authored by Simonas Šerlinskas's avatar Simonas Šerlinskas
Browse files

Merge pull request #67 from sbofirov/master

Implemented suggest support
parents 6bc53628 f9f94326
No related branches found
No related tags found
No related merge requests found
......@@ -26,6 +26,8 @@ use ONGR\ElasticsearchDSL\SearchEndpoint\SortEndpoint;
use ONGR\ElasticsearchDSL\Serializer\Normalizer\CustomReferencedNormalizer;
use ONGR\ElasticsearchDSL\Serializer\OrderedSerializer;
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
use ONGR\ElasticsearchDSL\SearchEndpoint\SuggestEndpoint;
use ONGR\ElasticsearchDSL\Suggest\Suggest;
/**
* Search object that can be executed by a manager.
......@@ -378,6 +380,30 @@ class Search
return $highlightEndpoint->getHighlight();
}
/**
* Adds suggest into search.
*
* @param Suggest $suggest
*
* @return $this
*/
public function addSuggest(Suggest $suggest)
{
$this->getEndpoint(SuggestEndpoint::NAME)->add($suggest, $suggest->getName());
return $this;
}
/**
* Returns all suggests.
*
* @return BuilderInterface[]
*/
public function getSuggests()
{
return $this->getEndpoint(SuggestEndpoint::NAME)->getAll();
}
/**
* Exclude documents which have a _score less than the minimum specified.
*
......
......@@ -26,6 +26,7 @@ class SearchEndpointFactory
'sort' => 'ONGR\ElasticsearchDSL\SearchEndpoint\SortEndpoint',
'highlight' => 'ONGR\ElasticsearchDSL\SearchEndpoint\HighlightEndpoint',
'aggregations' => 'ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint',
'suggest' => 'ONGR\ElasticsearchDSL\SearchEndpoint\SuggestEndpoint',
];
/**
......
<?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\SearchEndpoint;
use ONGR\ElasticsearchDSL\Suggest\Suggest;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Search suggest dsl endpoint.
*/
class SuggestEndpoint extends AbstractSearchEndpoint
{
/**
* Endpoint name
*/
const NAME = 'suggest';
/**
* {@inheritdoc}
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
$output = [];
if (count($this->getAll()) > 0) {
/** @var Suggest $suggest */
foreach ($this->getAll() as $suggest) {
$output[$suggest->getName()] = $suggest->toArray();
}
}
return $output;
}
}
<?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 Suggest 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 'suggest';
}
/**
* Returns suggest name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* {@inheritdoc}
*/
public function toArray()
{
if (!$this->hasParameter('field')) {
$this->addParameter('field', '_all');
}
if (!$this->hasParameter('size')) {
$this->addParameter('size', self::DEFAULT_SIZE);
}
$output = [
'text' => $this->text,
'term' => $this->getParameters(),
];
return $output;
}
}
<?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\Unit\SearchEndpoint;
use ONGR\ElasticsearchDSL\SearchEndpoint\SuggestEndpoint;
use ONGR\ElasticsearchDSL\Suggest\Suggest;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class SuggestEndpointTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests constructor.
*/
public function testItCanBeInstantiated()
{
$this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\SuggestEndpoint', new SuggestEndpoint());
}
/**
* Tests if endpoint returns builders.
*/
public function testEndpointGetter()
{
$suggestName = 'acme_suggest';
$text = 'foo';
$suggest = new Suggest($suggestName, $text);
$endpoint = new SuggestEndpoint();
$endpoint->add($suggest, $suggestName);
$builders = $endpoint->getAll();
$this->assertCount(1, $builders);
$this->assertSame($suggest, $builders[$suggestName]);
}
/**
* Tests endpoint normalization.
*/
public function testNormalize()
{
$instance = new SuggestEndpoint();
/** @var NormalizerInterface|MockObject $normalizerInterface */
$normalizerInterface = $this->getMockForAbstractClass(
'Symfony\Component\Serializer\Normalizer\NormalizerInterface'
);
$suggest = new Suggest('foo', 'bar');
$instance->add($suggest);
$this->assertEquals(
['foo' => $suggest->toArray()],
$instance->normalize($normalizerInterface)
);
}
}
......@@ -15,6 +15,7 @@ use ONGR\ElasticsearchDSL\Query\MissingQuery;
use ONGR\ElasticsearchDSL\Query\TermQuery;
use ONGR\ElasticsearchDSL\Search;
use ONGR\ElasticsearchDSL\Sort\FieldSort;
use ONGR\ElasticsearchDSL\Suggest\Suggest;
/**
* Test for Search.
......@@ -256,6 +257,36 @@ class SearchTest extends \PHPUnit_Framework_TestCase
(new Search())->addSort(new FieldSort('price', 'asc')),
];
$cases['single_suggest'] = [
[
'suggest' => [
'foo' => [
'text' => 'bar',
'term' => ['field' => 'title', 'size' => 2],
],
],
],
(new Search())->addSuggest(new Suggest('foo', 'bar', ['field' => 'title', 'size' => 2])),
];
$cases['multiple_suggests'] = [
[
'suggest' => [
'foo' => [
'text' => 'bar',
'term' => ['field' => 'title', 'size' => 2],
],
'bar' => [
'text' => 'foo',
'term' => ['field' => 'title', 'size' => 2],
],
],
],
(new Search())
->addSuggest(new Suggest('foo', 'bar', ['field' => 'title', 'size' => 2]))
->addSuggest(new Suggest('bar', 'foo', ['field' => 'title', 'size' => 2])),
];
return $cases;
}
......
<?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\Suggest;
class SuggestTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests getType method.
*/
public function testSuggestGetType()
{
$suggest = new Suggest('foo', 'bar');
$result = $suggest->getType();
$this->assertEquals('suggest', $result);
}
/**
* Tests toArray() method.
*/
public function testSuggestWithoutFieldAndSize()
{
// Case #1 suggest without field and size params.
$suggest = new Suggest('foo', 'bar');
$expected = [
'text' => 'bar',
'term' => [
'field' => '_all',
'size' => 3,
],
];
$this->assertEquals($expected, $suggest->toArray());
}
/**
* Tests toArray() method.
*/
public function testToArray()
{
$suggest = new Suggest(
'foo',
'bar',
[
'size' => 5,
'field' => 'title',
'analyzer' => 'whitespace',
]
);
$expected = [
'text' => 'bar',
'term' => [
'field' => 'title',
'size' => 5,
'analyzer' => 'whitespace',
],
];
$this->assertEquals($expected, $suggest->toArray());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment