Skip to content

Commit

Permalink
Added Filter\Indices
Browse files Browse the repository at this point in the history
  • Loading branch information
jlinn committed Mar 25, 2014
1 parent 3afa6d7 commit 5ae01bb
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
CHANGES
2014-03-24
- Added Filter\Indices

2014-03-20
- Allow for request params in delete by query calls #573
Expand Down
51 changes: 51 additions & 0 deletions lib/Elastica/Filter/Indices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Elastica\Filter;


/**
* Class Indices
* @package Elastica\Filter
* @link http://www.elasticsearch.org/guide/en/elasticsearch/reference/0.90/query-dsl-indices-filter.html
*/
class Indices extends AbstractFilter
{
/**
* @param AbstractFilter $filter filter which will be applied to docs in the specified indices
* @param string[] $indices
*/
public function __construct(AbstractFilter $filter, array $indices)
{
$this->setIndices($indices)->setFilter($filter);
}

/**
* Set the names of the indices on which this filter should be applied
* @param string[] $indices
* @return Indices
*/
public function setIndices(array $indices)
{
return $this->setParam('indices', $indices);
}

/**
* Set the filter to be applied to docs in the specified indices
* @param AbstractFilter $filter
* @return Indices
*/
public function setFilter(AbstractFilter $filter)
{
return $this->setParam('filter', $filter->toArray());
}

/**
* Set the filter to be applied to docs in indices which do not match those specified in the "indices" parameter
* @param AbstractFilter $filter
* @return Indices
*/
public function setNoMatchFilter(AbstractFilter $filter)
{
return $this->setParam('no_match_filter', $filter->toArray());
}
}
93 changes: 93 additions & 0 deletions test/lib/Elastica/Test/Filter/IndicesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Elastica\Test\Filter;

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

class IndicesTest extends BaseTest
{
/**
* @var Index
*/
protected $_index1;

/**
* @var Index
*/
protected $_index2;

protected function setUp()
{
parent::setUp();
$this->_index1 = $this->_createIndex('indices_filter_1');
$this->_index2 = $this->_createIndex('indices_filter_2');
$this->_index1->addAlias("indices_filter");
$this->_index2->addAlias("indices_filter");
$docs = array(
new Document("1", array("color" => "blue")),
new Document("2", array("color" => "green")),
new Document("3", array("color" => "blue")),
new Document("4", array("color" => "yellow")),
);
$this->_index1->getType("test")->addDocuments($docs);
$this->_index2->getType("test")->addDocuments($docs);
$this->_index1->refresh();
$this->_index2->refresh();
}

protected function tearDown()
{
$this->_index1->delete();
$this->_index2->delete();
parent::tearDown();
}

public function testToArray()
{
$expected = array(
"indices" => array(
"indices" => array("index1", "index2"),
"filter" => array(
"term" => array("tag" => "wow")
),
"no_match_filter" => array(
"term" => array("tag" => "such filter")
)
)
);
$filter = new Indices(new Term(array("tag" => "wow")), array("index1", "index2"));
$filter->setNoMatchFilter(new Term(array("tag" => "such filter")));
$this->assertEquals($expected, $filter->toArray());
}

public function testIndicesFilter()
{
$filter = new Indices(new BoolNot(new Term(array("color" => "blue"))), array($this->_index1->getName()));
$filter->setNoMatchFilter(new BoolNot(new Term(array("color" => "yellow"))));
$query = new Query();
$query->setFilter($filter);

// search over the alias
$index = $this->_getClient()->getIndex("indices_filter");
$results = $index->search($query);

// ensure that the proper docs have been filtered out for each index
$this->assertEquals(5, $results->count());
foreach ($results->getResults() as $result) {
$data = $result->getData();
$color = $data["color"];
if ($result->getIndex() == $this->_index1->getName()) {
$this->assertNotEquals("blue", $color);
} else {
$this->assertNotEquals("yellow", $color);
}
}
}
}

0 comments on commit 5ae01bb

Please sign in to comment.