From efe0b5c60d1041472be40de3a32e2c136658ab0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9rick?= Date: Fri, 3 Aug 2012 14:39:02 -0400 Subject: [PATCH] Add search suggestions to search results on the OPAC. The suggestions are based on the "see from" values in authority records. --- .../prog/en/modules/admin/preferences/opac.pref | 6 ++++ .../prog/en/modules/opac-results-grouped.tt | 9 +++++ .../opac-tmpl/prog/en/modules/opac-results.tt | 9 +++++ opac/opac-search.pl | 32 ++++++++++++++++++++ 4 files changed, 56 insertions(+), 0 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref index eb74d81..de40b10 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref @@ -330,6 +330,12 @@ OPAC: yes: Enable no: Disable - browsing and paging search results from the OPAC detail page. + - + - pref: OPACSearchSuggestions + choices: + yes: Show + no: "Don't show" + - search suggestions in the OPAC's search results page. Suggestions are found by searching for the search terms in the "see-from" of authority records. Policy: - diff --git a/koha-tmpl/opac-tmpl/prog/en/modules/opac-results-grouped.tt b/koha-tmpl/opac-tmpl/prog/en/modules/opac-results-grouped.tt index 99c31bd..8fedd76 100644 --- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-results-grouped.tt +++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-results-grouped.tt @@ -98,6 +98,15 @@ function highlightOn() { [% END %] +[% IF ( suggestionsloop ) %] +
+ Did you mean : + [% FOREACH suggestionsloo IN suggestionsloop %] + [% suggestionsloo.suggestionterm %]   + [% END %] +
+[% END %] + [% IF ( query_error ) %]

Error:

diff --git a/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tt b/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tt index 9361419..5160561 100644 --- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tt +++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-results.tt @@ -260,6 +260,15 @@ $(document).ready(function(){ [% END %] +[% IF ( suggestionsloop ) %] +
+ Did you mean : + [% FOREACH suggestionsloo IN suggestionsloop %] + [% suggestionsloo.suggestionterm %]   + [% END %] +
+[% END %] + [% IF ( query_error ) %]

Error:

diff --git a/opac/opac-search.pl b/opac/opac-search.pl index 5f3ad86..546adcb 100755 --- a/opac/opac-search.pl +++ b/opac/opac-search.pl @@ -780,6 +780,38 @@ if ($query_desc || $limit_desc) { $template->param(searchdesc => 1); } +# III. LOOK FOR SEARCH SUGGESTIONS IN AUTHORITIES + +if (C4::Context->preference('OPACSearchSuggestions')) { + # Search for authorities that could better represent the search query + my @suggestions; + + # Set the index of all operands for Match-heading-see-from search + my @indexes_match; + foreach(@operands){push(@indexes_match, 'Match-heading-see-from,ext');} + + # Build the query and do a simple search with it + ($error, $query) = buildQuery(\@operators,\@operands,\@indexes_match,0,0,0,$lang); + my ($error2, $results, $total_hits) = SimpleSearch( $query, undef, undef, [ "authorityserver" ] ); + + # Add the main Heading of each search result and add it to the suggestion list + foreach my $r (@$results) + { + my $record = MARC::Record->new_from_usmarc( $r ); + my @subfields = ($record->field('1..'))[0]->subfields(); + + # Fuse the subfields + my $suggestionterm = ''; + foreach(@subfields){$suggestionterm .= $_->[1] . ', ';} + $suggestionterm =~ s/,\s+$//; + + my $suggestion = {'suggestionterm' => $suggestionterm, 'suggestionquery' => $suggestionterm}; + push(@suggestions, $suggestion); + } + + $template->param( 'suggestionsloop' => \@suggestions ); +} + # VI. BUILD THE TEMPLATE # Build drop-down list for 'Add To:' menu... my ($totalref, $pubshelves, $barshelves)= -- 1.7.2.5