From 4934515e69d18bb68432a0ba180b006326b29db1 Mon Sep 17 00:00:00 2001 From: Mantas Varatiejus <mantas.varatiejus@nfq.com> Date: Tue, 26 Jan 2016 12:59:16 +0200 Subject: [PATCH] Include query type in query's toArray() response --- CHANGELOG.md | 1 + src/Aggregation/FilterAggregation.php | 4 +- src/Aggregation/TopHitsAggregation.php | 13 ++- src/BuilderInterface.php | 5 +- src/Query/BoolQuery.php | 23 ++--- src/Query/BoostingQuery.php | 6 +- src/Query/CommonTermsQuery.php | 2 +- src/Query/ConstantScoreQuery.php | 6 +- src/Query/DisMaxQuery.php | 6 +- src/Query/ExistsQuery.php | 4 +- src/Query/FunctionScoreQuery.php | 18 ++-- src/Query/FuzzyLikeThisFieldQuery.php | 69 -------------- src/Query/FuzzyLikeThisQuery.php | 70 -------------- src/Query/FuzzyQuery.php | 2 +- src/Query/GeoBoundingBoxQuery.php | 2 +- src/Query/GeoDistanceQuery.php | 2 +- src/Query/GeoDistanceRangeQuery.php | 2 +- src/Query/GeoPolygonQuery.php | 2 +- src/Query/GeoShapeQuery.php | 2 +- src/Query/GeohashCellQuery.php | 2 +- src/Query/HasChildQuery.php | 6 +- src/Query/HasParentQuery.php | 6 +- src/Query/IdsQuery.php | 2 +- src/Query/IndicesQuery.php | 6 +- src/Query/LimitQuery.php | 4 +- src/Query/MatchAllQuery.php | 8 +- src/Query/MatchQuery.php | 2 +- src/Query/MissingQuery.php | 2 +- src/Query/MoreLikeThisQuery.php | 2 +- src/Query/MultiMatchQuery.php | 2 +- src/Query/NestedQuery.php | 16 ++-- src/Query/PrefixQuery.php | 2 +- src/Query/QueryStringQuery.php | 2 +- src/Query/RangeQuery.php | 2 +- src/Query/RegexpQuery.php | 2 +- src/Query/ScriptQuery.php | 2 +- src/Query/SimpleQueryStringQuery.php | 2 +- src/Query/Span/SpanFirstQuery.php | 6 +- src/Query/Span/SpanMultiTermQuery.php | 6 +- src/Query/Span/SpanNearQuery.php | 6 +- src/Query/Span/SpanNotQuery.php | 10 +- src/Query/Span/SpanOrQuery.php | 6 +- src/Query/Span/SpanTermQuery.php | 2 + src/Query/TermQuery.php | 2 +- src/Query/TermsQuery.php | 2 +- src/Query/TypeQuery.php | 4 +- src/Query/WildcardQuery.php | 2 +- src/SearchEndpoint/PostFilterEndpoint.php | 4 +- src/SearchEndpoint/QueryEndpoint.php | 2 +- src/Sort/FieldSort.php | 6 +- tests/Aggregation/FilterAggregationTest.php | 18 +--- tests/Query/BoolQueryTest.php | 83 +++++++++-------- tests/Query/ExistsQueryTest.php | 4 +- tests/Query/FunctionScoreQueryTest.php | 12 +-- tests/Query/FuzzyLikeThisQueryTest.php | 76 --------------- tests/Query/GeoBoundingBoxQueryTest.php | 2 +- tests/Query/GeoDistanceQueryTest.php | 2 +- tests/Query/GeoDistanceRangeQueryTest.php | 2 +- tests/Query/GeoPolygonQueryTest.php | 2 +- tests/Query/GeoShapeQueryTest.php | 28 +++--- tests/Query/GeohashCellQueryTest.php | 2 +- tests/Query/LimitQueryTest.php | 4 +- tests/Query/MissingQueryTest.php | 2 +- tests/Query/NestedQueryTest.php | 30 +----- tests/Query/ScriptQueryTest.php | 10 +- tests/Query/Span/SpanFirstQueryTest.php | 54 +++-------- tests/Query/Span/SpanMultiTermQueryTest.php | 93 +++---------------- tests/Query/Span/SpanNearQueryTest.php | 62 ++++--------- tests/Query/Span/SpanNotQueryTest.php | 58 +++--------- tests/Query/Span/SpanOrQueryTest.php | 54 +++-------- tests/Query/Span/SpanTermQueryTest.php | 26 +++++- tests/Query/TypeQueryTest.php | 5 +- .../SearchEndpoint/PostFilterEndpointTest.php | 2 +- tests/SearchEndpoint/QueryEndpointTest.php | 2 +- tests/SearchTest.php | 18 +--- 75 files changed, 292 insertions(+), 726 deletions(-) delete mode 100644 src/Query/FuzzyLikeThisFieldQuery.php delete mode 100644 src/Query/FuzzyLikeThisQuery.php delete mode 100644 tests/Query/FuzzyLikeThisQueryTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 9796b50..a1b1eaa 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 2e512ba..1782b46 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 e88e996..d315bcd 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 d30cefb..de07683 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 ed65f90..8c49911 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 0365482..c88f0eb 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 cc70fb8..def85c0 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 bb3dc5a..bafba28 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 de539d8..49c68b9 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 1c62382..fd7f4de 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 3f76121..c5f970c 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 a4dbbcf..0000000 --- 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 4b402cf..0000000 --- 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 e22a475..27f8846 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 f77ce22..2940eb7 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 477d872..d124179 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 ab9a8a3..ef3544d 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 680ed8e..eecf0e4 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 4360c85..ae481db 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 8c54a7d..9aa15c7 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 e122140..9e5044e 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 4b98a0e..c57bca4 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 60cf333..fa55b73 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 501ba16..30160b7 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 939b913..ced3a81 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 88ce0b6..ec9fde7 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 4815ed2..05f799a 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 2acb7db..f6d0f3b 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 1b72039..ce4e492 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 ccf608d..0fab953 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 76a30aa..0b020c9 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 9f99411..903218f 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 684e8bd..5863bb9 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 26a8338..75c446a 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 8887000..0752503 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 805907b..1a1a0c7 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 3c08331..960f04d 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 40d222d..46f16ef 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 c176188..9042c59 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 f71a0cc..5643603 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 9e2359a..7e34339 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 5b3c432..9f3ec54 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 2a898e4..bbb036c 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 d52fff9..783bb6e 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 0c5e544..647078f 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 3c2ed25..04c62c0 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 eed2834..a998221 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 4eff244..cacb662 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 e7f040c..41062b3 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 4223eae..2f3eeeb 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 6f8fe99..fcc432d 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 dd0147f..af0cce8 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 4695fad..292b8ad 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 7e2fbb4..5f0bb5a 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 ce68de1..0000000 --- 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 65b329a..c192fc8 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 a69a4ab..496bec8 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 d8e5227..0394658 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 abf344f..3f8d09f 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 47ac496..f188878 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 208e09f..91687e6 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 c2aba61..d1ddcb9 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 f571aeb..710af17 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 6a21f7f..a5beaa0 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 12aab73..083ddf4 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 33122bb..819aee9 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 f89c44d..73c6914 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 0843dfa..cbfb5e8 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 d1a227a..2d5a8f5 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 d41c891..10efb9b 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 3220c16..075c122 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 f0284cb..dc2bad8 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 297249b..1e9ab86 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 997f82f..b626caa 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 d4f50b5..23ed4c2 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'], ], ], ], -- GitLab