diff --git a/CHANGELOG.md b/CHANGELOG.md index 9796b503482c762a35f631fe9fb449d1b237f221..a1b1eaa9993de3dc3912b1d72631903f3d933e09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ v2.0.0 (2016-x) - [BC break] Aggregation name is not prefixed anymore - [BC break] Removed all filters and filtered query +- [BC break] Query's `toArray()` now returns array WITH query type v1.1.1 (2016-01-26) --- diff --git a/src/Aggregation/FilterAggregation.php b/src/Aggregation/FilterAggregation.php index 2e512baa6e30bc603fd492bc56c164763b643c1e..1782b46a57f3cb4d55f46ab460323ad8bd1119ae 100644 --- a/src/Aggregation/FilterAggregation.php +++ b/src/Aggregation/FilterAggregation.php @@ -68,9 +68,7 @@ class FilterAggregation extends AbstractAggregation throw new \LogicException("Filter aggregation `{$this->getName()}` has no filter added"); } - $filterData = [$this->filter->getType() => $this->filter->toArray()]; - - return $filterData; + return $this->filter->toArray(); } /** diff --git a/src/Aggregation/TopHitsAggregation.php b/src/Aggregation/TopHitsAggregation.php index e88e99660e4f8a035c6089e38dd7c09e4c04cf72..d315bcd1819a6a196cebd934962fbf5a49bf61a1 100644 --- a/src/Aggregation/TopHitsAggregation.php +++ b/src/Aggregation/TopHitsAggregation.php @@ -125,12 +125,15 @@ class TopHitsAggregation extends AbstractAggregation */ public function getArray() { + $output = $this->getSort() ? $this->getSort()->toArray() : []; $output = array_filter( - [ - 'sort' => $this->getSort() ? $this->getSort()->toArray() : null, - 'size' => $this->getSize(), - 'from' => $this->getFrom(), - ], + array_merge( + $output, + [ + 'size' => $this->getSize(), + 'from' => $this->getFrom(), + ] + ), function ($val) { return (($val || is_array($val) || ($val || is_numeric($val)))); } diff --git a/src/BuilderInterface.php b/src/BuilderInterface.php index d30cefbe38ad984372ccbad75e9dd4081c3a3fab..de07683d2307c9ff217f7b3722f0c5e58fa45bf9 100644 --- a/src/BuilderInterface.php +++ b/src/BuilderInterface.php @@ -19,10 +19,7 @@ interface BuilderInterface /** * Generates array which will be passed to elasticsearch-php client. * - * WARNING: the output of this method will change in version v2.0. It will - * always return array WITH query/aggregation type as key. - * - * @return array|object + * @return array */ public function toArray(); diff --git a/src/Query/BoolQuery.php b/src/Query/BoolQuery.php index ed65f902a42796bb186b833b3e8877b2a15e4272..8c499111faa419b51e3c6af2ff9eb0ede8848b60 100644 --- a/src/Query/BoolQuery.php +++ b/src/Query/BoolQuery.php @@ -41,18 +41,6 @@ class BoolQuery implements BuilderInterface $this->container = []; } - /** - * Checks if bool expression is relevant. - * - * @return bool - * - * @deprecated Will be removed in 2.0. No replacement. Always use full structure. - */ - public function isRelevant() - { - return true; - } - /** * @param null $boolType * @return array @@ -103,16 +91,23 @@ class BoolQuery implements BuilderInterface */ 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->getType() => $builder->toArray()]; + $output[$boolType][] = $builder->toArray(); } } - return $output; + return [$this->getType() => $output]; } /** diff --git a/src/Query/BoostingQuery.php b/src/Query/BoostingQuery.php index 0365482c0defd937e66ea34b4af168dbc0d06f6d..c88f0eb89223c568a758b9af5e6bae137654f1d5 100644 --- a/src/Query/BoostingQuery.php +++ b/src/Query/BoostingQuery.php @@ -59,11 +59,11 @@ class BoostingQuery implements BuilderInterface public function toArray() { $query = [ - 'positive' => [$this->positive->getType() => $this->positive->toArray()], - 'negative' => [$this->negative->getType() => $this->negative->toArray()], + 'positive' => $this->positive->toArray(), + 'negative' => $this->negative->toArray(), 'negative_boost' => $this->negativeBoost, ]; - return $query; + return [$this->getType() => $query]; } } diff --git a/src/Query/CommonTermsQuery.php b/src/Query/CommonTermsQuery.php index cc70fb8b0d663b6ea2c58f9f166209938010e749..def85c06394df5e13bf0d9e744cbd09cada24e77 100644 --- a/src/Query/CommonTermsQuery.php +++ b/src/Query/CommonTermsQuery.php @@ -64,6 +64,6 @@ class CommonTermsQuery implements BuilderInterface $this->field => $this->processArray($query), ]; - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/ConstantScoreQuery.php b/src/Query/ConstantScoreQuery.php index bb3dc5abf33d0486ee5058860ad4c049fabccd13..bafba281c80623d22d070104c231824d0541468a 100644 --- a/src/Query/ConstantScoreQuery.php +++ b/src/Query/ConstantScoreQuery.php @@ -50,13 +50,11 @@ class ConstantScoreQuery implements BuilderInterface public function toArray() { $query = [ - 'filter' => [ - $this->query->getType() => $this->query->toArray(), - ], + 'filter' => $this->query->toArray(), ]; $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/DisMaxQuery.php b/src/Query/DisMaxQuery.php index de539d8ba858bedf49b4a8f1ee60ca8af8e05c26..49c68b9485a60a5e416d0fda4d1330a9fc9ceb37 100644 --- a/src/Query/DisMaxQuery.php +++ b/src/Query/DisMaxQuery.php @@ -65,10 +65,10 @@ class DisMaxQuery implements BuilderInterface { $query = []; foreach ($this->queries as $type) { - $query['queries'][] = [$type->getType() => $type->toArray()]; + $query = array_merge($query, $type->toArray()); } - $output = $this->processArray($query); + $output = $this->processArray(['queries' => $query]); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/ExistsQuery.php b/src/Query/ExistsQuery.php index 1c62382a502c19f26fc5960596238162aa8f932c..fd7f4de834dfa9844b0167a2919febe20ad94ce6 100644 --- a/src/Query/ExistsQuery.php +++ b/src/Query/ExistsQuery.php @@ -49,7 +49,9 @@ class ExistsQuery implements BuilderInterface public function toArray() { return [ - 'field' => $this->field, + $this->getType() => [ + 'field' => $this->field, + ], ]; } } diff --git a/src/Query/FunctionScoreQuery.php b/src/Query/FunctionScoreQuery.php index 3f76121c95132d898a01d8f8bab01cad38d013aa..c5f970c8ffec33ed63db5f94352ad6041dad806e 100644 --- a/src/Query/FunctionScoreQuery.php +++ b/src/Query/FunctionScoreQuery.php @@ -15,17 +15,15 @@ use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\ParametersTrait; /** - * Elasticsearch function_score query class. + * 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; /** - * Query of filter. - * - * In Function score could be used query or filter. Use setDslType() to change type. - * * @var BuilderInterface */ private $query; @@ -62,9 +60,7 @@ class FunctionScoreQuery implements BuilderInterface private function applyQuery(array &$function, BuilderInterface $query = null) { if ($query) { - $function['query'] = [ - $query->getType() => $query->toArray(), - ]; + $function['query'] = $query->toArray(); } } @@ -222,14 +218,12 @@ class FunctionScoreQuery implements BuilderInterface public function toArray() { $query = [ - 'query' => [ - $this->query->getType() => $this->query->toArray(), - ], + 'query' => $this->query->toArray(), 'functions' => $this->functions, ]; $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/FuzzyLikeThisFieldQuery.php b/src/Query/FuzzyLikeThisFieldQuery.php deleted file mode 100644 index a4dbbcfa6e03b16ea7541eabc975a0e59f39b694..0000000000000000000000000000000000000000 --- a/src/Query/FuzzyLikeThisFieldQuery.php +++ /dev/null @@ -1,69 +0,0 @@ -<?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; - -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - -/** - * Elasticsearch fuzzy_like_this_field query class. - */ -class FuzzyLikeThisFieldQuery implements BuilderInterface -{ - use ParametersTrait; - - /** - * @var string - */ - private $field; - - /** - * @var string - */ - private $likeText; - - /** - * @param string $field - * @param string $likeText - * @param array $parameters - */ - public function __construct($field, $likeText, array $parameters = []) - { - $this->field = $field; - $this->likeText = $likeText; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'fuzzy_like_this_field'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - 'like_text' => $this->likeText, - ]; - - $output = [ - $this->field => $this->processArray($query), - ]; - - return $output; - } -} diff --git a/src/Query/FuzzyLikeThisQuery.php b/src/Query/FuzzyLikeThisQuery.php deleted file mode 100644 index 4b402cf027c5fca171ca77ec71608c832767d189..0000000000000000000000000000000000000000 --- a/src/Query/FuzzyLikeThisQuery.php +++ /dev/null @@ -1,70 +0,0 @@ -<?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; - -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - -/** - * Elasticsearch fuzzy_like_this query class. - */ -class FuzzyLikeThisQuery implements BuilderInterface -{ - use ParametersTrait; - - /** - * @var string[] - */ - private $fields; - - /** - * @var string - */ - private $likeText; - - /** - * @param string|string[] $fields Multiple fields can be set via comma or just an array. - * @param string $likeText - * @param array $parameters - */ - public function __construct($fields, $likeText, array $parameters = []) - { - if (!is_array($fields)) { - $fields = explode(',', $fields); - } - - $this->fields = $fields; - $this->likeText = $likeText; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'fuzzy_like_this'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $output = [ - 'fields' => $this->fields, - 'like_text' => $this->likeText, - ]; - - return $this->processArray($output); - } -} diff --git a/src/Query/FuzzyQuery.php b/src/Query/FuzzyQuery.php index e22a47558dcfd30b6db9feffc9a32d7b910e5e7d..27f88460044ab6ad0a1b9cc28a65ac10bb01d413 100644 --- a/src/Query/FuzzyQuery.php +++ b/src/Query/FuzzyQuery.php @@ -64,6 +64,6 @@ class FuzzyQuery implements BuilderInterface $this->field => $this->processArray($query), ]; - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/GeoBoundingBoxQuery.php b/src/Query/GeoBoundingBoxQuery.php index f77ce225ad98cddddf34081af0305b233ff4981b..2940eb7ba71571e81b2c93468fe642578a98f56b 100644 --- a/src/Query/GeoBoundingBoxQuery.php +++ b/src/Query/GeoBoundingBoxQuery.php @@ -80,6 +80,6 @@ class GeoBoundingBoxQuery implements BuilderInterface $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/GeoDistanceQuery.php b/src/Query/GeoDistanceQuery.php index 477d8726e4e6b2a4a19655833ca56c6ec3ad2aa8..d1241791c686ddfb21af251a4ba9910cf33f4899 100644 --- a/src/Query/GeoDistanceQuery.php +++ b/src/Query/GeoDistanceQuery.php @@ -72,6 +72,6 @@ class GeoDistanceQuery implements BuilderInterface ]; $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/GeoDistanceRangeQuery.php b/src/Query/GeoDistanceRangeQuery.php index ab9a8a31b10df856c309a993fb017bfb6aa6f866..ef3544d9bf91c474a356a0e2bade3614e8665193 100644 --- a/src/Query/GeoDistanceRangeQuery.php +++ b/src/Query/GeoDistanceRangeQuery.php @@ -69,6 +69,6 @@ class GeoDistanceRangeQuery implements BuilderInterface $query = $this->range + [$this->field => $this->location]; $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/GeoPolygonQuery.php b/src/Query/GeoPolygonQuery.php index 680ed8ec2c83299ce75190c2b97b8f44499fac5a..eecf0e4db1dc71ffa14d15328b9fa4e29b1ec17a 100644 --- a/src/Query/GeoPolygonQuery.php +++ b/src/Query/GeoPolygonQuery.php @@ -61,6 +61,6 @@ class GeoPolygonQuery implements BuilderInterface $query = [$this->field => ['points' => $this->points]]; $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/GeoShapeQuery.php b/src/Query/GeoShapeQuery.php index 4360c855d8353cca2b3fd082468a2247f470dbd6..ae481db123e6a04260fefd243d8b2f8e54108138 100644 --- a/src/Query/GeoShapeQuery.php +++ b/src/Query/GeoShapeQuery.php @@ -97,6 +97,6 @@ class GeoShapeQuery implements BuilderInterface { $output = $this->processArray($this->fields); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/GeohashCellQuery.php b/src/Query/GeohashCellQuery.php index 8c54a7d6848db5d6aab6fc92f26aa48e4fb3494a..9aa15c7b8013eafbfbcb2d62a81bf1f0d3262981 100644 --- a/src/Query/GeohashCellQuery.php +++ b/src/Query/GeohashCellQuery.php @@ -62,6 +62,6 @@ class GeohashCellQuery implements BuilderInterface $query = [$this->field => $this->location]; $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/HasChildQuery.php b/src/Query/HasChildQuery.php index e12214059cdf292fe470de8dc0521f271183ce85..9e5044e53864db625c5a88d900f1ad5f260b4c20 100644 --- a/src/Query/HasChildQuery.php +++ b/src/Query/HasChildQuery.php @@ -60,13 +60,11 @@ class HasChildQuery implements BuilderInterface { $query = [ 'type' => $this->type, - 'query' => [ - $this->query->getType() => $this->query->toArray(), - ], + 'query' => $this->query->toArray(), ]; $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/HasParentQuery.php b/src/Query/HasParentQuery.php index 4b98a0e255e7be916cd5bb372897897d4bd20e0a..c57bca4eb31e137eabe6fcc7ee2896e976d7e400 100644 --- a/src/Query/HasParentQuery.php +++ b/src/Query/HasParentQuery.php @@ -60,13 +60,11 @@ class HasParentQuery implements BuilderInterface { $query = [ 'parent_type' => $this->parentType, - 'query' => [ - $this->query->getType() => $this->query->toArray(), - ], + 'query' => $this->query->toArray(), ]; $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/IdsQuery.php b/src/Query/IdsQuery.php index 60cf333044efaa1ead428bfdd4094ac26ac898d0..fa55b73e14decfc7a427a807244af73b657e0416 100644 --- a/src/Query/IdsQuery.php +++ b/src/Query/IdsQuery.php @@ -57,6 +57,6 @@ class IdsQuery implements BuilderInterface $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/IndicesQuery.php b/src/Query/IndicesQuery.php index 501ba169e378fdf272cef60ef1faf329c652cc89..30160b7b0b5e6a9bce48fb3082bbf647c8855691 100644 --- a/src/Query/IndicesQuery.php +++ b/src/Query/IndicesQuery.php @@ -66,16 +66,16 @@ class IndicesQuery implements BuilderInterface $output = ['index' => $this->indices[0]]; } - $output['query'] = [$this->query->getType() => $this->query->toArray()]; + $output['query'] = $this->query->toArray(); if ($this->noMatchQuery !== null) { if (is_a($this->noMatchQuery, 'ONGR\ElasticsearchDSL\BuilderInterface')) { - $output['no_match_query'] = [$this->noMatchQuery->getType() => $this->noMatchQuery->toArray()]; + $output['no_match_query'] = $this->noMatchQuery->toArray(); } else { $output['no_match_query'] = $this->noMatchQuery; } } - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/LimitQuery.php b/src/Query/LimitQuery.php index 939b913ecada47488e0527f71974000f648314e9..ced3a81884fdfe290a310e4b707435247074e576 100644 --- a/src/Query/LimitQuery.php +++ b/src/Query/LimitQuery.php @@ -47,7 +47,9 @@ class LimitQuery implements BuilderInterface public function toArray() { return [ - 'value' => $this->value, + $this->getType() => [ + 'value' => $this->value, + ], ]; } } diff --git a/src/Query/MatchAllQuery.php b/src/Query/MatchAllQuery.php index 88ce0b673c77827dbe59667d32d0b717d8190288..ec9fde76dca20c3061708f27c2d032d3c257490f 100644 --- a/src/Query/MatchAllQuery.php +++ b/src/Query/MatchAllQuery.php @@ -15,7 +15,7 @@ use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\ParametersTrait; /** - * Represents Elasticsearch "bool" query. + * Represents Elasticsearch "match_all" query. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html */ @@ -44,10 +44,6 @@ class MatchAllQuery implements BuilderInterface */ public function toArray() { - if (count($this->getParameters()) > 0) { - return $this->getParameters(); - } - - return []; + return [$this->getType() => $this->getParameters()]; } } diff --git a/src/Query/MatchQuery.php b/src/Query/MatchQuery.php index 4815ed2847459e1ab57c3279e904d369a6bc9478..05f799ad83c035d5076e761b4d94ea96b44fe150 100644 --- a/src/Query/MatchQuery.php +++ b/src/Query/MatchQuery.php @@ -64,6 +64,6 @@ class MatchQuery implements BuilderInterface $this->field => $this->processArray($query), ]; - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/MissingQuery.php b/src/Query/MissingQuery.php index 2acb7db4b2d8a90d3f0cfd4d7903692347acb3cb..f6d0f3b93260421e82d317aa4bc901d92f2aa11c 100644 --- a/src/Query/MissingQuery.php +++ b/src/Query/MissingQuery.php @@ -56,6 +56,6 @@ class MissingQuery implements BuilderInterface $query = ['field' => $this->field]; $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/MoreLikeThisQuery.php b/src/Query/MoreLikeThisQuery.php index 1b7203929dc7cba46e0c61fbbc09a605cb2e7e6c..ce4e492cfd198c4add081e75462e84fd18786dbb 100644 --- a/src/Query/MoreLikeThisQuery.php +++ b/src/Query/MoreLikeThisQuery.php @@ -57,6 +57,6 @@ class MoreLikeThisQuery implements BuilderInterface $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/MultiMatchQuery.php b/src/Query/MultiMatchQuery.php index ccf608d782590ad4595bbf63f4892725255fe4be..0fab953807abe93626fda838e9eef1f9e8481f06 100644 --- a/src/Query/MultiMatchQuery.php +++ b/src/Query/MultiMatchQuery.php @@ -63,6 +63,6 @@ class MultiMatchQuery implements BuilderInterface $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/NestedQuery.php b/src/Query/NestedQuery.php index 76a30aa318dc3caf17a6b3e54bd879eaa2390552..0b020c9d4b310c669ae134daf24705eb7a422428 100644 --- a/src/Query/NestedQuery.php +++ b/src/Query/NestedQuery.php @@ -58,13 +58,13 @@ class NestedQuery implements BuilderInterface */ public function toArray() { - return $this->processArray( - [ - 'path' => $this->path, - 'query' => [ - $this->query->getType() => $this->query->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 9f99411e6903bda7df23fb32ba535aa62dd6bf6d..903218fcc8460937f4665da1cbe95c4b6c47c329 100644 --- a/src/Query/PrefixQuery.php +++ b/src/Query/PrefixQuery.php @@ -66,6 +66,6 @@ class PrefixQuery implements BuilderInterface $this->field => $this->processArray($query), ]; - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/QueryStringQuery.php b/src/Query/QueryStringQuery.php index 684e8bdf3bf8299470021f2576f4065744bb6192..5863bb9f600bc73cf52e4091d39c07df6a037d63 100644 --- a/src/Query/QueryStringQuery.php +++ b/src/Query/QueryStringQuery.php @@ -55,6 +55,6 @@ class QueryStringQuery implements BuilderInterface $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/RangeQuery.php b/src/Query/RangeQuery.php index 26a8338535bdadd30177f9c18a15bd7fd865d3a8..75c446a9dea5f941db6040841778d9428c91d29f 100644 --- a/src/Query/RangeQuery.php +++ b/src/Query/RangeQuery.php @@ -70,6 +70,6 @@ class RangeQuery implements BuilderInterface $this->field => $this->getParameters(), ]; - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/RegexpQuery.php b/src/Query/RegexpQuery.php index 8887000d7c526e71ad3fde4432cc04e15d4607f4..0752503277df5e53963071cdf74b74117addcaa5 100644 --- a/src/Query/RegexpQuery.php +++ b/src/Query/RegexpQuery.php @@ -66,6 +66,6 @@ class RegexpQuery implements BuilderInterface $this->field => $this->processArray($query), ]; - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/ScriptQuery.php b/src/Query/ScriptQuery.php index 805907b06a333071472e52b0162b868f1c4d00bb..1a1a0c7bed23808ac150a18200835ae3e1595945 100644 --- a/src/Query/ScriptQuery.php +++ b/src/Query/ScriptQuery.php @@ -54,6 +54,6 @@ class ScriptQuery implements BuilderInterface $query = ['inline' => $this->script]; $output = $this->processArray($query); - return ['script' => $output]; + return [$this->getType() => ['script' => $output]]; } } diff --git a/src/Query/SimpleQueryStringQuery.php b/src/Query/SimpleQueryStringQuery.php index 3c08331960c20b8d05b92bdb2c7dce50ca823217..960f04d083072bdaee9261e0f958c818575ce682 100644 --- a/src/Query/SimpleQueryStringQuery.php +++ b/src/Query/SimpleQueryStringQuery.php @@ -55,6 +55,6 @@ class SimpleQueryStringQuery implements BuilderInterface $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/Span/SpanFirstQuery.php b/src/Query/Span/SpanFirstQuery.php index 40d222de2f417e406efec48625e1cfe8f43dc8a2..46f16ef90daeb646872d9043463113ac69f018bb 100644 --- a/src/Query/Span/SpanFirstQuery.php +++ b/src/Query/Span/SpanFirstQuery.php @@ -15,6 +15,8 @@ use ONGR\ElasticsearchDSL\ParametersTrait; /** * Elasticsearch span first query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-first-query.html */ class SpanFirstQuery implements SpanQueryInterface { @@ -58,10 +60,10 @@ class SpanFirstQuery implements SpanQueryInterface public function toArray() { $query = []; - $query['match'] = [$this->query->getType() => $this->query->toArray()]; + $query['match'] = $this->query->toArray(); $query['end'] = $this->end; $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/Span/SpanMultiTermQuery.php b/src/Query/Span/SpanMultiTermQuery.php index c176188fa46b19d62d0112b5b9805decd57678d1..9042c59897c48e33c35308be9dd97ddd29c36967 100644 --- a/src/Query/Span/SpanMultiTermQuery.php +++ b/src/Query/Span/SpanMultiTermQuery.php @@ -16,6 +16,8 @@ use ONGR\ElasticsearchDSL\ParametersTrait; /** * Elasticsearch span multi term query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-multi-term-query.html */ class SpanMultiTermQuery implements SpanQueryInterface { @@ -54,9 +56,9 @@ class SpanMultiTermQuery implements SpanQueryInterface public function toArray() { $query = []; - $query['match'] = [$this->query->getType() => $this->query->toArray()]; + $query['match'] = $this->query->toArray(); $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/Span/SpanNearQuery.php b/src/Query/Span/SpanNearQuery.php index f71a0cce7dfb73d019cb1fbaa8ab354de979ebb3..56436038fcb2a0bf3031a927198168b76421abe4 100644 --- a/src/Query/Span/SpanNearQuery.php +++ b/src/Query/Span/SpanNearQuery.php @@ -13,6 +13,8 @@ namespace ONGR\ElasticsearchDSL\Query\Span; /** * Elasticsearch span near query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html */ class SpanNearQuery extends SpanOrQuery implements SpanQueryInterface { @@ -52,11 +54,11 @@ class SpanNearQuery extends SpanOrQuery implements SpanQueryInterface { $query = []; foreach ($this->getQueries() as $type) { - $query['clauses'][] = [$type->getType() => $type->toArray()]; + $query['clauses'][] = $type->toArray(); } $query['slop'] = $this->getSlop(); $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/Span/SpanNotQuery.php b/src/Query/Span/SpanNotQuery.php index 9e2359a19108569535d6ecc225fcece8315d7b9c..7e34339ec439beaf7470639b04ec92013dd1380b 100644 --- a/src/Query/Span/SpanNotQuery.php +++ b/src/Query/Span/SpanNotQuery.php @@ -15,6 +15,10 @@ use ONGR\ElasticsearchDSL\ParametersTrait; /** * Elasticsearch Span not query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-not-query.html + * + * @todo Add options support */ class SpanNotQuery implements SpanQueryInterface { @@ -56,10 +60,10 @@ class SpanNotQuery implements SpanQueryInterface public function toArray() { $query = [ - 'include' => [$this->include->getType() => $this->include->toArray()], - 'exclude' => [$this->exclude->getType() => $this->exclude->toArray()], + 'include' => $this->include->toArray(), + 'exclude' => $this->exclude->toArray(), ]; - return $query; + return [$this->getType() => $query]; } } diff --git a/src/Query/Span/SpanOrQuery.php b/src/Query/Span/SpanOrQuery.php index 5b3c4321a571f5135527a6136d3bdf60e78cda28..9f3ec54ddee340f27f7f372f221d5a466d413bac 100644 --- a/src/Query/Span/SpanOrQuery.php +++ b/src/Query/Span/SpanOrQuery.php @@ -15,6 +15,8 @@ use ONGR\ElasticsearchDSL\ParametersTrait; /** * Elasticsearch span or query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-or-query.html */ class SpanOrQuery implements SpanQueryInterface { @@ -70,10 +72,10 @@ class SpanOrQuery implements SpanQueryInterface { $query = []; foreach ($this->queries as $type) { - $query['clauses'][] = [$type->getType() => $type->toArray()]; + $query['clauses'][] = $type->toArray(); } $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/Span/SpanTermQuery.php b/src/Query/Span/SpanTermQuery.php index 2a898e42841ba070925489081a4256f4d5dcea11..bbb036c887aa2fce67481f2f809533e064ca4d90 100644 --- a/src/Query/Span/SpanTermQuery.php +++ b/src/Query/Span/SpanTermQuery.php @@ -15,6 +15,8 @@ use ONGR\ElasticsearchDSL\Query\TermQuery; /** * Elasticsearch span_term query class. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-term-query.html */ class SpanTermQuery extends TermQuery implements SpanQueryInterface { diff --git a/src/Query/TermQuery.php b/src/Query/TermQuery.php index d52fff9516ccc35c9388305e501e0ab34147f682..783bb6e5fe777c7694397b3e9a9f3cbc4312d6dd 100644 --- a/src/Query/TermQuery.php +++ b/src/Query/TermQuery.php @@ -70,6 +70,6 @@ class TermQuery implements BuilderInterface $this->field => $query, ]; - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/TermsQuery.php b/src/Query/TermsQuery.php index 0c5e544282231e20d7b3f377abe847e7e999b489..647078faff08b075b2e4a0b5cf793166c88cd548 100644 --- a/src/Query/TermsQuery.php +++ b/src/Query/TermsQuery.php @@ -66,6 +66,6 @@ class TermsQuery implements BuilderInterface $output = $this->processArray($query); - return $output; + return [$this->getType() => $output]; } } diff --git a/src/Query/TypeQuery.php b/src/Query/TypeQuery.php index 3c2ed250e457165bab21d85e28accacbd8174e56..04c62c0bb213edd5cfb28cd54f16ef64109e66cb 100644 --- a/src/Query/TypeQuery.php +++ b/src/Query/TypeQuery.php @@ -49,7 +49,9 @@ class TypeQuery implements BuilderInterface public function toArray() { return [ - 'value' => $this->type, + $this->getType() => [ + 'value' => $this->type, + ], ]; } } diff --git a/src/Query/WildcardQuery.php b/src/Query/WildcardQuery.php index eed28344cf8813faa4a1cecbc45a09feaa2752dd..a9982218982dd8779ddc016f5efea11a6e94101c 100644 --- a/src/Query/WildcardQuery.php +++ b/src/Query/WildcardQuery.php @@ -64,6 +64,6 @@ class WildcardQuery implements BuilderInterface $this->field => $this->processArray($query), ]; - return $output; + return [$this->getType() => $output]; } } diff --git a/src/SearchEndpoint/PostFilterEndpoint.php b/src/SearchEndpoint/PostFilterEndpoint.php index 4eff244d1bfa9a9b74c9930fe467c1d47d72c0ca..cacb66274ab7cdc43e2cf52dfe1c096916086f57 100644 --- a/src/SearchEndpoint/PostFilterEndpoint.php +++ b/src/SearchEndpoint/PostFilterEndpoint.php @@ -32,9 +32,7 @@ class PostFilterEndpoint extends FilterEndpoint return null; } - $filter = $this->getBool(); - - return [$filter->getType() => $filter->toArray()]; + return $this->getBool()->toArray(); } /** diff --git a/src/SearchEndpoint/QueryEndpoint.php b/src/SearchEndpoint/QueryEndpoint.php index e7f040cbb73003ec686e00a94be2503ea59298c9..41062b34aa1bd36d4defdb9208105010f9472da0 100644 --- a/src/SearchEndpoint/QueryEndpoint.php +++ b/src/SearchEndpoint/QueryEndpoint.php @@ -52,7 +52,7 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI return null; } - return [$this->bool->getType() => $this->bool->toArray()]; + return $this->bool->toArray(); } /** diff --git a/src/Sort/FieldSort.php b/src/Sort/FieldSort.php index 4223eae103183ee3aca87bf64d86067a76bf291c..2f3eeeb3090ac77e5384fb849d6a70386a7c99be 100644 --- a/src/Sort/FieldSort.php +++ b/src/Sort/FieldSort.php @@ -92,9 +92,7 @@ class FieldSort implements BuilderInterface $fieldValues = array_merge( $this->params, [ - 'nested_filter' => [ - $this->nestedFilter->getType() => $this->nestedFilter->toArray(), - ], + 'nested_filter' => $this->nestedFilter->toArray(), ] ); } else { @@ -105,6 +103,6 @@ class FieldSort implements BuilderInterface $this->field => empty($fieldValues) ? new \stdClass() : $fieldValues, ]; - return $output; + return [$this->getType() => $output]; } } diff --git a/tests/Aggregation/FilterAggregationTest.php b/tests/Aggregation/FilterAggregationTest.php index 6f8fe99f6409a6cc61c2ba294ef2d3b738a766a1..fcc432d317fab683cf57ccb2bae3b189cb82f2c9 100644 --- a/tests/Aggregation/FilterAggregationTest.php +++ b/tests/Aggregation/FilterAggregationTest.php @@ -36,9 +36,7 @@ class FilterAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->setFilter($filter); $result = [ - 'filter' => [ - $filter->getType() => $filter->toArray(), - ], + 'filter' => $filter->toArray(), ]; $out[] = [ @@ -54,9 +52,7 @@ class FilterAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addAggregation($histogramAgg); $result = [ - 'filter' => [ - $filter->getType() => $filter->toArray(), - ], + 'filter' => $filter->toArray(), 'aggregations' => [ $histogramAgg->getName() => $histogramAgg->toArray(), ], @@ -78,9 +74,7 @@ class FilterAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->setFilter($boolFilter); $result = [ - 'filter' => [ - $boolFilter->getType() => $boolFilter->toArray(), - ], + 'filter' => $boolFilter->toArray(), ]; @@ -147,11 +141,9 @@ class FilterAggregationTest extends \PHPUnit_Framework_TestCase { $matchAllFilter = new MatchAllQuery(); $aggregation = new FilterAggregation('test', $matchAllFilter); - $this->assertSame( + $this->assertEquals( [ - 'filter' => [ - $matchAllFilter->getType() => $matchAllFilter->toArray(), - ], + 'filter' => $matchAllFilter->toArray(), ], $aggregation->toArray() ); diff --git a/tests/Query/BoolQueryTest.php b/tests/Query/BoolQueryTest.php index dd0147faa32624267271cfdc96123e61d6fa1fda..af0cce85f42fb9ee75b9d7f200879a12e0f8e898 100644 --- a/tests/Query/BoolQueryTest.php +++ b/tests/Query/BoolQueryTest.php @@ -20,14 +20,6 @@ use ONGR\ElasticsearchDSL\Query\TermQuery; */ class BoolQueryTest extends \PHPUnit_Framework_TestCase { - /** - * Tests isRelevant method. - */ - public function testIsRelevant() - { - $bool = new BoolQuery(); - $this->assertTrue($bool->isRelevant()); - } /** * Test for addToBool() without setting a correct bool operator. * @@ -50,24 +42,26 @@ class BoolQueryTest extends \PHPUnit_Framework_TestCase $bool->add(new TermQuery('key2', 'value2'), BoolQuery::MUST); $bool->add(new TermQuery('key3', 'value3'), BoolQuery::MUST_NOT); $expected = [ - 'should' => [ - [ - 'term' => [ - 'key1' => 'value1', + 'bool' => [ + 'should' => [ + [ + 'term' => [ + 'key1' => 'value1', + ], ], ], - ], - 'must' => [ - [ - 'term' => [ - 'key2' => 'value2', + 'must' => [ + [ + 'term' => [ + 'key2' => 'value2', + ], ], ], - ], - 'must_not' => [ - [ - 'term' => [ - 'key3' => 'value3', + 'must_not' => [ + [ + 'term' => [ + 'key3' => 'value3', + ], ], ], ], @@ -75,16 +69,6 @@ class BoolQueryTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, $bool->toArray()); } - /** - * Test getType method. - */ - public function testBoolGetType() - { - $bool = new BoolQuery(); - $result = $bool->getType(); - $this->assertEquals('bool', $result); - } - /** * Tests bool query in filter context. */ @@ -94,21 +78,38 @@ class BoolQueryTest extends \PHPUnit_Framework_TestCase $bool->add(new TermQuery('key1', 'value1'), BoolQuery::FILTER); $bool->add(new TermQuery('key2', 'value2'), BoolQuery::MUST); $expected = [ - 'filter' => [ - [ - 'term' => [ - 'key1' => 'value1', + 'bool' => [ + 'filter' => [ + [ + 'term' => [ + 'key1' => 'value1', + ], ], ], - ], - 'must' => [ - [ - 'term' => [ - 'key2' => 'value2', + 'must' => [ + [ + 'term' => [ + 'key2' => 'value2', + ], ], ], ], ]; $this->assertEquals($expected, $bool->toArray()); } + + /** + * Test if simplified structure is returned in case single MUST query given. + */ + public function testSingleMust() + { + $bool = new BoolQuery(); + $bool->add(new TermQuery('key2', 'value2'), BoolQuery::MUST); + $expected = [ + 'term' => [ + 'key2' => 'value2', + ], + ]; + $this->assertEquals($expected, $bool->toArray()); + } } diff --git a/tests/Query/ExistsQueryTest.php b/tests/Query/ExistsQueryTest.php index 4695fad525f72a9fa013a16df7f2beb00d75e5ac..292b8adb39e5e97b80a6486aa5ebf9d338512e68 100644 --- a/tests/Query/ExistsQueryTest.php +++ b/tests/Query/ExistsQueryTest.php @@ -19,11 +19,11 @@ use ONGR\ElasticsearchDSL\Query\ExistsQuery; class ExistsQueryTest extends \PHPUnit_Framework_TestCase { /** - * Tests ExistsQuery#toArray() method. + * Tests toArray() method. */ public function testToArray() { $query = new ExistsQuery('bar'); - $this->assertEquals(['field' => 'bar'], $query->toArray()); + $this->assertEquals(['exists' => ['field' => 'bar']], $query->toArray()); } } diff --git a/tests/Query/FunctionScoreQueryTest.php b/tests/Query/FunctionScoreQueryTest.php index 7e2fbb45005a60ca7722530a22c746f71d4f2c18..5f0bb5aeba0f28434973ab72e1f05892c4f94448 100644 --- a/tests/Query/FunctionScoreQueryTest.php +++ b/tests/Query/FunctionScoreQueryTest.php @@ -33,7 +33,7 @@ class FunctionScoreQueryTest extends \PHPUnit_Framework_TestCase [ 'seed' => null, 'expectedArray' => [ - 'query' => [ null => null ], + 'query' => null, 'functions' => [ [ 'random_score' => new \stdClass(), @@ -45,7 +45,7 @@ class FunctionScoreQueryTest extends \PHPUnit_Framework_TestCase [ 'seed' => 'someSeed', 'expectedArray' => [ - 'query' => [ null => null ], + 'query' => null, 'functions' => [ [ 'random_score' => [ 'seed' => 'someSeed'], @@ -72,7 +72,7 @@ class FunctionScoreQueryTest extends \PHPUnit_Framework_TestCase $functionScoreQuery = new FunctionScoreQuery($matchAllQuery); $functionScoreQuery->addRandomFunction($seed); - $this->assertEquals($expectedArray, $functionScoreQuery->toArray()); + $this->assertEquals(['function_score' => $expectedArray], $functionScoreQuery->toArray()); } /** @@ -86,9 +86,9 @@ class FunctionScoreQueryTest extends \PHPUnit_Framework_TestCase $functionScoreQuery->addFieldValueFactorFunction('field1', 2); $functionScoreQuery->addFieldValueFactorFunction('field2', 1.5, 'ln'); - $this->assertSame( + $this->assertEquals( [ - 'query' => [null => null], + 'query' => null, 'functions' => [ [ 'field_value_factor' => [ @@ -106,7 +106,7 @@ class FunctionScoreQueryTest extends \PHPUnit_Framework_TestCase ], ], ], - $functionScoreQuery->toArray() + $functionScoreQuery->toArray()['function_score'] ); } } diff --git a/tests/Query/FuzzyLikeThisQueryTest.php b/tests/Query/FuzzyLikeThisQueryTest.php deleted file mode 100644 index ce68de1c70951c6b3b563be25771a55252aac23d..0000000000000000000000000000000000000000 --- a/tests/Query/FuzzyLikeThisQueryTest.php +++ /dev/null @@ -1,76 +0,0 @@ -<?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\Tests\Query; - -use ONGR\ElasticsearchDSL\Query\FuzzyLikeThisQuery; - -/** - * Class FuzzyLikeThisQueryTest. - */ -class FuzzyLikeThisQueryTest extends \PHPUnit_Framework_TestCase -{ - /** - * Tests if toArray returns data in correct format with right data from constructor. - */ - public function testQuery() - { - $fuzzyLikeThisQuery = new FuzzyLikeThisQuery( - ['name.first', 'name.last'], - 'text like this one', - [ 'max_query_terms' => 12 ] - ); - - $this->assertSame( - [ - 'fields' => ['name.first', 'name.last'], - 'like_text' => 'text like this one', - 'max_query_terms' => 12, - ], - $fuzzyLikeThisQuery->toArray() - ); - } - - /** - * Tests if correct type is returned. - */ - public function testGetType() - { - /** @var FuzzyLikeThisQuery $fuzzyLikeThisQuery */ - $fuzzyLikeThisQuery = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\FuzzyLikeThisQuery') - ->disableOriginalConstructor() - ->setMethods(null) - ->getMock(); - - $this->assertEquals('fuzzy_like_this', $fuzzyLikeThisQuery->getType()); - } - - /** - * Tests if query accepts single field as string. - */ - public function testSingleField() - { - $fuzzyLikeThisQuery = new FuzzyLikeThisQuery( - 'name.first', - 'text like this one', - [ 'max_query_terms' => 12 ] - ); - - $this->assertSame( - [ - 'fields' => ['name.first'], - 'like_text' => 'text like this one', - 'max_query_terms' => 12, - ], - $fuzzyLikeThisQuery->toArray() - ); - } -} diff --git a/tests/Query/GeoBoundingBoxQueryTest.php b/tests/Query/GeoBoundingBoxQueryTest.php index 65b329a642fac1c05137ea7e171c62752913ff62..c192fc8974ace721ce506e29ec322ee24a7f4ddf 100644 --- a/tests/Query/GeoBoundingBoxQueryTest.php +++ b/tests/Query/GeoBoundingBoxQueryTest.php @@ -82,6 +82,6 @@ class GeoBoundingBoxQueryTest extends \PHPUnit_Framework_TestCase { $query = new GeoBoundingBoxQuery($field, $values, $parameters); $result = $query->toArray(); - $this->assertEquals($expected, $result); + $this->assertEquals(['geo_bounding_box' => $expected], $result); } } diff --git a/tests/Query/GeoDistanceQueryTest.php b/tests/Query/GeoDistanceQueryTest.php index a69a4ab4eb3b71b9820ecef54028716045beaf05..496bec83ec769551e06891fb28507c311f3bb20a 100644 --- a/tests/Query/GeoDistanceQueryTest.php +++ b/tests/Query/GeoDistanceQueryTest.php @@ -57,6 +57,6 @@ class GeoDistanceQueryTest extends \PHPUnit_Framework_TestCase { $query = new GeoDistanceQuery($field, $distance, $location, $parameters); $result = $query->toArray(); - $this->assertEquals($expected, $result); + $this->assertEquals(['geo_distance' => $expected], $result); } } diff --git a/tests/Query/GeoDistanceRangeQueryTest.php b/tests/Query/GeoDistanceRangeQueryTest.php index d8e5227cd3ec066747248cf525867c73f79d99b5..039465830f80bac28e8929d9a01d7f7c6efef769 100644 --- a/tests/Query/GeoDistanceRangeQueryTest.php +++ b/tests/Query/GeoDistanceRangeQueryTest.php @@ -57,6 +57,6 @@ class GeoDistanceRangeQueryTest extends \PHPUnit_Framework_TestCase { $query = new GeoDistanceRangeQuery($field, $range, $location, $parameters); $result = $query->toArray(); - $this->assertEquals($expected, $result); + $this->assertEquals(['geo_distance_range' => $expected], $result); } } diff --git a/tests/Query/GeoPolygonQueryTest.php b/tests/Query/GeoPolygonQueryTest.php index abf344f036c7969e4cdfa491be29e60a61957469..3f8d09fbe33bd543a6c9a8b6c1b1195232160db1 100644 --- a/tests/Query/GeoPolygonQueryTest.php +++ b/tests/Query/GeoPolygonQueryTest.php @@ -83,6 +83,6 @@ class GeoPolygonQueryTest extends \PHPUnit_Framework_TestCase { $filter = new GeoPolygonQuery($field, $points, $parameters); $result = $filter->toArray(); - $this->assertEquals($expected, $result); + $this->assertEquals(['geo_polygon' => $expected], $result); } } diff --git a/tests/Query/GeoShapeQueryTest.php b/tests/Query/GeoShapeQueryTest.php index 47ac4965b3c8e462834a166a8f6190cf6b0a82e9..f18887857b91eb4197c279ab9ef9434edd27f125 100644 --- a/tests/Query/GeoShapeQueryTest.php +++ b/tests/Query/GeoShapeQueryTest.php @@ -24,13 +24,15 @@ class GeoShapeQueryTest extends \PHPUnit_Framework_TestCase $filter->addShape('location', 'envelope', [[13, 53], [14, 52]]); $expected = [ - 'location' => [ - 'shape' => [ - 'type' => 'envelope', - 'coordinates' => [[13, 53], [14, 52]], + 'geo_shape' => [ + 'location' => [ + 'shape' => [ + 'type' => 'envelope', + 'coordinates' => [[13, 53], [14, 52]], + ], ], + 'param1' => 'value1', ], - 'param1' => 'value1', ]; $this->assertEquals($expected, $filter->toArray()); @@ -45,15 +47,17 @@ class GeoShapeQueryTest extends \PHPUnit_Framework_TestCase $filter->addPreIndexedShape('location', 'DEU', 'countries', 'shapes', 'location'); $expected = [ - 'location' => [ - 'indexed_shape' => [ - 'id' => 'DEU', - 'type' => 'countries', - 'index' => 'shapes', - 'path' => 'location', + 'geo_shape' => [ + 'location' => [ + 'indexed_shape' => [ + 'id' => 'DEU', + 'type' => 'countries', + 'index' => 'shapes', + 'path' => 'location', + ], ], + 'param1' => 'value1', ], - 'param1' => 'value1', ]; $this->assertEquals($expected, $filter->toArray()); diff --git a/tests/Query/GeohashCellQueryTest.php b/tests/Query/GeohashCellQueryTest.php index 208e09fa2c3e86ebdfda5dc34b5a5ae018b8af94..91687e63409d3a542d5d3c3990c825e0b82e512c 100644 --- a/tests/Query/GeohashCellQueryTest.php +++ b/tests/Query/GeohashCellQueryTest.php @@ -54,6 +54,6 @@ class GeohashCellQueryTest extends \PHPUnit_Framework_TestCase { $query = new GeohashCellQuery($field, $location, $parameters); $result = $query->toArray(); - $this->assertEquals($expected, $result); + $this->assertEquals(['geohash_cell' => $expected], $result); } } diff --git a/tests/Query/LimitQueryTest.php b/tests/Query/LimitQueryTest.php index c2aba613f8de7c404b51508a09e81d4a57ef7b81..d1ddcb93ff7b87dc76312955a124d91be71adcf3 100644 --- a/tests/Query/LimitQueryTest.php +++ b/tests/Query/LimitQueryTest.php @@ -21,7 +21,9 @@ class LimitQueryTest extends \PHPUnit_Framework_TestCase public function testToArray() { $query = new LimitQuery(3); - $expectedResult = ['value' => 3]; + $expectedResult = [ + 'limit' => ['value' => 3] + ]; $this->assertEquals($expectedResult, $query->toArray()); } } diff --git a/tests/Query/MissingQueryTest.php b/tests/Query/MissingQueryTest.php index f571aeb1a985b1ea39aafe57db3b7ce103ff8251..710af1795679efa3e057443cb6172766ac400e52 100644 --- a/tests/Query/MissingQueryTest.php +++ b/tests/Query/MissingQueryTest.php @@ -43,6 +43,6 @@ class MissingQueryTest extends \PHPUnit_Framework_TestCase { $query = new MissingQuery($field, $parameters); $result = $query->toArray(); - $this->assertEquals($expected, $result); + $this->assertEquals(['missing' => $expected], $result); } } diff --git a/tests/Query/NestedQueryTest.php b/tests/Query/NestedQueryTest.php index 6a21f7f705f6e06ca8751c54541970f3b77f735f..a5beaa02fc498bac87dd100fa30b13b12bb1c37c 100644 --- a/tests/Query/NestedQueryTest.php +++ b/tests/Query/NestedQueryTest.php @@ -11,10 +11,8 @@ namespace ONGR\ElasticsearchDSL\Tests\Query; -use ONGR\ElasticsearchDSL\Query\BoolQuery; use ONGR\ElasticsearchDSL\Query\NestedQuery; use ONGR\ElasticsearchDSL\Query\TermsQuery; -use ONGR\ElasticsearchDSL\Query\TermQuery; class NestedQueryTest extends \PHPUnit_Framework_TestCase { @@ -64,32 +62,6 @@ class NestedQueryTest extends \PHPUnit_Framework_TestCase $query = new TermsQuery('foo', 'bar'); $query = new NestedQuery($path, $query, $parameters); $result = $query->toArray(); - $this->assertEquals($expected, $result); - } - - /** - * Test for toArray() in case bool with single clause given. - */ - public function testSingleBoolMust() - { - $bool = new BoolQuery(); - $bool->add(new TermQuery('field1', 'value1')); - - $query = new NestedQuery('obj1', $bool); - - $expected = [ - 'path' => 'obj1', - 'query' => [ - 'bool' => [ - 'must' => [ - [ - 'term' => ['field1' => 'value1'], - ], - ], - ], - ], - ]; - - $this->assertEquals($expected, $query->toArray()); + $this->assertEquals(['nested' => $expected], $result); } } diff --git a/tests/Query/ScriptQueryTest.php b/tests/Query/ScriptQueryTest.php index 12aab734c835dfd916d1c9cd1477cc37a4b4c896..083ddf4778c3a9c73a0b027fc4f3a2243b043beb 100644 --- a/tests/Query/ScriptQueryTest.php +++ b/tests/Query/ScriptQueryTest.php @@ -37,11 +37,11 @@ class ScriptQueryTest extends \PHPUnit_Framework_TestCase } /** - * Test for filter toArray() method. + * Test for toArray(). * - * @param string $script Script. - * @param array $parameters Optional parameters. - * @param array $expected Expected values. + * @param string $script Script + * @param array $parameters Optional parameters + * @param array $expected Expected values * * @dataProvider getArrayDataProvider */ @@ -49,6 +49,6 @@ class ScriptQueryTest extends \PHPUnit_Framework_TestCase { $filter = new ScriptQuery($script, $parameters); $result = $filter->toArray(); - $this->assertEquals($expected, $result); + $this->assertEquals(['script' => $expected], $result); } } diff --git a/tests/Query/Span/SpanFirstQueryTest.php b/tests/Query/Span/SpanFirstQueryTest.php index 33122bbf05a0e526a5f89c6f9d4d220ab27c5afe..819aee94e933c886fbaa8314dbd08e5ace501e69 100644 --- a/tests/Query/Span/SpanFirstQueryTest.php +++ b/tests/Query/Span/SpanFirstQueryTest.php @@ -12,7 +12,6 @@ namespace ONGR\ElasticsearchDSL\Tests\Query\Span; use ONGR\ElasticsearchDSL\Query\Span\SpanFirstQuery; -use ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface; /** * Unit test for SpanFirstQuery. @@ -20,54 +19,25 @@ use ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface; class SpanFirstQueryTest extends \PHPUnit_Framework_TestCase { /** - * @var SpanQueryInterface|\PHPUnit_Framework_MockObject_MockObject + * Tests for toArray(). */ - protected $mock; - - /** - * Create mock object. - */ - protected function setUp() + public function testToArray() { - $this->mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock(); - $this->mock->expects($this->atMost(1)) - ->method('getType') - ->will($this->returnValue('span_or')); - $this->mock->expects($this->atMost(1)) + $mock = $this->getMock('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface'); + $mock + ->expects($this->once()) ->method('toArray') - ->will($this->returnValue(['key' => 'value'])); - } - - /** - * Reset mock object. - */ - public function tearDown() - { - unset($this->mock); - } + ->willReturn(['span_term' => ['user' => 'bob']]); - /** - * Tests toArray method. - */ - public function testSpanFirstQueryToArray() - { - $query = new SpanFirstQuery($this->mock, 5); + $query = new SpanFirstQuery($mock, 5); $result = [ - 'match' => [ - 'span_or' => [ 'key' => 'value'], + 'span_first' => [ + 'match' => [ + 'span_term' => ['user' => 'bob'], + ], + 'end' => 5, ], - 'end' => 5, ]; $this->assertEquals($result, $query->toArray()); } - - /** - * Tests get Type method. - */ - public function testSpanFirstQueryGetType() - { - $query = new SpanFirstQuery($this->mock, 5); - $result = $query->getType(); - $this->assertEquals('span_first', $result); - } } diff --git a/tests/Query/Span/SpanMultiTermQueryTest.php b/tests/Query/Span/SpanMultiTermQueryTest.php index f89c44dfeb8dedd81150fdd1fd14abc17e43b3d7..73c69146aafe3277a4d5e5559873b030c327d3d4 100644 --- a/tests/Query/Span/SpanMultiTermQueryTest.php +++ b/tests/Query/Span/SpanMultiTermQueryTest.php @@ -11,7 +11,6 @@ namespace ONGR\ElasticsearchDSL\Tests\Query\Span; -use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\Query\Span\SpanMultiTermQuery; /** @@ -20,95 +19,25 @@ use ONGR\ElasticsearchDSL\Query\Span\SpanMultiTermQuery; class SpanMultiTermQueryTest extends \PHPUnit_Framework_TestCase { /** - * @var array + * Test for toArray(). */ - protected $mock; - - /** - * Create mock object. - */ - protected function setUp() - { - $allowedQueries = ['\FuzzyQuery', '\PrefixQuery', '\TermQuery', '\WildcardQuery', '\RegexpQuery']; - // Same constructors for all of these queries. - foreach ($allowedQueries as $query) { - $this->mock[$query] = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query' . "{$query}") - ->setConstructorArgs(['field', 'value']) - ->getMock(); - $this->mock[$query]->expects($this->atMost(1)) - ->method('getType') - ->will($this->returnValue('span')); - $this->mock[$query]->expects($this->atMost(1)) - ->method('toArray') - ->will($this->returnValue(['field' => 'value'])); - } - } - - /** - * Reset mock object. - */ - public function tearDown() + public function testToArray() { - unset($this->mock); - } - - /** - * Tests toArray method using these queries: Fuzzy, Prefix, Term, Wildcard, Regexp. - */ - public function testSpanMultiTermQueryToArray() - { - /** @var BuilderInterface $mock */ - $mock = $this->mock; - - foreach ($mock as $mocked) { - $query = new SpanMultiTermQuery($mocked); - $result = [ - 'match' => [ - 'span' => [ - 'field' => 'value', - ], - ], - ]; - $this->assertEquals($result, $query->toArray()); - } - } - - /** - * Tests toArray method using this query: Range. - */ - public function testSpanMultiTermQueryToArrayNext() - { - /** @var BuilderInterface $mock */ - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\RangeQuery') - ->setConstructorArgs(['field', ['gte']]) - ->getMock(); - $mock->expects($this->once()) - ->method('getType') - ->will($this->returnValue('range')); - $mock->expects($this->once()) + $mock = $this->getMock('ONGR\ElasticsearchDSL\BuilderInterface'); + $mock + ->expects($this->once()) ->method('toArray') - ->will($this->returnValue(['field' => ['gte']])); + ->willReturn(['prefix' => ['user' => ['value' => 'ki']]]); $query = new SpanMultiTermQuery($mock); - $result = [ - 'match' => [ - 'range' => [ - 'field' => ['gte'], + $expected = [ + 'span_multi' => [ + 'match' => [ + 'prefix' => ['user' => ['value' => 'ki']], ], ], ]; - $this->assertEquals($result, $query->toArray()); - } - /** - * Tests get Type method. - */ - public function testSpanMultiTermQueryGetType() - { - /** @var BuilderInterface $mock */ - $mock = $this->mock['\FuzzyQuery']; - $query = new SpanMultiTermQuery($mock); - $result = $query->getType(); - $this->assertEquals('span_multi', $result); + $this->assertEquals($expected, $query->toArray()); } } diff --git a/tests/Query/Span/SpanNearQueryTest.php b/tests/Query/Span/SpanNearQueryTest.php index 0843dfa7cd3d6127f9aca5b83a87d04494886a3e..cbfb5e86387ec36e33234c9eb4a6650d071c30ef 100644 --- a/tests/Query/Span/SpanNearQueryTest.php +++ b/tests/Query/Span/SpanNearQueryTest.php @@ -12,7 +12,6 @@ namespace ONGR\ElasticsearchDSL\Tests\Query\Span; use ONGR\ElasticsearchDSL\Query\Span\SpanNearQuery; -use ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface; /** * Unit test for SpanNearQuery. @@ -20,61 +19,32 @@ use ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface; class SpanNearQueryTest extends \PHPUnit_Framework_TestCase { /** - * @var SpanQueryInterface|\PHPUnit_Framework_MockObject_MockObject + * Tests for toArray(). */ - protected $mock; - - /** - * Create mock object. - */ - protected function setUp() + public function testToArray() { - $this->mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock(); - $this->mock->expects($this->atMost(1)) - ->method('getType') - ->will($this->returnValue('span_or')); - $this->mock->expects($this->atMost(1)) + $mock = $this->getMock('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface'); + $mock + ->expects($this->once()) ->method('toArray') - ->will($this->returnValue(['key' => 'value'])); - } - - /** - * Reset mock object. - */ - public function tearDown() - { - unset($this->mock); - } + ->willReturn(['span_term' => ['key' => 'value']]); - /** - * Tests toArray method. - */ - public function testSpanMultiTermQueryToArray() - { - $query = new SpanNearQuery(['name']); + $query = new SpanNearQuery(['in_order' => false]); $query->setSlop(5); - $query->addQuery($this->mock); + $query->addQuery($mock); $result = [ - 'clauses' => [ - 0 => [ - 'span_or' => [ - 'key' => 'value', + 'span_near' => [ + 'clauses' => [ + 0 => [ + 'span_term' => [ + 'key' => 'value', + ], ], ], + 'slop' => 5, + 'in_order' => false, ], - 'slop' => 5, - 0 => 'name', ]; $this->assertEquals($result, $query->toArray()); } - - /** - * Tests get Type method. - */ - public function testSpanNearQueryGetType() - { - $query = new SpanNearQuery(['name']); - $result = $query->getType(); - $this->assertEquals('span_near', $result); - } } diff --git a/tests/Query/Span/SpanNotQueryTest.php b/tests/Query/Span/SpanNotQueryTest.php index d1a227ac3aef46a506d711baa7569319a67c8010..2d5a8f5b57bc36851dcd4bb2836a0bc9bfd6cde4 100644 --- a/tests/Query/Span/SpanNotQueryTest.php +++ b/tests/Query/Span/SpanNotQueryTest.php @@ -12,7 +12,6 @@ namespace ONGR\ElasticsearchDSL\Tests\Query\Span; use ONGR\ElasticsearchDSL\Query\Span\SpanNotQuery; -use ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface; /** * Unit test for SpanNotQuery. @@ -20,54 +19,25 @@ use ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface; class SpanNotQueryTest extends \PHPUnit_Framework_TestCase { /** - * @var SpanQueryInterface|\PHPUnit_Framework_MockObject_MockObject + * Tests for toArray(). */ - protected $mock; - - /** - * Create mock object. - */ - protected function setUp() + public function testSpanNotQueryToArray() { - $this->mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock(); - $this->mock->expects($this->atMost(2)) - ->method('getType') - ->will($this->returnValue('span_or')); - $this->mock->expects($this->atMost(2)) + $mock = $this->getMock('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface'); + $mock + ->expects($this->exactly(2)) ->method('toArray') - ->will($this->returnValue(['key' => 'value'])); - } - - /** - * Reset mock object. - */ - public function tearDown() - { - unset($this->mock); - } + ->willReturn(['span_term' => ['key' => 'value']]); - /** - * Tests get Type method. - */ - public function testSpanNotQueryGetType() - { - $query = new SpanNotQuery($this->mock, $this->mock); - $result = $query->getType(); - $this->assertEquals('span_not', $result); - } - - /** - * Tests toArray method. - */ - public function testSpanNotQueryToArray() - { - $query = new SpanNotQuery($this->mock, $this->mock); + $query = new SpanNotQuery($mock, $mock); $result = [ - 'include' => [ - 'span_or' => ['key' => 'value'], - ], - 'exclude' => [ - 'span_or' => ['key' => 'value'], + 'span_not' => [ + 'include' => [ + 'span_term' => ['key' => 'value'], + ], + 'exclude' => [ + 'span_term' => ['key' => 'value'], + ], ], ]; $this->assertEquals($result, $query->toArray()); diff --git a/tests/Query/Span/SpanOrQueryTest.php b/tests/Query/Span/SpanOrQueryTest.php index d41c8915bde5f058ab41c53166af8f92b28ea9e1..10efb9b1e0a17cfea1519c5df4b42ec60784a00d 100644 --- a/tests/Query/Span/SpanOrQueryTest.php +++ b/tests/Query/Span/SpanOrQueryTest.php @@ -12,7 +12,6 @@ namespace ONGR\ElasticsearchDSL\Tests\Query\Span; use ONGR\ElasticsearchDSL\Query\Span\SpanOrQuery; -use ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface; /** * Unit test for SpanOrQuery. @@ -20,53 +19,24 @@ use ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface; class SpanOrQueryTest extends \PHPUnit_Framework_TestCase { /** - * @var SpanQueryInterface|\PHPUnit_Framework_MockObject_MockObject + * Tests for toArray(). */ - protected $mock; - - /** - * Create mock object. - */ - protected function setUp() + public function testToArray() { - $this->mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock(); - $this->mock->expects($this->atMost(1)) - ->method('getType') - ->will($this->returnValue('span_or')); - $this->mock->expects($this->atMost(1)) + $mock = $this->getMock('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface'); + $mock + ->expects($this->once()) ->method('toArray') - ->will($this->returnValue(['key' => 'value'])); - } - - /** - * Reset mock object. - */ - public function tearDown() - { - unset($this->mock); - } + ->willReturn(['span_term' => ['key' => 'value']]); - /** - * Tests get Type method. - */ - public function testSpanOrQueryGetType() - { - $query = new SpanOrQuery(); - $result = $query->getType(); - $this->assertEquals('span_or', $result); - } - - /** - * Tests toArray method. - */ - public function testSpanOrQueryToArray() - { $query = new SpanOrQuery(); - $query->addQuery($this->mock); + $query->addQuery($mock); $result = [ - 'clauses' => [ - 0 => [ - 'span_or' => ['key' => 'value'], + 'span_or' => [ + 'clauses' => [ + 0 => [ + 'span_term' => ['key' => 'value'], + ], ], ], ]; diff --git a/tests/Query/Span/SpanTermQueryTest.php b/tests/Query/Span/SpanTermQueryTest.php index 3220c169a94dee3a9e9fd7d0967429e6a1ce35d3..075c1221b85c340d04c3e5d14c4e3b906b70d7aa 100644 --- a/tests/Query/Span/SpanTermQueryTest.php +++ b/tests/Query/Span/SpanTermQueryTest.php @@ -19,12 +19,28 @@ use ONGR\ElasticsearchDSL\Query\Span\SpanTermQuery; class SpanTermQueryTest extends \PHPUnit_Framework_TestCase { /** - * Tests get Type method. + * Tests for toArray(). */ - public function testSpanTermQueryGetType() + public function testToArray() { - $query = new SpanTermQuery('field', 'value'); - $result = $query->getType(); - $this->assertEquals('span_term', $result); + $query = new SpanTermQuery('user', 'bob'); + $expected = [ + 'span_term' => ['user' => 'bob'], + ]; + + $this->assertEquals($expected, $query->toArray()); + } + + /** + * Tests for toArray() with parameters. + */ + public function testToArrayWithParameters() + { + $query = new SpanTermQuery('user', 'bob', ['boost' => 2]); + $expected = [ + 'span_term' => ['user' => ['value' => 'bob', 'boost' => 2]], + ]; + + $this->assertEquals($expected, $query->toArray()); } } diff --git a/tests/Query/TypeQueryTest.php b/tests/Query/TypeQueryTest.php index f0284cbff0db6f04e33d646ef84bdc23783225fb..dc2bad82da182d78bb7ba85dddc1667452c95e1b 100644 --- a/tests/Query/TypeQueryTest.php +++ b/tests/Query/TypeQueryTest.php @@ -21,7 +21,10 @@ class TypeQueryTest extends \PHPUnit_Framework_TestCase public function testToArray() { $query = new TypeQuery('foo'); - $expectedResult = ['value' => 'foo']; + $expectedResult = [ + 'type' => ['value' => 'foo'] + ]; + $this->assertEquals($expectedResult, $query->toArray()); } } diff --git a/tests/SearchEndpoint/PostFilterEndpointTest.php b/tests/SearchEndpoint/PostFilterEndpointTest.php index 297249b762aa67b8f0bf6ecc43d8f57b1f259983..1e9ab86c55f71dea7da44264511fce0366bd31c0 100644 --- a/tests/SearchEndpoint/PostFilterEndpointTest.php +++ b/tests/SearchEndpoint/PostFilterEndpointTest.php @@ -54,7 +54,7 @@ class PostFilterEndpointTest extends \PHPUnit_Framework_TestCase $instance->add($matchAll); $this->assertEquals( - json_encode(['bool' => ['must' => [[$matchAll->getType() => $matchAll->toArray()]]]]), + json_encode($matchAll->toArray()), json_encode($instance->normalize($normalizerInterface)) ); } diff --git a/tests/SearchEndpoint/QueryEndpointTest.php b/tests/SearchEndpoint/QueryEndpointTest.php index 997f82f75c9010fca0a18e4a15074ac8f74f263a..b626caa12cff77a8adaece25ab9a0ff0947df3fe 100644 --- a/tests/SearchEndpoint/QueryEndpointTest.php +++ b/tests/SearchEndpoint/QueryEndpointTest.php @@ -55,7 +55,7 @@ class QueryEndpointTest extends \PHPUnit_Framework_TestCase $instance->add($matchAll); $this->assertEquals( - ['bool' => ['must' => [[$matchAll->getType() => $matchAll->toArray()]]]], + $matchAll->toArray(), $instance->normalize($normalizerInterface) ); } diff --git a/tests/SearchTest.php b/tests/SearchTest.php index d4f50b5289c314a26faf9fb6aac4ca8f8af22f0b..23ed4c2d0e097a183597f2d5004f0edd17053409 100644 --- a/tests/SearchTest.php +++ b/tests/SearchTest.php @@ -203,11 +203,7 @@ class SearchTest extends \PHPUnit_Framework_TestCase $cases['single_term_query'] = [ [ 'query' => [ - 'bool' => [ - 'must' => [ - ['term' => ['foo' => 'bar']], - ], - ], + 'term' => ['foo' => 'bar'], ], ], (new Search())->addQuery(new TermQuery('foo', 'bar')), @@ -219,11 +215,7 @@ class SearchTest extends \PHPUnit_Framework_TestCase 'bool' => [ 'filter' => [ [ - 'bool' => [ - 'must' => [ - ['term' => ['foo' => 'bar']], - ], - ], + 'term' => ['foo' => 'bar'], ], ], ], @@ -241,11 +233,7 @@ class SearchTest extends \PHPUnit_Framework_TestCase ], 'filter' => [ [ - 'bool' => [ - 'must' => [ - ['missing' => ['field' => 'baz']], - ], - ], + 'missing' => ['field' => 'baz'], ], ], ],