@@ -, +, @@
- DEV atomic update: bug_27113-elasticsearch_autocomplete_input_search.perl
- Upgrade to XXX done : Bug 27113 - Autocomplete input on main page with elasticsearch
- IntranetAutocompleteElasticSearch;
- OPACAutocompleteElasticSearch;
- IntranetAutocompleteElasticSearch;
- OPACAutocompleteElasticSearch;
---
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(-)
--- a/Koha/SearchEngine/Elasticsearch/Browse.pm
+++ a/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
--- a/api/elasticsearch/elasticsearch.pl
+++ a/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
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/js_includes.inc
+++ a/koha-tmpl/intranet-tmpl/prog/en/includes/js_includes.inc
@@ -173,6 +173,6 @@
[% END %]
[% IF ( Koha.Preference('IntranetAutocompleteElasticSearch') ) %]
- [% Asset.js("js/elasticsearch/autocomplete.js") %]
+[% Asset.js("js/elasticsearch/autocomplete.js") | $raw %]
[% END %]
--- a/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-bottom.inc
+++ a/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-bottom.inc
@@ -295,7 +295,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') ) %]