Bug 12478: phrase searching for authorities is happier
authorRobin Sheat <robin@catalyst.net.nz>
Thu, 12 Mar 2015 05:11:30 +0000 (18:11 +1300)
committerTomas Cohen Arazi <tomascohen@theke.io>
Tue, 26 Apr 2016 12:55:40 +0000 (09:55 -0300)
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Jesse Weaver <jweaver@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Koha/ElasticSearch.pm
Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
Koha/SearchEngine/Elasticsearch/Search.pm

index dfc9556..56afaef 100644 (file)
@@ -133,13 +133,13 @@ sub get_elasticsearch_settings {
                 analyzer => {
                     analyser_phrase => {
                         tokenizer => 'keyword',
-                        filter    => 'lowercase',
+                        filter    => ['lowercase'],
                     },
                     analyser_standard => {
                         tokenizer => 'standard',
-                        filter    => 'lowercase',
+                        filter    => ['lowercase'],
                     }
-                }
+                },
             }
         }
     };
@@ -166,6 +166,11 @@ sub get_elasticsearch_mappings {
                     include_in_all => "false",
                     type           => "string",
                 },
+                '_all.phrase' => {
+                    search_analyzer => "analyser_phrase",
+                    index_analyzer  => "analyser_phrase",
+                    type            => "string",
+                },
             }
         }
     };
@@ -190,7 +195,8 @@ sub get_elasticsearch_mappings {
                     phrase => {
                         search_analyzer => "analyser_phrase",
                         index_analyzer  => "analyser_phrase",
-                        type            => "string"
+                        type            => "string",
+                        copy_to         => "_all.phrase",
                     },
                 },
             };
index 3fc238c..7e35224 100644 (file)
@@ -269,7 +269,10 @@ sub build_authorities_query {
         if ($op eq 'is' || $op eq '=') {
             # look for something that matches completely
             # note, '=' is about numerical vals. May need special handling.
-            push @filter_parts, { term => { $wh => $val }};
+            # _allphrase is a special field that only groups the exact
+            # matches. Also, we lowercase our search because the ES
+            # index lowercases its values.
+            push @filter_parts, { term => { "$wh.phrase" => lc $val }};
         } elsif ($op eq 'exact') {
             # left and right truncation, otherwise an exact phrase
             push @query_parts, { match_phrase => { $wh => $val }};
@@ -283,6 +286,8 @@ sub build_authorities_query {
     # 'should' behaves like 'or', if we want 'and', use 'must'
     my $query_part = { bool => { should => \@query_parts } };
     my $filter_part = { bool => { should => \@filter_parts }};
+    # extract the sort stuff
+    my %sort = ( sort => [ $search->{sort} ] ) if exists $search->{sort};
     my $query;
     if (@filter_parts) {
         $query = { query => { filtered => { filter => $filter_part, query => $query_part }}};
@@ -375,7 +380,7 @@ sub build_authorities_query_compat {
     for ( my $i = 0 ; $i < @$value ; $i++ ) {
         push @searches,
           {
-            where    => $marclist->[$i],
+            where    => $koha_to_index_name{$marclist->[$i]},
             operator => $operator->[$i],
             value    => $value->[$i],
           };
@@ -388,7 +393,7 @@ sub build_authorities_query_compat {
       :                              undef;
     if ($sort_field) {
         my $sort_order = ( $orderby =~ /Asc$/ ) ? 'asc' : 'desc';
-        %sort = ( $orderby => $sort_order, );
+        %sort = ( $sort_field => $sort_order, );
     }
     my %search = (
         searches     => \@searches,
index aa8640f..a9adcba 100644 (file)
@@ -106,7 +106,7 @@ sub count {
 
     my $params = $self->get_elasticsearch_params();
     $self->store(
-        Catmandu::Store::ElasticSearch->new( %$params, trace_calls => 1, ) )
+        Catmandu::Store::ElasticSearch->new( %$params, trace_calls => 0, ) )
       unless $self->store;
 
     my $searcher = $self->store->bag->searcher(query => $query);