Skip to content

Commit 0bedd92

Browse files
nomoaMarek Hernik
authored and
Marek Hernik
committed
Add MatchNone the inverse of MatchAll (ruflin#1276)
This query is sometimes handy and I think Elastica should support it.
1 parent 42ccdc7 commit 0bedd92

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file based on the
1616

1717
- 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)
1818
- 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)
19+
- Added `\Elastica\Query\MatchNone` the inverse of MatchAll.
1920

2021
### Improvements
2122

lib/Elastica/Query/MatchNone.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace Elastica\Query;
3+
4+
/**
5+
* Match none query. Returns no results.
6+
*
7+
* @author David Causse
8+
*
9+
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html#query-dsl-match-none-query
10+
*/
11+
class MatchNone extends AbstractQuery
12+
{
13+
/**
14+
* Creates match none query.
15+
*/
16+
public function __construct()
17+
{
18+
$this->_params = new \stdClass();
19+
}
20+
}

lib/Elastica/QueryBuilder/DSL/Query.php

+13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Elastica\Query\Ids;
1717
use Elastica\Query\Match;
1818
use Elastica\Query\MatchAll;
19+
use Elastica\Query\MatchNone;
1920
use Elastica\Query\MoreLikeThis;
2021
use Elastica\Query\MultiMatch;
2122
use Elastica\Query\Nested;
@@ -237,6 +238,18 @@ public function match_all()
237238
return new MatchAll();
238239
}
239240

241+
/**
242+
* match none query.
243+
*
244+
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html#query-dsl-match-none-query
245+
*
246+
* @return MatchNone
247+
*/
248+
public function match_none()
249+
{
250+
return new MatchNone();
251+
}
252+
240253
/**
241254
* more like this query.
242255
*

test/Elastica/Query/MatchNoneTest.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
namespace Elastica\Test\Query;
3+
4+
use Elastica\Document;
5+
use Elastica\Query\MatchNone;
6+
use Elastica\Search;
7+
use Elastica\Test\Base as BaseTest;
8+
9+
class MatchNoneTest extends BaseTest
10+
{
11+
/**
12+
* @group unit
13+
*/
14+
public function testToArray()
15+
{
16+
$query = new MatchNone();
17+
18+
$expectedArray = ['match_none' => new \stdClass()];
19+
20+
$this->assertEquals($expectedArray, $query->toArray());
21+
}
22+
23+
/**
24+
* @group functional
25+
*/
26+
public function testMatchNone()
27+
{
28+
$index = $this->_createIndex();
29+
$client = $index->getClient();
30+
31+
$doc = new Document(1, ['name' => 'ruflin']);
32+
$index->getType('test')->addDocument($doc);
33+
34+
$index->refresh();
35+
36+
$search = new Search($client);
37+
$resultSet = $search->search(new MatchNone());
38+
39+
$this->assertEquals(0, $resultSet->getTotalHits());
40+
}
41+
}

test/Elastica/QueryBuilder/DSL/QueryTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function testInterface()
4848
$this->_assertImplemented($queryDSL, 'ids', Query\Ids::class, ['type', []]);
4949
$this->_assertImplemented($queryDSL, 'match', Match::class, ['field', 'values']);
5050
$this->_assertImplemented($queryDSL, 'match_all', Query\MatchAll::class, []);
51+
$this->_assertImplemented($queryDSL, 'match_none', Query\MatchNone::class, []);
5152
$this->_assertImplemented($queryDSL, 'more_like_this', Query\MoreLikeThis::class, []);
5253
$this->_assertImplemented($queryDSL, 'multi_match', Query\MultiMatch::class, []);
5354
$this->_assertImplemented($queryDSL, 'nested', Query\Nested::class, []);

0 commit comments

Comments
 (0)