From ff0ea8f946a52931cc8153866b9276e09f992429 Mon Sep 17 00:00:00 2001 From: David Causse Date: Thu, 23 Mar 2017 20:06:14 +0100 Subject: [PATCH] Add MatchNone the inverse of MatchAll This query is sometimes handy and I think Elastica should support it. --- CHANGELOG.md | 1 + lib/Elastica/Query/MatchNone.php | 20 ++++++++++ lib/Elastica/QueryBuilder/DSL/Query.php | 13 +++++++ test/Elastica/Query/MatchNoneTest.php | 41 ++++++++++++++++++++ test/Elastica/QueryBuilder/DSL/QueryTest.php | 1 + 5 files changed, 76 insertions(+) create mode 100644 lib/Elastica/Query/MatchNone.php create mode 100644 test/Elastica/Query/MatchNoneTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index ca86d739fc..9c4f25cbe6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file based on the - Added `\Elastica\Client::requestEndpoint`, `\Elastica\Index::requestEndpoint`, `\Elastica\Type::requestEndpoint` that allow make requests with official client Endpoint usage. [#1275](https://github.com/ruflin/Elastica/pull/1275) - Added `\Elastica\Aggregation\GeoBounds` that computes the bounding box containing all geo_point values for a field. [#1271](https://github.com/ruflin/Elastica/pull/1271) +- Added `\Elastica\Query\MatchNone` the inverse of MatchAll. ### Improvements diff --git a/lib/Elastica/Query/MatchNone.php b/lib/Elastica/Query/MatchNone.php new file mode 100644 index 0000000000..952677a649 --- /dev/null +++ b/lib/Elastica/Query/MatchNone.php @@ -0,0 +1,20 @@ +_params = new \stdClass(); + } +} diff --git a/lib/Elastica/QueryBuilder/DSL/Query.php b/lib/Elastica/QueryBuilder/DSL/Query.php index 10ac2dd446..cb6cdc0740 100644 --- a/lib/Elastica/QueryBuilder/DSL/Query.php +++ b/lib/Elastica/QueryBuilder/DSL/Query.php @@ -16,6 +16,7 @@ use Elastica\Query\Ids; use Elastica\Query\Match; use Elastica\Query\MatchAll; +use Elastica\Query\MatchNone; use Elastica\Query\MoreLikeThis; use Elastica\Query\MultiMatch; use Elastica\Query\Nested; @@ -237,6 +238,18 @@ public function match_all() return new MatchAll(); } + /** + * match none query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html#query-dsl-match-none-query + * + * @return MatchNone + */ + public function match_none() + { + return new MatchNone(); + } + /** * more like this query. * diff --git a/test/Elastica/Query/MatchNoneTest.php b/test/Elastica/Query/MatchNoneTest.php new file mode 100644 index 0000000000..f74bc73af4 --- /dev/null +++ b/test/Elastica/Query/MatchNoneTest.php @@ -0,0 +1,41 @@ + new \stdClass()]; + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group functional + */ + public function testMatchNone() + { + $index = $this->_createIndex(); + $client = $index->getClient(); + + $doc = new Document(1, ['name' => 'ruflin']); + $index->getType('test')->addDocument($doc); + + $index->refresh(); + + $search = new Search($client); + $resultSet = $search->search(new MatchNone()); + + $this->assertEquals(0, $resultSet->getTotalHits()); + } +} diff --git a/test/Elastica/QueryBuilder/DSL/QueryTest.php b/test/Elastica/QueryBuilder/DSL/QueryTest.php index fe08af033f..a3805100b6 100644 --- a/test/Elastica/QueryBuilder/DSL/QueryTest.php +++ b/test/Elastica/QueryBuilder/DSL/QueryTest.php @@ -48,6 +48,7 @@ public function testInterface() $this->_assertImplemented($queryDSL, 'ids', Query\Ids::class, ['type', []]); $this->_assertImplemented($queryDSL, 'match', Match::class, ['field', 'values']); $this->_assertImplemented($queryDSL, 'match_all', Query\MatchAll::class, []); + $this->_assertImplemented($queryDSL, 'match_none', Query\MatchNone::class, []); $this->_assertImplemented($queryDSL, 'more_like_this', Query\MoreLikeThis::class, []); $this->_assertImplemented($queryDSL, 'multi_match', Query\MultiMatch::class, []); $this->_assertImplemented($queryDSL, 'nested', Query\Nested::class, []);