Commit 0d17a1dd authored by Josh Turmel's avatar Josh Turmel

Add extract from response capability for facets

* Since some Solr setups might configure their facets in
  solrconfig.xml, this option allows you to tell the
  response builder to construct the facet response from
  what is actually returned in the response rather than having
  to define it in the query object first.
parent 0ed951d3
...@@ -55,6 +55,29 @@ class Solarium_Client_ResponseParser_Select_Component_FacetSet ...@@ -55,6 +55,29 @@ class Solarium_Client_ResponseParser_Select_Component_FacetSet
*/ */
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;
default:
continue;
}
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()) {
...@@ -186,4 +209,5 @@ class Solarium_Client_ResponseParser_Select_Component_FacetSet ...@@ -186,4 +209,5 @@ class Solarium_Client_ResponseParser_Select_Component_FacetSet
} }
} }
} }
\ No newline at end of file
...@@ -110,17 +110,46 @@ class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Com ...@@ -110,17 +110,46 @@ class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Com
*/ */
protected function _init() protected function _init()
{ {
if (isset($this->_options['facet'])) { foreach ($this->_options AS $name => $value) {
foreach ($this->_options['facet'] AS $key => $config) { switch ($name) {
if (!isset($config['key'])) { case 'facet':
$config['key'] = $key; foreach ($this->_options['facet'] AS $key => $config) {
} if (!isset($config['key'])) {
$config['key'] = $key;
$this->addFacet($config); }
$this->addFacet($config);
}
break;
case 'extractfromresponse':
$this->setExtractFromResponse($value);
break;
} }
} }
} }
/**
* Allow extraction of facets without having to define
* them on the query
*
* @param boolean $extract
* @return Solarium_Query_Select_Component_FacetSet 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
* *
...@@ -271,7 +300,7 @@ class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Com ...@@ -271,7 +300,7 @@ class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Com
if (array_key_exists($key, $this->_facets) && $this->_facets[$key] !== $facet) { if (array_key_exists($key, $this->_facets) && $this->_facets[$key] !== $facet) {
throw new Solarium_Exception('A facet must have a unique key value within a query'); throw new Solarium_Exception('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;
...@@ -451,4 +480,5 @@ class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Com ...@@ -451,4 +480,5 @@ class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Com
return $this->createFacet(self::FACET_RANGE, $options); return $this->createFacet(self::FACET_RANGE, $options);
} }
} }
\ No newline at end of file
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