Skip to content

Commit

Permalink
Filtering queries while percolating #384
Browse files Browse the repository at this point in the history
  • Loading branch information
ruflin committed May 19, 2013
1 parent fffe6a4 commit 4b38a77
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
4 changes: 4 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ CHANGES
- Elastica\Filter\AbstractMulti::getFilters() added
- Implement Elastica\Type\Mapping::enableAllField
- Refresh for Elastica\Index::flush implemented #316
- Added optional parameter to filter result while percolate #384

2013-05-07
- Added EXPERIMENTAL DocumentObjectInterface to be used with Type::addObject()/addObjects()

2013-04-23
- Removed Elastica\Exception\AbstractException
Expand Down
8 changes: 7 additions & 1 deletion lib/Elastica/Percolator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,20 @@ public function unregisterQuery($name)
* Match a document to percolator queries
*
* @param \Elastica\Document $doc
* @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query Not implemented yet
* @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query Query to filter the data
* @return \Elastica\Response
*/
public function matchDoc(Document $doc, $query = null)
{
$path = $this->_index->getName() . '/type/_percolate';
$data = array('doc' => $doc->getData());

// Add query to filter results after percolation
if ($query) {
$query = Query::create($query);
$data['query'] = $query->getQuery();
}

$response = $this->getIndex()->getClient()->request($path, Request::GET, $data);
$data = $response->getData();

Expand Down
34 changes: 33 additions & 1 deletion test/lib/Elastica/Test/PercolatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Elastica\Index;
use Elastica\Percolator;
use Elastica\Query\Term;
use Elastica\Query;
use Elastica\Test\Base as BaseTest;

class PercolatorTest extends BaseTest
Expand Down Expand Up @@ -42,13 +43,26 @@ public function testMatchDoc()
$percolator = new Percolator($index);

$percolatorName = 'percotest';
$percolatorSecondName = 'percotest_color';

$query = new Term(array('name' => 'ruflin'));
$response = $percolator->registerQuery($percolatorName, $query);

$this->assertTrue($response->isOk());
$this->assertFalse($response->hasError());

// Add index with the same query and additional parameter
$secondParamKey = 'color';
$secondParamValue = 'blue';
$querySecond = new Query();
$querySecond->setQuery($query);
$querySecond->setParam($secondParamKey, $secondParamValue);
$responseSecond = $percolator->registerQuery($percolatorSecondName, $querySecond);

// Check if it's ok
$this->assertTrue($responseSecond->isOk());
$this->assertFalse($responseSecond->hasError());

$doc1 = new Document();
$doc1->set('name', 'ruflin');

Expand All @@ -62,9 +76,27 @@ public function testMatchDoc()
$matches1 = $percolator->matchDoc($doc1);

$this->assertTrue(in_array($percolatorName, $matches1));
$this->assertEquals(1, count($matches1));
$this->assertTrue(in_array($percolatorSecondName, $matches1));
$this->assertEquals(2, count($matches1));

$matches2 = $percolator->matchDoc($doc2);
$this->assertEmpty($matches2);

// Test using document with additional parameter
$docSecond = $doc1;
$docSecond->set($secondParamKey, $secondParamValue);

// Create term for our parameter to filter data while percolating
$secondTerm = new Term(array($secondParamKey => $secondParamValue));
$secondQuery = new Query();
$secondQuery->setQuery($secondTerm);

// Match the document to percolator queries and filter results using optional parameter
$secondMatches = $percolator->matchDoc($docSecond, $secondQuery);

// Check if only one proper index was returned
$this->assertFalse(in_array($percolatorName, $secondMatches));
$this->assertTrue(in_array($percolatorSecondName, $secondMatches));
$this->assertEquals(1, count($secondMatches));
}
}

0 comments on commit 4b38a77

Please sign in to comment.