Commit 75fcd89d authored by Bas de Nooijer's avatar Bas de Nooijer Committed by GitHub

Merge pull request #441 from pavelsmolka/function_groups

Add support for grouping result using a function
parents 926e0b59 61f57d82
......@@ -11,7 +11,9 @@ In most cases only one type of grouping is used, but you can mix any number of q
Examples
--------
Grouped by field: ```php
Grouped by field:
```php
<?php
require(__DIR__.'/init.php');
......@@ -69,7 +71,9 @@ htmlFooter();
```
Grouped by query: ```php
Grouped by query:
```php
<?php
require(__DIR__.'/init.php');
......
......@@ -67,7 +67,9 @@ class Grouping implements ComponentParserInterface
// parse field groups
$valueResultClass = $grouping->getOption('resultvaluegroupclass');
$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])) {
$result = $data['grouped'][$field];
......
......@@ -64,6 +64,7 @@ class GroupingTest extends \PHPUnit_Framework_TestCase
$this->query = new Query();
$this->grouping = $this->query->getGrouping();
$this->grouping->addField('fieldA');
$this->grouping->setFunction('functionF');
$this->grouping->addQuery('cat:1');
$data = array(
......@@ -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(
'matches' => 40,
'doclist' => array(
......@@ -101,13 +117,15 @@ class GroupingTest extends \PHPUnit_Framework_TestCase
public function testGroupParsing()
{
$this->assertEquals(2, count($this->result->getGroups()));
$this->assertEquals(3, count($this->result->getGroups()));
$fieldGroup = $this->result->getGroup('fieldA');
$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\QueryGroup', get_class($queryGroup));
$this->assertEquals('Solarium\QueryType\Select\Result\Grouping\FieldGroup', get_class($functionGroup));
}
public function testFieldGroupParsing()
......@@ -142,4 +160,20 @@ class GroupingTest extends \PHPUnit_Framework_TestCase
$result = $this->parser->parse($this->query, $this->grouping, array());
$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