fix parsing of queries with fti prefixes
[Biblio-Z3950.git] / CROSBI.pm
index b090760..e523d0f 100644 (file)
--- a/CROSBI.pm
+++ b/CROSBI.pm
@@ -31,15 +31,15 @@ sub diag {
 # @attr 1=1016 any
 
 sub usemap {{
-       4               => '',
-       7               => '',
-       8               => '',
-       1003    => '',
-#      16              => '',
-       21              => '',
-       12              => '',
-#      1007    => '',
-       1016    => '',
+       4               => 'fti_pr:',
+       7               => 'fti_pr:',
+       8               => 'fti_pr:',
+       1003            => 'fti_au:',
+       16              => 'fti_pr:',
+       21              => 'fti_pr:',
+       12              => 'fti_pr:',
+       1007            => 'pti_pr:',
+       1016            => 'fti_au,fti_pr:',
 }};
 
 =for sql
@@ -47,6 +47,8 @@ sub usemap {{
 =cut
 
 my $dbname = 'bibliografija';
+my @and;
+my @exec;
 
 sub search {
        my ( $self, $query ) = @_;
@@ -56,8 +58,6 @@ sub search {
 
        die "need query" unless defined $query;
 
-       my $tsquery = join(' & ', split(/\s+/,$query) );
-
        my $table = lc $self->{database};
        $table =~ s/^crosbi-//g;
 
@@ -71,30 +71,59 @@ left outer join rad_projekt using (id)
 left outer join rad_godina using (id)
 left outer join rad_podrucje using (id)
 left outer join url using (id)
-where rad_ustanova.sifra = ? and (
-          fti_au @@ to_tsquery(?)
-       or fti_pr @@ to_tsquery(?)
-)
-
        };
 
-       my $dbh = DBI->connect_cached("dbi:Pg:dbname=$dbname", '', '', {AutoCommit => 0});
+       @and = ( qq{ rad_ustanova.sifra = ? } );
+       @exec =  ( 130 ); # FIXME ustanova
+
+               sub parse_fti {
+                       my $query = shift;
+                       warn "## parse_fti [$query]";
+                       my $fti;
+                       if ( $query =~ s/^(fti_.+):// ) {
+                               $fti = $1;
+                       } else {
+                               warn "INVALID QUERY no fti_xxx: [$query]";
+                       }
+
+                       my $tsquery = join(' & ', split(/\s+/,$query) );
+
+                       my @or;
+                       foreach my $f ( split(/,/,$fti) ) {
+                               push @or, "$f @@ to_tsquery(?)";
+                               push @exec, $tsquery;
+                       };
+                       push @and, "( " . join(" or ", @or) . ")";
+               }
+
+       if ( $query =~ / AND / ) {
+               foreach my $and ( split(/ AND /, $query) ) {
+                       parse_fti $and;
+               }
+       } elsif ( $query =~ m/fti_.+:/ ) {
+               parse_fti $query;
+       } else { # no " AND " in query
+               my $tsquery = join(' & ', split(/\s+/,$query) );
+               push @and, "( fti_au @@ to_tsquery(?) or fti_pr @@ to_tsquery(?) )";
+               push @exec, $tsquery,  $tsquery;
+       }
+
 
-warn "XXX SQL = ",$sql;
+       $sql .= "where " . join(" and ", @and);
+
+warn "XXX SQL = ",$sql, dump( @exec );
+
+       my $dbh = DBI->connect_cached("dbi:Pg:dbname=$dbname", '', '', {AutoCommit => 0});
 
        my $sth = $dbh->prepare( $sql );
 
-       $sth->execute(
-               130, # FIXME ustanova
-               $tsquery,
-               $tsquery,
-#              , '%' . $query . '%'
-       );
+       $sth->execute( @exec );
 
-       $self->{_sth} = $sth;
        my $hits = $sth->rows;
 
-       warn "# [$tsquery] $hits hits\n";
+       $self->{_sth} = $sth;
+
+       warn "# [$query] $hits hits\n";
 
        return $self->{hits} = $hits;
 }
@@ -400,7 +429,7 @@ sub next_marc {
 =cut
 
 #      diag "# hash ",dump($hash);
-       diag "# marc\n", $marc->as_formatted;
+       diag "# marc\n", $marc->as_formatted if $ENV{DEBUG};
 
        $self->save_marc( "$id.marc", $marc->as_usmarc );