From a5d77e615efd60acaf55df4e71d69399b712f343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simonas=20=C5=A0erlinskas?= <saimaz@users.noreply.github.com> Date: Fri, 27 Jan 2017 17:04:31 +0200 Subject: [PATCH] Restructure queries (#195) * move queries to specific domain level folder * fixed namespaces * added deprecation messages --- src/Query/BoolQuery.php | 113 +-------- src/Query/BoostingQuery.php | 54 +---- src/Query/CommonTermsQuery.php | 54 +---- src/Query/Compound/BoolQuery.php | 129 ++++++++++ src/Query/Compound/BoostingQuery.php | 71 ++++++ src/Query/Compound/ConstantScoreQuery.php | 62 +++++ src/Query/Compound/DisMaxQuery.php | 76 ++++++ src/Query/Compound/FunctionScoreQuery.php | 229 ++++++++++++++++++ src/Query/Compound/IndicesQuery.php | 81 +++++++ src/Query/ConstantScoreQuery.php | 45 +--- src/Query/DisMaxQuery.php | 59 +---- src/Query/ExistsQuery.php | 40 +-- src/Query/FullText/CommonTermsQuery.php | 71 ++++++ src/Query/FullText/MatchPhrasePrefixQuery.php | 29 +++ src/Query/FullText/MatchPhraseQuery.php | 29 +++ src/Query/FullText/MatchQuery.php | 71 ++++++ src/Query/FullText/MultiMatchQuery.php | 70 ++++++ src/Query/FullText/QueryStringQuery.php | 62 +++++ src/Query/FullText/SimpleQueryStringQuery.php | 62 +++++ src/Query/FunctionScoreQuery.php | 212 +--------------- src/Query/FuzzyQuery.php | 54 +---- src/Query/Geo/GeoBoundingBoxQuery.php | 85 +++++++ src/Query/Geo/GeoDistanceQuery.php | 77 ++++++ src/Query/Geo/GeoDistanceRangeQuery.php | 74 ++++++ src/Query/Geo/GeoPolygonQuery.php | 66 +++++ src/Query/Geo/GeoShapeQuery.php | 102 ++++++++ src/Query/GeoBoundingBoxQuery.php | 68 +----- src/Query/GeoDistanceQuery.php | 60 +---- src/Query/GeoDistanceRangeQuery.php | 55 +---- src/Query/GeoPolygonQuery.php | 49 +--- src/Query/GeoShapeQuery.php | 85 +------ src/Query/GeohashCellQuery.php | 1 + src/Query/HasChildQuery.php | 53 +--- src/Query/HasParentQuery.php | 53 +--- src/Query/IdsQuery.php | 45 +--- src/Query/IndicesQuery.php | 64 +---- src/Query/Joining/HasChildQuery.php | 70 ++++++ src/Query/Joining/HasParentQuery.php | 70 ++++++ src/Query/Joining/NestedQuery.php | 70 ++++++ src/Query/LimitQuery.php | 1 + src/Query/MatchPhrasePrefixQuery.php | 12 +- src/Query/MatchPhraseQuery.php | 12 +- src/Query/MatchQuery.php | 54 +---- src/Query/MissingQuery.php | 1 + src/Query/MoreLikeThisQuery.php | 47 +--- src/Query/MultiMatchQuery.php | 53 +--- src/Query/NestedQuery.php | 53 +--- src/Query/PrefixQuery.php | 54 +---- src/Query/QueryStringQuery.php | 45 +--- src/Query/RangeQuery.php | 60 +---- src/Query/RegexpQuery.php | 54 +---- src/Query/ScriptQuery.php | 42 +--- src/Query/SimpleQueryStringQuery.php | 42 +--- src/Query/Specialized/MoreLikeThisQuery.php | 64 +++++ src/Query/Specialized/ScriptQuery.php | 59 +++++ src/Query/Specialized/TemplateQuery.php | 132 ++++++++++ src/Query/TemplateQuery.php | 115 +-------- src/Query/TermLevel/ExistsQuery.php | 57 +++++ src/Query/TermLevel/FuzzyQuery.php | 71 ++++++ src/Query/TermLevel/IdsQuery.php | 62 +++++ src/Query/TermLevel/PrefixQuery.php | 71 ++++++ src/Query/TermLevel/RangeQuery.php | 77 ++++++ src/Query/TermLevel/RegexpQuery.php | 71 ++++++ src/Query/TermLevel/TermQuery.php | 75 ++++++ src/Query/TermLevel/TermsQuery.php | 71 ++++++ src/Query/TermLevel/TypeQuery.php | 57 +++++ src/Query/TermLevel/WildcardQuery.php | 71 ++++++ src/Query/TermQuery.php | 58 +---- src/Query/TermsQuery.php | 54 +---- src/Query/TypeQuery.php | 40 +-- src/Query/WildcardQuery.php | 54 +---- 71 files changed, 2698 insertions(+), 1911 deletions(-) create mode 100644 src/Query/Compound/BoolQuery.php create mode 100644 src/Query/Compound/BoostingQuery.php create mode 100644 src/Query/Compound/ConstantScoreQuery.php create mode 100644 src/Query/Compound/DisMaxQuery.php create mode 100644 src/Query/Compound/FunctionScoreQuery.php create mode 100644 src/Query/Compound/IndicesQuery.php create mode 100644 src/Query/FullText/CommonTermsQuery.php create mode 100644 src/Query/FullText/MatchPhrasePrefixQuery.php create mode 100644 src/Query/FullText/MatchPhraseQuery.php create mode 100644 src/Query/FullText/MatchQuery.php create mode 100644 src/Query/FullText/MultiMatchQuery.php create mode 100644 src/Query/FullText/QueryStringQuery.php create mode 100644 src/Query/FullText/SimpleQueryStringQuery.php create mode 100644 src/Query/Geo/GeoBoundingBoxQuery.php create mode 100644 src/Query/Geo/GeoDistanceQuery.php create mode 100644 src/Query/Geo/GeoDistanceRangeQuery.php create mode 100644 src/Query/Geo/GeoPolygonQuery.php create mode 100644 src/Query/Geo/GeoShapeQuery.php create mode 100644 src/Query/Joining/HasChildQuery.php create mode 100644 src/Query/Joining/HasParentQuery.php create mode 100644 src/Query/Joining/NestedQuery.php create mode 100644 src/Query/Specialized/MoreLikeThisQuery.php create mode 100644 src/Query/Specialized/ScriptQuery.php create mode 100644 src/Query/Specialized/TemplateQuery.php create mode 100644 src/Query/TermLevel/ExistsQuery.php create mode 100644 src/Query/TermLevel/FuzzyQuery.php create mode 100644 src/Query/TermLevel/IdsQuery.php create mode 100644 src/Query/TermLevel/PrefixQuery.php create mode 100644 src/Query/TermLevel/RangeQuery.php create mode 100644 src/Query/TermLevel/RegexpQuery.php create mode 100644 src/Query/TermLevel/TermQuery.php create mode 100644 src/Query/TermLevel/TermsQuery.php create mode 100644 src/Query/TermLevel/TypeQuery.php create mode 100644 src/Query/TermLevel/WildcardQuery.php diff --git a/src/Query/BoolQuery.php b/src/Query/BoolQuery.php index f34d38a..ff217b9 100644 --- a/src/Query/BoolQuery.php +++ b/src/Query/BoolQuery.php @@ -11,120 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; -use Ramsey\Uuid\Uuid; - /** * Represents Elasticsearch "bool" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class BoolQuery implements BuilderInterface +class BoolQuery extends \ONGR\ElasticsearchDSL\Query\Compound\BoolQuery { - use ParametersTrait; - - const MUST = 'must'; - const MUST_NOT = 'must_not'; - const SHOULD = 'should'; - const FILTER = 'filter'; - - /** - * @var array - */ - private $container = []; - - /** - * Constructor to prepare container. - */ - public function __construct() - { - $this->container = []; - } - - /** - * Returns the query instances (by bool type). - * - * @param string|null $boolType - * - * @return array - */ - public function getQueries($boolType = null) - { - if ($boolType === null) { - $queries = []; - - foreach ($this->container as $item) { - $queries = array_merge($queries, $item); - } - - return $queries; - } - - if (isset($this->container[$boolType])) { - return $this->container[$boolType]; - } - - return []; - } - - /** - * Add BuilderInterface object to bool operator. - * - * @param BuilderInterface $query Query add to the bool. - * @param string $type Bool type. Example: must, must_not, should. - * @param string $key Key that indicates a builder id. - * - * @return string Key of added builder. - * - * @throws \UnexpectedValueException - */ - public function add(BuilderInterface $query, $type = self::MUST, $key = null) - { - if (!in_array($type, [self::MUST, self::MUST_NOT, self::SHOULD, self::FILTER])) { - throw new \UnexpectedValueException(sprintf('The bool operator %s is not supported', $type)); - } - - if (!$key) { - $key = bin2hex(random_bytes(30)); - } - - $this->container[$type][$key] = $query; - - return $key; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - if (count($this->container) === 1 && isset($this->container[self::MUST]) - && count($this->container[self::MUST]) === 1) { - $query = reset($this->container[self::MUST]); - - return $query->toArray(); - } - - $output = []; - - foreach ($this->container as $boolType => $builders) { - /** @var BuilderInterface $builder */ - foreach ($builders as $builder) { - $output[$boolType][] = $builder->toArray(); - } - } - - $output = $this->processArray($output); - - return [$this->getType() => $output]; - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'bool'; - } } diff --git a/src/Query/BoostingQuery.php b/src/Query/BoostingQuery.php index 2e6fe64..c59f367 100644 --- a/src/Query/BoostingQuery.php +++ b/src/Query/BoostingQuery.php @@ -11,61 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; - /** * Represents Elasticsearch "boosting" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class BoostingQuery implements BuilderInterface +class BoostingQuery extends \ONGR\ElasticsearchDSL\Query\Compound\BoostingQuery { - /** - * @var BuilderInterface - */ - private $positive; - - /** - * @var BuilderInterface - */ - private $negative; - - /** - * @var int|float - */ - private $negativeBoost; - - /** - * @param BuilderInterface $positive - * @param BuilderInterface $negative - * @param int|float $negativeBoost - */ - public function __construct(BuilderInterface $positive, BuilderInterface $negative, $negativeBoost) - { - $this->positive = $positive; - $this->negative = $negative; - $this->negativeBoost = $negativeBoost; - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'boosting'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - 'positive' => $this->positive->toArray(), - 'negative' => $this->negative->toArray(), - 'negative_boost' => $this->negativeBoost, - ]; - - return [$this->getType() => $query]; - } } diff --git a/src/Query/CommonTermsQuery.php b/src/Query/CommonTermsQuery.php index aa1dfc7..c919c67 100644 --- a/src/Query/CommonTermsQuery.php +++ b/src/Query/CommonTermsQuery.php @@ -11,61 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "common" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-common-terms-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class CommonTermsQuery implements BuilderInterface +class CommonTermsQuery extends \ONGR\ElasticsearchDSL\Query\FullText\CommonTermsQuery { - use ParametersTrait; - - /** - * @var string - */ - private $field; - - /** - * @var string - */ - private $query; - - /** - * @param string $field - * @param string $query - * @param array $parameters - */ - public function __construct($field, $query, array $parameters = []) - { - $this->field = $field; - $this->query = $query; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'common'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - 'query' => $this->query, - ]; - - $output = [ - $this->field => $this->processArray($query), - ]; - - return [$this->getType() => $output]; - } } diff --git a/src/Query/Compound/BoolQuery.php b/src/Query/Compound/BoolQuery.php new file mode 100644 index 0000000..4b30287 --- /dev/null +++ b/src/Query/Compound/BoolQuery.php @@ -0,0 +1,129 @@ +<?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\Query\Compound; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "bool" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html + */ +class BoolQuery implements BuilderInterface +{ + use ParametersTrait; + + const MUST = 'must'; + const MUST_NOT = 'must_not'; + const SHOULD = 'should'; + const FILTER = 'filter'; + + /** + * @var array + */ + private $container = []; + + /** + * Constructor to prepare container. + */ + public function __construct() + { + $this->container = []; + } + + /** + * Returns the query instances (by bool type). + * + * @param string|null $boolType + * + * @return array + */ + public function getQueries($boolType = null) + { + if ($boolType === null) { + $queries = []; + + foreach ($this->container as $item) { + $queries = array_merge($queries, $item); + } + + return $queries; + } + + if (isset($this->container[$boolType])) { + return $this->container[$boolType]; + } + + return []; + } + + /** + * Add BuilderInterface object to bool operator. + * + * @param BuilderInterface $query Query add to the bool. + * @param string $type Bool type. Example: must, must_not, should. + * @param string $key Key that indicates a builder id. + * + * @return string Key of added builder. + * + * @throws \UnexpectedValueException + */ + public function add(BuilderInterface $query, $type = self::MUST, $key = null) + { + if (!in_array($type, [self::MUST, self::MUST_NOT, self::SHOULD, self::FILTER])) { + throw new \UnexpectedValueException(sprintf('The bool operator %s is not supported', $type)); + } + + if (!$key) { + $key = bin2hex(random_bytes(30)); + } + + $this->container[$type][$key] = $query; + + return $key; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + if (count($this->container) === 1 && isset($this->container[self::MUST]) + && count($this->container[self::MUST]) === 1) { + $query = reset($this->container[self::MUST]); + + return $query->toArray(); + } + + $output = []; + + foreach ($this->container as $boolType => $builders) { + /** @var BuilderInterface $builder */ + foreach ($builders as $builder) { + $output[$boolType][] = $builder->toArray(); + } + } + + $output = $this->processArray($output); + + return [$this->getType() => $output]; + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'bool'; + } +} diff --git a/src/Query/Compound/BoostingQuery.php b/src/Query/Compound/BoostingQuery.php new file mode 100644 index 0000000..87d3523 --- /dev/null +++ b/src/Query/Compound/BoostingQuery.php @@ -0,0 +1,71 @@ +<?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\Query\Compound; + +use ONGR\ElasticsearchDSL\BuilderInterface; + +/** + * Represents Elasticsearch "boosting" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html + */ +class BoostingQuery implements BuilderInterface +{ + /** + * @var BuilderInterface + */ + private $positive; + + /** + * @var BuilderInterface + */ + private $negative; + + /** + * @var int|float + */ + private $negativeBoost; + + /** + * @param BuilderInterface $positive + * @param BuilderInterface $negative + * @param int|float $negativeBoost + */ + public function __construct(BuilderInterface $positive, BuilderInterface $negative, $negativeBoost) + { + $this->positive = $positive; + $this->negative = $negative; + $this->negativeBoost = $negativeBoost; + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'boosting'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [ + 'positive' => $this->positive->toArray(), + 'negative' => $this->negative->toArray(), + 'negative_boost' => $this->negativeBoost, + ]; + + return [$this->getType() => $query]; + } +} diff --git a/src/Query/Compound/ConstantScoreQuery.php b/src/Query/Compound/ConstantScoreQuery.php new file mode 100644 index 0000000..ee3373a --- /dev/null +++ b/src/Query/Compound/ConstantScoreQuery.php @@ -0,0 +1,62 @@ +<?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\Query\Compound; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "constant_score" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html + */ +class ConstantScoreQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var BuilderInterface + */ + private $query; + + /** + * @param BuilderInterface $query + * @param array $parameters + */ + public function __construct(BuilderInterface $query, array $parameters = []) + { + $this->query = $query; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'constant_score'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [ + 'filter' => $this->query->toArray(), + ]; + + $output = $this->processArray($query); + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/Compound/DisMaxQuery.php b/src/Query/Compound/DisMaxQuery.php new file mode 100644 index 0000000..adb77c3 --- /dev/null +++ b/src/Query/Compound/DisMaxQuery.php @@ -0,0 +1,76 @@ +<?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\Query\Compound; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "dis_max" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-dis-max-query.html + */ +class DisMaxQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var BuilderInterface[] + */ + private $queries = []; + + /** + * Initializes Dis Max query. + * + * @param array $parameters + */ + public function __construct(array $parameters = []) + { + $this->setParameters($parameters); + } + + /** + * Add query. + * + * @param BuilderInterface $query + * + * @return DisMaxQuery + */ + public function addQuery(BuilderInterface $query) + { + $this->queries[] = $query; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'dis_max'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = []; + foreach ($this->queries as $type) { + $query[] = $type->toArray(); + } + $output = $this->processArray(['queries' => $query]); + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/Compound/FunctionScoreQuery.php b/src/Query/Compound/FunctionScoreQuery.php new file mode 100644 index 0000000..38e01fe --- /dev/null +++ b/src/Query/Compound/FunctionScoreQuery.php @@ -0,0 +1,229 @@ +<?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\Query\Compound; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "function_score" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html + */ +class FunctionScoreQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var BuilderInterface + */ + private $query; + + /** + * @var array[] + */ + private $functions; + + /** + * @param BuilderInterface $query + * @param array $parameters + */ + public function __construct(BuilderInterface $query, array $parameters = []) + { + $this->query = $query; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'function_score'; + } + + /** + * Modifier to apply filter to the function score function. + * + * @param array $function + * @param BuilderInterface $query + */ + private function applyFilter(array &$function, BuilderInterface $query = null) + { + if ($query) { + $function['filter'] = $query->toArray(); + } + } + + /** + * Creates field_value_factor function. + * + * @param string $field + * @param float $factor + * @param string $modifier + * @param BuilderInterface $query + * + * @return $this + */ + public function addFieldValueFactorFunction($field, $factor, $modifier = 'none', BuilderInterface $query = null) + { + $function = [ + 'field_value_factor' => [ + 'field' => $field, + 'factor' => $factor, + 'modifier' => $modifier, + ], + ]; + + $this->applyFilter($function, $query); + + $this->functions[] = $function; + + return $this; + } + + /** + * Add decay function to function score. Weight and query are optional. + * + * @param string $type + * @param string $field + * @param array $function + * @param array $options + * @param BuilderInterface $query + * + * @return $this + */ + public function addDecayFunction( + $type, + $field, + array $function, + array $options = [], + BuilderInterface $query = null + ) { + $function = [ + $type => array_merge( + [$field => $function], + $options + ), + ]; + + $this->applyFilter($function, $query); + + $this->functions[] = $function; + + return $this; + } + + /** + * Adds function to function score without decay function. Influence search score only for specific query. + * + * @param float $weight + * @param BuilderInterface $query + * + * @return $this + */ + public function addWeightFunction($weight, BuilderInterface $query = null) + { + $function = [ + 'weight' => $weight, + ]; + + $this->applyFilter($function, $query); + + $this->functions[] = $function; + + return $this; + } + + /** + * Adds random score function. Seed is optional. + * + * @param mixed $seed + * @param BuilderInterface $query + * + * @return $this + */ + public function addRandomFunction($seed = null, BuilderInterface $query = null) + { + $function = [ + 'random_score' => $seed ? [ 'seed' => $seed ] : new \stdClass(), + ]; + + $this->applyFilter($function, $query); + + $this->functions[] = $function; + + return $this; + } + + /** + * Adds script score function. + * + * @param string $script + * @param array $params + * @param array $options + * @param BuilderInterface $query + * + * @return $this + */ + public function addScriptScoreFunction( + $script, + array $params = [], + array $options = [], + BuilderInterface $query = null + ) { + $function = [ + 'script_score' => array_merge( + [ + 'script' => $script, + 'params' => $params, + ], + $options + ), + ]; + + $this->applyFilter($function, $query); + + $this->functions[] = $function; + + return $this; + } + + /** + * Adds custom simple function. You can add to the array whatever you want. + * + * @param array $function + * + * @return $this + */ + public function addSimpleFunction(array $function) + { + $this->functions[] = $function; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [ + 'query' => $this->query->toArray(), + 'functions' => $this->functions, + ]; + + $output = $this->processArray($query); + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/Compound/IndicesQuery.php b/src/Query/Compound/IndicesQuery.php new file mode 100644 index 0000000..0348465 --- /dev/null +++ b/src/Query/Compound/IndicesQuery.php @@ -0,0 +1,81 @@ +<?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\Query\Compound; + +use ONGR\ElasticsearchDSL\BuilderInterface; + +/** + * Represents Elasticsearch "indices" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-indices-query.html + */ +class IndicesQuery implements BuilderInterface +{ + /** + * @var string[] + */ + private $indices; + + /** + * @var BuilderInterface + */ + private $query; + + /** + * @var string|BuilderInterface + */ + private $noMatchQuery; + + /** + * @param string[] $indices + * @param BuilderInterface $query + * @param BuilderInterface $noMatchQuery + */ + public function __construct($indices, $query, $noMatchQuery = null) + { + $this->indices = $indices; + $this->query = $query; + $this->noMatchQuery = $noMatchQuery; + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'indices'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + if (count($this->indices) > 1) { + $output = ['indices' => $this->indices]; + } else { + $output = ['index' => $this->indices[0]]; + } + + $output['query'] = $this->query->toArray(); + + if ($this->noMatchQuery !== null) { + if (is_a($this->noMatchQuery, 'ONGR\ElasticsearchDSL\BuilderInterface')) { + $output['no_match_query'] = $this->noMatchQuery->toArray(); + } else { + $output['no_match_query'] = $this->noMatchQuery; + } + } + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/ConstantScoreQuery.php b/src/Query/ConstantScoreQuery.php index 6ec1150..b0b31fd 100644 --- a/src/Query/ConstantScoreQuery.php +++ b/src/Query/ConstantScoreQuery.php @@ -11,52 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "constant_score" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class ConstantScoreQuery implements BuilderInterface +class ConstantScoreQuery extends \ONGR\ElasticsearchDSL\Query\Compound\ConstantScoreQuery { - use ParametersTrait; - - /** - * @var BuilderInterface - */ - private $query; - - /** - * @param BuilderInterface $query - * @param array $parameters - */ - public function __construct(BuilderInterface $query, array $parameters = []) - { - $this->query = $query; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'constant_score'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - 'filter' => $this->query->toArray(), - ]; - - $output = $this->processArray($query); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/DisMaxQuery.php b/src/Query/DisMaxQuery.php index aee0d33..0485c64 100644 --- a/src/Query/DisMaxQuery.php +++ b/src/Query/DisMaxQuery.php @@ -11,66 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "dis_max" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-dis-max-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class DisMaxQuery implements BuilderInterface +class DisMaxQuery extends \ONGR\ElasticsearchDSL\Query\Compound\DisMaxQuery { - use ParametersTrait; - - /** - * @var BuilderInterface[] - */ - private $queries = []; - - /** - * Initializes Dis Max query. - * - * @param array $parameters - */ - public function __construct(array $parameters = []) - { - $this->setParameters($parameters); - } - - /** - * Add query. - * - * @param BuilderInterface $query - * - * @return DisMaxQuery - */ - public function addQuery(BuilderInterface $query) - { - $this->queries[] = $query; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'dis_max'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = []; - foreach ($this->queries as $type) { - $query[] = $type->toArray(); - } - $output = $this->processArray(['queries' => $query]); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/ExistsQuery.php b/src/Query/ExistsQuery.php index fd7f4de..26227c6 100644 --- a/src/Query/ExistsQuery.php +++ b/src/Query/ExistsQuery.php @@ -11,47 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; - /** * Represents Elasticsearch "exists" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class ExistsQuery implements BuilderInterface +class ExistsQuery extends \ONGR\ElasticsearchDSL\Query\TermLevel\ExistsQuery { - /** - * @var string - */ - private $field; - - /** - * Constructor. - * - * @param string $field Field value - */ - public function __construct($field) - { - $this->field = $field; - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'exists'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - return [ - $this->getType() => [ - 'field' => $this->field, - ], - ]; - } } diff --git a/src/Query/FullText/CommonTermsQuery.php b/src/Query/FullText/CommonTermsQuery.php new file mode 100644 index 0000000..2e31b13 --- /dev/null +++ b/src/Query/FullText/CommonTermsQuery.php @@ -0,0 +1,71 @@ +<?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\Query\FullText; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "common" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-common-terms-query.html + */ +class CommonTermsQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string + */ + private $field; + + /** + * @var string + */ + private $query; + + /** + * @param string $field + * @param string $query + * @param array $parameters + */ + public function __construct($field, $query, array $parameters = []) + { + $this->field = $field; + $this->query = $query; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'common'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [ + 'query' => $this->query, + ]; + + $output = [ + $this->field => $this->processArray($query), + ]; + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/FullText/MatchPhrasePrefixQuery.php b/src/Query/FullText/MatchPhrasePrefixQuery.php new file mode 100644 index 0000000..58f8a83 --- /dev/null +++ b/src/Query/FullText/MatchPhrasePrefixQuery.php @@ -0,0 +1,29 @@ +<?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\Query\FullText; + +/** + * Represents Elasticsearch "match_phrase_prefix" query. + * + * @author Ron Rademaker + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html + */ +class MatchPhrasePrefixQuery extends MatchQuery +{ + /** + * {@inheritdoc} + */ + public function getType() + { + return 'match_phrase_prefix'; + } +} diff --git a/src/Query/FullText/MatchPhraseQuery.php b/src/Query/FullText/MatchPhraseQuery.php new file mode 100644 index 0000000..2560088 --- /dev/null +++ b/src/Query/FullText/MatchPhraseQuery.php @@ -0,0 +1,29 @@ +<?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\Query\FullText; + +/** + * Represents Elasticsearch "match_phrase" query. + * + * @author Ron Rademaker + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html + */ +class MatchPhraseQuery extends MatchQuery +{ + /** + * {@inheritdoc} + */ + public function getType() + { + return 'match_phrase'; + } +} diff --git a/src/Query/FullText/MatchQuery.php b/src/Query/FullText/MatchQuery.php new file mode 100644 index 0000000..6ce6801 --- /dev/null +++ b/src/Query/FullText/MatchQuery.php @@ -0,0 +1,71 @@ +<?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\Query\FullText; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "match" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html + */ +class MatchQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string + */ + private $field; + + /** + * @var string + */ + private $query; + + /** + * @param string $field + * @param string $query + * @param array $parameters + */ + public function __construct($field, $query, array $parameters = []) + { + $this->field = $field; + $this->query = $query; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'match'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [ + 'query' => $this->query, + ]; + + $output = [ + $this->field => $this->processArray($query), + ]; + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/FullText/MultiMatchQuery.php b/src/Query/FullText/MultiMatchQuery.php new file mode 100644 index 0000000..ec7f04f --- /dev/null +++ b/src/Query/FullText/MultiMatchQuery.php @@ -0,0 +1,70 @@ +<?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\Query\FullText; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "multi_match" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html + */ +class MultiMatchQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var array + */ + private $fields = []; + + /** + * @var string + */ + private $query; + + /** + * @param array $fields + * @param string $query + * @param array $parameters + */ + public function __construct(array $fields, $query, array $parameters = []) + { + $this->fields = $fields; + $this->query = $query; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'multi_match'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [ + 'fields' => $this->fields, + 'query' => $this->query, + ]; + + $output = $this->processArray($query); + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/FullText/QueryStringQuery.php b/src/Query/FullText/QueryStringQuery.php new file mode 100644 index 0000000..29eda3f --- /dev/null +++ b/src/Query/FullText/QueryStringQuery.php @@ -0,0 +1,62 @@ +<?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\Query\FullText; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "query_string" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html + */ +class QueryStringQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string The actual query to be parsed. + */ + private $query; + + /** + * @param string $query + * @param array $parameters + */ + public function __construct($query, array $parameters = []) + { + $this->query = $query; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'query_string'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [ + 'query' => $this->query, + ]; + + $output = $this->processArray($query); + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/FullText/SimpleQueryStringQuery.php b/src/Query/FullText/SimpleQueryStringQuery.php new file mode 100644 index 0000000..976ffce --- /dev/null +++ b/src/Query/FullText/SimpleQueryStringQuery.php @@ -0,0 +1,62 @@ +<?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\Query\FullText; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "simple_query_string" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html + */ +class SimpleQueryStringQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string The actual query to be parsed. + */ + private $query; + + /** + * @param string $query + * @param array $parameters + */ + public function __construct($query, array $parameters = []) + { + $this->query = $query; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'simple_query_string'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [ + 'query' => $this->query, + ]; + + $output = $this->processArray($query); + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/FunctionScoreQuery.php b/src/Query/FunctionScoreQuery.php index c154092..3657e6e 100644 --- a/src/Query/FunctionScoreQuery.php +++ b/src/Query/FunctionScoreQuery.php @@ -11,219 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "function_score" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class FunctionScoreQuery implements BuilderInterface +class FunctionScoreQuery extends \ONGR\ElasticsearchDSL\Query\Compound\FunctionScoreQuery { - use ParametersTrait; - - /** - * @var BuilderInterface - */ - private $query; - - /** - * @var array[] - */ - private $functions; - - /** - * @param BuilderInterface $query - * @param array $parameters - */ - public function __construct(BuilderInterface $query, array $parameters = []) - { - $this->query = $query; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'function_score'; - } - - /** - * Modifier to apply filter to the function score function. - * - * @param array $function - * @param BuilderInterface $query - */ - private function applyFilter(array &$function, BuilderInterface $query = null) - { - if ($query) { - $function['filter'] = $query->toArray(); - } - } - - /** - * Creates field_value_factor function. - * - * @param string $field - * @param float $factor - * @param string $modifier - * @param BuilderInterface $query - * - * @return $this - */ - public function addFieldValueFactorFunction($field, $factor, $modifier = 'none', BuilderInterface $query = null) - { - $function = [ - 'field_value_factor' => [ - 'field' => $field, - 'factor' => $factor, - 'modifier' => $modifier, - ], - ]; - - $this->applyFilter($function, $query); - - $this->functions[] = $function; - - return $this; - } - - /** - * Add decay function to function score. Weight and query are optional. - * - * @param string $type - * @param string $field - * @param array $function - * @param array $options - * @param BuilderInterface $query - * - * @return $this - */ - public function addDecayFunction( - $type, - $field, - array $function, - array $options = [], - BuilderInterface $query = null - ) { - $function = [ - $type => array_merge( - [$field => $function], - $options - ), - ]; - - $this->applyFilter($function, $query); - - $this->functions[] = $function; - - return $this; - } - - /** - * Adds function to function score without decay function. Influence search score only for specific query. - * - * @param float $weight - * @param BuilderInterface $query - * - * @return $this - */ - public function addWeightFunction($weight, BuilderInterface $query = null) - { - $function = [ - 'weight' => $weight, - ]; - - $this->applyFilter($function, $query); - - $this->functions[] = $function; - - return $this; - } - - /** - * Adds random score function. Seed is optional. - * - * @param mixed $seed - * @param BuilderInterface $query - * - * @return $this - */ - public function addRandomFunction($seed = null, BuilderInterface $query = null) - { - $function = [ - 'random_score' => $seed ? [ 'seed' => $seed ] : new \stdClass(), - ]; - - $this->applyFilter($function, $query); - - $this->functions[] = $function; - - return $this; - } - - /** - * Adds script score function. - * - * @param string $script - * @param array $params - * @param array $options - * @param BuilderInterface $query - * - * @return $this - */ - public function addScriptScoreFunction( - $script, - array $params = [], - array $options = [], - BuilderInterface $query = null - ) { - $function = [ - 'script_score' => array_merge( - [ - 'script' => $script, - 'params' => $params, - ], - $options - ), - ]; - - $this->applyFilter($function, $query); - - $this->functions[] = $function; - - return $this; - } - - /** - * Adds custom simple function. You can add to the array whatever you want. - * - * @param array $function - * - * @return $this - */ - public function addSimpleFunction(array $function) - { - $this->functions[] = $function; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - 'query' => $this->query->toArray(), - 'functions' => $this->functions, - ]; - - $output = $this->processArray($query); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/FuzzyQuery.php b/src/Query/FuzzyQuery.php index c5feb00..81bf38f 100644 --- a/src/Query/FuzzyQuery.php +++ b/src/Query/FuzzyQuery.php @@ -11,61 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "fuzzy" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class FuzzyQuery implements BuilderInterface +class FuzzyQuery extends \ONGR\ElasticsearchDSL\Query\TermLevel\FuzzyQuery { - use ParametersTrait; - - /** - * @var string - */ - private $field; - - /** - * @var string - */ - private $value; - - /** - * @param string $field - * @param string $value - * @param array $parameters - */ - public function __construct($field, $value, array $parameters = []) - { - $this->field = $field; - $this->value = $value; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'fuzzy'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - 'value' => $this->value, - ]; - - $output = [ - $this->field => $this->processArray($query), - ]; - - return [$this->getType() => $output]; - } } diff --git a/src/Query/Geo/GeoBoundingBoxQuery.php b/src/Query/Geo/GeoBoundingBoxQuery.php new file mode 100644 index 0000000..1429cfa --- /dev/null +++ b/src/Query/Geo/GeoBoundingBoxQuery.php @@ -0,0 +1,85 @@ +<?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\Query\Geo; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "geo_bounding_box" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html + */ +class GeoBoundingBoxQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var array + */ + private $values; + + /** + * @var string + */ + private $field; + + /** + * @param string $field + * @param array $values + * @param array $parameters + */ + public function __construct($field, $values, array $parameters = []) + { + $this->field = $field; + $this->values = $values; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'geo_bounding_box'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + if (count($this->values) === 2) { + $query = [ + $this->field => [ + 'top_left' => $this->values[0], + 'bottom_right' => $this->values[1], + ], + ]; + } elseif (count($this->values) === 4) { + $query = [ + $this->field => [ + 'top' => $this->values[0], + 'left' => $this->values[1], + 'bottom' => $this->values[2], + 'right' => $this->values[3], + ], + ]; + } else { + throw new \LogicException('Geo Bounding Box filter must have 2 or 4 geo points set.'); + } + + $output = $this->processArray($query); + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/Geo/GeoDistanceQuery.php b/src/Query/Geo/GeoDistanceQuery.php new file mode 100644 index 0000000..03cf202 --- /dev/null +++ b/src/Query/Geo/GeoDistanceQuery.php @@ -0,0 +1,77 @@ +<?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\Query\Geo; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "geo_distance" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html + */ +class GeoDistanceQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string + */ + private $field; + + /** + * @var string + */ + private $distance; + + /** + * @var mixed + */ + private $location; + + /** + * @param string $field + * @param string $distance + * @param mixed $location + * @param array $parameters + */ + public function __construct($field, $distance, $location, array $parameters = []) + { + $this->field = $field; + $this->distance = $distance; + $this->location = $location; + + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'geo_distance'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [ + 'distance' => $this->distance, + $this->field => $this->location, + ]; + $output = $this->processArray($query); + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/Geo/GeoDistanceRangeQuery.php b/src/Query/Geo/GeoDistanceRangeQuery.php new file mode 100644 index 0000000..8cced0c --- /dev/null +++ b/src/Query/Geo/GeoDistanceRangeQuery.php @@ -0,0 +1,74 @@ +<?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\Query\Geo; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "geo_distance_range" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-range-query.html + */ +class GeoDistanceRangeQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string + */ + private $field; + + /** + * @var array + */ + private $range; + + /** + * @var mixed + */ + private $location; + + /** + * @param string $field + * @param array $range + * @param mixed $location + * @param array $parameters + */ + public function __construct($field, $range, $location, array $parameters = []) + { + $this->field = $field; + $this->range = $range; + $this->location = $location; + + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'geo_distance_range'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = $this->range + [$this->field => $this->location]; + $output = $this->processArray($query); + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/Geo/GeoPolygonQuery.php b/src/Query/Geo/GeoPolygonQuery.php new file mode 100644 index 0000000..51a9c4e --- /dev/null +++ b/src/Query/Geo/GeoPolygonQuery.php @@ -0,0 +1,66 @@ +<?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\Query\Geo; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "geo_polygon" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-polygon-query.html + */ +class GeoPolygonQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string + */ + private $field; + + /** + * @var array + */ + private $points; + + /** + * @param string $field + * @param array $points + * @param array $parameters + */ + public function __construct($field, array $points = [], array $parameters = []) + { + $this->field = $field; + $this->points = $points; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'geo_polygon'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [$this->field => ['points' => $this->points]]; + $output = $this->processArray($query); + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/Geo/GeoShapeQuery.php b/src/Query/Geo/GeoShapeQuery.php new file mode 100644 index 0000000..9e51537 --- /dev/null +++ b/src/Query/Geo/GeoShapeQuery.php @@ -0,0 +1,102 @@ +<?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\Query\Geo; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "geo_shape" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html + */ +class GeoShapeQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var array + */ + private $fields = []; + + /** + * @param array $parameters + */ + public function __construct(array $parameters = []) + { + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'geo_shape'; + } + + /** + * Add geo-shape provided filter. + * + * @param string $field Field name. + * @param string $type Shape type. + * @param array $coordinates Shape coordinates. + * @param array $parameters Additional parameters. + */ + public function addShape($field, $type, array $coordinates, array $parameters = []) + { + $filter = array_merge( + $parameters, + [ + 'type' => $type, + 'coordinates' => $coordinates, + ] + ); + + $this->fields[$field]['shape'] = $filter; + } + + /** + * Add geo-shape pre-indexed filter. + * + * @param string $field Field name. + * @param string $id The ID of the document that containing the pre-indexed shape. + * @param string $type Name of the index where the pre-indexed shape is. + * @param string $index Index type where the pre-indexed shape is. + * @param string $path The field specified as path containing the pre-indexed shape. + * @param array $parameters Additional parameters. + */ + public function addPreIndexedShape($field, $id, $type, $index, $path, array $parameters = []) + { + $filter = array_merge( + $parameters, + [ + 'id' => $id, + 'type' => $type, + 'index' => $index, + 'path' => $path, + ] + ); + + $this->fields[$field]['indexed_shape'] = $filter; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $output = $this->processArray($this->fields); + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/GeoBoundingBoxQuery.php b/src/Query/GeoBoundingBoxQuery.php index 2940eb7..7e00821 100644 --- a/src/Query/GeoBoundingBoxQuery.php +++ b/src/Query/GeoBoundingBoxQuery.php @@ -11,75 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "geo_bounding_box" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class GeoBoundingBoxQuery implements BuilderInterface +class GeoBoundingBoxQuery extends \ONGR\ElasticsearchDSL\Query\Geo\GeoBoundingBoxQuery { - use ParametersTrait; - - /** - * @var array - */ - private $values; - - /** - * @var string - */ - private $field; - - /** - * @param string $field - * @param array $values - * @param array $parameters - */ - public function __construct($field, $values, array $parameters = []) - { - $this->field = $field; - $this->values = $values; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'geo_bounding_box'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - if (count($this->values) === 2) { - $query = [ - $this->field => [ - 'top_left' => $this->values[0], - 'bottom_right' => $this->values[1], - ], - ]; - } elseif (count($this->values) === 4) { - $query = [ - $this->field => [ - 'top' => $this->values[0], - 'left' => $this->values[1], - 'bottom' => $this->values[2], - 'right' => $this->values[3], - ], - ]; - } else { - throw new \LogicException('Geo Bounding Box filter must have 2 or 4 geo points set.'); - } - - $output = $this->processArray($query); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/GeoDistanceQuery.php b/src/Query/GeoDistanceQuery.php index d124179..eca616a 100644 --- a/src/Query/GeoDistanceQuery.php +++ b/src/Query/GeoDistanceQuery.php @@ -11,67 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "geo_distance" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class GeoDistanceQuery implements BuilderInterface +class GeoDistanceQuery extends \ONGR\ElasticsearchDSL\Query\Geo\GeoDistanceQuery { - use ParametersTrait; - - /** - * @var string - */ - private $field; - - /** - * @var string - */ - private $distance; - - /** - * @var mixed - */ - private $location; - - /** - * @param string $field - * @param string $distance - * @param mixed $location - * @param array $parameters - */ - public function __construct($field, $distance, $location, array $parameters = []) - { - $this->field = $field; - $this->distance = $distance; - $this->location = $location; - - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'geo_distance'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - 'distance' => $this->distance, - $this->field => $this->location, - ]; - $output = $this->processArray($query); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/GeoDistanceRangeQuery.php b/src/Query/GeoDistanceRangeQuery.php index ef3544d..d5457ac 100644 --- a/src/Query/GeoDistanceRangeQuery.php +++ b/src/Query/GeoDistanceRangeQuery.php @@ -11,64 +11,11 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "geo_distance_range" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-range-query.html */ -class GeoDistanceRangeQuery implements BuilderInterface +class GeoDistanceRangeQuery extends \ONGR\ElasticsearchDSL\Query\Geo\GeoDistanceRangeQuery { - use ParametersTrait; - - /** - * @var string - */ - private $field; - - /** - * @var array - */ - private $range; - - /** - * @var mixed - */ - private $location; - - /** - * @param string $field - * @param array $range - * @param mixed $location - * @param array $parameters - */ - public function __construct($field, $range, $location, array $parameters = []) - { - $this->field = $field; - $this->range = $range; - $this->location = $location; - - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'geo_distance_range'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = $this->range + [$this->field => $this->location]; - $output = $this->processArray($query); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/GeoPolygonQuery.php b/src/Query/GeoPolygonQuery.php index eecf0e4..3bc9158 100644 --- a/src/Query/GeoPolygonQuery.php +++ b/src/Query/GeoPolygonQuery.php @@ -11,56 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "geo_polygon" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-polygon-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class GeoPolygonQuery implements BuilderInterface +class GeoPolygonQuery extends \ONGR\ElasticsearchDSL\Query\Geo\GeoPolygonQuery { - use ParametersTrait; - - /** - * @var string - */ - private $field; - - /** - * @var array - */ - private $points; - - /** - * @param string $field - * @param array $points - * @param array $parameters - */ - public function __construct($field, array $points = [], array $parameters = []) - { - $this->field = $field; - $this->points = $points; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'geo_polygon'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [$this->field => ['points' => $this->points]]; - $output = $this->processArray($query); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/GeoShapeQuery.php b/src/Query/GeoShapeQuery.php index ae481db..02efa21 100644 --- a/src/Query/GeoShapeQuery.php +++ b/src/Query/GeoShapeQuery.php @@ -11,92 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "geo_shape" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class GeoShapeQuery implements BuilderInterface +class GeoShapeQuery extends \ONGR\ElasticsearchDSL\Query\Geo\GeoShapeQuery { - use ParametersTrait; - - /** - * @var array - */ - private $fields = []; - - /** - * @param array $parameters - */ - public function __construct(array $parameters = []) - { - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'geo_shape'; - } - - /** - * Add geo-shape provided filter. - * - * @param string $field Field name. - * @param string $type Shape type. - * @param array $coordinates Shape coordinates. - * @param array $parameters Additional parameters. - */ - public function addShape($field, $type, array $coordinates, array $parameters = []) - { - $filter = array_merge( - $parameters, - [ - 'type' => $type, - 'coordinates' => $coordinates, - ] - ); - - $this->fields[$field]['shape'] = $filter; - } - - /** - * Add geo-shape pre-indexed filter. - * - * @param string $field Field name. - * @param string $id The ID of the document that containing the pre-indexed shape. - * @param string $type Name of the index where the pre-indexed shape is. - * @param string $index Index type where the pre-indexed shape is. - * @param string $path The field specified as path containing the pre-indexed shape. - * @param array $parameters Additional parameters. - */ - public function addPreIndexedShape($field, $id, $type, $index, $path, array $parameters = []) - { - $filter = array_merge( - $parameters, - [ - 'id' => $id, - 'type' => $type, - 'index' => $index, - 'path' => $path, - ] - ); - - $this->fields[$field]['indexed_shape'] = $filter; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $output = $this->processArray($this->fields); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/GeohashCellQuery.php b/src/Query/GeohashCellQuery.php index 9aa15c7..d792507 100644 --- a/src/Query/GeohashCellQuery.php +++ b/src/Query/GeohashCellQuery.php @@ -18,6 +18,7 @@ use ONGR\ElasticsearchDSL\ParametersTrait; * Represents Elasticsearch "geohash_cell" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geohash-cell-query.html + * @deprecated Use geohash_grid aggregation instead. */ class GeohashCellQuery implements BuilderInterface { diff --git a/src/Query/HasChildQuery.php b/src/Query/HasChildQuery.php index 9e5044e..f231cfc 100644 --- a/src/Query/HasChildQuery.php +++ b/src/Query/HasChildQuery.php @@ -11,60 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "has_child" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class HasChildQuery implements BuilderInterface +class HasChildQuery extends \ONGR\ElasticsearchDSL\Query\Joining\HasChildQuery { - use ParametersTrait; - - /** - * @var string - */ - private $type; - - /** - * @var BuilderInterface - */ - private $query; - - /** - * @param string $type - * @param BuilderInterface $query - * @param array $parameters - */ - public function __construct($type, BuilderInterface $query, array $parameters = []) - { - $this->type = $type; - $this->query = $query; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'has_child'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - 'type' => $this->type, - 'query' => $this->query->toArray(), - ]; - - $output = $this->processArray($query); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/HasParentQuery.php b/src/Query/HasParentQuery.php index c57bca4..490a43d 100644 --- a/src/Query/HasParentQuery.php +++ b/src/Query/HasParentQuery.php @@ -11,60 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "has_parent" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-parent-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class HasParentQuery implements BuilderInterface +class HasParentQuery extends \ONGR\ElasticsearchDSL\Query\Joining\HasParentQuery { - use ParametersTrait; - - /** - * @var string - */ - private $parentType; - - /** - * @var BuilderInterface - */ - private $query; - - /** - * @param string $parentType - * @param BuilderInterface $query - * @param array $parameters - */ - public function __construct($parentType, BuilderInterface $query, array $parameters = []) - { - $this->parentType = $parentType; - $this->query = $query; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'has_parent'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - 'parent_type' => $this->parentType, - 'query' => $this->query->toArray(), - ]; - - $output = $this->processArray($query); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/IdsQuery.php b/src/Query/IdsQuery.php index fa55b73..bec0576 100644 --- a/src/Query/IdsQuery.php +++ b/src/Query/IdsQuery.php @@ -11,52 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "ids" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class IdsQuery implements BuilderInterface +class IdsQuery extends \ONGR\ElasticsearchDSL\Query\TermLevel\IdsQuery { - use ParametersTrait; - - /** - * @var array - */ - private $values; - - /** - * @param array $values - * @param array $parameters - */ - public function __construct(array $values, array $parameters = []) - { - $this->values = $values; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'ids'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - 'values' => $this->values, - ]; - - $output = $this->processArray($query); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/IndicesQuery.php b/src/Query/IndicesQuery.php index 30160b7..8244aaf 100644 --- a/src/Query/IndicesQuery.php +++ b/src/Query/IndicesQuery.php @@ -11,71 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; - /** * Represents Elasticsearch "indices" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-indices-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class IndicesQuery implements BuilderInterface +class IndicesQuery extends \ONGR\ElasticsearchDSL\Query\Compound\IndicesQuery { - /** - * @var string[] - */ - private $indices; - - /** - * @var BuilderInterface - */ - private $query; - - /** - * @var string|BuilderInterface - */ - private $noMatchQuery; - - /** - * @param string[] $indices - * @param BuilderInterface $query - * @param BuilderInterface $noMatchQuery - */ - public function __construct($indices, $query, $noMatchQuery = null) - { - $this->indices = $indices; - $this->query = $query; - $this->noMatchQuery = $noMatchQuery; - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'indices'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - if (count($this->indices) > 1) { - $output = ['indices' => $this->indices]; - } else { - $output = ['index' => $this->indices[0]]; - } - - $output['query'] = $this->query->toArray(); - - if ($this->noMatchQuery !== null) { - if (is_a($this->noMatchQuery, 'ONGR\ElasticsearchDSL\BuilderInterface')) { - $output['no_match_query'] = $this->noMatchQuery->toArray(); - } else { - $output['no_match_query'] = $this->noMatchQuery; - } - } - - return [$this->getType() => $output]; - } } diff --git a/src/Query/Joining/HasChildQuery.php b/src/Query/Joining/HasChildQuery.php new file mode 100644 index 0000000..4682f8c --- /dev/null +++ b/src/Query/Joining/HasChildQuery.php @@ -0,0 +1,70 @@ +<?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\Query\Joining; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "has_child" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html + */ +class HasChildQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string + */ + private $type; + + /** + * @var BuilderInterface + */ + private $query; + + /** + * @param string $type + * @param BuilderInterface $query + * @param array $parameters + */ + public function __construct($type, BuilderInterface $query, array $parameters = []) + { + $this->type = $type; + $this->query = $query; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'has_child'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [ + 'type' => $this->type, + 'query' => $this->query->toArray(), + ]; + + $output = $this->processArray($query); + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/Joining/HasParentQuery.php b/src/Query/Joining/HasParentQuery.php new file mode 100644 index 0000000..a575639 --- /dev/null +++ b/src/Query/Joining/HasParentQuery.php @@ -0,0 +1,70 @@ +<?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\Query\Joining; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "has_parent" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-parent-query.html + */ +class HasParentQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string + */ + private $parentType; + + /** + * @var BuilderInterface + */ + private $query; + + /** + * @param string $parentType + * @param BuilderInterface $query + * @param array $parameters + */ + public function __construct($parentType, BuilderInterface $query, array $parameters = []) + { + $this->parentType = $parentType; + $this->query = $query; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'has_parent'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [ + 'parent_type' => $this->parentType, + 'query' => $this->query->toArray(), + ]; + + $output = $this->processArray($query); + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/Joining/NestedQuery.php b/src/Query/Joining/NestedQuery.php new file mode 100644 index 0000000..4db5960 --- /dev/null +++ b/src/Query/Joining/NestedQuery.php @@ -0,0 +1,70 @@ +<?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\Query\Joining; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "nested" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html + */ +class NestedQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string + */ + private $path; + + /** + * @var BuilderInterface + */ + private $query; + + /** + * @param string $path + * @param BuilderInterface $query + * @param array $parameters + */ + public function __construct($path, BuilderInterface $query, array $parameters = []) + { + $this->path = $path; + $this->query = $query; + $this->parameters = $parameters; + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'nested'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + return [ + $this->getType() => $this->processArray( + [ + 'path' => $this->path, + 'query' => $this->query->toArray(), + ] + ) + ]; + } +} diff --git a/src/Query/LimitQuery.php b/src/Query/LimitQuery.php index ced3a81..fb65d32 100644 --- a/src/Query/LimitQuery.php +++ b/src/Query/LimitQuery.php @@ -17,6 +17,7 @@ use ONGR\ElasticsearchDSL\BuilderInterface; * Represents Elasticsearch "limit" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-limit-query.html + * @deprecated Use the terminate_after parameter instead. */ class LimitQuery implements BuilderInterface { diff --git a/src/Query/MatchPhrasePrefixQuery.php b/src/Query/MatchPhrasePrefixQuery.php index 8d2d61c..d6a1a5d 100644 --- a/src/Query/MatchPhrasePrefixQuery.php +++ b/src/Query/MatchPhrasePrefixQuery.php @@ -14,16 +14,10 @@ namespace ONGR\ElasticsearchDSL\Query; /** * Represents Elasticsearch "match_phrase_prefix" query. * - * @author Ron Rademaker * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class MatchPhrasePrefixQuery extends MatchQuery +class MatchPhrasePrefixQuery extends \ONGR\ElasticsearchDSL\Query\FullText\MatchPhrasePrefixQuery { - /** - * {@inheritdoc} - */ - public function getType() - { - return 'match_phrase_prefix'; - } } diff --git a/src/Query/MatchPhraseQuery.php b/src/Query/MatchPhraseQuery.php index e6f222d..52a1278 100644 --- a/src/Query/MatchPhraseQuery.php +++ b/src/Query/MatchPhraseQuery.php @@ -16,14 +16,10 @@ namespace ONGR\ElasticsearchDSL\Query; * * @author Ron Rademaker * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. + * */ -class MatchPhraseQuery extends MatchQuery +class MatchPhraseQuery extends \ONGR\ElasticsearchDSL\Query\FullText\MatchPhraseQuery { - /** - * {@inheritdoc} - */ - public function getType() - { - return 'match_phrase'; - } } diff --git a/src/Query/MatchQuery.php b/src/Query/MatchQuery.php index 0bc220e..98f19ab 100644 --- a/src/Query/MatchQuery.php +++ b/src/Query/MatchQuery.php @@ -11,61 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "match" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class MatchQuery implements BuilderInterface +class MatchQuery extends \ONGR\ElasticsearchDSL\Query\FullText\MatchQuery { - use ParametersTrait; - - /** - * @var string - */ - private $field; - - /** - * @var string - */ - private $query; - - /** - * @param string $field - * @param string $query - * @param array $parameters - */ - public function __construct($field, $query, array $parameters = []) - { - $this->field = $field; - $this->query = $query; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'match'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - 'query' => $this->query, - ]; - - $output = [ - $this->field => $this->processArray($query), - ]; - - return [$this->getType() => $output]; - } } diff --git a/src/Query/MissingQuery.php b/src/Query/MissingQuery.php index f6d0f3b..d95f2b0 100644 --- a/src/Query/MissingQuery.php +++ b/src/Query/MissingQuery.php @@ -18,6 +18,7 @@ use ONGR\ElasticsearchDSL\ParametersTrait; * Represents Elasticsearch "missing" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-missing-query.html + * @deprecated Use a negated exists query instead. (Also removed _missing_ from the query_string query). */ class MissingQuery implements BuilderInterface { diff --git a/src/Query/MoreLikeThisQuery.php b/src/Query/MoreLikeThisQuery.php index 7be6953..351accd 100644 --- a/src/Query/MoreLikeThisQuery.php +++ b/src/Query/MoreLikeThisQuery.php @@ -11,54 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "more_like_this" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class MoreLikeThisQuery implements BuilderInterface +class MoreLikeThisQuery extends \ONGR\ElasticsearchDSL\Query\Specialized\MoreLikeThisQuery { - use ParametersTrait; - - /** - * @var string The text to find documents like it, required if ids or docs are not specified. - */ - private $like; - - /** - * @param string $like - * @param array $parameters - */ - public function __construct($like, array $parameters = []) - { - $this->like = $like; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'more_like_this'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = []; - - if (($this->hasParameter('ids') === false) || ($this->hasParameter('docs') === false)) { - $query['like'] = $this->like; - } - - $output = $this->processArray($query); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/MultiMatchQuery.php b/src/Query/MultiMatchQuery.php index 35d0eec..63dcd3f 100644 --- a/src/Query/MultiMatchQuery.php +++ b/src/Query/MultiMatchQuery.php @@ -11,60 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "multi_match" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class MultiMatchQuery implements BuilderInterface +class MultiMatchQuery extends \ONGR\ElasticsearchDSL\Query\FullText\MultiMatchQuery { - use ParametersTrait; - - /** - * @var array - */ - private $fields = []; - - /** - * @var string - */ - private $query; - - /** - * @param array $fields - * @param string $query - * @param array $parameters - */ - public function __construct(array $fields, $query, array $parameters = []) - { - $this->fields = $fields; - $this->query = $query; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'multi_match'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - 'fields' => $this->fields, - 'query' => $this->query, - ]; - - $output = $this->processArray($query); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/NestedQuery.php b/src/Query/NestedQuery.php index 0b020c9..8080859 100644 --- a/src/Query/NestedQuery.php +++ b/src/Query/NestedQuery.php @@ -11,60 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "nested" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class NestedQuery implements BuilderInterface +class NestedQuery extends \ONGR\ElasticsearchDSL\Query\Joining\NestedQuery { - use ParametersTrait; - - /** - * @var string - */ - private $path; - - /** - * @var BuilderInterface - */ - private $query; - - /** - * @param string $path - * @param BuilderInterface $query - * @param array $parameters - */ - public function __construct($path, BuilderInterface $query, array $parameters = []) - { - $this->path = $path; - $this->query = $query; - $this->parameters = $parameters; - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'nested'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - return [ - $this->getType() => $this->processArray( - [ - 'path' => $this->path, - 'query' => $this->query->toArray(), - ] - ) - ]; - } } diff --git a/src/Query/PrefixQuery.php b/src/Query/PrefixQuery.php index 903218f..1d1f0f1 100644 --- a/src/Query/PrefixQuery.php +++ b/src/Query/PrefixQuery.php @@ -11,61 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "prefix" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class PrefixQuery implements BuilderInterface +class PrefixQuery extends \ONGR\ElasticsearchDSL\Query\TermLevel\PrefixQuery { - use ParametersTrait; - - /** - * @var string - */ - protected $field; - - /** - * @var string - */ - protected $value; - - /** - * @param string $field Field name. - * @param string $value Value. - * @param array $parameters Optional parameters. - */ - public function __construct($field, $value, array $parameters = []) - { - $this->field = $field; - $this->value = $value; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'prefix'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - 'value' => $this->value, - ]; - - $output = [ - $this->field => $this->processArray($query), - ]; - - return [$this->getType() => $output]; - } } diff --git a/src/Query/QueryStringQuery.php b/src/Query/QueryStringQuery.php index 3a8cb52..3a3828e 100644 --- a/src/Query/QueryStringQuery.php +++ b/src/Query/QueryStringQuery.php @@ -11,52 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "query_string" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class QueryStringQuery implements BuilderInterface +class QueryStringQuery extends \ONGR\ElasticsearchDSL\Query\FullText\QueryStringQuery { - use ParametersTrait; - - /** - * @var string The actual query to be parsed. - */ - private $query; - - /** - * @param string $query - * @param array $parameters - */ - public function __construct($query, array $parameters = []) - { - $this->query = $query; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'query_string'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - 'query' => $this->query, - ]; - - $output = $this->processArray($query); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/RangeQuery.php b/src/Query/RangeQuery.php index 486009d..e96a830 100644 --- a/src/Query/RangeQuery.php +++ b/src/Query/RangeQuery.php @@ -11,67 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "range" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class RangeQuery implements BuilderInterface +class RangeQuery extends \ONGR\ElasticsearchDSL\Query\TermLevel\RangeQuery { - use ParametersTrait; - - /** - * Range control names. - */ - const LT = 'lt'; - const GT = 'gt'; - const LTE = 'lte'; - const GTE = 'gte'; - - /** - * @var string Field name. - */ - private $field; - - /** - * @param string $field - * @param array $parameters - */ - public function __construct($field, array $parameters = []) - { - $this->setParameters($parameters); - - if ($this->hasParameter(self::GTE) && $this->hasParameter(self::GT)) { - unset($this->parameters[self::GT]); - } - - if ($this->hasParameter(self::LTE) && $this->hasParameter(self::LT)) { - unset($this->parameters[self::LT]); - } - - $this->field = $field; - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'range'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $output = [ - $this->field => $this->getParameters(), - ]; - - return [$this->getType() => $output]; - } } diff --git a/src/Query/RegexpQuery.php b/src/Query/RegexpQuery.php index 0752503..6f6a669 100644 --- a/src/Query/RegexpQuery.php +++ b/src/Query/RegexpQuery.php @@ -11,61 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "regexp" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class RegexpQuery implements BuilderInterface +class RegexpQuery extends \ONGR\ElasticsearchDSL\Query\TermLevel\RegexpQuery { - use ParametersTrait; - - /** - * @var string Field to be queried. - */ - private $field; - - /** - * @var string The actual regexp value to be used. - */ - private $regexpValue; - - /** - * @param string $field - * @param string $regexpValue - * @param array $parameters - */ - public function __construct($field, $regexpValue, array $parameters = []) - { - $this->field = $field; - $this->regexpValue = $regexpValue; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'regexp'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - 'value' => $this->regexpValue, - ]; - - $output = [ - $this->field => $this->processArray($query), - ]; - - return [$this->getType() => $output]; - } } diff --git a/src/Query/ScriptQuery.php b/src/Query/ScriptQuery.php index 1a1a0c7..42d82f7 100644 --- a/src/Query/ScriptQuery.php +++ b/src/Query/ScriptQuery.php @@ -11,49 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "script" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class ScriptQuery implements BuilderInterface +class ScriptQuery extends \ONGR\ElasticsearchDSL\Query\Specialized\ScriptQuery { - use ParametersTrait; - - /** - * @var string - */ - private $script; - - /** - * @param string $script Script - * @param array $parameters Optional parameters - */ - public function __construct($script, array $parameters = []) - { - $this->script = $script; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'script'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = ['inline' => $this->script]; - $output = $this->processArray($query); - - return [$this->getType() => ['script' => $output]]; - } } diff --git a/src/Query/SimpleQueryStringQuery.php b/src/Query/SimpleQueryStringQuery.php index a110c5b..6e529f8 100644 --- a/src/Query/SimpleQueryStringQuery.php +++ b/src/Query/SimpleQueryStringQuery.php @@ -18,45 +18,9 @@ use ONGR\ElasticsearchDSL\ParametersTrait; * Represents Elasticsearch "simple_query_string" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class SimpleQueryStringQuery implements BuilderInterface +class SimpleQueryStringQuery extends \ONGR\ElasticsearchDSL\Query\FullText\SimpleQueryStringQuery { - use ParametersTrait; - - /** - * @var string The actual query to be parsed. - */ - private $query; - - /** - * @param string $query - * @param array $parameters - */ - public function __construct($query, array $parameters = []) - { - $this->query = $query; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'simple_query_string'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - 'query' => $this->query, - ]; - - $output = $this->processArray($query); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/Specialized/MoreLikeThisQuery.php b/src/Query/Specialized/MoreLikeThisQuery.php new file mode 100644 index 0000000..af721ad --- /dev/null +++ b/src/Query/Specialized/MoreLikeThisQuery.php @@ -0,0 +1,64 @@ +<?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\Query\Specialized; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "more_like_this" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html + */ +class MoreLikeThisQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string The text to find documents like it, required if ids or docs are not specified. + */ + private $like; + + /** + * @param string $like + * @param array $parameters + */ + public function __construct($like, array $parameters = []) + { + $this->like = $like; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'more_like_this'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = []; + + if (($this->hasParameter('ids') === false) || ($this->hasParameter('docs') === false)) { + $query['like'] = $this->like; + } + + $output = $this->processArray($query); + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/Specialized/ScriptQuery.php b/src/Query/Specialized/ScriptQuery.php new file mode 100644 index 0000000..07ef289 --- /dev/null +++ b/src/Query/Specialized/ScriptQuery.php @@ -0,0 +1,59 @@ +<?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\Query\Specialized; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "script" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html + */ +class ScriptQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string + */ + private $script; + + /** + * @param string $script Script + * @param array $parameters Optional parameters + */ + public function __construct($script, array $parameters = []) + { + $this->script = $script; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'script'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = ['inline' => $this->script]; + $output = $this->processArray($query); + + return [$this->getType() => ['script' => $output]]; + } +} diff --git a/src/Query/Specialized/TemplateQuery.php b/src/Query/Specialized/TemplateQuery.php new file mode 100644 index 0000000..b8b122d --- /dev/null +++ b/src/Query/Specialized/TemplateQuery.php @@ -0,0 +1,132 @@ +<?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\Query\Specialized; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "template" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-template-query.html + */ +class TemplateQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string + */ + private $file; + + /** + * @var string + */ + private $inline; + + /** + * @var array + */ + private $params; + + /** + * @param string $file A template of the query + * @param string $inline A template of the query + * @param array $params Parameters to insert into template + */ + public function __construct($file = null, $inline = null, array $params = []) + { + $this->setFile($file); + $this->setInline($inline); + $this->setParams($params); + } + + /** + * @return string + */ + public function getFile() + { + return $this->file; + } + + /** + * @param string $file + */ + public function setFile($file) + { + $this->file = $file; + } + + /** + * @return string + */ + public function getInline() + { + return $this->inline; + } + + /** + * @param string $inline + */ + public function setInline($inline) + { + $this->inline = $inline; + } + + /** + * @return array + */ + public function getParams() + { + return $this->params; + } + + /** + * @param array $params + */ + public function setParams($params) + { + $this->params = $params; + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'template'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $output = array_filter( + [ + 'file' => $this->getFile(), + 'inline' => $this->getInline(), + 'params' => $this->getParams(), + ] + ); + + if (!isset($output['file']) && !isset($output['inline'])) { + throw new \InvalidArgumentException( + 'Template query requires that either `inline` or `file` parameters are set' + ); + } + + $output = $this->processArray($output); + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/TemplateQuery.php b/src/Query/TemplateQuery.php index 4719ac5..d90aae4 100644 --- a/src/Query/TemplateQuery.php +++ b/src/Query/TemplateQuery.php @@ -11,122 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "template" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-template-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class TemplateQuery implements BuilderInterface +class TemplateQuery extends \ONGR\ElasticsearchDSL\Query\Specialized\TemplateQuery { - use ParametersTrait; - - /** - * @var string - */ - private $file; - - /** - * @var string - */ - private $inline; - - /** - * @var array - */ - private $params; - - /** - * @param string $file A template of the query - * @param string $inline A template of the query - * @param array $params Parameters to insert into template - */ - public function __construct($file = null, $inline = null, array $params = []) - { - $this->setFile($file); - $this->setInline($inline); - $this->setParams($params); - } - - /** - * @return string - */ - public function getFile() - { - return $this->file; - } - - /** - * @param string $file - */ - public function setFile($file) - { - $this->file = $file; - } - - /** - * @return string - */ - public function getInline() - { - return $this->inline; - } - - /** - * @param string $inline - */ - public function setInline($inline) - { - $this->inline = $inline; - } - - /** - * @return array - */ - public function getParams() - { - return $this->params; - } - - /** - * @param array $params - */ - public function setParams($params) - { - $this->params = $params; - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'template'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $output = array_filter( - [ - 'file' => $this->getFile(), - 'inline' => $this->getInline(), - 'params' => $this->getParams(), - ] - ); - - if (!isset($output['file']) && !isset($output['inline'])) { - throw new \InvalidArgumentException( - 'Template query requires that either `inline` or `file` parameters are set' - ); - } - - $output = $this->processArray($output); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/TermLevel/ExistsQuery.php b/src/Query/TermLevel/ExistsQuery.php new file mode 100644 index 0000000..f72e6e5 --- /dev/null +++ b/src/Query/TermLevel/ExistsQuery.php @@ -0,0 +1,57 @@ +<?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\Query\TermLevel; + +use ONGR\ElasticsearchDSL\BuilderInterface; + +/** + * Represents Elasticsearch "exists" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html + */ +class ExistsQuery implements BuilderInterface +{ + /** + * @var string + */ + private $field; + + /** + * Constructor. + * + * @param string $field Field value + */ + public function __construct($field) + { + $this->field = $field; + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'exists'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + return [ + $this->getType() => [ + 'field' => $this->field, + ], + ]; + } +} diff --git a/src/Query/TermLevel/FuzzyQuery.php b/src/Query/TermLevel/FuzzyQuery.php new file mode 100644 index 0000000..e7e7535 --- /dev/null +++ b/src/Query/TermLevel/FuzzyQuery.php @@ -0,0 +1,71 @@ +<?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\Query\TermLevel; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "fuzzy" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html + */ +class FuzzyQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string + */ + private $field; + + /** + * @var string + */ + private $value; + + /** + * @param string $field + * @param string $value + * @param array $parameters + */ + public function __construct($field, $value, array $parameters = []) + { + $this->field = $field; + $this->value = $value; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'fuzzy'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [ + 'value' => $this->value, + ]; + + $output = [ + $this->field => $this->processArray($query), + ]; + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/TermLevel/IdsQuery.php b/src/Query/TermLevel/IdsQuery.php new file mode 100644 index 0000000..29edd33 --- /dev/null +++ b/src/Query/TermLevel/IdsQuery.php @@ -0,0 +1,62 @@ +<?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\Query\TermLevel; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "ids" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html + */ +class IdsQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var array + */ + private $values; + + /** + * @param array $values + * @param array $parameters + */ + public function __construct(array $values, array $parameters = []) + { + $this->values = $values; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'ids'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [ + 'values' => $this->values, + ]; + + $output = $this->processArray($query); + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/TermLevel/PrefixQuery.php b/src/Query/TermLevel/PrefixQuery.php new file mode 100644 index 0000000..c453121 --- /dev/null +++ b/src/Query/TermLevel/PrefixQuery.php @@ -0,0 +1,71 @@ +<?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\Query\TermLevel; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "prefix" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html + */ +class PrefixQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string + */ + protected $field; + + /** + * @var string + */ + protected $value; + + /** + * @param string $field Field name. + * @param string $value Value. + * @param array $parameters Optional parameters. + */ + public function __construct($field, $value, array $parameters = []) + { + $this->field = $field; + $this->value = $value; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'prefix'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [ + 'value' => $this->value, + ]; + + $output = [ + $this->field => $this->processArray($query), + ]; + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/TermLevel/RangeQuery.php b/src/Query/TermLevel/RangeQuery.php new file mode 100644 index 0000000..9a33142 --- /dev/null +++ b/src/Query/TermLevel/RangeQuery.php @@ -0,0 +1,77 @@ +<?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\Query\TermLevel; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "range" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html + */ +class RangeQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * Range control names. + */ + const LT = 'lt'; + const GT = 'gt'; + const LTE = 'lte'; + const GTE = 'gte'; + + /** + * @var string Field name. + */ + private $field; + + /** + * @param string $field + * @param array $parameters + */ + public function __construct($field, array $parameters = []) + { + $this->setParameters($parameters); + + if ($this->hasParameter(self::GTE) && $this->hasParameter(self::GT)) { + unset($this->parameters[self::GT]); + } + + if ($this->hasParameter(self::LTE) && $this->hasParameter(self::LT)) { + unset($this->parameters[self::LT]); + } + + $this->field = $field; + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'range'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $output = [ + $this->field => $this->getParameters(), + ]; + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/TermLevel/RegexpQuery.php b/src/Query/TermLevel/RegexpQuery.php new file mode 100644 index 0000000..4fcee0f --- /dev/null +++ b/src/Query/TermLevel/RegexpQuery.php @@ -0,0 +1,71 @@ +<?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\Query\TermLevel; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "regexp" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html + */ +class RegexpQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string Field to be queried. + */ + private $field; + + /** + * @var string The actual regexp value to be used. + */ + private $regexpValue; + + /** + * @param string $field + * @param string $regexpValue + * @param array $parameters + */ + public function __construct($field, $regexpValue, array $parameters = []) + { + $this->field = $field; + $this->regexpValue = $regexpValue; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'regexp'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [ + 'value' => $this->regexpValue, + ]; + + $output = [ + $this->field => $this->processArray($query), + ]; + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/TermLevel/TermQuery.php b/src/Query/TermLevel/TermQuery.php new file mode 100644 index 0000000..fc762a4 --- /dev/null +++ b/src/Query/TermLevel/TermQuery.php @@ -0,0 +1,75 @@ +<?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\Query\TermLevel; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "term" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html + */ +class TermQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string + */ + private $field; + + /** + * @var string + */ + private $value; + + /** + * @param string $field + * @param string $value + * @param array $parameters + */ + public function __construct($field, $value, array $parameters = []) + { + $this->field = $field; + $this->value = $value; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'term'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = $this->processArray(); + + if (empty($query)) { + $query = $this->value; + } else { + $query['value'] = $this->value; + } + + $output = [ + $this->field => $query, + ]; + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/TermLevel/TermsQuery.php b/src/Query/TermLevel/TermsQuery.php new file mode 100644 index 0000000..3833223 --- /dev/null +++ b/src/Query/TermLevel/TermsQuery.php @@ -0,0 +1,71 @@ +<?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\Query\TermLevel; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "terms" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html + */ +class TermsQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string + */ + private $field; + + /** + * @var array + */ + private $terms; + + /** + * Constructor. + * + * @param string $field Field name + * @param array $terms An array of terms + * @param array $parameters Optional parameters + */ + public function __construct($field, $terms, array $parameters = []) + { + $this->field = $field; + $this->terms = $terms; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'terms'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [ + $this->field => $this->terms, + ]; + + $output = $this->processArray($query); + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/TermLevel/TypeQuery.php b/src/Query/TermLevel/TypeQuery.php new file mode 100644 index 0000000..a5ae21c --- /dev/null +++ b/src/Query/TermLevel/TypeQuery.php @@ -0,0 +1,57 @@ +<?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\Query\TermLevel; + +use ONGR\ElasticsearchDSL\BuilderInterface; + +/** + * Represents Elasticsearch "type" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-type-query.html + */ +class TypeQuery implements BuilderInterface +{ + /** + * @var string + */ + private $type; + + /** + * Constructor. + * + * @param string $type Type name + */ + public function __construct($type) + { + $this->type = $type; + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'type'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + return [ + $this->getType() => [ + 'value' => $this->type, + ], + ]; + } +} diff --git a/src/Query/TermLevel/WildcardQuery.php b/src/Query/TermLevel/WildcardQuery.php new file mode 100644 index 0000000..8453c9b --- /dev/null +++ b/src/Query/TermLevel/WildcardQuery.php @@ -0,0 +1,71 @@ +<?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\Query\TermLevel; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "wildcard" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html + */ +class WildcardQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string + */ + private $field; + + /** + * @var string + */ + private $value; + + /** + * @param string $field + * @param string $value + * @param array $parameters + */ + public function __construct($field, $value, array $parameters = []) + { + $this->field = $field; + $this->value = $value; + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'wildcard'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [ + 'value' => $this->value, + ]; + + $output = [ + $this->field => $this->processArray($query), + ]; + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/TermQuery.php b/src/Query/TermQuery.php index 783bb6e..c2751a7 100644 --- a/src/Query/TermQuery.php +++ b/src/Query/TermQuery.php @@ -11,65 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "term" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class TermQuery implements BuilderInterface +class TermQuery extends \ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery { - use ParametersTrait; - - /** - * @var string - */ - private $field; - - /** - * @var string - */ - private $value; - - /** - * @param string $field - * @param string $value - * @param array $parameters - */ - public function __construct($field, $value, array $parameters = []) - { - $this->field = $field; - $this->value = $value; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'term'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = $this->processArray(); - - if (empty($query)) { - $query = $this->value; - } else { - $query['value'] = $this->value; - } - - $output = [ - $this->field => $query, - ]; - - return [$this->getType() => $output]; - } } diff --git a/src/Query/TermsQuery.php b/src/Query/TermsQuery.php index 647078f..f72d73c 100644 --- a/src/Query/TermsQuery.php +++ b/src/Query/TermsQuery.php @@ -11,61 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "terms" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class TermsQuery implements BuilderInterface +class TermsQuery extends \ONGR\ElasticsearchDSL\Query\TermLevel\TermsQuery { - use ParametersTrait; - - /** - * @var string - */ - private $field; - - /** - * @var array - */ - private $terms; - - /** - * Constructor. - * - * @param string $field Field name - * @param array $terms An array of terms - * @param array $parameters Optional parameters - */ - public function __construct($field, $terms, array $parameters = []) - { - $this->field = $field; - $this->terms = $terms; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'terms'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - $this->field => $this->terms, - ]; - - $output = $this->processArray($query); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/TypeQuery.php b/src/Query/TypeQuery.php index 04c62c0..aea3d41 100644 --- a/src/Query/TypeQuery.php +++ b/src/Query/TypeQuery.php @@ -11,47 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; - /** * Represents Elasticsearch "type" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-type-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class TypeQuery implements BuilderInterface +class TypeQuery extends \ONGR\ElasticsearchDSL\Query\TermLevel\TypeQuery { - /** - * @var string - */ - private $type; - - /** - * Constructor. - * - * @param string $type Type name - */ - public function __construct($type) - { - $this->type = $type; - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'type'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - return [ - $this->getType() => [ - 'value' => $this->type, - ], - ]; - } } diff --git a/src/Query/WildcardQuery.php b/src/Query/WildcardQuery.php index 6447769..5383c1c 100644 --- a/src/Query/WildcardQuery.php +++ b/src/Query/WildcardQuery.php @@ -11,61 +11,13 @@ namespace ONGR\ElasticsearchDSL\Query; -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - /** * Represents Elasticsearch "wildcard" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html + * + * @deprecated Use the extended class instead. This class is left only for BC compatibility. */ -class WildcardQuery implements BuilderInterface +class WildcardQuery extends \ONGR\ElasticsearchDSL\Query\TermLevel\WildcardQuery { - use ParametersTrait; - - /** - * @var string - */ - private $field; - - /** - * @var string - */ - private $value; - - /** - * @param string $field - * @param string $value - * @param array $parameters - */ - public function __construct($field, $value, array $parameters = []) - { - $this->field = $field; - $this->value = $value; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'wildcard'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - 'value' => $this->value, - ]; - - $output = [ - $this->field => $this->processArray($query), - ]; - - return [$this->getType() => $output]; - } } -- GitLab