Skip to content

Commit

Permalink
Add setPostFilter method to Elastica\Query
Browse files Browse the repository at this point in the history
  • Loading branch information
krzaczek authored and root committed Jul 3, 2014
1 parent 572e44b commit 27667c5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 18 deletions.
83 changes: 83 additions & 0 deletions test/lib/Elastica/Test/Query/PostFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Elastica\Test\Query;

use Elastica\Document;
use Elastica\Filter\Term;
use Elastica\Index;
use Elastica\Query\Match;
use Elastica\Query;
use Elastica\Test\Base as BaseTest;

class PostFilterText extends BaseTest
{
/**
* @var Index
*/
protected $_index;

protected function setUp()
{
parent::setUp();
$this->_index = $this->_createIndex("query");
$docs = array(
new Document("1", array("color" => "green", "make" => "ford")),
new Document("2", array("color" => "blue", "make" => "volvo")),
new Document("3", array("color" => "red", "make" => "ford")),
new Document("4", array("color" => "green", "make" => "renault")),
);
$this->_index->getType("test")->addDocuments($docs);
$this->_index->refresh();

}

protected function tearDown()
{
parent::tearDown();
if ($this->_index instanceof Index) {
$this->_index->delete();
}
}

public function testToArray()
{
$query = new Query();

$post_filter = new Term(array('color' => 'green'));
$query->setPostFilter($post_filter->toArray());

$data = $query->toArray();

$this->assertArrayHasKey('post_filter', $data);
$this->assertEquals(array('term' => array('color' => 'green')), $data['post_filter']);

$query->setPostFilter(array());

$this->assertArrayNotHasKey('post_filter', $query->toArray());
}

public function testQuery()
{
$query = new Query();

$match = new Match();
$match->setField('make', 'ford');

$query->setQuery($match);

$filter = new Term();
$filter->setTerm('color', 'green');

$query->setPostFilter($filter->toArray());

$results = $this->_index->search($query);

$this->assertEquals(1, $results->getTotalHits());

}

protected function _createIndex($name = 'test', $delete = true, $shards = 1)
{
return parent::_createIndex('test_postfilter_' . $name, $delete, $shards);
}
}
18 changes: 0 additions & 18 deletions test/lib/Elastica/Test/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Elastica\Query\Text;
use Elastica\Query;
use Elastica\Facet\Terms;
use Elastica\Filter\Term as FilterTerm;
use Elastica\Test\Base as BaseTest;

class QueryTest extends BaseTest
Expand Down Expand Up @@ -192,21 +191,4 @@ public function testSetFacets()

$this->assertArrayNotHasKey('facets', $query->toArray());
}

public function testSetPostQuery()
{
$query = new Query();

$post_filter = new FilterTerm(array('color' => 'green'));
$query->setPostFilter($post_filter->toArray());

$data = $query->toArray();

$this->assertArrayHasKey('post_filter', $data);
$this->assertEquals(array('term' => array('color' => 'green')), $data['post_filter']);

$query->setPostFilter(array());

$this->assertArrayNotHasKey('post_filter', $query->toArray());
}
}

0 comments on commit 27667c5

Please sign in to comment.