Commit 429d2d9c authored by Bas de Nooijer's avatar Bas de Nooijer Committed by GitHub

Merge pull request #468 from chadicus/bug/group-format-simple

Enable Simple Group Format
parents 71bf3f5a c975aad9
......@@ -61,49 +61,39 @@ class Grouping implements ComponentParserInterface
*/
public function parse($query, $grouping, $data)
{
if (!isset($data['grouped'])) {
return new Result(array());
}
$groups = array();
if (isset($data['grouped'])) {
// parse field groups
$valueResultClass = $grouping->getOption('resultvaluegroupclass');
$documentClass = $query->getOption('documentclass');
// 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])) {
continue;
}
$result = $data['grouped'][$field];
$matches = (isset($result['matches'])) ? $result['matches'] : null;
$groupCount = (isset($result['ngroups'])) ? $result['ngroups'] : null;
$valueGroups = array();
foreach ($result['groups'] as $valueGroupResult) {
$value = (isset($valueGroupResult['groupValue'])) ?
$valueGroupResult['groupValue'] : null;
$numFound = (isset($valueGroupResult['doclist']['numFound'])) ?
$valueGroupResult['doclist']['numFound'] : null;
$start = (isset($valueGroupResult['doclist']['start'])) ?
$valueGroupResult['doclist']['start'] : null;
$maxScore = (isset($valueGroupResult['doclist']['maxScore'])) ?
$valueGroupResult['doclist']['maxScore'] : null;
// create document instances
$documents = array();
if (isset($valueGroupResult['doclist']['docs']) &&
is_array($valueGroupResult['doclist']['docs'])) {
foreach ($valueGroupResult['doclist']['docs'] as $doc) {
$documents[] = new $documentClass($doc);
}
if ($grouping->getFormat() === GroupingComponent::FORMAT_SIMPLE) {
$valueGroups = [$this->extractValueGroup($valueResultClass, $documentClass, $result, $query)];
$groups[$field] = new FieldGroup($matches, $groupCount, $valueGroups);
continue;
}
$valueGroups[] = new $valueResultClass($value, $numFound, $start, $documents, $maxScore, $query);
$valueGroups = array();
foreach ($result['groups'] as $valueGroupResult) {
$valueGroups[] = $this->extractValueGroup($valueResultClass, $documentClass, $valueGroupResult, $query);
}
$groups[$field] = new FieldGroup($matches, $groupCount, $valueGroups);
}
}
// parse query groups
$groupResultClass = $grouping->getOption('resultquerygroupclass');
......@@ -131,8 +121,43 @@ class Grouping implements ComponentParserInterface
$groups[$groupQuery] = $group;
}
}
}
return new Result($groups);
}
/**
* Helper method to extract a ValueGroup object from the given value group result array.
*
* @param string $valueResultClass The grouping resultvaluegroupclass option.
* @param string $documentClass The name of the solr document class to use.
* @param array $valueGroupResult The group result from the solr response.
* @param Query $query The current solr query.
*
* @return object
*/
private function extractValueGroup($valueResultClass, $documentClass, $valueGroupResult, $query)
{
$value = (isset($valueGroupResult['groupValue'])) ?
$valueGroupResult['groupValue'] : null;
$numFound = (isset($valueGroupResult['doclist']['numFound'])) ?
$valueGroupResult['doclist']['numFound'] : null;
$start = (isset($valueGroupResult['doclist']['start'])) ?
$valueGroupResult['doclist']['start'] : null;
$maxScore = (isset($valueGroupResult['doclist']['maxScore'])) ?
$valueGroupResult['doclist']['maxScore'] : null;
// create document instances
$documents = array();
if (isset($valueGroupResult['doclist']['docs']) &&
is_array($valueGroupResult['doclist']['docs'])) {
foreach ($valueGroupResult['doclist']['docs'] as $doc) {
$documents[] = new $documentClass($doc);
}
}
return new $valueResultClass($value, $numFound, $start, $documents, $maxScore, $query);
}
}
......@@ -161,6 +161,43 @@ class GroupingTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array(), $result->getGroups());
}
public function testParseMissingGroupField()
{
//data does not contain 'fieldA'
$data = array(
'grouped' => array(
'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(
'numFound' => 22,
'docs' => array(
array('id' => 2, 'name' => 'dummy2'),
array('id' => 5, 'name' => 'dummy5'),
),
),
),
),
);
$result = $this->parser->parse($this->query, $this->grouping, $data);
$this->assertNull($result->getGroup('fieldA'));
}
public function testFunctionGroupParsing()
{
$fieldGroup = $this->result->getGroup('functionF');
......@@ -176,4 +213,40 @@ class GroupingTest extends \PHPUnit_Framework_TestCase
$docs = $valueGroup->getDocuments();
$this->assertEquals('fun', $docs[0]->name);
}
public function testsParseWithSimpleFormat()
{
$data = array(
'grouped' => array(
'fieldA' => array(
'matches' => 25,
'ngroups' => 12,
'doclist' => array(
'numFound' => 13,
'docs' => array(
array('id' => 1, 'name' => 'test'),
array('id' => 2, 'name' => 'test2'),
),
),
),
),
);
$this->grouping->setFormat(Component::FORMAT_SIMPLE);
$result = $this->parser->parse($this->query, $this->grouping, $data);
$fieldGroup = $result->getGroup('fieldA');
$valueGroups = $fieldGroup->getValueGroups();
$this->assertEquals(25, $fieldGroup->getMatches());
$this->assertEquals(12, $fieldGroup->getNumberOfGroups());
$this->assertEquals(1, count($valueGroups));
$valueGroup = $valueGroups[0];
$this->assertEquals(13, $valueGroup->getNumFound());
$docs = $valueGroup->getDocuments();
$this->assertEquals('test2', $docs[1]->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