Skip to content

Commit

Permalink
Added support for a relevance sort override for empty Solr/Summon sea…
Browse files Browse the repository at this point in the history
…rches.
  • Loading branch information
EreMaijala authored and demiankatz committed Mar 25, 2015
1 parent a114d91 commit fdf2de8
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
5 changes: 5 additions & 0 deletions config/vufind/Summon.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
; option should be one of the options present in the [Sorting] section below.
default_sort = relevance

; This setting controls the sort order to be used for empty search when relevance
; sort is selected. Since relevance doesn't have a meaningful function with an empty
; search, this can be set to e.g. "PublicationDate:desc".
;empty_search_relevance_override = "PublicationDate:desc"

; This setting controls the default view for search results; the selected option
; should be one of the options present in the [Views] section below.
default_view = list
Expand Down
5 changes: 5 additions & 0 deletions config/vufind/searches.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ default_handler = AllFields ; Search handler to use if none is specified
; below.
default_sort = relevance

; This setting controls the sort order to be used for empty search when relevance
; sort is selected. Since relevance doesn't have a meaningful function with an empty
; search, this can be set to e.g. "title".
;empty_search_relevance_override = title

; This setting controls the default view for search results; the selected option
; should be one of the options present in the [Views] section below.
default_view = list
Expand Down
21 changes: 21 additions & 0 deletions module/VuFind/src/VuFind/Search/Solr/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ class Options extends \VuFind\Search\Base\Options
*/
protected $hierarchicalFacetSeparators = [];

/**
* Relevance sort override for empty searches
*
* @var string
*/
protected $emptySearchRelevanceOverride = null;

/**
* Constructor
*
Expand All @@ -78,6 +85,10 @@ public function __construct(\VuFind\Config\PluginManager $configLoader)
if (isset($searchSettings->General->default_sort)) {
$this->defaultSort = $searchSettings->General->default_sort;
}
if (isset($searchSettings->General->empty_search_relevance_override)) {
$this->emptySearchRelevanceOverride
= $searchSettings->General->empty_search_relevance_override;
}
if (isset($searchSettings->DefaultSortingByType)
&& count($searchSettings->DefaultSortingByType) > 0
) {
Expand Down Expand Up @@ -252,6 +263,16 @@ public function getAdvancedSearchAction()
return 'search-advanced';
}

/**
* Get the relevance sort override for empty searches.
*
* @return string Sort field or null if not set
*/
public function getEmptySearchRelevanceOverride()
{
return $this->emptySearchRelevanceOverride;
}

/**
* Get an array of hierarchical facets.
*
Expand Down
7 changes: 7 additions & 0 deletions module/VuFind/src/VuFind/Search/Solr/Params.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,13 @@ public function getBackendParameters()
// Sort
$sort = $this->getSort();
if ($sort) {
// If we have an empty search with relevance sort, see if there is
// an override configured:
if ($sort == 'relevance' && $this->getQuery()->getAllTerms() == ''
&& ($relOv = $this->getOptions()->getEmptySearchRelevanceOverride())
) {
$sort = $relOv;
}
$backendParams->add('sort', $this->normalizeSort($sort));
}

Expand Down
21 changes: 21 additions & 0 deletions module/VuFind/src/VuFind/Search/Summon/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ class Options extends \VuFind\Search\Base\Options
*/
protected $maxTopicRecommendations = false;

/**
* Relevance sort override for empty searches
*
* @var string
*/
protected $emptySearchRelevanceOverride = null;

/**
* Constructor
*
Expand Down Expand Up @@ -142,6 +149,10 @@ public function __construct(\VuFind\Config\PluginManager $configLoader)
$this->defaultSortByHandler[$key] = $val;
}
}
if (isset($searchSettings->General->empty_search_relevance_override)) {
$this->emptySearchRelevanceOverride
= $searchSettings->General->empty_search_relevance_override;
}

// Load view preferences (or defaults if none in .ini file):
if (isset($searchSettings->Views)) {
Expand Down Expand Up @@ -176,6 +187,16 @@ public function getAdvancedSearchAction()
return 'summon-advanced';
}

/**
* Get the relevance sort override for empty searches.
*
* @return string Sort field or null if not set
*/
public function getEmptySearchRelevanceOverride()
{
return $this->emptySearchRelevanceOverride;
}

/**
* If there is a limit to how many search results a user can access, this
* method will return that limit. If there is no limit, this will return
Expand Down
12 changes: 11 additions & 1 deletion module/VuFind/src/VuFind/Search/Summon/Params.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,19 @@ public function getBackendParameters()

$options = $this->getOptions();

$sort = $this->getSort();
if ($sort) {
// If we have an empty search with relevance sort, see if there is
// an override configured:
if ($sort == 'relevance' && $this->getQuery()->getAllTerms() == ''
&& ($relOv = $this->getOptions()->getEmptySearchRelevanceOverride())
) {
$sort = $relOv;
}
}

// The "relevance" sort option is a VuFind reserved word; we need to make
// this null in order to achieve the desired effect with Summon:
$sort = $this->getSort();
$finalSort = ($sort == 'relevance') ? null : $sort;
$backendParams->set('sort', $finalSort);

Expand Down

0 comments on commit fdf2de8

Please sign in to comment.