Skip to content
Snippets Groups Projects
Commit 1439a10d authored by Chris Hallberg's avatar Chris Hallberg Committed by Demian Katz
Browse files

More generic bulk facet retrieval, including Summon support.

parent e301ee5e
Branches
Tags
No related merge requests found
...@@ -621,4 +621,52 @@ abstract class Results implements ServiceLocatorAwareInterface ...@@ -621,4 +621,52 @@ abstract class Results implements ServiceLocatorAwareInterface
[$this->getOptions(), 'translate'], func_get_args() [$this->getOptions(), 'translate'], func_get_args()
); );
} }
/**
* Get complete facet counts for several index fields
*
* @param array $facetfields name of the Solr fields to return facets for
* @param bool $removeFilter Clear existing filters from selected fields (true)
* or retain them (false)?
* @param int $limit A limit for the number of facets returned, this
* may be useful for very large amounts of facets that can break the JSON parse
* method because of PHP out of memory exceptions (default = -1, no limit).
* @param string $facetSort A facet sort value to use (null to retain current)
*
* @return array an array with the facet values for each index field
*/
public function getFullFieldFacets($facetfields, $removeFilter = true,
$limit = -1, $facetSort = null
) {
if (!method_exists($this, 'getPartialFieldFacets')) {
throw new \Exception('getPartialFieldFacets not implemented');
}
$page = 1;
$facets = [];
do {
$facetpage = $this->getPartialFieldFacets(
$facetfields, $removeFilter, $limit, $facetSort, $page
);
$nextfields = [];
foreach ($facetfields as $field) {
if (!empty($facetpage[$field]['data']['list'])) {
if (!isset($facets[$field])) {
$facets[$field] = $facetpage[$field];
$facets[$field]['more'] = false;
} else {
$facets[$field]['data']['list'] = array_merge(
$facets[$field]['data']['list'],
$facetpage[$field]['data']['list']
);
}
if ($facetpage[$field]['more'] !== false) {
$nextfields[] = $field;
}
}
}
$facetfields = $nextfields;
$page++;
} while ($limit == -1 && !empty($facetfields));
return $facets;
}
} }
...@@ -294,11 +294,13 @@ class Results extends \VuFind\Search\Base\Results ...@@ -294,11 +294,13 @@ class Results extends \VuFind\Search\Base\Results
* may be useful for very large amounts of facets that can break the JSON parse * may be useful for very large amounts of facets that can break the JSON parse
* method because of PHP out of memory exceptions (default = -1, no limit). * method because of PHP out of memory exceptions (default = -1, no limit).
* @param string $facetSort A facet sort value to use (null to retain current) * @param string $facetSort A facet sort value to use (null to retain current)
* @param int $page 1 based. Offsets results by limit.
* @param bool $ored Whether or not facet is an OR facet or not
* *
* @return array an array with the facet values for each index field * @return array list facet values for each index field with label and more bool
*/ */
public function getFullFieldFacets($facetfields, $removeFilter = true, public function getPartialFieldFacets($facetfields, $removeFilter = true,
$limit = -1, $facetSort = null $limit = -1, $facetSort = null, $page = null, $ored = false
) { ) {
$clone = clone($this); $clone = clone($this);
$params = $clone->getParams(); $params = $clone->getParams();
...@@ -306,11 +308,17 @@ class Results extends \VuFind\Search\Base\Results ...@@ -306,11 +308,17 @@ class Results extends \VuFind\Search\Base\Results
// Manipulate facet settings temporarily: // Manipulate facet settings temporarily:
$params->resetFacetConfig(); $params->resetFacetConfig();
$params->setFacetLimit($limit); $params->setFacetLimit($limit);
if (null !== $page && $limit != -1) {
$offset = ($page - 1) * $limit;
$params->setFacetOffset($offset);
// Return limit plus one so we know there's another page
$params->setFacetLimit($limit + 1);
}
if (null !== $facetSort) { if (null !== $facetSort) {
$params->setFacetSort($facetSort); $params->setFacetSort($facetSort);
} }
foreach ($facetfields as $facetName) { foreach ($facetfields as $facetName) {
$params->addFacet($facetName); $params->addFacet($facetName, null, $ored);
// Clear existing filters for the selected field if necessary: // Clear existing filters for the selected field if necessary:
if ($removeFilter) { if ($removeFilter) {
...@@ -335,8 +343,15 @@ class Results extends \VuFind\Search\Base\Results ...@@ -335,8 +343,15 @@ class Results extends \VuFind\Search\Base\Results
// Reformat into a hash: // Reformat into a hash:
foreach ($result as $key => $value) { foreach ($result as $key => $value) {
unset($result[$key]); // Detect next page and crop results if necessary
$result[$key]['data'] = $value; $more = false;
if (isset($page) && count($value['list']) > 0
&& count($value['list']) == $limit + 1
) {
$more = true;
array_pop($value['list']);
}
$result[$key] = ['more' => $more, 'data' => $value];
} }
// Send back data: // Send back data:
......
...@@ -322,4 +322,59 @@ class Results extends \VuFind\Search\Base\Results ...@@ -322,4 +322,59 @@ class Results extends \VuFind\Search\Base\Results
{ {
return $this->topicRecommendations; return $this->topicRecommendations;
} }
/**
* Get complete facet counts for several index fields
*
* @param array $facetfields name of the Solr fields to return facets for
* @param bool $removeFilter Clear existing filters from selected fields (true)
* or retain them (false)?
* @param int $limit A limit for the number of facets returned, this
* may be useful for very large amounts of facets that can break the JSON parse
* method because of PHP out of memory exceptions (default = -1, no limit).
* @param string $facetSort A facet sort value to use (null to retain current)
* @param int $page 1 based. Offsets results by limit.
*
* @return array an array with the facet values for each index field
*/
public function getPartialFieldFacets($facetfields, $removeFilter = true,
$limit = -1, $facetSort = null, $page = null
) {
$params = $this->getParams();
$query = $params->getQuery();
// No limit not implemented with Summon: cause page loop
if ($limit == -1) {
if ($page === null) {
$page = 1;
}
$limit = 50;
}
$params->resetFacetConfig();
foreach ($facetfields as $facet) {
$params->addFacet($facet . ',or,' . $page . ',' . $limit);
}
$params = $params->getBackendParameters();
$collection = $this->getSearchService()->search(
'Summon', $query, 0, 0, $params
);
$facets = $collection->getFacets();
$ret = [];
foreach ($facets as $data) {
if (in_array($data['displayName'], $facetfields)) {
$formatted = $this->formatFacetData($data);
$list = $formatted['counts'];
$ret[$data['displayName']] = [
'data' => [
'label' => $data['displayName'],
'list' => $list,
],
'more' => null
];
}
}
// Send back data:
return $ret;
}
} }
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