using Unicode::Normalize to get all incoming text into Unicode normal form D
[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 = join('&', $self->words);
25
26                 if (@{$self->nots}) {
27                         $newterm = '('.$newterm.')&!('. join('|', $self->nots) . ')';
28                 }
29
30                 $log->debug("Compiled term is [$newterm]", DEBUG);
31                 $newterm = OpenILS::Application::Storage::Driver::Pg->quote($newterm);
32                 $log->debug("Quoted term is [$newterm]", DEBUG);
33
34                 $self->{fts_query} = ["to_tsquery('default',$newterm)"];
35                 $self->{fts_query_nots} = [];
36                 $self->{fts_op} = '@@';
37                 $self->{text_col} = shift;
38                 $self->{fts_col} = shift;
39
40                 return $self;
41         }
42
43         sub sql_where_clause {
44                 my $self = shift;
45                 my $column = $self->fts_col;
46                 my @output;
47         
48                 my @ranks;
49                 for my $fts ( $self->fts_query ) {
50                         push @output, join(' ', $self->fts_col, $self->{fts_op}, $fts);
51                         push @ranks, "rank($column, $fts)";
52                 }
53                 $self->{fts_rank} = \@ranks;
54         
55                 my $phrase_match = $self->sql_exact_phrase_match();
56                 return join(' AND ', @output) . $phrase_match;
57         }
58
59 }
60
61 1;