Skip to content
Snippets Groups Projects
Search.php 15.7 KiB
Newer Older
ONGR Team's avatar
ONGR Team committed
<?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;
ONGR Team's avatar
ONGR Team committed

use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
use ONGR\ElasticsearchDSL\Filter\BoolFilter;
use ONGR\ElasticsearchDSL\Highlight\Highlight;
use ONGR\ElasticsearchDSL\Query\BoolQuery;
use ONGR\ElasticsearchDSL\SearchEndpoint\AbstractSearchEndpoint;
use ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint;
use ONGR\ElasticsearchDSL\SearchEndpoint\FilterEndpoint;
use ONGR\ElasticsearchDSL\SearchEndpoint\HighlightEndpoint;
use ONGR\ElasticsearchDSL\SearchEndpoint\PostFilterEndpoint;
use ONGR\ElasticsearchDSL\SearchEndpoint\QueryEndpoint;
use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointFactory;
use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointInterface;
use ONGR\ElasticsearchDSL\SearchEndpoint\SortEndpoint;
use ONGR\ElasticsearchDSL\Serializer\Normalizer\CustomReferencedNormalizer;
use ONGR\ElasticsearchDSL\Serializer\OrderedSerializer;
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
ONGR Team's avatar
ONGR Team committed
/**
 * Search object that can be executed by a manager.
 */
class Search
{
    /**
     * @var int
     */
    private $size;

    /**
     * @var int
     */
    private $from;

    /**
     * @var string
     */
    private $timeout;

    /**
     * @var int
     */
    private $terminateAfter;

ONGR Team's avatar
ONGR Team committed
    /**
     * @var string|null
     */
    private $scroll;
ONGR Team's avatar
ONGR Team committed

    /**
     * @var array|bool|string
     */
    private $source;

    /**
     * @var array
     */
    private $fields;

    /**
     * @var array
     */
    private $scriptFields;

    /**
     * @var string
     */
    private $searchType;

    /**
     * @var string
     */
    private $requestCache;

ONGR Team's avatar
ONGR Team committed
    /**
     * @var bool
     */
    private $explain;

    /**
     * @var array
     */
    private $stats;

    /**
     * @var string[]
     */
    private $preference;

Mantas Jonušas's avatar
Mantas Jonušas committed
    /**
     * @var float
     */
    private $minScore;

    /**
     * @var OrderedSerializer
Mantas Jonušas's avatar
Mantas Jonušas committed
     */
    private $serializer;

    /**
     * @var SearchEndpointInterface[]
     */
    private $endpoints = [];

    /**
     * Initializes serializer.
     */
    public function __construct()
Mantas Jonušas's avatar
Mantas Jonušas committed
    {
        $this->serializer = new OrderedSerializer(
            [
                new CustomReferencedNormalizer(),
                new CustomNormalizer(),
            ]
        );
     * Returns endpoint instance.
ONGR Team's avatar
ONGR Team committed
     *
     * @param string $type Endpoint type.
ONGR Team's avatar
ONGR Team committed
     *
     * @return SearchEndpointInterface
ONGR Team's avatar
ONGR Team committed
     */
    private function getEndpoint($type)
ONGR Team's avatar
ONGR Team committed
    {
        if (!array_key_exists($type, $this->endpoints)) {
            $this->endpoints[$type] = SearchEndpointFactory::get($type);
        }
ONGR Team's avatar
ONGR Team committed

        return $this->endpoints[$type];
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Destroys search endpoint.
ONGR Team's avatar
ONGR Team committed
     *
     * @param string $type Endpoint type.
    public function destroyEndpoint($type)
        unset($this->endpoints[$type]);
    /**
     * Sets parameters to the endpoint.
     *
     * @param string $endpointName
     * @param array  $parameters
    private function setEndpointParameters($endpointName, array $parameters)
    {
        /** @var AbstractSearchEndpoint $endpoint */
        $endpoint = $this->getEndpoint($endpointName);
        $endpoint->setParameters($parameters);
    }

     * Adds query to the search.
     *
     * @param BuilderInterface $query
     * @param string           $boolType
     * @param string           $key
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function addQuery(BuilderInterface $query, $boolType = BoolQuery::MUST, $key = null)
ONGR Team's avatar
ONGR Team committed
    {
        $endpoint = $this->getEndpoint(QueryEndpoint::NAME);
        $endpoint->addToBool($query, $boolType, $key);
ONGR Team's avatar
ONGR Team committed

        return $this;
    }

    /**
     * Returns queries inside BoolQuery instance.
     *
     * @return BuilderInterface
     */
    public function getQueries()
    {
        $endpoint = $this->getEndpoint(QueryEndpoint::NAME);

        return $endpoint->getBool();
    }

    /**
     * Sets query endpoint parameters.
     *
     * @param array $parameters
     *
     * @return $this
     */
    public function setQueryParameters(array $parameters)
    {
        $this->setEndpointParameters(QueryEndpoint::NAME, $parameters);

        return $this;
    }

ONGR Team's avatar
ONGR Team committed
    /**
     * Adds a filter to the search.
ONGR Team's avatar
ONGR Team committed
     *
     * @param BuilderInterface $filter   Filter.
     * @param string           $boolType Example boolType values:
     * @param string           $key
ONGR Team's avatar
ONGR Team committed
     *
     * @return $this
ONGR Team's avatar
ONGR Team committed
     */
    public function addFilter(BuilderInterface $filter, $boolType = BoolFilter::MUST, $key = null)
ONGR Team's avatar
ONGR Team committed
    {
        $this->getEndpoint(QueryEndpoint::NAME);
        $endpoint = $this->getEndpoint(FilterEndpoint::NAME);
        $endpoint->addToBool($filter, $boolType, $key);
ONGR Team's avatar
ONGR Team committed

ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Returns queries inside BoolFilter instance.
ONGR Team's avatar
ONGR Team committed
     *
     * @return BuilderInterface
ONGR Team's avatar
ONGR Team committed
     */
    public function getFilters()
ONGR Team's avatar
ONGR Team committed
    {
        $endpoint = $this->getEndpoint(FilterEndpoint::NAME);
ONGR Team's avatar
ONGR Team committed

        return $endpoint->getBool();
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Sets filter endpoint parameters.
     *
     * @param array $parameters
     *
     * @return $this
     */
    public function setFilterParameters(array $parameters)
    {
        $this->setEndpointParameters(FilterEndpoint::NAME, $parameters);

        return $this;
    }

ONGR Team's avatar
ONGR Team committed
    /**
     * Adds a post filter to search.
     *
     * @param BuilderInterface $filter   Filter.
     * @param string           $boolType Example boolType values:
     *                                   - must
     *                                   - must_not
     *                                   - should.
     * @param string           $key
ONGR Team's avatar
ONGR Team committed
     *
     * @return int Key of post filter.
ONGR Team's avatar
ONGR Team committed
     */
    public function addPostFilter(BuilderInterface $filter, $boolType = BoolFilter::MUST, $key = null)
ONGR Team's avatar
ONGR Team committed
    {
        $this
            ->getEndpoint(PostFilterEndpoint::NAME)
            ->addToBool($filter, $boolType, $key);
ONGR Team's avatar
ONGR Team committed

ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Returns queries inside BoolFilter instance.
     *
     * @return BuilderInterface
     */
    public function getPostFilters()
    {
        $endpoint = $this->getEndpoint(PostFilterEndpoint::NAME);

        return $endpoint->getBool();
    }

    /**
     * Sets post filter endpoint parameters.
     *
     * @param array $parameters
     *
     * @return $this
     */
    public function setPostFilterParameters(array $parameters)
    {
        $this->setEndpointParameters(PostFilterEndpoint::NAME, $parameters);

        return $this;
    }

    /**
     * Adds aggregation into search.
     *
     * @param AbstractAggregation $aggregation
     *
Mantas Varatiejus's avatar
Mantas Varatiejus committed
     * @return $this
     */
    public function addAggregation(AbstractAggregation $aggregation)
    {
        $this->getEndpoint(AggregationsEndpoint::NAME)->add($aggregation, $aggregation->getName());

        return $this;
    }

    /**
     * Returns all aggregations.
     *
     * @return BuilderInterface[]
     */
    public function getAggregations()
    {
        return $this->getEndpoint(AggregationsEndpoint::NAME)->getAll();
    }

ONGR Team's avatar
ONGR Team committed
    /**
     * Adds sort to search.
ONGR Team's avatar
ONGR Team committed
     *
     * @param BuilderInterface $sort
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function addSort(BuilderInterface $sort)
ONGR Team's avatar
ONGR Team committed
    {
        $this->getEndpoint(SortEndpoint::NAME)->add($sort);
ONGR Team's avatar
ONGR Team committed

        return $this;
    }

    /**
     * Returns all set sorts.
     *
     * @return BuilderInterface[]
     */
    public function getSorts()
    {
        return $this->getEndpoint(SortEndpoint::NAME)->getAll();
    }

ONGR Team's avatar
ONGR Team committed
    /**
     * Allows to highlight search results on one or more fields.
ONGR Team's avatar
ONGR Team committed
     *
     * @param Highlight $highlight
     *
     * @return int Key of highlight.
ONGR Team's avatar
ONGR Team committed
     */
    public function addHighlight($highlight)
ONGR Team's avatar
ONGR Team committed
    {
        $this->getEndpoint(HighlightEndpoint::NAME)->add($highlight);

        return $this;
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Returns highlight builder.
     *
     * @return BuilderInterface
     */
    public function getHighlight()
    {
        /** @var HighlightEndpoint $highlightEndpoint */
        $highlightEndpoint = $this->getEndpoint(HighlightEndpoint::NAME);

        return $highlightEndpoint->getHighlight();
    }

ONGR Team's avatar
ONGR Team committed
    /**
     * Exclude documents which have a _score less than the minimum specified.
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function setMinScore($minScore)
ONGR Team's avatar
ONGR Team committed
    {
        $this->minScore = $minScore;
ONGR Team's avatar
ONGR Team committed

        return $this;
    }

    /**
     * Returns min score value.
     *
     * @return float
     */
    public function getMinScore()
    {
        return $this->minScore;
    }

    /**
     * Paginate reed removed lts from.
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function setFrom($from)
ONGR Team's avatar
ONGR Team committed
    {
ONGR Team's avatar
ONGR Team committed

        return $this;
    }

    /**
     * Sets timeout for query execution.
     *
     * @param $timeout
     *
     * @return $this
     */
    public function setTimeout($timeout)
    {
        $this->timeout = $timeout;

        return $this;
    }

    /**
     * Sets maximum number of documents per shard.
     *
     * @param $terminateAfter
     *
     * @return $this
     */
    public function setTerminateAfter($terminateAfter)
    {
        $this->terminateAfter = $terminateAfter;

        return $this;
    }

ONGR Team's avatar
ONGR Team committed
    /**
     * Returns results offset value.
ONGR Team's avatar
ONGR Team committed
     *
     * @return int
     */
    public function getFrom()
    {
        return $this->from;
    }

    /**
     * Set maximum number of results.
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function setSize($size)
ONGR Team's avatar
ONGR Team committed
    {
ONGR Team's avatar
ONGR Team committed

        return $this;
    }

    /**
     * Returns maximum number of results query can request.
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     */
ONGR Team's avatar
ONGR Team committed
    {
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Allows to control how the _source field is returned with every hit.
ONGR Team's avatar
ONGR Team committed
     *
     * @param array|bool|string $source
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function setSource($source)
ONGR Team's avatar
ONGR Team committed
    {
ONGR Team's avatar
ONGR Team committed

        return $this;
    }

    /**
     * Returns source value.
     *
     * @return array|bool|string
ONGR Team's avatar
ONGR Team committed
     */
    public function getSource()
ONGR Team's avatar
ONGR Team committed
    {
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Allows to selectively load specific stored fields for each document represented by a search hit.
     *
     * @param array $fields
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function setFields(array $fields)
ONGR Team's avatar
ONGR Team committed
    {
ONGR Team's avatar
ONGR Team committed

        return $this;
    }

    /**
ONGR Team's avatar
ONGR Team committed
     *
     * @return array
     */
    public function getFields()
ONGR Team's avatar
ONGR Team committed
    {
ONGR Team's avatar
ONGR Team committed

    /**
     * Allows to return a script evaluation (based on different fields) for each hit.
     *
     * @param array $scriptFields
     *
     */
    public function setScriptFields($scriptFields)
    {
        $this->scriptFields = $scriptFields;
ONGR Team's avatar
ONGR Team committed

ONGR Team's avatar
ONGR Team committed

    /**
     * Returns containing script fields.
     *
     * @return array
     */
    public function getScriptFields()
    {
        return $this->scriptFields;
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Sets explain property in request body search.
     *
     * @param bool $explain
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function setExplain($explain)
ONGR Team's avatar
ONGR Team committed
    {
        $this->explain = $explain;

        return $this;
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Returns if explain property is set in request body search.
     *
ONGR Team's avatar
ONGR Team committed
     * @return bool
     */
    public function isExplain()
    {
        return $this->explain;
    }

    /**
     * Sets a stats group.
     *
     * @param array $stats
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function setStats($stats)
ONGR Team's avatar
ONGR Team committed
    {
        $this->stats = $stats;

        return $this;
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Returns a stats group.
     *
     * @return array
ONGR Team's avatar
ONGR Team committed
     */
    public function getStats()
ONGR Team's avatar
ONGR Team committed
    {
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Setter for scroll duration, effectively setting if search is scrolled or not.
     *
     * @param string|null $duration
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function setScroll($duration = '5m')
ONGR Team's avatar
ONGR Team committed
    {
        $this->scroll = $duration;
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Returns scroll duration.
     *
     * @return string|null
ONGR Team's avatar
ONGR Team committed
     */
    public function getScroll()
ONGR Team's avatar
ONGR Team committed
    {
        return $this->scroll;
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Set search type.
     *
     * @param string $searchType
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function setSearchType($searchType)
ONGR Team's avatar
ONGR Team committed
    {
        $this->searchType = $searchType;

        return $this;
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Returns search type used.
     *
     * @return string
ONGR Team's avatar
ONGR Team committed
     */
    public function getSearchType()
ONGR Team's avatar
ONGR Team committed
    {
ONGR Team's avatar
ONGR Team committed
    }


    /**
     * Set request cache.
     *
     * @param string $requestCache
     *
     * @return $this
     */
    public function setRequestCache($requestCache)
    {
        $this->requestCache = $requestCache;

        return $this;
    }

    /**
     * Returns request cache.
     *
     * @return string
     */
    public function getRequestCache()
    {
        return $this->requestCache;
    }

ONGR Team's avatar
ONGR Team committed
    /**
     * Setter for preference.
     *
     * Controls which shard replicas to execute the search request on.
     *
     * @param mixed $preferenceParams Example values:
     *                                _primary
     *                                _primary_first
     *                                _local
     *                                _only_node:xyz (xyz - node id)
     *                                _prefer_node:xyz (xyz - node id)
     *                                _shards:2,3 (2 and 3 specified shards)
     *                                custom value
     *                                string[] combination of params.
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function setPreference($preferenceParams)
ONGR Team's avatar
ONGR Team committed
    {
        if (is_string($preferenceParams)) {
            $this->preference[] = $preferenceParams;
        }

        if (is_array($preferenceParams) && !empty($preferenceParams)) {
            $this->preference = $preferenceParams;
        }

        return $this;
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Returns preference params as string.
     *
     * @return string
ONGR Team's avatar
ONGR Team committed
     */
    public function getPreference()
ONGR Team's avatar
ONGR Team committed
    {
        return $this->preference ? implode(';', $this->preference) : null;
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Returns query url parameters.
     *
     * @return array
ONGR Team's avatar
ONGR Team committed
     */
    public function getQueryParams()
ONGR Team's avatar
ONGR Team committed
    {
        return array_filter(
            [
                'scroll' => $this->getScroll(),
                'search_type' => $this->getSearchType(),
                'request_cache' => $this->getRequestCache(),
                'preference' => $this->getPreference(),
            ]
        );
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * {@inheritdoc}
     */
    public function toArray()
    {
        $output = array_filter($this->serializer->normalize($this->endpoints));
ONGR Team's avatar
ONGR Team committed

        $params = [
            'from' => 'from',
            'size' => 'size',
            'fields' => 'fields',
            'scriptFields' => 'script_fields',
            'explain' => 'explain',
            'stats' => 'stats',
Mantas Jonušas's avatar
Mantas Jonušas committed
            'minScore' => 'min_score',
            'timeout' => 'timeout',
            'terminateAfter' => 'terminate_after',
ONGR Team's avatar
ONGR Team committed
        ];

        foreach ($params as $field => $param) {
            if ($this->$field !== null) {
                $output[$param] = $this->$field;
            }
        }

ONGR Team's avatar
ONGR Team committed
}