Skip to content
Permalink
Browse files

Refactored Results::createBackendParameters to Params::getBackendPara…

…meters.

Cleaned up some unnecessary "use" declarations.
  • Loading branch information...
demiankatz committed Aug 30, 2013
1 parent c325048 commit b5458e2aaf6188dbd04c70adf8cd7c4651dafd7f
@@ -26,6 +26,7 @@
* @link http://www.vufind.org Main Page
*/
namespace VuFind\Search\Pazpar2;
use VuFindSearch\ParamBag;
/**
* Pazpar2 Search Parameters
@@ -38,4 +39,25 @@
*/
class Params extends \VuFind\Search\Base\Params
{
/**
* Create search backend parameters for advanced features.
*
* @return ParamBag
*/
public function getBackendParameters()
{
$backendParams = new ParamBag();
// Sources
$sources = $this->getSelectedShards();
if (!empty($sources)) {
$allShards = $this->getOptions()->getShards();
foreach ($sources as $i=>$current) {
$sources[$i] = $allShards[$current];
}
$backendParams->set('filter', 'pz:id='.implode('|', $sources));
}
return $backendParams;
}
}
@@ -26,8 +26,6 @@
* @link http://www.vufind.org Main Page
*/
namespace VuFind\Search\Pazpar2;
use VuFindSearch\Query\AbstractQuery;
use VuFindSearch\ParamBag;
/**
* Pazpar2 Search Parameters
@@ -51,7 +49,7 @@ protected function performSearch()
$query = $this->getParams()->getQuery();
$limit = $this->getParams()->getLimit();
$offset = $this->getStartRecord() - 1;
$params = $this->createBackendParameters($query, $this->getParams());
$params = $this->getParams()->getBackendParameters();
$collection = $this->getSearchService()
->search('Pazpar2', $query, $offset, $limit, $params);
@@ -72,29 +70,4 @@ public function getFacetList($filter = null)
// No facets in Pazpar2:
return array();
}
/**
* Create search backend parameters for advanced features.
*
* @param AbstractQuery $query Query being processed
* @param Params $params Search parameters
*
* @return ParamBag
*/
protected function createBackendParameters(AbstractQuery $query, Params $params)
{
$backendParams = new ParamBag();
// Sources
$sources = $params->getSelectedShards();
if (!empty($sources)) {
$allShards = $params->getOptions()->getShards();
foreach ($sources as $i=>$current) {
$sources[$i] = $allShards[$current];
}
$backendParams->set('filter', 'pz:id='.implode('|', $sources));
}
return $backendParams;
}
}
@@ -26,6 +26,7 @@
* @link http://www.vufind.org Main Page
*/
namespace VuFind\Search\Solr;
use VuFindSearch\ParamBag;
/**
* Solr Search Parameters
@@ -373,4 +374,105 @@ public function getQueryIDLimit()
return isset($config->Index->maxBooleanClauses)
? $config->Index->maxBooleanClauses : 1024;
}
/**
* Normalize sort parameters.
*
* @param string $sort Sort parameter
*
* @return string
*/
protected function normalizeSort($sort)
{
static $table = array(
'year' => array('field' => 'publishDateSort', 'order' => 'desc'),
'publishDateSort' =>
array('field' => 'publishDateSort', 'order' => 'desc'),
'author' => array('field' => 'authorStr', 'order' => 'asc'),
'title' => array('field' => 'title_sort', 'order' => 'asc'),
'relevance' => array('field' => 'score', 'order' => 'desc'),
'callnumber' => array('field' => 'callnumber', 'order' => 'asc'),
);
$normalized = array();
foreach (explode(',', $sort) as $component) {
$parts = explode(' ', trim($component));
$field = reset($parts);
$order = next($parts);
if (isset($table[$field])) {
$normalized[] = sprintf(
'%s %s',
$table[$field]['field'],
$order ?: $table[$field]['order']
);
} else {
$normalized[] = sprintf(
'%s %s',
$field,
$order ?: 'asc'
);
}
}
return implode(',', $normalized);
}
/**
* Create search backend parameters for advanced features.
*
* @return ParamBag
*/
public function getBackendParameters()
{
$backendParams = new ParamBag();
// Spellcheck
$backendParams->set(
'spellcheck', $this->getOptions()->spellcheckEnabled() ? 'true' : 'false'
);
// Facets
$facets = $this->getFacetSettings();
if (!empty($facets)) {
$backendParams->add('facet', 'true');
foreach ($facets as $key => $value) {
$backendParams->add("facet.{$key}", $value);
}
$backendParams->add('facet.mincount', 1);
}
// Filters
$filters = $this->getFilterSettings();
foreach ($filters as $filter) {
$backendParams->add('fq', $filter);
}
// Shards
$allShards = $this->getOptions()->getShards();
$shards = $this->getSelectedShards();
if (is_null($shards)) {
$shards = array_keys($allShards);
}
// If we have selected shards, we need to format them:
if (!empty($shards)) {
$selectedShards = array();
foreach ($shards as $current) {
$selectedShards[$current] = $allShards[$current];
}
$shards = $selectedShards;
$backendParams->add('shards', implode(',', $selectedShards));
}
// Sort
$sort = $this->getSort();
if ($sort) {
$backendParams->add('sort', $this->normalizeSort($sort));
}
// Highlighting -- on by default, but we should disable if necessary:
if (!$this->getOptions()->highlightEnabled()) {
$backendParams->add('hl', 'false');
}
return $backendParams;
}
}
@@ -26,14 +26,6 @@
* @link http://www.vufind.org Main Page
*/
namespace VuFind\Search\Solr;
use VuFind\Exception\RecordMissing as RecordMissingException,
VuFind\Search\Base\Results as BaseResults;
use VuFindSearch\Query\AbstractQuery;
use VuFindSearch\Query\QueryGroup;
use VuFindSearch\Query\Query;
use VuFindSearch\ParamBag;
use VuFindSearch\Backend\Solr\Response\Json\Spellcheck;
/**
@@ -46,7 +38,7 @@
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://www.vufind.org Main Page
*/
class Results extends BaseResults
class Results extends \VuFind\Search\Base\Results
{
/**
* Facet details:
@@ -80,7 +72,7 @@ protected function performSearch()
$query = $this->getParams()->getQuery();
$limit = $this->getParams()->getLimit();
$offset = $this->getStartRecord() - 1;
$params = $this->createBackendParameters($query, $this->getParams());
$params = $this->getParams()->getBackendParameters();
$collection = $this->getSearchService()
->search($this->backendId, $query, $offset, $limit, $params);
@@ -94,111 +86,6 @@ protected function performSearch()
$this->results = $collection->getRecords();
}
/**
* Normalize sort parameters.
*
* @param string $sort Sort parameter
*
* @return string
*/
protected function normalizeSort($sort)
{
static $table = array(
'year' => array('field' => 'publishDateSort', 'order' => 'desc'),
'publishDateSort' =>
array('field' => 'publishDateSort', 'order' => 'desc'),
'author' => array('field' => 'authorStr', 'order' => 'asc'),
'title' => array('field' => 'title_sort', 'order' => 'asc'),
'relevance' => array('field' => 'score', 'order' => 'desc'),
'callnumber' => array('field' => 'callnumber', 'order' => 'asc'),
);
$normalized = array();
foreach (explode(',', $sort) as $component) {
$parts = explode(' ', trim($component));
$field = reset($parts);
$order = next($parts);
if (isset($table[$field])) {
$normalized[] = sprintf(
'%s %s',
$table[$field]['field'],
$order ?: $table[$field]['order']
);
} else {
$normalized[] = sprintf(
'%s %s',
$field,
$order ?: 'asc'
);
}
}
return implode(',', $normalized);
}
/**
* Create search backend parameters for advanced features.
*
* @param AbstractQuery $query Current search query
* @param Params $params Search parameters
*
* @return ParamBag
*/
protected function createBackendParameters(AbstractQuery $query, Params $params)
{
$backendParams = new ParamBag();
// Spellcheck
$backendParams->set(
'spellcheck',
$params->getOptions()->spellcheckEnabled() ? 'true' : 'false'
);
// Facets
$facets = $params->getFacetSettings();
if (!empty($facets)) {
$backendParams->add('facet', 'true');
foreach ($facets as $key => $value) {
$backendParams->add("facet.{$key}", $value);
}
$backendParams->add('facet.mincount', 1);
}
// Filters
$filters = $params->getFilterSettings();
foreach ($filters as $filter) {
$backendParams->add('fq', $filter);
}
// Shards
$allShards = $params->getOptions()->getShards();
$shards = $params->getSelectedShards();
if (is_null($shards)) {
$shards = array_keys($allShards);
}
// If we have selected shards, we need to format them:
if (!empty($shards)) {
$selectedShards = array();
foreach ($shards as $current) {
$selectedShards[$current] = $allShards[$current];
}
$shards = $selectedShards;
$backendParams->add('shards', implode(',', $selectedShards));
}
// Sort
$sort = $params->getSort();
if ($sort) {
$backendParams->add('sort', $this->normalizeSort($sort));
}
// Highlighting -- on by default, but we should disable if necessary:
if (!$params->getOptions()->highlightEnabled()) {
$backendParams->add('hl', 'false');
}
return $backendParams;
}
/**
* Process SOLR spelling suggestions.
*
@@ -27,10 +27,6 @@
*/
namespace VuFind\Search\SolrAuthorFacets;
use VuFindSearch\Query\AbstractQuery;
use VuFindSearch\ParamBag;
/**
* AuthorFacets Search Results
*
@@ -51,7 +47,7 @@ class Results extends \VuFind\Search\Solr\Results
protected function performSearch()
{
$query = $this->getParams()->getQuery();
$params = $this->createBackendParameters($query, $this->getParams());
$params = $this->getParams()->getBackendParameters();
// Perform the search:
$collection = $this->getSearchService()
->search($this->backendId, $query, 0, 0, $params);

0 comments on commit b5458e2

Please sign in to comment.
You can’t perform that action at this time.