removing useless chars from search
[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         my $log = 'OpenSRF::Utils::Logger';
7
8         sub compile {
9                 my $self = shift;
10                 my $term = shift;
11
12                 $log->debug("Raw term: $term",DEBUG);
13
14                 $term =~ s/\&//go;
15                 $term =~ s/\|//go;
16
17                 $self = ref($self) || $self;
18                 $self = bless {} => $self;
19
20                 $term =~ s/\pM//gos;
21                 $self->decompose($term);
22
23                 my $newterm = join('&', $self->words);
24
25                 if (@{$self->nots}) {
26                         $newterm = '('.$newterm.')&!('. join('|', $self->nots) . ')';
27                 }
28
29                 $log->debug("Compiled term is [$newterm]", DEBUG);
30                 $newterm = OpenILS::Application::Storage::Driver::Pg->quote($newterm);
31                 $log->debug("Quoted term is [$newterm]", DEBUG);
32
33                 $self->{fts_query} = ["to_tsquery('default',$newterm)"];
34                 $self->{fts_query_nots} = [];
35                 $self->{fts_op} = '@@';
36                 $self->{text_col} = shift;
37                 $self->{fts_col} = shift;
38
39                 return $self;
40         }
41
42         sub sql_where_clause {
43                 my $self = shift;
44                 my $column = $self->fts_col;
45                 my @output;
46         
47                 my @ranks;
48                 for my $fts ( $self->fts_query ) {
49                         push @output, join(' ', $self->fts_col, $self->{fts_op}, $fts);
50                         push @ranks, "rank($column, $fts)";
51                 }
52                 $self->{fts_rank} = \@ranks;
53         
54                 my $phrase_match = $self->sql_exact_phrase_match();
55                 return join(' AND ', @output) . $phrase_match;
56         }
57
58 }
59
60 1;