fixing case of only negated words
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Driver / Pg / fts.pm
1 { # Every driver needs to provide a 'compile()' method to OpenILS::Application::Storage::FTS.
2   # If that driver wants to support FTI, that is...
3         #-------------------------------------------------------------------------------
4         package OpenILS::Application::Storage::FTS;
5         use OpenSRF::Utils::Logger qw/:level/;
6         use Unicode::Normalize;
7         my $log = 'OpenSRF::Utils::Logger';
8
9         sub compile {
10                 my $self = shift;
11                 my $term = NFD(shift());
12
13                 $log->debug("Raw term: $term",DEBUG);
14
15                 $term =~ s/\&//go;
16                 $term =~ s/\|//go;
17
18                 $self = ref($self) || $self;
19                 $self = bless {} => $self;
20
21                 $term =~ s/(\pM+)//gos;
22                 $self->decompose($term);
23
24                 my $newterm = '';
25                 $newterm = join('&', $self->words) if ($self->words);
26
27                 if (@{$self->nots}) {
28                         $newterm = '('.$newterm.')&' if ($newterm);
29                         $newterm .= '!('. join('|', $self->nots) . ')';
30                 }
31
32                 $log->debug("Compiled term is [$newterm]", DEBUG);
33                 $newterm = OpenILS::Application::Storage::Driver::Pg->quote($newterm);
34                 $log->debug("Quoted term is [$newterm]", DEBUG);
35
36                 $self->{fts_query} = ["to_tsquery('default',$newterm)"];
37                 $self->{fts_query_nots} = [];
38                 $self->{fts_op} = '@@';
39                 $self->{text_col} = shift;
40                 $self->{fts_col} = shift;
41
42                 return $self;
43         }
44
45         sub sql_where_clause {
46                 my $self = shift;
47                 my $column = $self->fts_col;
48                 my @output;
49         
50                 my @ranks;
51                 for my $fts ( $self->fts_query ) {
52                         push @output, join(' ', $self->fts_col, $self->{fts_op}, $fts);
53                         push @ranks, "rank($column, $fts)";
54                 }
55                 $self->{fts_rank} = \@ranks;
56         
57                 my $phrase_match = $self->sql_exact_phrase_match();
58                 return join(' AND ', @output) . $phrase_match;
59         }
60
61 }
62
63 1;