Newer
Older
<?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;
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
use ONGR\ElasticsearchDSL\Highlight\Highlight;
use ONGR\ElasticsearchDSL\InnerHit\NestedInnerHit;
use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery;
use ONGR\ElasticsearchDSL\SearchEndpoint\AbstractSearchEndpoint;
use ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint;
use ONGR\ElasticsearchDSL\SearchEndpoint\HighlightEndpoint;
use ONGR\ElasticsearchDSL\SearchEndpoint\InnerHitsEndpoint;
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;
use ONGR\ElasticsearchDSL\SearchEndpoint\SuggestEndpoint;
/**
* Search object that can be executed by a manager.
*/
class Search
{
/**
* If you don’t need to track the total number of hits at all you can improve
* query times by setting this option to false. Defaults to true.
*
* @var bool
*/
private $trackTotalHits;
* To retrieve hits from a certain offset. Defaults to 0.
*
* The number of hits to return. Defaults to 10. If you do not care about getting some
* hits back but only about the number of matches and/or aggregations, setting the value
* to 0 will help performance.
*
* @var int
*/
* Allows to control how the _source field is returned with every hit. By default
* operations return the contents of the _source field unless you have used the
* stored_fields parameter or if the _source field is disabled.
*
* @var bool
* Allows to selectively load specific stored fields for each document represented by a search hit.
*
private $storedFields;
* Allows to return a script evaluation (based on different fields) for each hit.
* Script fields can work on fields that are not stored, and allow to return custom
* values to be returned (the evaluated value of the script). Script fields can
* also access the actual _source document indexed and extract specific elements
* to be returned from it (can be an "object" type).
*
* Allows to return the doc value representation of a field for each hit. Doc value
* fields can work on fields that are not stored. Note that if the fields parameter
* specifies fields without docvalues it will try to load the value from the fielddata
* cache causing the terms for that field to be loaded to memory (cached), which will
* result in more memory consumption.
*
* @var array
* Enables explanation for each hit on how its score was computed.
*
* @var bool
* Returns a version for each search hit.
*
private $version;
/**
* Allows to configure different boost level per index when searching across more
* than one indices. This is very handy when hits coming from one index matter more
* than hits coming from another index (think social graph where each user has an index).
*
* @var array
*/
private $indicesBoost;
* Exclude documents which have a _score less than the minimum specified in min_score.
*
* @var int
*/
private $minScore;
/**
* Pagination of results can be done by using the from and size but the cost becomes
* prohibitive when the deep pagination is reached. The index.max_result_window which
* defaults to 10,000 is a safeguard, search requests take heap memory and time
* proportional to from + size. The Scroll api is recommended for efficient deep
* scrolling but scroll contexts are costly and it is not recommended to use it for
* real time user requests. The search_after parameter circumvents this problem by
* providing a live cursor. The idea is to use the results from the previous page to
* help the retrieval of the next page.
*
* URI parameters alongside Request body search.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html
*
* @var array
* While a search request returns a single “page” of results, the scroll API can be used to retrieve
* large numbers of results (or even all results) from a single search request, in much the same way
* as you would use a cursor on a traditional database. Scrolling is not intended for real time user
* requests, but rather for processing large amounts of data, e.g. in order to reindex the contents
* of one index into a new index with a different configuration.
*
* @var string
* @var OrderedSerializer
private static $serializer;
/**
* @var SearchEndpointInterface[]
*/
private $endpoints = [];
/**
* Constructor to initialize static properties
*/
public function __construct()
{
$this->initializeSerializer();
}
/**
* Wakeup method to initialize static properties
*/
public function __wakeup()
{
$this->initializeSerializer();
}
/**
* Initializes the serializer
*/
private function initializeSerializer()
if (static::$serializer === null) {
static::$serializer = new OrderedSerializer(
[
new CustomReferencedNormalizer(),
new CustomNormalizer(),
]
);
}
* @param string $type Endpoint type.
public function destroyEndpoint($type)
unset($this->endpoints[$type]);
* Adds query to the search.
*
* @param BuilderInterface $query
* @param string $boolType
* @param string $key
public function addQuery(BuilderInterface $query, $boolType = BoolQuery::MUST, $key = null)
$endpoint = $this->getEndpoint(QueryEndpoint::NAME);
$endpoint->addToBool($query, $boolType, $key);
/**
* 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);
}
return $this->endpoints[$type];
}
/**
* 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;
}
* Sets parameters to the endpoint.
* @param string $endpointName
* @param array $parameters
public function setEndpointParameters($endpointName, array $parameters)
/** @var AbstractSearchEndpoint $endpoint */
$endpoint = $this->getEndpoint($endpointName);
$endpoint->setParameters($parameters);
* Adds a post filter to search.
*
* @param BuilderInterface $filter Filter.
* @param string $boolType Example boolType values:
* - must
* - must_not
* - should.
* @param string $key
public function addPostFilter(BuilderInterface $filter, $boolType = BoolQuery::MUST, $key = null)
$this
->getEndpoint(PostFilterEndpoint::NAME)
->addToBool($filter, $boolType, $key);
/**
* 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
*
*/
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();
}
/**
* Adds inner hit into search.
*
* @param NestedInnerHit $innerHit
public function addInnerHit(NestedInnerHit $innerHit)
{
$this->getEndpoint(InnerHitsEndpoint::NAME)->add($innerHit, $innerHit->getName());
return $this;
}
/**
* Returns all inner hits.
*
* @return BuilderInterface[]
*/
public function getInnerHits()
{
return $this->getEndpoint(InnerHitsEndpoint::NAME)->getAll();
}
* @param BuilderInterface $sort
public function addSort(BuilderInterface $sort)
$this->getEndpoint(SortEndpoint::NAME)->add($sort);
/**
* Returns all set sorts.
*
* @return BuilderInterface[]
*/
public function getSorts()
{
return $this->getEndpoint(SortEndpoint::NAME)->getAll();
}
* Allows to highlight search results on one or more fields.
* @param Highlight $highlight
*
public function addHighlight($highlight)
$this->getEndpoint(HighlightEndpoint::NAME)->add($highlight);
return $this;
/**
* Returns highlight builder.
*
* @return BuilderInterface
*/
{
/** @var HighlightEndpoint $highlightEndpoint */
$highlightEndpoint = $this->getEndpoint(HighlightEndpoint::NAME);
return $highlightEndpoint->getHighlight();
}
public function addSuggest(NamedBuilderInterface $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();
}
* @param int $from
public function setFrom($from)
$this->from = $from;
/**
* @return bool
*/
public function isTrackTotalHits()
{
return $this->trackTotalHits;
}
/**
* @param bool $trackTotalHits
* @return $this
*/
public function setTrackTotalHits(bool $trackTotalHits)
{
$this->trackTotalHits = $trackTotalHits;
/**
* @return bool
*/
public function isTrackTotalHits()
{
return $this->trackTotalHits;
}
/**
* @param bool $trackTotalHits
* @return $this
*/
public function setTrackTotalHits(bool $trackTotalHits)
{
$this->trackTotalHits = $trackTotalHits;
return $this;
}
* @return int
*/
}
/**
* @param int $size
public function setSize($size)
$this->size = $size;
public function setSource($source)
$this->source = $source;
public function getStoredFields()
* @param array $storedFields
public function setStoredFields($storedFields)
$this->storedFields = $storedFields;
return $this;
}
/**
* @return array
*/
public function getScriptFields()
/**
* @param array $scriptFields
*/
public function setScriptFields($scriptFields)
{
$this->scriptFields = $scriptFields;
return $this;
}
/**
* @return array
*/
public function getDocValueFields()
public function setDocValueFields($docValueFields)
$this->docValueFields = $docValueFields;
}
/**
* @return bool
*/
public function isExplain()
{
return $this->explain;
}
/**
public function setExplain($explain)
/**
* @return bool
*/
public function isVersion()
{
return $this->version;
}
/**
* @param bool $version
* @return $this
*/
public function setVersion($version)
{
$this->version = $version;
* @return array
public function getIndicesBoost()
public function setIndicesBoost($indicesBoost)
$this->indicesBoost = $indicesBoost;
public function setMinScore($minScore)
public function getSearchAfter()
public function setSearchAfter($searchAfter)
$this->searchAfter = $searchAfter;
return $this;
}
/**
* @return string
*/
public function setScroll($scroll = '5m')
$this->addUriParam('scroll', $this->scroll);
* @param string $name
* @param string|array|bool $value
public function addUriParam($name, $value)
{
if (in_array($name, [
'q',
'df',
'analyzer',
'analyze_wildcard',
'default_operator',
'lenient',
'explain',
'_source',
'_source_exclude',
'_source_include',
'stored_fields',
'sort',
'track_scores',
'timeout',
'terminate_after',
'from',
'size',
'search_type',
'allow_no_indices',
'ignore_unavailable',
'typed_keys',
'pre_filter_shard_size',
'ignore_unavailable',
])) {
$this->uriParams[$name] = $value;
} else {
throw new \InvalidArgumentException(sprintf('Parameter %s is not supported.', $value));
}
return $this;
* Returns query url parameters.
*
* @return array
}
/**
* {@inheritdoc}
*/
public function toArray()
{
$output = array_filter(static::$serializer->normalize($this->endpoints));
$params = [
'from' => 'from',
'size' => 'size',
'storedFields' => 'stored_fields',
'docValueFields' => 'docvalue_fields',
'version' => 'version',
'indicesBoost' => 'indices_boost',
'searchAfter' => 'search_after',
'trackTotalHits' => 'track_total_hits',
];
foreach ($params as $field => $param) {
if ($this->$field !== null) {
$output[$param] = $this->$field;
}
}
return $output;
}