Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MatchNone the inverse of MatchAll #1276

Merged
merged 1 commit into from Mar 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
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
@@ -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
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
@@ -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
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