Bug 33159: Simplify ES handling and fix zebra handling
[srvgit] / Koha / SearchEngine / Elasticsearch / QueryBuilder.pm
index 7250ea6..d5dbdee 100644 (file)
@@ -305,10 +305,16 @@ sub build_query_compat {
         # would be to pass them separately into build_query and let it build
         # them into a structured ES query itself. Maybe later, though that'd be
         # more robust.
-        $search_param_query_str = join( ' ', $self->_create_query_string(@search_params) );
+        my @search_param_query_array = $self->_create_query_string(@search_params);
+        $search_param_query_str = join( ' ', @search_param_query_array );
+        my $search_param_limit_str =
+          $self->_join_queries( $self->_convert_index_strings(@$limits) );
+        if ( @search_param_query_array > 1 && $search_param_limit_str ) {
+            $search_param_query_str = "($search_param_query_str)";
+        }
         $query_str = join( ' AND ',
             $search_param_query_str || (),
-            $self->_join_queries( $self->_convert_index_strings(@$limits) ) || () );
+            $search_param_limit_str || () );
 
         # If there's no query on the left, let's remove the junk left behind
         $query_str =~ s/^ AND //;
@@ -553,10 +559,21 @@ our $koha_to_index_name = {
     'match-heading' => 'match-heading',
     'see-from'      => 'match-heading-see-from',
     thesaurus       => 'subject-heading-thesaurus',
+    'thesaurus-conventions' => 'subject-heading-thesaurus-conventions',
     any             => '',
     all             => ''
 };
 
+our $thesaurus_to_value = {
+   lcsh => 'a',
+   lcac => 'b',
+   mesh => 'c',
+   nal  => 'd',
+   notspecified => 'n',
+   cash => 'k',
+   rvm => 'v',
+};
+
 sub build_authorities_query_compat {
     my ( $self, $marclist, $and_or, $excluding, $operator, $value,
         $authtypecode, $orderby )
@@ -581,6 +598,8 @@ sub build_authorities_query_compat {
     }
     for ( my $i = 0 ; $i < @$value ; $i++ ) {
         next unless $value->[$i]; #clean empty form values, ES doesn't like undefined searches
+        $value->[$i] = $thesaurus_to_value->{ $value->[$i] }
+            if( defined $thesaurus_to_value->{ $value->[$i] } && $indexes[$i] eq 'subject-heading-thesaurus' );
         push @searches,
           {
             where    => $indexes[$i],
@@ -694,6 +713,7 @@ sub _convert_sort_fields {
         relevance   => undef,       # default
         title       => 'title',
         pubdate     => 'date-of-publication',
+        biblionumber => 'local-number',
     );
     my %sort_order_convert =
       ( qw( desc desc ), qw( dsc desc ), qw( asc asc ), qw( az asc ), qw( za desc ) );
@@ -784,7 +804,7 @@ will have to wait for a real query parser.
 
 sub _convert_index_strings_freeform {
     my ( $self, $search ) = @_;
-    # @TODO: Currenty will alter also fields contained within quotes:
+    # @TODO: Currently will alter also fields contained within quotes:
     # `searching for "stuff cn:123"` for example will become
     # `searching for "stuff local-number:123"
     #
@@ -798,7 +818,7 @@ sub _convert_index_strings_freeform {
     # Lower case field names
     $search =~ s/($field_name_pattern)(?:,[\w-]*)?($multi_field_pattern):/\L$1\E$2:/og;
     # Resolve possible field aliases
-    $search =~ s/($field_name_pattern)($multi_field_pattern):/(exists $index_field_convert{$1} ? $index_field_convert{$1} : $1)."$2:"/oge;
+    $search =~ s/($field_name_pattern)($multi_field_pattern):/(exists $index_field_convert{$1} ? $index_field_convert{$1} : $1).($1 eq 'kw' ? "$2" : "$2:")/oge;
     return $search;
 }
 
@@ -1021,9 +1041,9 @@ sub _query_regex_escape_process {
             # Will escape unescaped slashes (/) while preserving
             # unescaped slashes within quotes
             # @TODO: assumes quotes are always balanced and will
-            # not handle escaped qoutes properly, should perhaps be
+            # not handle escaped quotes properly, should perhaps be
             # replaced with a more general parser solution
-            # so that this function is ever only provided with unqouted
+            # so that this function is ever only provided with unquoted
             # query parts
             $query =~ s@(?:(?<!\\)((?:[\\]{2})*)(?=/))(?![^"]*"(?:[^"]*"[^"]*")*[^"]*$)@\\$1@g;
         }
@@ -1055,14 +1075,28 @@ sub _fix_limit_special_cases {
     foreach my $l (@$limits) {
 
         # This is set up by opac-search.pl
-        if ( $l =~ /^yr,st-numeric,ge=/ ) {
+        if ( $l =~ /^yr,st-numeric,ge[=:]/ ) {
             my ( $start, $end ) =
-              ( $l =~ /^yr,st-numeric,ge=(.*) and yr,st-numeric,le=(.*)$/ );
+              ( $l =~ /^yr,st-numeric,ge[=:](.*) and yr,st-numeric,le[=:](.*)$/ );
             next unless defined($start) && defined($end);
             push @new_lim, "date-of-publication:[$start TO $end]";
         }
-        elsif ( $l =~ /^yr,st-numeric=/ ) {
-            my ($date) = ( $l =~ /^yr,st-numeric=(.*)$/ );
+        elsif( $l =~ /^search_filter:/ ){
+            # Here we are going to get the query as a string, clean it, and take care of the part of the limit
+            # Calling build_query_compat here is avoided because we generate more complex query structures
+            my ($filter_id) = ( $l =~ /^search_filter:(.*)$/ );
+            my $search_filter = Koha::SearchFilters->find( $filter_id );
+            next unless $search_filter;
+            my ($expanded_lim,$query_lim) = $search_filter->expand_filter;
+            # In the case of nested filters we need to expand them all
+            foreach my $el ( @{$self->_fix_limit_special_cases($expanded_lim)} ){
+                push @new_lim, $el;
+            }
+            # We need to clean the query part as we have built a string from the original search
+            push @new_lim, $self->clean_search_term( $query_lim );
+        }
+        elsif ( $l =~ /^yr,st-numeric[=:]/ ) {
+            my ($date) = ( $l =~ /^yr,st-numeric[=:](.*)$/ );
             next unless defined($date);
             $date = $self->_modify_string_by_type(type => 'st-year', operand => $date);
             push @new_lim, "date-of-publication:$date";
@@ -1095,7 +1129,7 @@ sub _fix_limit_special_cases {
             }
         }
         elsif ( $l =~ /^available$/ ) {
-            push @new_lim, 'onloan:false';
+            push @new_lim, 'available:true';
         }
         else {
             my ( $field, $term ) = $l =~ /^\s*([\w,-]*?):(.*)/;