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';
11 my $term = NFD(shift());
13 $log->debug("Raw term: $term",DEBUG);
18 $self = ref($self) || $self;
19 $self = bless {} => $self;
21 $term =~ s/(\pM+)//gos;
22 $term =~ s/(\b\.\b)//gos;
23 $self->decompose($term);
26 $newterm = join('&', $self->words) if ($self->words);
29 $newterm = '('.$newterm.')&' if ($newterm);
30 $newterm .= '!('. join('|', $self->nots) . ')';
33 $log->debug("Compiled term is [$newterm]", DEBUG);
34 $newterm = OpenILS::Application::Storage::Driver::Pg->quote($newterm);
35 $log->debug("Quoted term is [$newterm]", DEBUG);
37 $self->{fts_query} = ["to_tsquery('default',$newterm)"];
38 $self->{fts_query_nots} = [];
39 $self->{fts_op} = '@@';
40 $self->{text_col} = shift;
41 $self->{fts_col} = shift;
46 sub sql_where_clause {
48 my $column = $self->fts_col;
52 for my $fts ( $self->fts_query ) {
53 push @output, join(' ', $self->fts_col, $self->{fts_op}, $fts);
54 push @ranks, "rank($column, $fts)";
56 $self->{fts_rank} = \@ranks;
58 my $phrase_match = $self->sql_exact_phrase_match();
59 return join(' AND ', @output) . $phrase_match;
62 sub sql_exact_phrase_match {
64 my $column = $self->text_col;
66 for my $phrase ( $self->phrases ) {
67 $phrase =~ s/\*/\\*/go;
68 $phrase =~ s/\./\\./go;
69 $phrase =~ s/'/\\'/go;
70 $phrase =~ s/\s+/\\s+/go;
71 $log->debug("Adding phrase [$phrase] to the match list", DEBUG);
72 $output .= " AND $column ~* \$\$(^|\\W+)$phrase(\\W+|\$)\$\$";
74 $log->debug("Phrase list is [$output]", DEBUG);