Skip to content
Snippets Groups Projects
Search.php 14.1 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\Highlight\Highlight;
use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointFactory;
use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointInterface;
use ONGR\ElasticsearchDSL\Serializer\Normalizer\CustomReferencedNormalizer;
use ONGR\ElasticsearchDSL\Serializer\OrderedSerializer;
use ONGR\ElasticsearchDSL\Sort\AbstractSort;
use ONGR\ElasticsearchDSL\Sort\Sorts;
use ONGR\ElasticsearchDSL\Suggester\AbstractSuggester;
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|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 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(),
            ]
        );
Mantas Jonušas's avatar
Mantas Jonušas committed
     *
     * @param BuilderInterface $query
     * @param string           $boolType
Mantas Jonušas's avatar
Mantas Jonušas committed
     *
Mantas Jonušas's avatar
Mantas Jonušas committed
     */
    public function addQuery(BuilderInterface $query, $boolType = '')
Mantas Jonušas's avatar
Mantas Jonušas committed
    {
        $this
            ->getEndpoint('query')
            ->addBuilder($query, ['bool_type' => $boolType]);
Mantas Jonušas's avatar
Mantas Jonušas committed

        return $this;
    }

ONGR Team's avatar
ONGR Team committed
    /**
     * Sets parameters for bool query.
ONGR Team's avatar
ONGR Team committed
     *
     * @param array $params Example values:
     *                      - minimum_should_match => 1
     *                      - boost => 1.
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function setBoolQueryParameters(array $params)
ONGR Team's avatar
ONGR Team committed
    {
        $this
            ->getEndpoint('query')
            ->setParameters($params);
ONGR Team's avatar
ONGR Team committed

        return $this;
    }

    /**
     * Returns contained query.
ONGR Team's avatar
ONGR Team committed
     *
     * @return BuilderInterface
     */
    public function getQuery()
    {
        return $this
            ->getEndpoint('query')
            ->getBuilder();
    }

    /**
     * Destroys query part.
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function destroyQuery()
ONGR Team's avatar
ONGR Team committed
    {
        $this->destroyEndpoint('query');
ONGR Team's avatar
ONGR Team committed

        return $this;
    }

    /**
     * Adds a filter to search.
ONGR Team's avatar
ONGR Team committed
     *
     * @param BuilderInterface $filter   Filter.
     * @param string           $boolType Possible boolType values:
     *                                   - must
     *                                   - must_not
     *                                   - should.
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function addFilter(BuilderInterface $filter, $boolType = '')
ONGR Team's avatar
ONGR Team committed
    {
        $this->getEndpoint('query');
ONGR Team's avatar
ONGR Team committed

        $this
            ->getEndpoint('filter')
            ->addBuilder($filter, ['bool_type' => $boolType]);
ONGR Team's avatar
ONGR Team committed

        return $this;
    }

    /**
     * Returns currently contained filters.
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
    {
        return $this
            ->getEndpoint('filter')
            ->getBuilder();
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Sets bool filter parameters.
ONGR Team's avatar
ONGR Team committed
     *
     * @param array $params Possible values:
     *                      _cache => true
     *                      false.
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function setBoolFilterParameters($params)
ONGR Team's avatar
ONGR Team committed
    {
        $this
            ->getEndpoint('filter')
            ->setParameters($params);
ONGR Team's avatar
ONGR Team committed

        return $this;
    }

    /**
ONGR Team's avatar
ONGR Team committed
     */
    public function destroyFilters()
ONGR Team's avatar
ONGR Team committed
    {
        $this->destroyEndpoint('filter');
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Adds a post filter to search.
     *
ONGR Team's avatar
ONGR Team committed
     * @param BuilderInterface $postFilter Post filter.
     * @param string           $boolType   Possible boolType values:
     *                                     - must
     *                                     - must_not
     *                                     - should.
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function addPostFilter(BuilderInterface $postFilter, $boolType = '')
ONGR Team's avatar
ONGR Team committed
    {
        $this
            ->getEndpoint('post_filter')
            ->addBuilder($postFilter, ['bool_type' => $boolType]);
ONGR Team's avatar
ONGR Team committed

        return $this;
    }

    /**
     * Returns all contained post filters.
ONGR Team's avatar
ONGR Team committed
     *
     * @return BuilderInterface
ONGR Team's avatar
ONGR Team committed
     */
    public function getPostFilters()
ONGR Team's avatar
ONGR Team committed
    {
        return $this
            ->getEndpoint('post_filter')
            ->getBuilder();
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Sets bool post filter parameters.
ONGR Team's avatar
ONGR Team committed
     *
     * @param array $params Possible values:
     *                      _cache => true
     *                      false.
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function setBoolPostFilterParameters($params)
ONGR Team's avatar
ONGR Team committed
    {
        $this
            ->getEndpoint('post_filter')
            ->setParameters($params);
ONGR Team's avatar
ONGR Team committed

        return $this;
    }

    /**
     * Returns min score value.
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function getMinScore()
ONGR Team's avatar
ONGR Team committed
    {
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;
    }

    /**
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     *
     * @return Search
     */
    public function setFrom($from)
ONGR Team's avatar
ONGR Team committed
    {
ONGR Team's avatar
ONGR Team committed

        return $this;
    }

    /**
     * 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
    }

    /**
     * Adds sort to search.
     *
     * @param AbstractSort $sort
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function addSort(AbstractSort $sort)
ONGR Team's avatar
ONGR Team committed
    {
        $this
            ->getEndpoint('sort')
            ->addBuilder($sort);

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

    /**
Mantas Jonušas's avatar
Mantas Jonušas committed
     * Returns sorts object.
ONGR Team's avatar
ONGR Team committed
     *
ONGR Team's avatar
ONGR Team committed
     */
    public function getSorts()
ONGR Team's avatar
ONGR Team committed
    {
        return $this
            ->getEndpoint('sort')
            ->getBuilder();
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
     *
     * @return Search
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
     *
     * @return Search
     */
    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
     *
     * @return Search
     */
    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
    }

    /**
     * Allows to highlight search results on one or more fields.
     *
     * @param Highlight $highlight
     *
     * @return Search
ONGR Team's avatar
ONGR Team committed
     */
    public function setHighlight($highlight)
ONGR Team's avatar
ONGR Team committed
    {
        $this
            ->getEndpoint('highlight')
            ->addBuilder($highlight);

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

    /**
     * Returns containing highlight object.
     *
     * @return Highlight
ONGR Team's avatar
ONGR Team committed
     */
    public function getHighlight()
ONGR Team's avatar
ONGR Team committed
    {
        return $this
            ->getEndpoint('highlight')
            ->getBuilder();
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Sets explain property in request body search.
     *
     * @param bool $explain
     *
     * @return Search
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
     *
     * @return Search
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
    }

    /**
     * Adds aggregation into search.
     *
     * @param AbstractAggregation $aggregation
     *
     * @return Search
ONGR Team's avatar
ONGR Team committed
     */
    public function addAggregation(AbstractAggregation $aggregation)
ONGR Team's avatar
ONGR Team committed
    {
        $this
            ->getEndpoint('aggregations')
            ->addBuilder($aggregation);

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

    /**
     * Returns contained aggregations.
     *
     * @return AbstractAggregation[]
ONGR Team's avatar
ONGR Team committed
     */
    public function getAggregations()
ONGR Team's avatar
ONGR Team committed
    {
        return $this
            ->getEndpoint('aggregations')
            ->getBuilder();
ONGR Team's avatar
ONGR Team committed
    }

    /**
     * Setter for scroll duration, effectively setting if search is scrolled or not.
     *
     * @param string|null $duration
     *
     * @return Search
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
     *
     * @return Search
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
    }

    /**
     * Setter for preference.
     *
     * Controls which shard replicas to execute the search request on.
     *
     * @param mixed $preferenceParams Possible 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.
     *
     * @return Search
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(),
                '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',
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

    /**
     * Returns endpoint instance.
     *
     * @param string $type Endpoint type.
     *
     * @return SearchEndpointInterface
     */
    private function getEndpoint($type)
    {
        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.
     *
     * @param string $type Endpoint type.
     */
    private function destroyEndpoint($type)
    {
        unset($this->endpoints[$type]);
ONGR Team's avatar
ONGR Team committed
    }
}