Skip to content
Snippets Groups Projects
Commit 705508af authored by Martynas Sudintas's avatar Martynas Sudintas
Browse files

Implemented search endpoints

parent 4d22b993
No related branches found
No related tags found
No related merge requests found
<?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\ElasticsearchBundle\DSL\SearchEndpoint;
use ONGR\ElasticsearchBundle\Serializer\Normalizer\AbstractNormalizable;
/**
* Abstract class used to define search endpoint with references.
*/
abstract class AbstractSearchEndpoint extends AbstractNormalizable implements SearchEndpointInterface
{
}
<?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\ElasticsearchBundle\DSL\SearchEndpoint;
use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
use ONGR\ElasticsearchBundle\DSL\NamedBuilderBag;
use ONGR\ElasticsearchBundle\DSL\NamedBuilderInterface;
use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Search aggregations dsl endpoint.
*/
class AggregationsEndpoint implements SearchEndpointInterface
{
/**
* @var NamedBuilderBag
*/
private $bag;
/**
* Initialized aggregations bag.
*/
public function __construct()
{
$this->bag = new NamedBuilderBag();
}
/**
* {@inheritdoc}
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
if (!empty($this->bag->all())) {
return $this->bag->toArray();
}
}
/**
* {@inheritdoc}
*/
public function addBuilder(BuilderInterface $builder, $parameters = [])
{
if ($builder instanceof NamedBuilderInterface) {
$this->bag->add($builder);
}
}
/**
* {@inheritdoc}
*/
public function getBuilder()
{
return $this->bag->all();
}
}
<?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\ElasticsearchBundle\DSL\SearchEndpoint;
use ONGR\ElasticsearchBundle\DSL\Query\FilteredQuery;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Search filter dsl endpoint.
*/
class FilterEndpoint extends QueryEndpoint
{
/**
* {@inheritdoc}
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
if ($this->getBuilder()) {
$query = new FilteredQuery();
$query->setBoolParameters($this->getParameters());
$query->setFilter($this->getBuilder());
$this->addReference('filtered_query', $query);
}
}
/**
* {@inheritdoc}
*/
public function getOrder()
{
return 1;
}
}
<?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\ElasticsearchBundle\DSL\SearchEndpoint;
use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Search highlight dsl endpoint.
*/
class HighlightEndpoint implements SearchEndpointInterface
{
/**
* @var BuilderInterface
*/
private $highlight;
/**
* {@inheritdoc}
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
if ($this->getBuilder()) {
return $this->getBuilder()->toArray();
}
}
/**
* {@inheritdoc}
*/
public function addBuilder(BuilderInterface $builder, $parameters = [])
{
$this->highlight = $builder;
}
/**
* {@inheritdoc}
*/
public function getBuilder()
{
return $this->highlight;
}
}
<?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\ElasticsearchBundle\DSL\SearchEndpoint;
use ONGR\ElasticsearchBundle\DSL\Filter\PostFilter;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Search post filter dsl endpoint.
*/
class PostFilterEndpoint extends QueryEndpoint
{
/**
* {@inheritdoc}
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
if ($this->getBuilder()) {
$postFilter = new PostFilter();
$postFilter->setBoolParameters($this->getParameters());
$postFilter->setFilter($this->getBuilder());
return $postFilter->toArray();
}
return null;
}
}
<?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\ElasticsearchBundle\DSL\SearchEndpoint;
use ONGR\ElasticsearchBundle\DSL\Bool\Bool;
use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
use ONGR\ElasticsearchBundle\DSL\Query\FilteredQuery;
use ONGR\ElasticsearchBundle\Serializer\Normalizer\OrderedNormalizerInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Search query dsl endpoint.
*/
class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerInterface
{
use ParametersTrait;
/**
* @var BuilderInterface|Bool
*/
private $query;
/**
* @var OptionsResolver
*/
private $resolver;
/**
* Instantiates resolver.
*/
public function __construct()
{
$this->resolver = new OptionsResolver();
$this->configureResolver($this->resolver);
}
/**
* {@inheritdoc}
*/
public function addBuilder(BuilderInterface $builder, $parameters = [])
{
if (!$this->query && !(array_key_exists('bool_type', $parameters) && !empty($parameters['bool_type']))) {
$this->setBuilder($builder);
} else {
$parameters = $this->resolver->resolve(array_filter($parameters));
$this->query instanceof Bool ? : $this->convertToBool();
$this->query->addToBool($builder, $parameters['bool_type']);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
$isRelevant = false;
if ($this->hasReference('filtered_query')) {
/** @var FilteredQuery $query */
$query = $this->getReference('filtered_query');
$this->addBuilder($query);
$isRelevant = true;
}
if ($this->getBuilder()) {
if (method_exists($this->getBuilder(), 'setParameters') && count($this->getParameters()) > 0) {
$this
->getBuilder()
->setParameters($this->processArray($this->getBuilder()->getParameters()));
}
$isRelevant = true;
}
return $isRelevant ? [$this->getBuilder()->getType() => $this->getBuilder()->toArray()] : null;
}
/**
* {@inheritdoc}
*/
public function getOrder()
{
return 2;
}
/**
* {@inheritdoc}
*/
public function getBuilder()
{
return $this->query;
}
/**
* Sets builder.
*
* @param BuilderInterface $builder
*/
protected function setBuilder(BuilderInterface $builder)
{
$this->query = $builder;
}
/**
* Default properties for query.
*
* @param OptionsResolver $resolver
*/
protected function configureResolver(OptionsResolver $resolver)
{
$resolver
->setDefaults(
['bool_type' => Bool::MUST]
);
}
/**
* Converts query to bool.
*/
private function convertToBool()
{
$bool = new Bool();
if ($this->query !== null) {
$bool->addToBool($this->query);
}
$this->query = $bool;
}
}
<?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\ElasticsearchBundle\DSL\SearchEndpoint;
/**
* Factory for search endpoints.
*/
class SearchEndpointFactory
{
/**
* @var array Holds namespaces for endpoints.
*/
private static $endpoints = [
'query' => 'ONGR\ElasticsearchBundle\DSL\SearchEndpoint\QueryEndpoint',
'filter' => 'ONGR\ElasticsearchBundle\DSL\SearchEndpoint\FilterEndpoint',
'post_filter' => 'ONGR\ElasticsearchBundle\DSL\SearchEndpoint\PostFilterEndpoint',
'sort' => 'ONGR\ElasticsearchBundle\DSL\SearchEndpoint\SortEndpoint',
'highlight' => 'ONGR\ElasticsearchBundle\DSL\SearchEndpoint\HighlightEndpoint',
'aggregations' => 'ONGR\ElasticsearchBundle\DSL\SearchEndpoint\AggregationsEndpoint',
'suggest' => 'ONGR\ElasticsearchBundle\DSL\SearchEndpoint\SuggestEndpoint',
];
/**
* Returns a search endpoint instance.
*
* @param string $type Type of endpoint.
*
* @return SearchEndpointInterface
*
* @throws \RuntimeException Endpoint does not exist.
* @throws \DomainException Endpoint is not implementing SearchEndpointInterface
*/
public static function get($type)
{
if (!array_key_exists($type, self::$endpoints)) {
throw new \RuntimeException();
}
$endpoint = new self::$endpoints[$type]();
if ($endpoint instanceof SearchEndpointInterface) {
return $endpoint;
}
throw new \DomainException();
}
}
<?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\ElasticsearchBundle\DSL\SearchEndpoint;
use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
/**
* Interface used to define search endpoint.
*/
interface SearchEndpointInterface extends NormalizableInterface
{
/**
* Adds builder to search endpoint.
*
* @param BuilderInterface $builder Builder to add.
* @param array $parameters Additional parameters relevant to builder.
*
* @return SearchEndpointInterface
*/
public function addBuilder(BuilderInterface $builder, $parameters = []);
/**
* Returns currectly contained builder.
*
* @return BuilderInterface|BuilderInterface[]
*/
public function getBuilder();
}
<?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\ElasticsearchBundle\DSL\SearchEndpoint;
use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
use ONGR\ElasticsearchBundle\DSL\Sort\Sorts;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Search sort dsl endpoint.
*/
class SortEndpoint implements SearchEndpointInterface
{
/**
* @var Sorts
*/
protected $sorts;
/**
* Initializes Sorts object.
*/
public function __construct()
{
$this->sorts = new Sorts();
}
/**
* {@inheritdoc}
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
if ($this->sorts->isRelevant()) {
return $this->sorts->toArray();
}
}
/**
* {@inheritdoc}
*/
public function addBuilder(BuilderInterface $builder, $parameters = [])
{
$this->sorts->addSort($builder);
}
/**
* {@inheritdoc}
*/
public function getBuilder()
{
return $this->sorts;
}
}
<?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\ElasticsearchBundle\DSL\SearchEndpoint;
use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Search suggesters dsl endpoint.
*/
class SuggestEndpoint extends AggregationsEndpoint
{
}
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