From 28e5a6d93a070844eaa2d01124a65d6e19238f73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9rick=20Capovilla?= Date: Tue, 26 May 2015 00:49:38 -0400 Subject: [PATCH] Bug 8571: Add search suggestions to search results on the OPAC. This patch will add search suggestions at the beginning of search results on the OPAC if the "OPACSearchSuggestions" preference is activated. This feature can be compared to "Did you mean?" or autocorrect features in search engines. Authorities records are used as a source of suggestions. The search terms the user entered are used in a "see also" authority search to find suggestions. --- .../prog/en/modules/admin/preferences/opac.pref | 7 +++++ .../bootstrap/en/modules/opac-results-grouped.tt | 9 ++++++ .../opac-tmpl/bootstrap/en/modules/opac-results.tt | 8 ++++++ opac/opac-search.pl | 32 ++++++++++++++++++++++ 4 files changed, 56 insertions(+) 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 c26d048..ca6aee2 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 @@ -468,6 +468,13 @@ OPAC: yes: Display no: Don't display - the acquisition details on OPAC detail pages. + - + - 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: - - pref: singleBranchMode diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results-grouped.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results-grouped.tt index 05d022a..a357d73 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results-grouped.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results-grouped.tt @@ -27,6 +27,15 @@ href="/cgi-bin/koha/opac-rss.pl?[% query_cgi %][% limit_cgi |html %]" /> + [% IF ( suggestionsloop ) %] +
+ Did you mean : + [% FOREACH suggestionsloo IN suggestionsloop %] + [% suggestionsloo.suggestionterm %]   + [% END %] +
+ [% END %] + [% IF ( query_error ) %]
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results.tt index 27c5a10..375683e 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results.tt @@ -114,6 +114,14 @@
[% END %] [% 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 0ff2507..7cce869 100755 --- a/opac/opac-search.pl +++ b/opac/opac-search.pl @@ -918,6 +918,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)= -- 2.1.4