Bug 18316: Add weighting/relevancy options to ES query on simple search
authorNick Clemens <nick@bywatersolutions.com>
Wed, 21 Mar 2018 13:27:12 +0000 (13:27 +0000)
committerNick Clemens <nick@bywatersolutions.com>
Wed, 7 Nov 2018 22:00:30 +0000 (22:00 +0000)
The idea is the following: if some search field(s) are weighted in
search engine config page, Koha will query ES on all fields plus those with
the coresponding weights. Else, search is done on the entire record with
no weighting. The advanced search page is unaffected by these changes

Test plan (having Koha working with Elasticsearch):
  - apply this patch
  - have some weights defined for various fields
  - try searches from the search bar and from the advanced search page
  - confirm weighting affects the relevancy (in expected ways)
      e.g.
      1. search for 'a' from advanced search, note results
      2. give 'title' a weight
      3. search for 'a' using the simple search bar
      4. results with 'a' in the title should now be more relevant
  - confirm search results on advanced search page are unaffected

Signed-off-by: Séverine QUEUNE <severine.queune@bulac.fr>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Séverine QUEUNE <severine.queune@bulac.fr>

Rebased-by: Alex Arnaud <alex.arnaud@biblibre.com>
Signed-off-by: Ere Maijala <ere.maijala@helsinki.fi>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
Koha/SearchFields.pm
catalogue/search.pl
koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/advsearch.tt
t/db_dependent/Koha/SearchField.t

index 40c88b9..91b09ab 100644 (file)
@@ -92,6 +92,8 @@ sub build_query {
             default_operator => 'AND',
             default_field    => '_all',
             lenient          => JSON::true,
+            fields           => $options{fields},
+            use_dis_max      => JSON::true,
         }
     };
 
@@ -228,9 +230,19 @@ sub build_query_compat {
         join( ' ', $self->_create_query_string(@search_params) ) || (),
         $self->_join_queries( $self->_convert_index_strings(@$limits) ) || () );
 
+    my @weights = $params->{weight};
+    my @w_fields = $params->{w_fields};
+    my @fields = '_all';
+    if ( defined $weights[0] ) {
+        for (my $i = 0 ; $i < (scalar @weights) ; $i++ ){
+            push @fields, "$w_fields[$i]^$weights[$i]";
+        }
+    }
+
     # If there's no query on the left, let's remove the junk left behind
     $query_str =~ s/^ AND //;
     my %options;
+    $options{fields} = \@fields;
     $options{sort} = \@sort_params;
     $options{expanded_facet} = $params->{expanded_facet};
     my $query = $self->build_query( $query_str, %options );
index 7af3c0b..3cfcec3 100644 (file)
@@ -35,6 +35,29 @@ Koha::SearchFields - Koha SearchField Object set class
 
 =cut
 
+=head3 weighted_fields
+
+my (@w_fields, @weight) = Koha::SearchFields->weighted_fields();
+
+=cut
+
+sub weighted_fields {
+    my ($self) = @_;
+
+    my ($w_fields, $weight) = ([], []);
+    my $fields = $self->search(
+        { weight => { '>' => 0, '!=' => undef } },
+        { order_by => { -desc => 'weight' } }
+    );
+
+    while ( my $field = $fields->next ) {
+        push @$w_fields, $field->name;
+        push @$weight, $field->weight;
+    }
+
+    return ($w_fields, $weight);
+}
+
 =head3 type
 
 =cut
index d9961a0..d24f424 100755 (executable)
@@ -155,6 +155,7 @@ use Koha::Patrons;
 use Koha::SearchEngine::Search;
 use Koha::SearchEngine::QueryBuilder;
 use Koha::Virtualshelves;
+use Koha::SearchFields;
 
 use URI::Escape;
 
@@ -455,6 +456,11 @@ my $expanded_facet = $params->{'expand'};
 # Define some global variables
 my ( $error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type);
 
+my ($w_fields, $weight);
+unless ( $cgi->param('advsearch') ) {
+    ($w_fields, $weight) = Koha::SearchFields->weighted_fields();
+}
+
 my $builder = Koha::SearchEngine::QueryBuilder->new(
     { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
 my $searcher = Koha::SearchEngine::Search->new(
@@ -467,7 +473,7 @@ my $searcher = Koha::SearchEngine::Search->new(
     $query_type
   )
   = $builder->build_query_compat( \@operators, \@operands, \@indexes, \@limits,
-    \@sort_by, $scan, $lang );
+    \@sort_by, $scan, $lang, { w_fields => @$w_fields, weight => @$weight  } );
 
 ## parse the query_cgi string and put it into a form suitable for <input>s
 my @query_inputs;
index b8f2a6b..e6a143f 100644 (file)
@@ -18,6 +18,7 @@
 
 <form action="search.pl" method="get">
 <div id="advanced-search">
+<input type="hidden" name="advsearch" value="1"/>
 <h1>Advanced search</h1>
 <p>
   <a href="/cgi-bin/koha/catalogue/itemsearch.pl">Go to item search</a>
index 2e4bed1..3338b59 100644 (file)
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 8;
+use Test::More tests => 16;
 
 use Koha::Database;
 use Koha::SearchFields;
@@ -132,4 +132,72 @@ my $sf3 = $builder->build({
 $search_field = Koha::SearchFields->find($sf3->{id});
 ok(!$search_field->is_mapped_biblios, 'Search field is not mapped');
 
+Koha::SearchFields->search({})->delete;
+
+$builder->build({
+    source => 'SearchField',
+    value => {
+        name    => 'acqdate',
+        label   => 'acqdate',
+        weight  => undef
+    }
+});
+
+$builder->build({
+    source => 'SearchField',
+    value => {
+        name    => 'copydate',
+        label   => 'copydate',
+        weight  => undef
+    }
+});
+
+$builder->build({
+    source => 'SearchField',
+    value => {
+        name    => 'ccode',
+        label   => 'ccode',
+        weight  => 0
+    }
+});
+
+$builder->build({
+    source => 'SearchField',
+    value => {
+        name    => 'title',
+        label   => 'title',
+        weight  => 25
+    }
+});
+
+$builder->build({
+    source => 'SearchField',
+    value => {
+        name    => 'subject',
+        label   => 'subject',
+        weight  => 15
+    }
+});
+
+$builder->build({
+    source => 'SearchField',
+    value => {
+        name    => 'author',
+        label   => 'author',
+        weight  => 5
+    }
+});
+
+my ($w_fields, $weight) = Koha::SearchFields->weighted_fields();
+is(scalar(@$w_fields), 3, 'weighted_fields should return 3 weighted fields.');
+is(scalar(@$weight), 3, 'weighted_fields should return 3 weight.');
+
+is($w_fields->[0], 'title', 'First field is title.');
+is($w_fields->[1], 'subject', 'Second field is subject.');
+is($w_fields->[2], 'author', 'Third field is author.');
+
+is($weight->[0], 25, 'Title weight is 25.');
+is($weight->[1], 15, 'Subject weight is 15.');
+is($weight->[2], 5, 'Author weight is 5.');
+
 $schema->storage->txn_rollback;
\ No newline at end of file