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

enhance percolation of existing documents #570

Merged
merged 2 commits into from
Mar 16, 2014
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
CHANGES

2014-03-15
- percolate existing documents and add percolate options (#570)

2014-03-11
- Fixed request body reuse in http transport #567

Expand Down
35 changes: 33 additions & 2 deletions lib/Elastica/Percolator.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,51 @@ public function unregisterQuery($name)
* @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query Query to filter the percolator queries which
* are executed.
* @param string $type
* @param array $params
* @return array With matching registered queries.
*/
public function matchDoc(Document $doc, $query = null, $type = 'type')
public function matchDoc(Document $doc, $query = null, $type = 'type', $params = array())
{
$path = $this->_index->getName() . '/' . $type . '/_percolate';
$data = array('doc' => $doc->getData());

return $this->_percolate($path, $query, $data, $params);
}

/**
* Percolating an existing document
*
* @param string $id
* @param string $type
* @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query Query to filter the percolator queries which
* are executed.
* @param array $params
* @return array With matching registered queries.
*/
public function matchExistingDoc($id, $type, $query = null, $params = array())
{
$id = urlencode($id);
$path = $this->_index->getName() . '/' . $type . '/'. $id . '/_percolate';

return $this->_percolate($path, $query, array(), $params);
}

/**
* @param string $path
* @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query] $query [description]
* @param array $data
* @param array $params
* @return array
*/
protected function _percolate($path, $query, $data = array(), $params = array())
{
// Add query to filter the percolator queries which are executed.
if ($query) {
$query = Query::create($query);
$data['query'] = $query->getQuery();
}

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

if (isset($data['matches'])) {
Expand Down
66 changes: 66 additions & 0 deletions test/lib/Elastica/Test/PercolatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,70 @@ public function testRegisterAndUnregisterPercolator()

$index->delete();
}

protected function _getDefaultPercolator($percolatorName = 'existingDoc')
{
$index = $this->_createIndex();
$percolator = new Percolator($index);

$query = new Term(array('name' => 'foobar'));
$percolator->registerQuery($percolatorName, $query);
return $percolator;
}

protected function _addDefaultDocuments($index, $type='testing')
{
$type = $index->getType('testing');
$doc1 = new Document(1, array('name' => 'foobar'));
$doc2 = new Document(2, array('name' => 'barbaz'));
$type->addDocument($doc1);
$type->addDocument($doc2);
$index->refresh();
return $type;
}

public function testPercolateExistingDocWithoutAnyParameter()
{
$percolator = $this->_getDefaultPercolator();
$index = $percolator->getIndex();
$type = $this->_addDefaultDocuments($index);

$matches = $percolator->matchExistingDoc(1, $type->getName());

$this->assertCount(1, $matches);
$this->assertEquals('existingDoc', $matches[0]['_id']);
$index->delete();
}

public function testPercolateExistingDocWithPercolateFormatIds()
{
$percolator = $this->_getDefaultPercolator();
$index = $percolator->getIndex();
$type = $this->_addDefaultDocuments($index);

$parameter = array('percolate_format' => 'ids');
$matches = $percolator->matchExistingDoc(1, $type->getName(), null, $parameter);

$this->assertCount(1, $matches);
$this->assertEquals('existingDoc', $matches[0]);
$index->delete();
}

public function testPercolateExistingDocWithIdThatShouldBeUrlEncoded()
{
$percolator = $this->_getDefaultPercolator();
$index = $percolator->getIndex();
$type = $this->_addDefaultDocuments($index);

// id with whitespace, should be urlencoded
$id = "foo bar 1";

$type->addDocument(new Document($id, array('name' => 'foobar')));
$index->refresh();

$matches = $percolator->matchExistingDoc($id, $type->getName());

$this->assertCount(1, $matches);
$index->delete();
}
}