From 27aa276f3df5c74b05c038af75c5345cc68765fa Mon Sep 17 00:00:00 2001 From: Ivan Dziuba Date: Fri, 23 Apr 2021 17:08:55 -0400 Subject: [PATCH] Bug 27113: (follow-up) QA Tests Bug 27113: (follow-up) QA Tests (2) Bug 27113: (follow-up) Fixup warnings. TEST PLAN Important! In this patch we need to do reindex ElasticSearch. ElasticSearch must have all information in his index. 1. Go Intranet -> Preference -> SearchEngine -> ElasticSearch !! APPLY PATCH !! 2. Mapping is good (Intranet -> Catalog -> Search engine configuration (Elasticsearch) ). !Recommended 'Reset Mapping' -> 'Yes' 3. In your koha-conf.xml file you must have good for and version ES 4. Update Preference: ./installer/data/mysql/updatedatabase.pl If that passe good you can look the lines: - DEV atomic update: bug_27113-elasticsearch_autocomplete_input_search.perl - Upgrade to XXX done : Bug 27113 - Autocomplete input on main page with elasticsearch 5. After that we can look two options in the preferences: - IntranetAutocompleteElasticSearch; - OPACAutocompleteElasticSearch; 4. For add information in the index we must run script for reindexing: ./misc/search_tools/rebuild_elasticsearch.pl -v -d 5. Waiting for the end of indexing 6. Go on Preference and find : - IntranetAutocompleteElasticSearch; - OPACAutocompleteElasticSearch; Value "Show" turn on autocomplete. 7. Now we have Autocomplete for Intranet/OPAC search input (advanced search also). --- Koha/SearchEngine/Elasticsearch/Browse.pm | 69 +++++++++++++++++-- api/elasticsearch/elasticsearch.pl | 13 ++-- .../prog/en/includes/js_includes.inc | 2 +- .../bootstrap/en/includes/opac-bottom.inc | 2 +- opac/svc/elasticsearch/opac-elasticsearch.pl | 13 ++-- t/Koha_SearchEngine_Elasticsearch_Browse.t | 30 ++++++++ 6 files changed, 111 insertions(+), 18 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch/Browse.pm b/Koha/SearchEngine/Elasticsearch/Browse.pm index 57c0a74023..78f93832c7 100644 --- a/Koha/SearchEngine/Elasticsearch/Browse.pm +++ b/Koha/SearchEngine/Elasticsearch/Browse.pm @@ -40,7 +40,7 @@ Koha::SearchEngine::ElasticSearch::Browse - browse functions for Elasticsearch This provides an easy interface to the "browse" functionality. Essentially, it does a fast prefix search on defined fields. The fields have to be marked -as "suggestible" in the database when indexing takes place. +as "suggestible" in the database when indexing takes place(no action required for autocomplete). =head1 METHODS @@ -162,13 +162,25 @@ sub _build_query { return $query; } +=head2 autocomplete_one_idx + + my $query = $self->autocomplete_one_idx($cgi_q, $prefix, $analyzer, $token_counter); + +Does a prefix search for C<$prefix> (only one prefix), looking for C<$cgi_q> , using analyzer C<$analyzer> , +C<$token_counter> is used for identify which word to use in autocomplete + +=cut + +=head3 Returns + +This returns an arrayref of hashrefs with highlights. Each hashref contains a "text" element that contains the field as returned. + +=cut + sub autocomplete_one_idx { my ($self, $cgi_q, $prefix, $analyzer, $token_counter) = @_; my @source; my $elasticsearch = $self->get_elasticsearch(); - # we can change these variables - my ($nb_fragments, $size_fragment, $pre_tags, $post_tags) = (1, 100, [""], [""]); - my $query = $self->_build_query_autocomplete($cgi_q, $prefix, $analyzer); my $res = $elasticsearch->search( index => $self->index_name, @@ -181,6 +193,22 @@ sub autocomplete_one_idx { return $res; } +=head2 autocomplete_idx + + my $query = $self->autocomplete_idx($cgi_q, $prefix, $analyzer, $token_counter); + +Does a prefix search for C<$prefix> (many prefix), looking for C<$cgi_q>, using analyzer C<$analyzer>, +C<$token_counter> is used for identify which word to use in autocomplete + +=cut + +=head3 Returns + +This returns an arrayref for all prefix of hashrefs with highlights. Each hashref contains a "text" element +that contains the field as returned. + +=cut + sub autocomplete_idx { my ($self, $cgi_q, $prefix, $analyzer, $token_counter) = @_; my %results; @@ -195,6 +223,37 @@ sub autocomplete_idx { return \%results; } +=head2 _build_query_autocomplete + + my $query = $self->_build_query_autocomplete($cgi_q, $prefix, $analyzer); + +Arguments: + +=over 4 + +=item cgi_q + +GET request + +=item prefix + +Field(s) for autocomplete (title, author, etc...) + +=item analyzer + +Name of analyzer wich we use for autocomplete + +=back + +=cut + +=head3 Returns + +This returns an arrayref for all prefix of hashrefs with highlights. Each hashref contains a "text" element +that contains the field as returned. + +=cut + sub _build_query_autocomplete { my ($self, $cgi_q, $prefix, $analyzer) = @_; my (@source); @@ -236,6 +295,8 @@ __END__ =item Robin Sheat << >> +=item Ivan Dziuba << >> + =back =cut diff --git a/api/elasticsearch/elasticsearch.pl b/api/elasticsearch/elasticsearch.pl index 86890dcaab..09a79bc045 100755 --- a/api/elasticsearch/elasticsearch.pl +++ b/api/elasticsearch/elasticsearch.pl @@ -14,13 +14,13 @@ my $cgi = CGI->new; my $session = CGI::Session->new(); $session->param(-name=>'analyzer', -value=>"autocomplete"); -$session->param(-name=>'prefix', -value=>$cgi->param("prefix")); -$session->param(-name=>'q', -value=>$cgi->param("q")); -$session->param(-name=>'key', -value=>$cgi->param("key")); -$session->param(-name=>'token_counter', -value=>$cgi->param("token_counter")); +$session->param(-name=>'prefix', -value=>$cgi->multi_param("prefix")); +$session->param(-name=>'q', -value=>$cgi->multi_param("q")); +$session->param(-name=>'key', -value=>$cgi->multi_param("key")); +$session->param(-name=>'token_counter', -value=>$cgi->multi_param("token_counter")); $session->expire('+1h'); -if ($session->param("key") == "autocomplete") { +if ($session->param("key") eq "autocomplete") { my @prefix = split /,/, $session->param("prefix"); #fields for autocomplete my $length = scalar @prefix; @@ -49,6 +49,7 @@ else { } sub response404JSON { + my $res = CGI->new; my $json = JSON->new->utf8; my $header_type = "application/json"; my $header_status = "404"; @@ -56,7 +57,7 @@ sub response404JSON { "error" => "No data", "description" => "Bad request", }); - print $cgi->header( + print $res->header( -type => $header_type, -charset => "utf-8", -status => $header_status diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/js_includes.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/js_includes.inc index 371c2d9664..ba2f010393 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/js_includes.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/js_includes.inc @@ -146,6 +146,6 @@ [% END %] [% IF ( Koha.Preference('IntranetAutocompleteElasticSearch') ) %] - [% Asset.js("js/elasticsearch/autocomplete.js") %] +[% Asset.js("js/elasticsearch/autocomplete.js") | $raw %] [% END %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-bottom.inc b/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-bottom.inc index 29cc43ac08..827b96e133 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-bottom.inc +++ b/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-bottom.inc @@ -320,7 +320,7 @@ $(document).ready(function() { [% PROCESS jsinclude %] [% IF ( Koha.Preference('OPACAutocompleteElasticSearch') ) %] - [% Asset.js("js/opac-elasticsearch/opac-autocomplete.js") %] + [% Asset.js("js/opac-elasticsearch/opac-autocomplete.js") | $raw %] [% END %] [% IF ( Koha.Preference('OPACUserJS') ) %]