Commit b2612442 authored by Bas de Nooijer's avatar Bas de Nooijer

Merge commit '0d17a1dd' into develop and added unittests

Conflicts:
	library/Solarium/Client/ResponseParser/Select/Component/FacetSet.php
	library/Solarium/QueryType/Select/Query/Component/FacetSet.php
parents 3821b00b 0d17a1dd
...@@ -139,7 +139,7 @@ class FacetSet extends Component ...@@ -139,7 +139,7 @@ class FacetSet extends Component
protected function init() protected function init()
{ {
if (isset($this->options['facet'])) { if (isset($this->options['facet'])) {
foreach ($this->options['facet'] as $key => $config) { foreach ($this->options['facet'] AS $key => $config) {
if (!isset($config['key'])) { if (!isset($config['key'])) {
$config['key'] = $key; $config['key'] = $key;
} }
...@@ -149,6 +149,28 @@ class FacetSet extends Component ...@@ -149,6 +149,28 @@ class FacetSet extends Component
} }
} }
/**
* Allow extraction of facets without having to define
* them on the query
*
* @param boolean $extract
* @return self Provides fluent interface
*/
public function setExtractFromResponse($extract)
{
return $this->setOption('extractfromresponse', $extract);
}
/**
* Get the extractfromresponse option value
*
* @return boolean
*/
public function getExtractFromResponse()
{
return $this->getOption('extractfromresponse');
}
/** /**
* Limit the terms for faceting by a prefix * Limit the terms for faceting by a prefix
* *
...@@ -300,7 +322,7 @@ class FacetSet extends Component ...@@ -300,7 +322,7 @@ class FacetSet extends Component
if (array_key_exists($key, $this->facets) && $this->facets[$key] !== $facet) { if (array_key_exists($key, $this->facets) && $this->facets[$key] !== $facet) {
throw new InvalidArgumentException('A facet must have a unique key value within a query'); throw new InvalidArgumentException('A facet must have a unique key value within a query');
} else { } else {
$this->facets[$key] = $facet; $this->facets[$key] = $facet;
} }
return $this; return $this;
......
...@@ -68,6 +68,27 @@ class FacetSet extends ResponseParserAbstract implements ComponentParserInterfac ...@@ -68,6 +68,27 @@ class FacetSet extends ResponseParserAbstract implements ComponentParserInterfac
*/ */
public function parse($query, $facetSet, $data) public function parse($query, $facetSet, $data)
{ {
if ($facetSet->getExtractFromResponse() === true) {
if (empty($data['facet_counts']) === false) {
foreach ($data['facet_counts'] as $key => $facets) {
switch ($key) {
case 'facet_fields':
$method = 'createFacetField';
break;
case 'facet_queries':
$method = 'createFacetQuery';
break;
case 'facet_ranges':
$method = 'createFacetRange';
break;
}
foreach ($facets as $k => $facet) {
$facetSet->$method($k);
}
}
}
}
$facets = array(); $facets = array();
foreach ($facetSet->getFacets() as $key => $facet) { foreach ($facetSet->getFacets() as $key => $facet) {
switch ($facet->getType()) { switch ($facet->getType()) {
......
...@@ -58,6 +58,7 @@ class FacetSetTest extends \PHPUnit_Framework_TestCase ...@@ -58,6 +58,7 @@ class FacetSetTest extends \PHPUnit_Framework_TestCase
'sort' => 'index', 'sort' => 'index',
'mincount' => 10, 'mincount' => 10,
'missing' => 5, 'missing' => 5,
'extractfromresponse' => true,
); );
$this->facetSet->setOptions($options); $this->facetSet->setOptions($options);
...@@ -68,6 +69,7 @@ class FacetSetTest extends \PHPUnit_Framework_TestCase ...@@ -68,6 +69,7 @@ class FacetSetTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($options['sort'], $this->facetSet->getSort()); $this->assertEquals($options['sort'], $this->facetSet->getSort());
$this->assertEquals($options['mincount'], $this->facetSet->getMincount()); $this->assertEquals($options['mincount'], $this->facetSet->getMincount());
$this->assertEquals($options['missing'], $this->facetSet->getMissing()); $this->assertEquals($options['missing'], $this->facetSet->getMissing());
$this->assertEquals($options['extractfromresponse'], $this->facetSet->getExtractFromResponse());
} }
public function testGetType() public function testGetType()
...@@ -401,4 +403,10 @@ class FacetSetTest extends \PHPUnit_Framework_TestCase ...@@ -401,4 +403,10 @@ class FacetSetTest extends \PHPUnit_Framework_TestCase
$observer->createFacetRange($options, $add); $observer->createFacetRange($options, $add);
} }
public function testSetAndGetExtractFromResponse()
{
$this->facetSet->setExtractFromResponse(true);
$this->assertEquals(true, $this->facetSet->getExtractFromResponse());
}
} }
...@@ -130,6 +130,93 @@ class FacetSetTest extends \PHPUnit_Framework_TestCase ...@@ -130,6 +130,93 @@ class FacetSetTest extends \PHPUnit_Framework_TestCase
$this->query = new Query; $this->query = new Query;
} }
public function testParseExtractFromResponse()
{
$data = array(
'facet_counts' => array(
'facet_fields' => array(
'keyA' => array(
'value1',
12,
'value2',
3,
),
),
'facet_queries' => array(
'keyB' => 23,
'keyC_A' => 25,
'keyC_B' => 16,
),
'facet_ranges' => array(
'keyD' => array(
'before' => 3,
'after' => 5,
'between' => 4,
'counts' => array(
'1.0',
1,
'101.0',
2,
'201.0',
1,
)
)
)
)
);
$facetSet = new FacetSet();
$facetSet->setExtractFromResponse(true);
$result = $this->parser->parse($this->query, $facetSet, $data);
$facets = $result->getFacets();
$this->assertEquals(array('keyA','keyB','keyC_A','keyC_B','keyD'), array_keys($facets));
$this->assertEquals(
array('value1' => 12, 'value2' => 3),
$facets['keyA']->getValues()
);
$this->assertEquals(
23,
$facets['keyB']->getValue()
);
// As the multiquery facet is a Solarium virtual facet type, it cannot be detected based on Solr response data
$this->assertEquals(
25,
$facets['keyC_A']->getValue()
);
$this->assertEquals(
16,
$facets['keyC_B']->getValue()
);
$this->assertEquals(
array('1.0' => 1, '101.0' => 2, '201.0' => 1),
$facets['keyD']->getValues()
);
$this->assertEquals(
3,
$facets['keyD']->getBefore()
);
$this->assertEquals(
4,
$facets['keyD']->getBetween()
);
$this->assertEquals(
5,
$facets['keyD']->getAfter()
);
$this->query = new Query;
}
public function testParseNoData() public function testParseNoData()
{ {
$result = $this->parser->parse($this->query, $this->facetSet, array()); $result = $this->parser->parse($this->query, $this->facetSet, array());
......
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