Commit 61f57d82 authored by Pavel Smolka's avatar Pavel Smolka

Add support for grouping result using a function

Add functionality that checks whether the result data in
ResponseParser\Component\Grouping contain groups created by
solr parameter `group.func` (an arbitrary Solr-supported
function).

When such field is found, apply the same parsing logic
which is usually used for field-grouping, in order to ensure
the function-grouping is recognized and added to the FieldGroup
result object.
parent 91fe4085
...@@ -67,7 +67,9 @@ class Grouping implements ComponentParserInterface ...@@ -67,7 +67,9 @@ class Grouping implements ComponentParserInterface
// parse field groups // parse field groups
$valueResultClass = $grouping->getOption('resultvaluegroupclass'); $valueResultClass = $grouping->getOption('resultvaluegroupclass');
$documentClass = $query->getOption('documentclass'); $documentClass = $query->getOption('documentclass');
foreach ($grouping->getFields() as $field) {
// check grouping fields as well as the grouping function (either can be used in the query)
foreach (array_merge($grouping->getFields(), array($grouping->getFunction())) as $field) {
if (isset($data['grouped'][$field])) { if (isset($data['grouped'][$field])) {
$result = $data['grouped'][$field]; $result = $data['grouped'][$field];
......
...@@ -64,6 +64,7 @@ class GroupingTest extends \PHPUnit_Framework_TestCase ...@@ -64,6 +64,7 @@ class GroupingTest extends \PHPUnit_Framework_TestCase
$this->query = new Query(); $this->query = new Query();
$this->grouping = $this->query->getGrouping(); $this->grouping = $this->query->getGrouping();
$this->grouping->addField('fieldA'); $this->grouping->addField('fieldA');
$this->grouping->setFunction('functionF');
$this->grouping->addQuery('cat:1'); $this->grouping->addQuery('cat:1');
$data = array( $data = array(
...@@ -83,6 +84,21 @@ class GroupingTest extends \PHPUnit_Framework_TestCase ...@@ -83,6 +84,21 @@ class GroupingTest extends \PHPUnit_Framework_TestCase
) )
) )
), ),
'functionF' => array(
'matches' => 8,
'ngroups' => 3,
'groups' => array(
array(
'groupValue' => true,
'doclist' => array(
'numFound' => 5,
'docs' => array(
array('id' => 3, 'name' => 'fun')
)
)
)
)
),
'cat:1' => array( 'cat:1' => array(
'matches' => 40, 'matches' => 40,
'doclist' => array( 'doclist' => array(
...@@ -101,13 +117,15 @@ class GroupingTest extends \PHPUnit_Framework_TestCase ...@@ -101,13 +117,15 @@ class GroupingTest extends \PHPUnit_Framework_TestCase
public function testGroupParsing() public function testGroupParsing()
{ {
$this->assertEquals(2, count($this->result->getGroups())); $this->assertEquals(3, count($this->result->getGroups()));
$fieldGroup = $this->result->getGroup('fieldA'); $fieldGroup = $this->result->getGroup('fieldA');
$queryGroup = $this->result->getGroup('cat:1'); $queryGroup = $this->result->getGroup('cat:1');
$functionGroup = $this->result->getGroup('functionF');
$this->assertEquals('Solarium\QueryType\Select\Result\Grouping\FieldGroup', get_class($fieldGroup)); $this->assertEquals('Solarium\QueryType\Select\Result\Grouping\FieldGroup', get_class($fieldGroup));
$this->assertEquals('Solarium\QueryType\Select\Result\Grouping\QueryGroup', get_class($queryGroup)); $this->assertEquals('Solarium\QueryType\Select\Result\Grouping\QueryGroup', get_class($queryGroup));
$this->assertEquals('Solarium\QueryType\Select\Result\Grouping\FieldGroup', get_class($functionGroup));
} }
public function testFieldGroupParsing() public function testFieldGroupParsing()
...@@ -142,4 +160,20 @@ class GroupingTest extends \PHPUnit_Framework_TestCase ...@@ -142,4 +160,20 @@ class GroupingTest extends \PHPUnit_Framework_TestCase
$result = $this->parser->parse($this->query, $this->grouping, array()); $result = $this->parser->parse($this->query, $this->grouping, array());
$this->assertEquals(array(), $result->getGroups()); $this->assertEquals(array(), $result->getGroups());
} }
public function testFunctionGroupParsing()
{
$fieldGroup = $this->result->getGroup('functionF');
$valueGroups = $fieldGroup->getValueGroups();
$this->assertEquals(8, $fieldGroup->getMatches());
$this->assertEquals(3, $fieldGroup->getNumberOfGroups());
$this->assertEquals(1, count($valueGroups));
$valueGroup = $valueGroups[0];
$this->assertEquals(5, $valueGroup->getNumFound());
$docs = $valueGroup->getDocuments();
$this->assertEquals('fun', $docs[0]->name);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment