Skip to content

Commit

Permalink
Add MatchNone the inverse of MatchAll (ruflin#1276)
Browse files Browse the repository at this point in the history
This query is sometimes handy and I think Elastica should support it.
  • Loading branch information
nomoa authored and Marek Hernik committed Jul 24, 2017
1 parent 42ccdc7 commit 0bedd92
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions lib/Elastica/Query/MatchNone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace Elastica\Query;

/**
* Match none query. Returns no results.
*
* @author David Causse
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html#query-dsl-match-none-query
*/
class MatchNone extends AbstractQuery
{
/**
* Creates match none query.
*/
public function __construct()
{
$this->_params = new \stdClass();
}
}
13 changes: 13 additions & 0 deletions lib/Elastica/QueryBuilder/DSL/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
41 changes: 41 additions & 0 deletions test/Elastica/Query/MatchNoneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace Elastica\Test\Query;

use Elastica\Document;
use Elastica\Query\MatchNone;
use Elastica\Search;
use Elastica\Test\Base as BaseTest;

class MatchNoneTest extends BaseTest
{
/**
* @group unit
*/
public function testToArray()
{
$query = new MatchNone();

$expectedArray = ['match_none' => 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());
}
}
1 change: 1 addition & 0 deletions test/Elastica/QueryBuilder/DSL/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, []);
Expand Down

0 comments on commit 0bedd92

Please sign in to comment.