diff options
Diffstat (limited to 'vendor/ruflin/elastica/lib/Elastica/Query.php')
-rw-r--r-- | vendor/ruflin/elastica/lib/Elastica/Query.php | 284 |
1 files changed, 176 insertions, 108 deletions
diff --git a/vendor/ruflin/elastica/lib/Elastica/Query.php b/vendor/ruflin/elastica/lib/Elastica/Query.php index 2f37f2ea..175c8c95 100644 --- a/vendor/ruflin/elastica/lib/Elastica/Query.php +++ b/vendor/ruflin/elastica/lib/Elastica/Query.php @@ -1,6 +1,6 @@ <?php - namespace Elastica; + use Elastica\Aggregation\AbstractAggregation; use Elastica\Exception\InvalidException; use Elastica\Exception\NotImplementedException; @@ -10,36 +10,34 @@ use Elastica\Query\AbstractQuery; use Elastica\Query\MatchAll; use Elastica\Query\QueryString; use Elastica\Suggest\AbstractSuggest; -use Elastica\Suggest; /** - * Elastica query object + * Elastica query object. * * Creates different types of queries * - * @category Xodoa - * @package Elastica * @author Nicolas Ruflin <spam@ruflin.com> - * @link http://www.elasticsearch.org/guide/reference/api/search/ + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html */ class Query extends Param { /** - * Params + * Params. * * @var array Params */ protected $_params = array(); - + /** - * Suggest query or not - * - * @var int Suggest - */ + * Suggest query or not. + * + * @var int Suggest + */ protected $_suggest = 0; /** - * Creates a query object + * Creates a query object. * * @param array|\Elastica\Query\AbstractQuery $query OPTIONAL Query object (default = null) */ @@ -55,24 +53,26 @@ class Query extends Param } /** - * Transforms a string or an array to a query object + * Transforms a string or an array to a query object. * * If query is empty, * - * @param mixed $query + * @param mixed $query + * * @throws \Elastica\Exception\NotImplementedException - * @return \Elastica\Query + * + * @return self */ public static function create($query) { switch (true) { - case $query instanceof Query: + case $query instanceof self: return $query; case $query instanceof AbstractQuery: return new self($query); case $query instanceof AbstractFilter: $newQuery = new self(); - $newQuery->setFilter($query); + $newQuery->setPostFilter($query); return $newQuery; case empty($query): @@ -94,10 +94,11 @@ class Query extends Param } /** - * Sets query as raw array. Will overwrite all already set arguments + * Sets query as raw array. Will overwrite all already set arguments. * - * @param array $query Query array - * @return \Elastica\Query Query object + * @param array $query Query array + * + * @return $this */ public function setRawQuery(array $query) { @@ -107,10 +108,11 @@ class Query extends Param } /** - * Sets the query + * Sets the query. + * + * @param \Elastica\Query\AbstractQuery $query Query object * - * @param \Elastica\Query\AbstractQuery $query Query object - * @return \Elastica\Query Query object + * @return $this */ public function setQuery(AbstractQuery $query) { @@ -118,7 +120,7 @@ class Query extends Param } /** - * Gets the query array + * Gets the query array. * * @return array **/ @@ -128,21 +130,28 @@ class Query extends Param } /** - * Set Filter + * Set Filter. + * + * @param \Elastica\Filter\AbstractFilter $filter Filter object + * + * @return $this * - * @param \Elastica\Filter\AbstractFilter $filter Filter object - * @return \Elastica\Query Current object + * @link https://github.com/elasticsearch/elasticsearch/issues/7422 + * @deprecated */ public function setFilter(AbstractFilter $filter) { - return $this->setParam('filter', $filter->toArray()); + trigger_error('Deprecated: Elastica\Query::setFilter() is deprecated. Use Elastica\Query::setPostFilter() instead.', E_USER_DEPRECATED); + + return $this->setPostFilter($filter); } /** - * Sets the start from which the search results should be returned + * Sets the start from which the search results should be returned. + * + * @param int $from * - * @param int $from - * @return \Elastica\Query Query object + * @return $this */ public function setFrom($from) { @@ -151,11 +160,13 @@ class Query extends Param /** * Sets sort arguments for the query - * Replaces existing values + * Replaces existing values. + * + * @param array $sortArgs Sorting arguments * - * @param array $sortArgs Sorting arguments - * @return \Elastica\Query Query object - * @link http://www.elasticsearch.org/guide/reference/api/search/sort.html + * @return $this + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html */ public function setSort(array $sortArgs) { @@ -163,11 +174,13 @@ class Query extends Param } /** - * Adds a sort param to the query + * Adds a sort param to the query. + * + * @param mixed $sort Sort parameter * - * @param mixed $sort Sort parameter - * @return \Elastica\Query Query object - * @link http://www.elasticsearch.org/guide/reference/api/search/sort.html + * @return $this + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html */ public function addSort($sort) { @@ -175,11 +188,13 @@ class Query extends Param } /** - * Sets highlight arguments for the query + * Sets highlight arguments for the query. + * + * @param array $highlightArgs Set all highlight arguments * - * @param array $highlightArgs Set all highlight arguments - * @return \Elastica\Query Query object - * @link http://www.elasticsearch.org/guide/reference/api/search/highlighting.html + * @return $this + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html */ public function setHighlight(array $highlightArgs) { @@ -187,11 +202,13 @@ class Query extends Param } /** - * Adds a highlight argument + * Adds a highlight argument. + * + * @param mixed $highlight Add highlight argument * - * @param mixed $highlight Add highlight argument - * @return \Elastica\Query Query object - * @link http://www.elasticsearch.org/guide/reference/api/search/highlighting.html + * @return $this + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html */ public function addHighlight($highlight) { @@ -199,23 +216,25 @@ class Query extends Param } /** - * Sets maximum number of results for this query + * Sets maximum number of results for this query. + * + * @param int $size OPTIONAL Maximal number of results for query (default = 10) * - * @param int $size OPTIONAL Maximal number of results for query (default = 10) - * @return \Elastica\Query Query object + * @return $this */ public function setSize($size = 10) { return $this->setParam('size', $size); - } /** - * Alias for setSize + * Alias for setSize. * * @deprecated Use the setSize() method, this method will be removed in future releases - * @param int $limit OPTIONAL Maximal number of results for query (default = 10) - * @return \Elastica\Query Query object + * + * @param int $limit OPTIONAL Maximal number of results for query (default = 10) + * + * @return $this */ public function setLimit($limit = 10) { @@ -223,11 +242,13 @@ class Query extends Param } /** - * Enables explain on the query + * Enables explain on the query. + * + * @param bool $explain OPTIONAL Enabled or disable explain (default = true) + * + * @return $this * - * @param bool $explain OPTIONAL Enabled or disable explain (default = true) - * @return \Elastica\Query Current object - * @link http://www.elasticsearch.org/guide/reference/api/search/explain.html + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-explain.html */ public function setExplain($explain = true) { @@ -235,11 +256,13 @@ class Query extends Param } /** - * Enables version on the query + * Enables version on the query. * - * @param bool $version OPTIONAL Enabled or disable version (default = true) - * @return \Elastica\Query Current object - * @link http://www.elasticsearch.org/guide/reference/api/search/version.html + * @param bool $version OPTIONAL Enabled or disable version (default = true) + * + * @return $this + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-version.html */ public function setVersion($version = true) { @@ -248,10 +271,14 @@ class Query extends Param /** * Sets the fields to be returned by the search + * NOTICE php will encode modified(or named keys) array into object format in json format request + * so the fields array must a sequence(list) type of array. + * + * @param array $fields Fields to be returned * - * @param array $fields Fields to be returned - * @return \Elastica\Query Current object - * @link http://www.elasticsearch.org/guide/reference/api/search/fields.html + * @return $this + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-fields.html */ public function setFields(array $fields) { @@ -259,11 +286,13 @@ class Query extends Param } /** - * Set script fields + * Set script fields. + * + * @param array|\Elastica\ScriptFields $scriptFields Script fields * - * @param array|\Elastica\ScriptFields $scriptFields Script fields - * @return \Elastica\Query Current object - * @link http://www.elasticsearch.org/guide/reference/api/search/script-fields.html + * @return $this + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html */ public function setScriptFields($scriptFields) { @@ -275,11 +304,12 @@ class Query extends Param } /** - * Adds a Script to the query + * Adds a Script to the query. + * + * @param string $name + * @param \Elastica\Script $script Script object * - * @param string $name - * @param \Elastica\Script $script Script object - * @return \Elastica\Query Query object + * @return $this */ public function addScriptField($name, Script $script) { @@ -289,11 +319,14 @@ class Query extends Param } /** - * Sets all facets for this query object. Replaces existing facets + * Sets all facets for this query object. Replaces existing facets. * - * @param array $facets List of facet objects - * @return \Elastica\Query Query object - * @link http://www.elasticsearch.org/guide/reference/api/search/facets/ + * @param array $facets List of facet objects + * + * @return $this + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-facets.html + * @deprecated Facets are deprecated and will be removed in a future release. You are encouraged to migrate to aggregations instead. */ public function setFacets(array $facets) { @@ -306,10 +339,13 @@ class Query extends Param } /** - * Adds a Facet to the query + * Adds a Facet to the query. + * + * @param \Elastica\Facet\AbstractFacet $facet Facet object + * + * @return $this * - * @param \Elastica\Facet\AbstractFacet $facet Facet object - * @return \Elastica\Query Query object + * @deprecated Facets are deprecated and will be removed in a future release. You are encouraged to migrate to aggregations instead. */ public function addFacet(AbstractFacet $facet) { @@ -319,10 +355,11 @@ class Query extends Param } /** - * Adds an Aggregation to the query + * Adds an Aggregation to the query. * * @param AbstractAggregation $agg - * @return \Elastica\Query Query object + * + * @return $this */ public function addAggregation(AbstractAggregation $agg) { @@ -330,11 +367,12 @@ class Query extends Param $this->_params['aggs'] = array(); } $this->_params['aggs'][$agg->getName()] = $agg->toArray(); + return $this; } /** - * Converts all query params to an array + * Converts all query params to an array. * * @return array Query array */ @@ -356,11 +394,13 @@ class Query extends Param } /** - * Allows filtering of documents based on a minimum score + * Allows filtering of documents based on a minimum score. + * + * @param int $minScore Minimum score to filter documents by * - * @param int $minScore Minimum score to filter documents by * @throws \Elastica\Exception\InvalidException - * @return \Elastica\Query Query object + * + * @return $this */ public function setMinScore($minScore) { @@ -372,49 +412,77 @@ class Query extends Param } /** - * Add a suggest term + * Add a suggest term. * - * @param \Elastica\Suggest $suggest suggestion object + * @param \Elastica\Suggest $suggest suggestion object + * + * @return $this */ public function setSuggest(Suggest $suggest) { - $this->addParam(NULL, $suggest->toArray()); + $this->setParams(array_merge( + $this->getParams(), + $suggest->toArray() + )); + $this->_suggest = 1; + + return $this; } /** - * Add a Rescore + * Add a Rescore. + * + * @param mixed $rescore suggestion object * - * @param \Elastica\Rescore\AbstractRescore $suggest suggestion object + * @return $this */ public function setRescore($rescore) { - $this->setParam('rescore', $rescore->toArray()); + if (is_array($rescore)) { + $buffer = array(); + + foreach ($rescore as $rescoreQuery) { + $buffer [] = $rescoreQuery->toArray(); + } + } else { + $buffer = $rescore->toArray(); + } + + return $this->setParam('rescore', $buffer); } /** - * Sets the _source field to be returned with every hit + * Sets the _source field to be returned with every hit. * - * @param array $fields Fields to be returned - * @return \Elastica\Query Current object - * @link http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.x/search-request-source-filtering.html + * @param array|bool $params Fields to be returned or false to disable source + * + * @return $this + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html */ - public function setSource(array $fields) + public function setSource($params) { - return $this->setParam('_source', $fields); + return $this->setParam('_source', $params); } /** - * Sets post_filter argument for the query. The filter is applied after the query has executed - * @param array $post - * @return \Elastica\Query Current object - * @link http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/_filtering_queries_and_aggregations.html#_post_filter + * Sets post_filter argument for the query. The filter is applied after the query has executed. + * + * @param array|\Elastica\Filter\AbstractFilter $filter + * + * @return $this + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-post-filter.html */ - public function setPostFilter(array $post) + public function setPostFilter($filter) { - return $this->setParam("post_filter", $post); + if ($filter instanceof AbstractFilter) { + $filter = $filter->toArray(); + } else { + trigger_error('Deprecated: Elastica\Query::setPostFilter() passing filter as array is deprecated. Pass instance of AbstractFilter instead.', E_USER_DEPRECATED); + } + + return $this->setParam('post_filter', $filter); } } - - - |