From 8d36311399563d74db2d0fe24ee237dc3000eba3 Mon Sep 17 00:00:00 2001 From: Mantas Varatiejus <mantas.varatiejus@nfq.com> Date: Thu, 21 Jan 2016 17:09:42 +0200 Subject: [PATCH] Update docs --- docs/Aggregation/Filter.md | 2 +- docs/Aggregation/Filters.md | 8 ++-- docs/HowTo/HowToSearch.md | 15 +++---- docs/Query/ConstantScore.md | 2 +- docs/Query/Filtered.md | 60 --------------------------- docs/Query/FunctionScore.md | 6 +-- docs/Query/index.md | 1 - docs/index.md | 1 - src/DslTypeAwareTrait.php | 50 ----------------------- src/Query/FunctionScoreQuery.php | 51 +++++++++++------------ src/SearchEndpoint/QueryEndpoint.php | 8 +++- tests/DslTypeAwareTraitTest.php | 61 ---------------------------- tests/SearchTest.php | 3 ++ 13 files changed, 49 insertions(+), 219 deletions(-) delete mode 100644 docs/Query/Filtered.md delete mode 100644 src/DslTypeAwareTrait.php delete mode 100644 tests/DslTypeAwareTraitTest.php diff --git a/docs/Aggregation/Filter.md b/docs/Aggregation/Filter.md index 96a2dbb..62d5c7b 100644 --- a/docs/Aggregation/Filter.md +++ b/docs/Aggregation/Filter.md @@ -24,7 +24,7 @@ context to a specific set of documents. And now the query via DSL: ```php -$termFilter = new TermFilter('color', 'red'); +$termFilter = new TermQuery('color', 'red'); $avgAggregation = new AvgAggregation('avg_price', 'price'); $filterAggregation = new FilterAggregation('grades_stats', $termFilter); diff --git a/docs/Aggregation/Filters.md b/docs/Aggregation/Filters.md index b36fecc..26b7c4d 100644 --- a/docs/Aggregation/Filters.md +++ b/docs/Aggregation/Filters.md @@ -37,8 +37,8 @@ in exception. And now the query via DSL: ```php -$errorTermFilter = new TermFilter('body', 'error'); -$warningTermFilter = new TermFilter('body', 'warning'); +$errorTermFilter = new TermQuery('body', 'error'); +$warningTermFilter = new TermQuery('body', 'warning'); $histogramAggregation = new HistogramAggregation('monthly', 'timestamp'); $histogramAggregation->setInterval('1M'); @@ -86,8 +86,8 @@ $queryArray = $search->toArray(); And now the query via DSL: ```php -$errorTermFilter = new TermFilter('body', 'error'); -$warningTermFilter = new TermFilter('body', 'warning'); +$errorTermFilter = new TermQuery('body', 'error'); +$warningTermFilter = new TermQuery('body', 'warning'); $histogramAggregation = new HistogramAggregation('monthly', 'timestamp'); $histogramAggregation->setInterval('1M'); diff --git a/docs/HowTo/HowToSearch.md b/docs/HowTo/HowToSearch.md index 36e3229..3541c2b 100644 --- a/docs/HowTo/HowToSearch.md +++ b/docs/HowTo/HowToSearch.md @@ -12,7 +12,7 @@ $search = new Search(); So, when we have a `Search` object we can start add something to it. Usually you will add `Query`, `Filter` and `Aggregation`. -> More info how create [queries](../Query/index.md), [filters](../Filter/index.md) and [aggregations](../Aggregation/index.md) objects. +> More info how create [queries](../Query/index.md) and [aggregations](../Aggregation/index.md) objects. ### Form a Query @@ -43,11 +43,8 @@ At the end it will form this query: ### Form a Filter -> Since Elasticsearch 2.0 all filters were replaced by queries. Queries acts like -> filters when you use them in filter context. For easier future migration use query -> classes instead of filter. (Note, for Elasticsearch 1.* you still should use `ScriptFilter`, -> `HasChildFilter`, `HasParentFilter`, `IndicesFilter`, `NestedFilter`, `PrefixFilter`, -> `RegexpFilter`, `TermFilter` instead of query as they has different structure.) +Since Elasticsearch 2.0 all filters were replaced by queries. Queries acts like +filters when you use them in filter context. To add a filter is the same way like a query. First, lets create some `Filter` object. @@ -66,7 +63,7 @@ Unlike `Query`, when we add a `Filter` with our DSL library it will add a query ```JSON { "query": { - "filtered": { + "bool": { "filter": { "match_all": {} } @@ -120,7 +117,7 @@ The same way it works with a `Filter`. Take a look at this example: ```php $search = new Search(); -$termFilter = new TermFilter('name', 'ongr'); +$termFilter = new TermQuery('name', 'ongr'); $missingFilter = new MissingQuery('disabled'); $existsFilter = new ExistsQuery('tag'); $search->addFilter($termFilter); @@ -133,7 +130,7 @@ Elasticsearch DSL will form this query: ```JSON { "query": { - "filtered": { + "bool": { "filter": { "bool": { "must": [ diff --git a/docs/Query/ConstantScore.md b/docs/Query/ConstantScore.md index dda1c0d..733c450 100644 --- a/docs/Query/ConstantScore.md +++ b/docs/Query/ConstantScore.md @@ -20,7 +20,7 @@ Lets take an example to write a constant score query with filter inside. And now the query via DSL: ```php -$termFilter = new TermFilter("user", "kimchy"); +$termFilter = new TermQuery("user", "kimchy"); $constantScoreQuery = new ConstantScoreQuery($termFilter, ["boost" => 1.2]); $search = new Search(); diff --git a/docs/Query/Filtered.md b/docs/Query/Filtered.md deleted file mode 100644 index ab7444c..0000000 --- a/docs/Query/Filtered.md +++ /dev/null @@ -1,60 +0,0 @@ -# Filtered query - -__DEPRECATED__: filtered query is deprecated and will be removed in ElasticsearchDSL 2.0 - -> More info about filtered query is in the [official elasticsearch docs][1] - -The filtered query is used to combine another query with any filter. Filters are usually faster than queries. - -Lets try to write this example -```JSON -{ - "filtered": { - "query": { - "match": { "tweet": "full text search" } - }, - "filter": { - "range": { "created": { "gte": "now - 1d / d" }} - } - } -} -``` - -In DSL: - -```php -$matchQuery = new MatchQuery('tweet', 'full text search'); -$rangeFilter = new RangeFilter('created', ['gte' => 'now - 1d / d']); - -$filteredQuery = new FilteredQuery($matchQuery, $rangeFilter); - -$search = new Search(); -$search->addQuery($filteredQuery); - -$queryArray = $search->toArray(); -``` - -Or: - -```php -$matchQuery = new MatchQuery('tweet', 'full text search'); -$rangeFilter = new RangeFilter('created', ['gte' => 'now - 1d / d']); - -$filteredQuery = new FilteredQuery(); -$filteredQuery->setQuery($matchQuery); -$filteredQuery->setFilter($rangeFilter); - -$search = new Search(); -$search->addQuery($filteredQuery); - -$queryArray = $search->toArray(); -``` - -Multiple queries and filters can be used with help off [Bool Query][2] and [Bool Filter][3] respectively. - -If query is not set it defaults to Match all, thus Filtered query can be used as filter in places where query is -expected. - -[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html -[2]: Bool.md -[3]: ../Filter/Bool.md diff --git a/docs/Query/FunctionScore.md b/docs/Query/FunctionScore.md index 219ccf9..c45cfc0 100644 --- a/docs/Query/FunctionScore.md +++ b/docs/Query/FunctionScore.md @@ -91,7 +91,7 @@ In DSL: ```php $functionScoreQuery = new FunctionScoreQuery(new MatchAllQuery()); -$rangeFilter = new RangeFilter('price', ['gte' => 10, 'lte' => 100]); +$rangeFilter = new RangeQuery('price', ['gte' => 10, 'lte' => 100]); $functionScoreQuery->addWeightFunction(2, $rangeFilter); $search = new Search(); @@ -104,8 +104,8 @@ $queryArray = $search->toArray(); ```php $functionScoreQuery = new FunctionScoreQuery(new MatchAllQuery()); -$existsFilter = new ExistsFilter('price'); -$functionScoreQuery->addFieldValueFactorFunction('price', 0.5, 'ln', $existsFilter); +$existsQuery = new ExistsQuery('price'); +$functionScoreQuery->addFieldValueFactorFunction('price', 0.5, 'ln', $existsQuery); $search = new Search(); $search->addQuery($functionScoreQuery); diff --git a/docs/Query/index.md b/docs/Query/index.md index 3345e50..1540fa0 100644 --- a/docs/Query/index.md +++ b/docs/Query/index.md @@ -32,7 +32,6 @@ For more information how to combine search queries take a look at [How to search - [Common terms](CommonTerms.md) - [Constant Score](ConstantScore.md) - [DisMax](DisMax.md) - - [Filtered](Filtered.md) - [Function score](FunctionScore.md) - [Fuzzy](Fuzzy.md) - [Fuzzy like this field](FuzzyLikeThisField.md) diff --git a/docs/index.md b/docs/index.md index 937fd46..b14009c 100644 --- a/docs/index.md +++ b/docs/index.md @@ -6,7 +6,6 @@ Everything starts from the `Search` object. We recommend first to take a look at ### Topics: - [Build Queries](Query/index.md) -- [Build Filters](Filter/index.md) - [Build Aggregations](Aggregation/index.md) ### How to diff --git a/src/DslTypeAwareTrait.php b/src/DslTypeAwareTrait.php deleted file mode 100644 index 0b3ead2..0000000 --- a/src/DslTypeAwareTrait.php +++ /dev/null @@ -1,50 +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; - -/** - * A trait which handles dsl type. - * - * @deprecated Will be removed in 2.0. - */ -trait DslTypeAwareTrait -{ - /** - * @var string - */ - private $dslType; - - /** - * Returns a dsl type. - * - * @return string - */ - public function getDslType() - { - return $this->dslType; - } - - /** - * Sets a dsl type. - * - * @param string $dslType - * - * @throws \InvalidArgumentException - */ - public function setDslType($dslType) - { - if ($dslType !== 'filter' && $dslType !== 'query') { - throw new \InvalidArgumentException('Not supported dsl type'); - } - $this->dslType = $dslType; - } -} diff --git a/src/Query/FunctionScoreQuery.php b/src/Query/FunctionScoreQuery.php index 391121f..3f76121 100644 --- a/src/Query/FunctionScoreQuery.php +++ b/src/Query/FunctionScoreQuery.php @@ -12,7 +12,6 @@ namespace ONGR\ElasticsearchDSL\Query; use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\DslTypeAwareTrait; use ONGR\ElasticsearchDSL\ParametersTrait; /** @@ -21,7 +20,6 @@ use ONGR\ElasticsearchDSL\ParametersTrait; class FunctionScoreQuery implements BuilderInterface { use ParametersTrait; - use DslTypeAwareTrait; /** * Query of filter. @@ -45,7 +43,6 @@ class FunctionScoreQuery implements BuilderInterface { $this->query = $query; $this->setParameters($parameters); - $this->setDslType('query'); } /** @@ -57,16 +54,16 @@ class FunctionScoreQuery implements BuilderInterface } /** - * Modifier to apply filter to the function score function. + * Modifier to apply query to the function score function. * * @param array $function - * @param BuilderInterface $filter + * @param BuilderInterface $query */ - private function applyFilter(array &$function, BuilderInterface $filter = null) + private function applyQuery(array &$function, BuilderInterface $query = null) { - if ($filter) { - $function['filter'] = [ - $filter->getType() => $filter->toArray(), + if ($query) { + $function['query'] = [ + $query->getType() => $query->toArray(), ]; } } @@ -77,11 +74,11 @@ class FunctionScoreQuery implements BuilderInterface * @param string $field * @param float $factor * @param string $modifier - * @param BuilderInterface $filter + * @param BuilderInterface $query * * @return $this */ - public function addFieldValueFactorFunction($field, $factor, $modifier = 'none', BuilderInterface $filter = null) + public function addFieldValueFactorFunction($field, $factor, $modifier = 'none', BuilderInterface $query = null) { $function = [ 'field_value_factor' => [ @@ -91,7 +88,7 @@ class FunctionScoreQuery implements BuilderInterface ], ]; - $this->applyFilter($function, $filter); + $this->applyQuery($function, $query); $this->functions[] = $function; @@ -99,13 +96,13 @@ class FunctionScoreQuery implements BuilderInterface } /** - * Add decay function to function score. Weight and filter are optional. + * Add decay function to function score. Weight and query are optional. * * @param string $type * @param string $field * @param array $function * @param array $options - * @param BuilderInterface $filter + * @param BuilderInterface $query * * @return $this */ @@ -114,7 +111,7 @@ class FunctionScoreQuery implements BuilderInterface $field, array $function, array $options = [], - BuilderInterface $filter = null + BuilderInterface $query = null ) { $function = [ $type => array_merge( @@ -123,7 +120,7 @@ class FunctionScoreQuery implements BuilderInterface ), ]; - $this->applyFilter($function, $filter); + $this->applyQuery($function, $query); $this->functions[] = $function; @@ -131,20 +128,20 @@ class FunctionScoreQuery implements BuilderInterface } /** - * Adds function to function score without decay function. Influence search score only for specific filter. + * Adds function to function score without decay function. Influence search score only for specific query. * * @param float $weight - * @param BuilderInterface $filter + * @param BuilderInterface $query * * @return $this */ - public function addWeightFunction($weight, BuilderInterface $filter = null) + public function addWeightFunction($weight, BuilderInterface $query = null) { $function = [ 'weight' => $weight, ]; - $this->applyFilter($function, $filter); + $this->applyQuery($function, $query); $this->functions[] = $function; @@ -155,17 +152,17 @@ class FunctionScoreQuery implements BuilderInterface * Adds random score function. Seed is optional. * * @param mixed $seed - * @param BuilderInterface $filter + * @param BuilderInterface $query * * @return $this */ - public function addRandomFunction($seed = null, BuilderInterface $filter = null) + public function addRandomFunction($seed = null, BuilderInterface $query = null) { $function = [ 'random_score' => $seed ? [ 'seed' => $seed ] : new \stdClass(), ]; - $this->applyFilter($function, $filter); + $this->applyQuery($function, $query); $this->functions[] = $function; @@ -178,7 +175,7 @@ class FunctionScoreQuery implements BuilderInterface * @param string $script * @param array $params * @param array $options - * @param BuilderInterface $filter + * @param BuilderInterface $query * * @return $this */ @@ -186,7 +183,7 @@ class FunctionScoreQuery implements BuilderInterface $script, array $params = [], array $options = [], - BuilderInterface $filter = null + BuilderInterface $query = null ) { $function = [ 'script_score' => array_merge( @@ -198,7 +195,7 @@ class FunctionScoreQuery implements BuilderInterface ), ]; - $this->applyFilter($function, $filter); + $this->applyQuery($function, $query); $this->functions[] = $function; @@ -225,7 +222,7 @@ class FunctionScoreQuery implements BuilderInterface public function toArray() { $query = [ - strtolower($this->getDslType()) => [ + 'query' => [ $this->query->getType() => $this->query->toArray(), ], 'functions' => $this->functions, diff --git a/src/SearchEndpoint/QueryEndpoint.php b/src/SearchEndpoint/QueryEndpoint.php index e3b4c08..e7f040c 100644 --- a/src/SearchEndpoint/QueryEndpoint.php +++ b/src/SearchEndpoint/QueryEndpoint.php @@ -31,15 +31,21 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI */ private $bool; + /** + * @var bool + */ + private $filtersSet = false; + /** * {@inheritdoc} */ public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) { - if ($this->hasReference('filter_query')) { + if (!$this->filtersSet && $this->hasReference('filter_query')) { /** @var BuilderInterface $filter */ $filter = $this->getReference('filter_query'); $this->addToBool($filter, BoolQuery::FILTER); + $this->filtersSet = true; } if (!$this->bool) { diff --git a/tests/DslTypeAwareTraitTest.php b/tests/DslTypeAwareTraitTest.php deleted file mode 100644 index 2b87d14..0000000 --- a/tests/DslTypeAwareTraitTest.php +++ /dev/null @@ -1,61 +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\Unit\DSL; - -use ONGR\ElasticsearchDSL\DslTypeAwareTrait; - -/** - * Test for DslTypeAwareTrait. - */ -class DslTypeAwareTraitTest extends \PHPUnit_Framework_TestCase -{ - /** - * @var DslTypeAwareTrait - */ - private $mock; - - /** - * {@inheritdoc} - */ - public function setUp() - { - $this->mock = $this->getMockForTrait('ONGR\ElasticsearchDSL\DslTypeAwareTrait'); - } - - /** - * Tests if setDslType throws exception. - * - * @expectedException \InvalidArgumentException - */ - public function testIfSetDslTypeExceptionThrowsException() - { - $this->mock->setDslType('foo'); - } - - /** - * Tests if setDslType sets filter. - */ - public function testIfSetDslTypeSetsFilter() - { - $this->mock->setDslType('filter'); - $this->assertEquals('filter', $this->mock->getDslType()); - } - - /** - * Tests if setDslType sets query. - */ - public function testIfSetDslTypeSetsQuery() - { - $this->mock->setDslType('query'); - $this->assertEquals('query', $this->mock->getDslType()); - } -} diff --git a/tests/SearchTest.php b/tests/SearchTest.php index be97ad9..d4f50b5 100644 --- a/tests/SearchTest.php +++ b/tests/SearchTest.php @@ -266,5 +266,8 @@ class SearchTest extends \PHPUnit_Framework_TestCase public function testToArray($expected, $search) { $this->assertEquals($expected, $search->toArray()); + + // Double check + $this->assertEquals($expected, $search->toArray()); } } -- GitLab