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

all endpoints now using same behaviour

parent b3250e15
No related branches found
No related tags found
No related merge requests found
......@@ -11,6 +11,8 @@
namespace ONGR\ElasticsearchDSL\SearchEndpoint;
use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;
use ONGR\ElasticsearchDSL\Serializer\Normalizer\AbstractNormalizable;
/**
......@@ -18,5 +20,79 @@ use ONGR\ElasticsearchDSL\Serializer\Normalizer\AbstractNormalizable;
*/
abstract class AbstractSearchEndpoint extends AbstractNormalizable implements SearchEndpointInterface
{
use BuilderContainerAwareTrait;
use ParametersTrait;
/**
* @var BuilderInterface[]
*/
private $container = [];
/**
* {@inheritdoc}
*/
public function add(BuilderInterface $builder, $key = null)
{
if (array_key_exists($key, $this->container)) {
throw new \OverflowException(sprintf('Builder with %s name for endpoint has already been added!', $key));
}
if (!$key) {
$key = uniqid();
}
$this->container[$key] = $builder;
return $key;
}
/**
* {@inheritdoc}
*/
public function addToBool(BuilderInterface $builder, $boolType = null, $key = null)
{
throw new \BadFunctionCallException(sprintf("Endpoint %s doesn't support bool statements", static::NAME));
}
/**
* {@inheritdoc}
*/
public function remove($key)
{
if ($this->has($key)) {
unset($this->container[$key]);
}
return $this;
}
/**
* Checks if builder with specific key exists.
*
* @param $key
* @return bool
*/
public function has($key)
{
return array_key_exists($key, $this->container);
}
/**
* {@inheritdoc}
*/
public function get($key)
{
if ($this->has($key)) {
return $this->container[$key];
}
return null;
}
/**
* {@inheritdoc}
*/
public function getAll($boolType = null)
{
return $this->container;
}
}
......@@ -11,84 +11,31 @@
namespace ONGR\ElasticsearchDSL\SearchEndpoint;
use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\NamedBuilderBag;
use ONGR\ElasticsearchDSL\NamedBuilderInterface;
use ONGR\ElasticsearchDSL\BuilderBag;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Search aggregations dsl endpoint.
*/
class AggregationsEndpoint implements SearchEndpointInterface
class AggregationsEndpoint extends AbstractSearchEndpoint
{
use BuilderContainerAwareTrait;
/**
* @var NamedBuilderBag
* Endpoint name
*/
private $bag;
/**
* Initialized aggregations bag.
*/
public function __construct()
{
$this->bag = new NamedBuilderBag();
}
CONST NAME = 'aggregations';
/**
* {@inheritdoc}
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
if (count($this->bag->all()) > 0) {
return $this->bag->toArray();
}
return null;
}
/**
* {@inheritdoc}
*/
public function addBuilder(BuilderInterface $builder, $parameters = [])
{
if (!($builder instanceof NamedBuilderInterface)) {
throw new \InvalidArgumentException('Builder must be named builder');
}
$this->bag->add($builder);
return $builder->getName();
}
/**
* {@inheritdoc}
*/
public function getBuilders()
{
return $this->bag->all();
}
/**
* {@inheritdoc}
*/
public function getBuilder($key)
{
if (!$this->bag->has($key)) {
return null;
if (count($this->getAll()) > 0) {
$output = [];
foreach ($this->getAll() as $aggregation) {
$output[] = $aggregation->toArray();
}
}
return $this->bag->get($key);
}
/**
* {@inheritdoc}
*/
public function removeBuilder($key)
{
$this->bag->remove($key);
return $this;
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\SearchEndpoint;
use ONGR\ElasticsearchDSL\BuilderInterface;
/**
* Trait to implement SearchEndpointInterface builder methods.
*/
trait BuilderContainerAwareTrait
{
/**
* @var BuilderInterface[]
*/
private $builderContainer = [];
/**
* @var array
*/
private $parameterContainer = [];
/**
* {@inheritdoc}
*/
public function addBuilder(BuilderInterface $builder, $parameters = [])
{
$this->builderContainer[] = $builder;
end($this->builderContainer);
$key = key($this->builderContainer);
$this->parameterContainer[$key] = $parameters;
return $key;
}
/**
* {@inheritdoc}
*/
public function removeBuilder($key)
{
if (array_key_exists($key, $this->builderContainer)) {
unset($this->builderContainer[$key]);
unset($this->parameterContainer[$key]);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function getBuilder($key)
{
if (array_key_exists($key, $this->builderContainer)) {
return $this->builderContainer[$key];
}
return null;
}
/**
* {@inheritdoc}
*/
public function getBuilders()
{
return $this->builderContainer;
}
/**
* {@inheritdoc}
*/
public function getBuilderParameters($key)
{
if (array_key_exists($key, $this->parameterContainer)) {
return $this->parameterContainer[$key];
}
return [];
}
/**
* {@inheritdoc}
*/
public function setBuilderParameters($key, $parameters)
{
$this->parameterContainer[$key] = $parameters;
return $this;
}
}
......@@ -20,18 +20,29 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
*/
class FilterEndpoint extends QueryEndpoint
{
/**
* Endpoint name
*/
CONST NAME = 'filter';
/**
* {@inheritdoc}
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
$builder = $this->getBuilderForNormalization();
if (empty($builder)) {
return;
if (!$this->getBool()) {
return null;
}
$query = new FilteredQuery();
$query->setFilter($builder);
if ($this->getBool()->isRelevant()) {
$filters = $this->getBool()->getQueries(BoolFilter::MUST);
$filter = array_shift($filters);
} else {
$filter = $this->getBool();
}
$query->setFilter($filter);
$this->addReference('filtered_query', $query);
}
......@@ -44,7 +55,9 @@ class FilterEndpoint extends QueryEndpoint
}
/**
* {@inheritdoc}
* Returns bool instance for this endpoint case.
*
* @return BoolFilter
*/
protected function getBoolInstance()
{
......
......@@ -17,14 +17,15 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Search highlight dsl endpoint.
*/
class HighlightEndpoint implements SearchEndpointInterface
class HighlightEndpoint extends AbstractSearchEndpoint
{
use BuilderContainerAwareTrait {
addBuilder as private traitAddBuilder;
}
/**
* Endpoint name
*/
CONST NAME = 'highlight';
/**
* @var int
* @var BuilderInterface
*/
private $highlight;
......@@ -33,24 +34,22 @@ class HighlightEndpoint implements SearchEndpointInterface
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
if (!$this->getBuilder($this->highlight)) {
return null;
if ($this->highlight) {
return $this->highlight->toArray();
}
return $this->getBuilder($this->highlight)->toArray();
return null;
}
/**
* {@inheritdoc}
*/
public function addBuilder(BuilderInterface $builder, $parameters = [])
public function add(BuilderInterface $builder, $key = null)
{
if ($this->getBuilders()) {
throw new \OverflowException('Only one highlight is expected');
if ($this->highlight) {
throw new \OverflowException('Only one highlight can be set');
}
$this->highlight = $this->traitAddBuilder($builder, $parameters);
return $this->highlight;
$this->highlight = $builder;
}
}
......@@ -11,7 +11,7 @@
namespace ONGR\ElasticsearchDSL\SearchEndpoint;
use ONGR\ElasticsearchDSL\Filter\PostFilter;
use ONGR\ElasticsearchDSL\Filter\BoolFilter;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
......@@ -19,20 +19,35 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
*/
class PostFilterEndpoint extends FilterEndpoint
{
/**
* Endpoint name
*/
CONST NAME = 'post_filter';
/**
* {@inheritdoc}
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
$builder = $this->getBuilderForNormalization();
if (empty($builder)) {
if (!$this->getBool()) {
return null;
}
$postFilter = new PostFilter();
$postFilter->setFilter($builder);
if ($this->getBool()->isRelevant()) {
$filters = $this->getBool()->getQueries(BoolFilter::MUST);
$filter = array_shift($filters);
} else {
$filter = $this->getBool();
}
return $postFilter->toArray();
return [$filter->getType() => $filter->toArray()];
}
/**
* {@inheritdoc}
*/
public function getOrder()
{
return 2;
}
}
......@@ -25,21 +25,15 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
*/
class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerInterface
{
use ParametersTrait;
/**
* @var OptionsResolver
* Endpoint name
*/
private $resolver;
CONST NAME = 'query';
/**
* Instantiates resolver.
* @var BoolQuery
*/
public function __construct()
{
$this->resolver = new OptionsResolver();
$this->configureResolver($this->resolver);
}
private $bool;
/**
* {@inheritdoc}
......@@ -47,90 +41,67 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
if ($this->hasReference('filtered_query')) {
/** @var FilteredQuery $query */
$query = $this->getReference('filtered_query');
$this->addBuilder($query);
/** @var FilteredQuery $filteredQuery */
$filteredQuery = $this->getReference('filtered_query');
$this->add($filteredQuery);
}
$builder = $this->getBuilderForNormalization();
if (empty($builder)) {
if (!$this->bool) {
return null;
}
return [$builder->getType() => $builder->toArray()];
$queryArray = $this->bool->toArray();
if (!$this->bool->isRelevant()) {
$queryArray = [$this->bool->getType() => $queryArray];
}
return $queryArray;
}
/**
* Return builder that is ready to be normalized.
*
* @return BuilderInterface|null
* {@inheritdoc}
*/
protected function getBuilderForNormalization()
public function add(BuilderInterface $builder, $key = null)
{
$builders = $this->getbuilders();
if (empty($builders)) {
return null;
}
if (count($builders) > 1) {
$builder = $this->buildBool();
} else {
$builder = end($builders);
}
if (method_exists($builder, 'setParameters') && count($this->getParameters()) > 0) {
$builder->setParameters($this->processArray($builder->getParameters()));
}
return $builder;
return $this->addToBool($builder, BoolQuery::MUST, $key);
}
/**
* {@inheritdoc}
*/
public function getOrder()
public function addToBool(BuilderInterface $builder, $boolType = null, $key = null)
{
return 2;
if (!$this->bool) {
$this->bool = $this->createBoolInstance();
}
$this->bool->add($builder, $boolType, $key);
}
/**
* Default properties for query.
*
* @param OptionsResolver $resolver
* {@inheritdoc}
*/
protected function configureResolver(OptionsResolver $resolver)
public function getOrder()
{
$resolver
->setDefaults(
['bool_type' => BoolQuery::MUST]
);
return 3;
}
/**
* Returns bool instance for this endpoint case.
*
* @return BoolFilter|BoolQuery
* @return BoolQuery
*/
protected function getBoolInstance()
public function getBool()
{
return new BoolQuery();
return $this->bool;
}
/**
* Returns bool instance with builders set.
* Returns new bool instance for the endpoint.
*
* @return BoolFilter|BoolQuery
* @return BoolQuery
*/
protected function buildBool()
protected function createBoolInstance()
{
$boolInstance = $this->getBoolInstance();
foreach ($this->getBuilders() as $key => $builder) {
$parameters = $this->resolver->resolve(array_filter($this->getBuilderParameters($key)));
$boolInstance->add($builder, $parameters['bool_type']);
}
return $boolInstance;
return new BoolQuery();
}
}
......@@ -12,6 +12,7 @@
namespace ONGR\ElasticsearchDSL\SearchEndpoint;
use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\Query\BoolQuery;
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
/**
......@@ -23,11 +24,23 @@ interface SearchEndpointInterface extends NormalizableInterface
* Adds builder to search endpoint.
*
* @param BuilderInterface $builder Builder to add.
* @param array $parameters Additional parameters relevant to builder.
* @param array $key Additional parameters relevant to builder.
*
* @return int Returns builder key.
* @return string Key of added builder.
*/
public function addBuilder(BuilderInterface $builder, $parameters = []);
public function add(BuilderInterface $builder, $key = null);
/**
* Adds builder to search endpoint's specific bool type container.
*
* @param BuilderInterface $builder Builder to add.
* @param array $boolType Bool type for query or filter. If bool type is left null
* it will be treated as MUST.
* @param array $key Additional parameters relevant to builder.
*
* @return string Key of added builder.
*/
public function addToBool(BuilderInterface $builder, $boolType = null, $key = null);
/**
* Removes contained builder.
......@@ -36,7 +49,7 @@ interface SearchEndpointInterface extends NormalizableInterface
*
* @return $this
*/
public function removeBuilder($key);
public function remove($key);
/**
* Returns contained builder or null if Builder is not found.
......@@ -45,29 +58,14 @@ interface SearchEndpointInterface extends NormalizableInterface
*
* @return BuilderInterface|null
*/
public function getBuilder($key);
public function get($key);
/**
* Returns all contained builders.
*
* @return BuilderInterface[]
*/
public function getBuilders();
/**
* Returns parameters for contained builder or empty array if parameters are not found.
* Returns contained builder or null if Builder is not found.
*
* @param int $key
* @param string|null $boolType If bool type is left null it will return all builders from container.
*
* @return array
*/
public function getBuilderParameters($key);
/**
* @param int $key
* @param array $parameters
*
* @return $this
*/
public function setBuilderParameters($key, $parameters);
public function getAll($boolType = null);
}
......@@ -19,51 +19,24 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Search sort dsl endpoint.
*/
class SortEndpoint implements SearchEndpointInterface
class SortEndpoint extends AbstractSearchEndpoint
{
use BuilderContainerAwareTrait {
addBuilder as private traitAddBuilder;
}
/**
* {@inheritdoc}
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
$sorts = $this->buildSorts();
if ($sorts->isRelevant()) {
return $sorts->toArray();
}
return null;
}
/**
* Builds sorts object.
*
* @return Sorts
* Endpoint name
*/
protected function buildSorts()
{
$sorts = new Sorts();
/** @var AbstractSort $builder */
foreach ($this->getBuilders() as $builder) {
$sorts->addSort($builder);
}
return $sorts;
}
CONST NAME = 'sort';
/**
* {@inheritdoc}
*/
public function addBuilder(BuilderInterface $builder, $parameters = [])
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
if (!($builder instanceof AbstractSort)) {
throw new \InvalidArgumentException('Sort must must a subtype of AbstractSort');
$output = [];
foreach($this->getAll() as $sort) {
$output[] = $sort->toArray();
}
return $this->traitAddBuilder($builder, $parameters);
return $output;
}
}
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