Skip to content

Commit 4b38a77

Browse files
committed
Filtering queries while percolating #384
1 parent fffe6a4 commit 4b38a77

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

changes.txt

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ CHANGES
55
- Elastica\Filter\AbstractMulti::getFilters() added
66
- Implement Elastica\Type\Mapping::enableAllField
77
- Refresh for Elastica\Index::flush implemented #316
8+
- Added optional parameter to filter result while percolate #384
9+
10+
2013-05-07
11+
- Added EXPERIMENTAL DocumentObjectInterface to be used with Type::addObject()/addObjects()
812

913
2013-04-23
1014
- Removed Elastica\Exception\AbstractException

lib/Elastica/Percolator.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,20 @@ public function unregisterQuery($name)
6060
* Match a document to percolator queries
6161
*
6262
* @param \Elastica\Document $doc
63-
* @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query Not implemented yet
63+
* @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query Query to filter the data
6464
* @return \Elastica\Response
6565
*/
6666
public function matchDoc(Document $doc, $query = null)
6767
{
6868
$path = $this->_index->getName() . '/type/_percolate';
6969
$data = array('doc' => $doc->getData());
7070

71+
// Add query to filter results after percolation
72+
if ($query) {
73+
$query = Query::create($query);
74+
$data['query'] = $query->getQuery();
75+
}
76+
7177
$response = $this->getIndex()->getClient()->request($path, Request::GET, $data);
7278
$data = $response->getData();
7379

test/lib/Elastica/Test/PercolatorTest.php

+33-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Elastica\Index;
77
use Elastica\Percolator;
88
use Elastica\Query\Term;
9+
use Elastica\Query;
910
use Elastica\Test\Base as BaseTest;
1011

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

4445
$percolatorName = 'percotest';
46+
$percolatorSecondName = 'percotest_color';
4547

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

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

54+
// Add index with the same query and additional parameter
55+
$secondParamKey = 'color';
56+
$secondParamValue = 'blue';
57+
$querySecond = new Query();
58+
$querySecond->setQuery($query);
59+
$querySecond->setParam($secondParamKey, $secondParamValue);
60+
$responseSecond = $percolator->registerQuery($percolatorSecondName, $querySecond);
61+
62+
// Check if it's ok
63+
$this->assertTrue($responseSecond->isOk());
64+
$this->assertFalse($responseSecond->hasError());
65+
5266
$doc1 = new Document();
5367
$doc1->set('name', 'ruflin');
5468

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

6478
$this->assertTrue(in_array($percolatorName, $matches1));
65-
$this->assertEquals(1, count($matches1));
79+
$this->assertTrue(in_array($percolatorSecondName, $matches1));
80+
$this->assertEquals(2, count($matches1));
6681

6782
$matches2 = $percolator->matchDoc($doc2);
6883
$this->assertEmpty($matches2);
84+
85+
// Test using document with additional parameter
86+
$docSecond = $doc1;
87+
$docSecond->set($secondParamKey, $secondParamValue);
88+
89+
// Create term for our parameter to filter data while percolating
90+
$secondTerm = new Term(array($secondParamKey => $secondParamValue));
91+
$secondQuery = new Query();
92+
$secondQuery->setQuery($secondTerm);
93+
94+
// Match the document to percolator queries and filter results using optional parameter
95+
$secondMatches = $percolator->matchDoc($docSecond, $secondQuery);
96+
97+
// Check if only one proper index was returned
98+
$this->assertFalse(in_array($percolatorName, $secondMatches));
99+
$this->assertTrue(in_array($percolatorSecondName, $secondMatches));
100+
$this->assertEquals(1, count($secondMatches));
69101
}
70102
}

0 commit comments

Comments
 (0)