Bug 27673: Replace YAML with YAML::XS
[koha-ffzg.git] / C4 / Search.pm
index a9448e4..f5f91db 100644 (file)
@@ -23,7 +23,6 @@ use C4::Koha;      # getFacets
 use Koha::DateUtils;
 use Koha::Libraries;
 use Lingua::Stem;
-use C4::Search::PazPar2;
 use XML::Simple;
 use C4::XSLT;
 use C4::Reserves;    # GetReserveStatus
@@ -34,7 +33,6 @@ use Koha::ItemTypes;
 use Koha::Libraries;
 use Koha::Patrons;
 use Koha::RecordProcessor;
-use YAML;
 use URI::Escape;
 use Business::ISBN;
 use MARC::Record;
@@ -107,9 +105,6 @@ sub FindDuplicate {
         $result->{title} =~ s /\(//g;
         $result->{title} =~ s /\)//g;
 
-        # FIXME: instead of removing operators, could just do
-        # quotes around the value
-        $result->{title} =~ s/(and|or|not)//g;
         $query = "$titleindex:\"$result->{title}\"";
         if   ( $result->{author} ) {
             $result->{author} =~ s /\\//g;
@@ -117,8 +112,6 @@ sub FindDuplicate {
             $result->{author} =~ s /\(//g;
             $result->{author} =~ s /\)//g;
 
-            # remove valid operators
-            $result->{author} =~ s/(and|or|not)//g;
             $query .= " $op $authorindex:\"$result->{author}\"";
         }
     }
@@ -224,7 +217,7 @@ sub SimpleSearch {
         eval {
             $zconns[$i] = C4::Context->Zconn( $servers[$i], 1 );
             $query =~ s/:/=/g unless $options{skip_normalize};
-            $zoom_queries[$i] = new ZOOM::Query::CCL2RPN( $query, $zconns[$i]);
+            $zoom_queries[$i] = ZOOM::Query::CCL2RPN->new( $query, $zconns[$i]);
             $tmpresults[$i] = $zconns[$i]->search( $zoom_queries[$i] );
 
             # error handling
@@ -337,18 +330,18 @@ sub getRecords {
             if ($query_type) {
                 if ($query_type =~ /^ccl/) {
                     $query_to_use =~ s/\:/\=/g;    # change : to = last minute (FIXME)
-                    $results[$i] = $zconns[$i]->search(new ZOOM::Query::CCL2RPN($query_to_use, $zconns[$i]));
+                    $results[$i] = $zconns[$i]->search(ZOOM::Query::CCL2RPN->new($query_to_use, $zconns[$i]));
                 } elsif ($query_type =~ /^cql/) {
-                    $results[$i] = $zconns[$i]->search(new ZOOM::Query::CQL($query_to_use, $zconns[$i]));
+                    $results[$i] = $zconns[$i]->search(ZOOM::Query::CQL->new($query_to_use, $zconns[$i]));
                 } elsif ($query_type =~ /^pqf/) {
-                    $results[$i] = $zconns[$i]->search(new ZOOM::Query::PQF($query_to_use, $zconns[$i]));
+                    $results[$i] = $zconns[$i]->search(ZOOM::Query::PQF->new($query_to_use, $zconns[$i]));
                 } else {
                     warn "Unknown query_type '$query_type'.  Results undetermined.";
                 }
             } elsif ($scan) {
-                    $results[$i] = $zconns[$i]->scan(  new ZOOM::Query::CCL2RPN($query_to_use, $zconns[$i]));
+                    $results[$i] = $zconns[$i]->scan(  ZOOM::Query::CCL2RPN->new($query_to_use, $zconns[$i]));
             } else {
-                    $results[$i] = $zconns[$i]->search(new ZOOM::Query::CCL2RPN($query_to_use, $zconns[$i]));
+                    $results[$i] = $zconns[$i]->search(ZOOM::Query::CCL2RPN->new($query_to_use, $zconns[$i]));
             }
         };
         if ($@) {
@@ -680,6 +673,7 @@ sub _get_facets_data_from_record {
                 next if $field->indicator(1) eq 'z';
 
                 my $data = $field->as_string( $subfield_letters, $facet->{ sep } );
+                $data =~ s/\s*(?<!\p{Uppercase})[.\-,;]*\s*$//;
 
                 unless ( grep { $_ eq $data } @used_datas ) {
                     push @used_datas, $data;
@@ -778,8 +772,9 @@ sub _get_facet_from_result_set {
     my $facets = {};
     foreach my $term ( @terms ) {
         my $facet_value = $term->textContent;
+        $facet_value =~ s/\s*(?<!\p{Uppercase})[.\-,;]*\s*$//;
         $facet_value =~ s/\Q$internal_sep\E/$sep/ if defined $sep;
-        $facets->{ $facet_value } = $term->getAttribute( 'occur' );
+        $facets->{ $facet_value } += $term->getAttribute( 'occur' );
     }
 
     return $facets;
@@ -807,80 +802,6 @@ sub _get_facets_info {
     return $facets_info;
 }
 
-sub pazGetRecords {
-    my (
-        $koha_query,       $simple_query, $sort_by_ref,    $servers_ref,
-        $results_per_page, $offset,       $branches,       $query_type,
-        $scan
-    ) = @_;
-    #NOTE: Parameter $branches is not used here !
-
-    my $paz = C4::Search::PazPar2->new(C4::Context->config('pazpar2url'));
-    $paz->init();
-    $paz->search($simple_query);
-    sleep 1;   # FIXME: WHY?
-
-    # do results
-    my $results_hashref = {};
-    my $stats = XMLin($paz->stat);
-    my $results = XMLin($paz->show($offset, $results_per_page, 'work-title:1'), forcearray => 1);
-
-    # for a grouped search result, the number of hits
-    # is the number of groups returned; 'bib_hits' will have
-    # the total number of bibs.
-    $results_hashref->{'biblioserver'}->{'hits'} = $results->{'merged'}->[0];
-    $results_hashref->{'biblioserver'}->{'bib_hits'} = $stats->{'hits'};
-
-    HIT: foreach my $hit (@{ $results->{'hit'} }) {
-        my $recid = $hit->{recid}->[0];
-
-        my $work_title = $hit->{'md-work-title'}->[0];
-        my $work_author;
-        if (exists $hit->{'md-work-author'}) {
-            $work_author = $hit->{'md-work-author'}->[0];
-        }
-        my $group_label = (defined $work_author) ? "$work_title / $work_author" : $work_title;
-
-        my $result_group = {};
-        $result_group->{'group_label'} = $group_label;
-        $result_group->{'group_merge_key'} = $recid;
-
-        my $count = 1;
-        if (exists $hit->{count}) {
-            $count = $hit->{count}->[0];
-        }
-        $result_group->{'group_count'} = $count;
-
-        for (my $i = 0; $i < $count; $i++) {
-            # FIXME -- may need to worry about diacritics here
-            my $rec = $paz->record($recid, $i);
-            push @{ $result_group->{'RECORDS'} }, $rec;
-        }
-
-        push @{ $results_hashref->{'biblioserver'}->{'GROUPS'} }, $result_group;
-    }
-
-    # pass through facets
-    my $termlist_xml = $paz->termlist('author,subject');
-    my $terms = XMLin($termlist_xml, forcearray => 1);
-    my @facets_loop = ();
-    #die Dumper($results);
-#    foreach my $list (sort keys %{ $terms->{'list'} }) {
-#        my @facets = ();
-#        foreach my $facet (sort @{ $terms->{'list'}->{$list}->{'term'} } ) {
-#            push @facets, {
-#                facet_label_value => $facet->{'name'}->[0],
-#            };
-#        }
-#        push @facets_loop, ( {
-#            type_label => $list,
-#            facets => \@facets,
-#        } );
-#    }
-
-    return ( undef, $results_hashref, \@facets_loop );
-}
-
 # TRUNCATION
 sub _detect_truncation {
     my ( $operand, $index ) = @_;
@@ -1264,7 +1185,6 @@ See verbose embedded documentation.
 
 sub buildQuery {
     my ( $operators, $operands, $indexes, $limits, $sort_by, $scan, $lang) = @_;
-
     warn "---------\nEnter buildQuery\n---------" if $DEBUG;
 
     my $query_desc;
@@ -1376,7 +1296,7 @@ sub buildQuery {
 
                 # Add index-specific attributes
 
-                #Afaik, this 'yr' condition will only ever be met in the staff client advanced search
+                #Afaik, this 'yr' condition will only ever be met in the staff interface advanced search
                 #for "Publication date", since typing 'yr:YYYY' into the search box produces a CCL query,
                 #which is processed higher up in this sub. Other than that, year searches are typically
                 #handled as limits which are not processed her either.
@@ -1493,6 +1413,13 @@ sub buildQuery {
 
                 warn "FIELD WEIGHTED OPERAND: >$weighted_operand<" if $DEBUG;
 
+                #Use relevance ranking when not using a weighted query (which adds relevance ranking of its own)
+
+                #N.B. Truncation is mutually exclusive with Weighted Queries,
+                #so even if QueryWeightFields is turned on, QueryAutoTruncate will turn it off, thus
+                #the need for this relevance wrapper.
+                $operand = "(rk=($operand))" unless $weight_fields;
+
                 ($query,$query_cgi,$query_desc,$previous_operand) = _build_initial_query({
                     query => $query,
                     query_cgi => $query_cgi,
@@ -1534,7 +1461,7 @@ sub buildQuery {
             if ( $k !~ /mc-i(tem)?type/ ) {
                 # in case the mc-ccode value has complicating chars like ()'s inside it we wrap in quotes
                 $this_limit =~ tr/"//d;
-                $this_limit = $k.":'".$v."'";
+                $this_limit = $k.':"'.$v.'"';
             }
 
             $group_OR_limits{$k} .= " or " if $group_OR_limits{$k};