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

Add resultset transforming #1066

Merged
merged 1 commit into from
May 12, 2016
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ All notable changes to this project will be documented in this file based on the
### Bugfixes
- Fix fatal error on `Query::addScriptField()` if scripts were already set via `setScriptFields()` #1086

### Added
- Added the concept of ResultSet Transformers. The Transformer adds more information to a Result, for example the original object or data that created the Result. #1066

## [3.2.0](https://github.com/ruflin/Elastica/compare/3.1.1...3.2.0)

### Backward Compatibility Breaks
Expand Down
15 changes: 13 additions & 2 deletions lib/Elastica/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ public function getExplanation()
/**
* Returns Document.
*
* @return \Elastica\Document
* @return Document
*/
public function getDocument()
{
$doc = new \Elastica\Document();
$doc = new Document();
$doc->setData($this->getSource());
$hit = $this->getHit();
if ($this->hasParam('_source')) {
Expand All @@ -215,6 +215,17 @@ public function getDocument()
return $doc;
}

/**
* Sets a parameter on the hit.
*
* @param string $param
* @param mixed $value
*/
public function setParam($param, $value)
{
$this->_hit[$param] = $value;
}

/**
* Magic function to directly access keys inside the result.
*
Expand Down
35 changes: 35 additions & 0 deletions lib/Elastica/ResultSet/ChainProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Elastica\ResultSet;

use Elastica\ResultSet;

/**
* Allows multiple ProcessorInterface instances to operate on the same
* ResultSet, calling each in turn.
*/
class ChainProcessor implements ProcessorInterface
{
/**
* @var ProcessorInterface[]
*/
private $processors;

/**
* @param ProcessorInterface[] $processors
*/
public function __construct($processors)
{
$this->processors = $processors;
}

/**
* @inheritdoc
*/
public function process(ResultSet $resultSet)
{
foreach ($this->processors as $processor) {
$processor->process($resultSet);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Elastica\Result;
use Elastica\ResultSet;

class Builder implements BuilderInterface
class DefaultBuilder implements BuilderInterface
{
/**
* Builds a ResultSet for a given Response.
Expand Down
48 changes: 48 additions & 0 deletions lib/Elastica/ResultSet/ProcessingBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Elastica\ResultSet;

use Elastica\Query;
use Elastica\Response;
use Elastica\ResultSet;

class ProcessingBuilder implements BuilderInterface
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps here also "DefaultProcessingBuilder" or "BaseProcessingBuilder"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is actually a decorator for a normal Builder instance, i wouldnt be calling this one a Default or Base - it is designed to wrap another builder regardless of the implementation.

{
/**
* @var BuilderInterface
*/
private $builder;

/**
* @var ProcessorInterface
*/
private $processor;

/**
* @param BuilderInterface $builder
* @param ProcessorInterface $processor
*/
public function __construct(BuilderInterface $builder, ProcessorInterface $processor)
{
$this->builder = $builder;
$this->processor = $processor;
}

/**
* Runs any registered transformers on the ResultSet before
* returning it, allowing the transformers to inject additional
* data into each Result.
*
* @param Response $response
* @param Query $query
* @return ResultSet
*/
public function buildResultSet(Response $response, Query $query)
{
$resultSet = $this->builder->buildResultSet($response, $query);

$this->processor->process($resultSet);

return $resultSet;
}
}
16 changes: 16 additions & 0 deletions lib/Elastica/ResultSet/ProcessorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Elastica\ResultSet;

use Elastica\ResultSet;

interface ProcessorInterface
{
/**
* Iterates over a ResultSet allowing a processor to iterate over any
* Results as required.
*
* @param ResultSet $resultSet
*/
public function process(ResultSet $resultSet);
}
4 changes: 2 additions & 2 deletions lib/Elastica/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Elastica\Exception\InvalidException;
use Elastica\Filter\AbstractFilter;
use Elastica\ResultSet\Builder;
use Elastica\ResultSet\DefaultBuilder;
use Elastica\ResultSet\BuilderInterface;

/**
Expand Down Expand Up @@ -85,7 +85,7 @@ class Search
*/
public function __construct(Client $client, BuilderInterface $builder = null)
{
$this->_builder = $builder ?: new Builder();
$this->_builder = $builder ?: new DefaultBuilder();
$this->_client = $client;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testPartialFailure()

$this->fail('PartialShardFailureException should have been thrown');
} catch (PartialShardFailureException $e) {
$builder = new ResultSet\Builder();
$builder = new ResultSet\DefaultBuilder();
$resultSet = $builder->buildResultSet($e->getResponse(), $query);
$this->assertEquals(0, count($resultSet->getResults()));

Expand Down
6 changes: 3 additions & 3 deletions test/lib/Elastica/Test/ResultSet/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Elastica\Query;
use Elastica\Response;
use Elastica\ResultSet\Builder;
use Elastica\ResultSet\DefaultBuilder;
use Elastica\Test\Base as BaseTest;

/**
Expand All @@ -13,15 +13,15 @@
class BuilderTest extends BaseTest
{
/**
* @var Builder
* @var DefaultBuilder
*/
private $builder;

protected function setUp()
{
parent::setUp();

$this->builder = new Builder();
$this->builder = new DefaultBuilder();
}

public function testEmptyResponse()
Expand Down
33 changes: 33 additions & 0 deletions test/lib/Elastica/Test/ResultSet/ChainProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Elastica\Test\Transformer;

use Elastica\Query;
use Elastica\Response;
use Elastica\ResultSet;
use Elastica\ResultSet\ChainProcessor;
use Elastica\Test\Base as BaseTest;

/**
* @group unit
*/
class ChainProcessorTest extends BaseTest
{
public function testProcessor()
{
$processor = new ChainProcessor([
$processor1 = $this->getMock('Elastica\\ResultSet\\ProcessorInterface'),
$processor2 = $this->getMock('Elastica\\ResultSet\\ProcessorInterface')
]);
$resultSet = new ResultSet(new Response(''), new Query(), []);

$processor1->expects($this->once())
->method('process')
->with($resultSet);
$processor2->expects($this->once())
->method('process')
->with($resultSet);

$processor->process($resultSet);
}
}
57 changes: 57 additions & 0 deletions test/lib/Elastica/Test/ResultSet/ProcessingBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Elastica\Test\ResultSet;

use Elastica\Query;
use Elastica\Response;
use Elastica\ResultSet;
use Elastica\ResultSet\BuilderInterface;
use Elastica\Test\Base as BaseTest;

/**
* @group unit
*/
class ProcessingBuilderTest extends BaseTest
{
/**
* @var ResultSet\ProcessingBuilder
*/
private $builder;

/**
* @var BuilderInterface
*/
private $innerBuilder;

/**
* @var ResultSet\ProcessorInterface
*/
private $processor;

protected function setUp()
{
parent::setUp();

$this->innerBuilder = $this->getMock('Elastica\\ResultSet\\BuilderInterface');
$this->processor = $this->getMock('Elastica\\ResultSet\\ProcessorInterface');

$this->builder = new ResultSet\ProcessingBuilder($this->innerBuilder, $this->processor);
}

public function testProcessors()
{
$response = new Response('');
$query = new Query();
$resultSet = new ResultSet($response, $query, []);

$this->innerBuilder->expects($this->once())
->method('buildResultSet')
->with($response, $query)
->willReturn($resultSet);
$this->processor->expects($this->once())
->method('process')
->with($resultSet);

$this->builder->buildResultSet($response, $query);
}
}
2 changes: 1 addition & 1 deletion test/lib/Elastica/Test/Transport/GuzzleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function testBodyReuse()

$response = $index->request('/_search', 'POST');

$builder = new ResultSet\Builder();
$builder = new ResultSet\DefaultBuilder();
$resultSet = $builder->buildResultSet($response, Query::create(array()));

$this->assertEquals(1, $resultSet->getTotalHits());
Expand Down
2 changes: 1 addition & 1 deletion test/lib/Elastica/Test/Transport/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function testBodyReuse()

$response = $index->request('/_search', 'POST');

$builder = new ResultSet\Builder();
$builder = new ResultSet\DefaultBuilder();
$resultSet = $builder->buildResultSet($response, Query::create(array()));

$this->assertEquals(1, $resultSet->getTotalHits());
Expand Down