1 use OpenSRF::Utils::Logger qw/:level/;
2 my $log = 'OpenSRF::Utils::Logger';
4 #-------------------------------------------------------------------------------
5 package OpenILS::Application::Storage::FTS;
6 use OpenSRF::Utils::Logger qw/:level/;
8 use OpenILS::Utils::Normalize qw( naco_normalize );
10 my $_default_grammar_parser = new Parse::RecDescent ( <<'GRAMMAR' );
14 search_expression: or_expr(s) | and_expr(s) | expr(s)
15 or_expr: lexpr '||' rexpr
16 and_expr: lexpr '&&' rexpr
19 expr: phrase(s) | group(s) | word(s)
21 phrase: '"' token(s) '"'
22 group : '(' search_expression ')'
23 word: numeric_range | negative_token | token
24 negative_token: '-' .../\D+/ token
26 numeric_range: /\d+-\d*/
32 $log->debug("You must override me somewhere, or I will make searching really slow!!!!",ERROR);;
38 $self = ref($self) || $self;
39 $self = bless {} => $self;
41 $self->decompose($term);
43 for my $part ( $self->words, $self->phrases ) {
44 $part = OpenILS::Application::Storage::CDBI->quote($part);
45 push @{ $self->{ fts_query } }, "'\%$part\%'";
48 for my $part ( $self->nots ) {
49 $part = OpenILS::Application::Storage::CDBI->quote($part);
50 push @{ $self->{ fts_query_not } }, "'\%$part\%'";
57 my $parser = shift || $_default_grammar_parser;
60 $term =~ s/\s+--\s+/ /go;
61 $term =~ s/(?:&[^;]+;)//go;
63 $term =~ s/(^|\s+)-(\w+)/$1!$2/go;
64 $term =~ s/\b(\+)(\w+)/$2/go;
65 $term =~ s/^\s*\b(.+)\b\s*$/$1/o;
66 $term =~ s/(\d{4})-(\d{4})/$1 $2/go;
67 #$term =~ s/^(?:an?|the)\b(.*)/$1/o;
69 $log->debug("Stripped search term string is [$term]",DEBUG);
71 my $parsetree = $parser->search_expression( $term );
72 my @words = $term =~ /\b((?<!!)\w+)\b/go;
73 my @nots = $term =~ /\b(?<=!)(\w+)\b/go;
75 $log->debug("Stripped words are[".join(', ',@words)."]",DEBUG);
76 $log->debug("Stripped nots are[".join(', ',@nots)."]",DEBUG);
79 while ($term =~ s/ ((?<!\\)"{1}) (.*?) ((?<!\\)"){1} //x) {
84 push @parts, lc($part);
87 $self->{ fts_op } = 'ILIKE';
88 $self->{ fts_col } = $self->{ text_col } = 'value';
89 $self->{ raw } = $term;
90 $self->{ parsetree } = $parsetree;
91 $self->{ words } = \@words;
92 $self->{ nots } = \@nots;
93 $self->{ phrases } = \@parts;
100 return wantarray ? @{ $self->{fts_query_not} } : $self->{fts_query_not};
105 return wantarray ? @{ $self->{fts_rank} } : $self->{fts_rank};
110 return wantarray ? @{ $self->{fts_query} } : $self->{fts_query};
120 return $self->{parsetree};
125 return $self->{fts_col};
130 return $self->{text_col};
135 return wantarray ? @{ $self->{phrases} } : $self->{phrases};
140 return wantarray ? @{ $self->{words} } : $self->{words};
145 return wantarray ? @{ $self->{nots} } : $self->{nots};
148 sub sql_exact_phrase_match {
150 my $column = $self->text_col;
152 for my $phrase ( $self->phrases ) {
153 $phrase =~ s/%/\\%/go;
154 $phrase =~ s/_/\\_/go;
155 $phrase =~ s/'/\\'/go;
156 $log->debug("Adding phrase [$phrase] to the match list", DEBUG);
157 $output .= " AND $column ILIKE '\%$phrase\%'";
159 $log->debug("Phrase list is [$output]", DEBUG);
163 sub sql_exact_word_bump {
165 my $bump = shift || '0.1';
167 my $column = $self->text_col;
169 for my $word ( $self->words ) {
173 $log->debug("Adding word [$word] to the relevancy bump list", DEBUG);
174 $output .= " + CASE WHEN $column ILIKE '\%$word\%' THEN $bump ELSE 0 END";
176 $log->debug("Word bump list is [$output]", DEBUG);
180 sub sql_where_clause {
184 for my $fts ( $self->fts_query ) {
185 push @output, join(' ', $self->fts_col, $self->{fts_op}, $fts);
188 for my $fts ( $self->fts_query_not ) {
189 push @output, 'NOT (' . join(' ', $self->fts_col, $self->{fts_op}, $fts) . ')';
192 my $phrase_match = $self->sql_exact_phrase_match();
193 return join(' AND ', @output);
196 #-------------------------------------------------------------------------------
197 use UNIVERSAL::require;
199 'Class::DBI::Frozen::301'->use or 'Class::DBI'->use or die $@;
208 my ($proto, $search_type, @args) = @_;
209 my $class = ref $proto || $proto;
212 my $search_opts = (@args > 1 and ref($args[-1]) eq 'HASH') ? pop @args : {};
214 @args = %{ $args[0] } if ref $args[0] eq "HASH";
216 $search_opts->{offset} = int($search_opts->{page} - 1) * int($search_opts->{page_size}) if ($search_opts->{page_size});
217 $search_opts->{_placeholder} ||= '?';
220 while (my ($col, $val) = splice @args, 0, 2) {
221 my $column = $class->find_column($col)
222 || (List::Util::first { $_->accessor eq $col } $class->columns)
223 || $class->_croak("$col is not a column of $class");
225 if (!defined($val)) {
226 push @frags, "$col IS NULL";
227 } elsif (ref($val) and ref($val) eq 'ARRAY') {
228 push @frags, "$col IN (".join(',',map{'?'}@$val).")";
230 push @vals, ''.$class->_deflated_column($column, $v);
233 push @frags, "$col $search_type $$search_opts{_placeholder}";
234 push @vals, $class->_deflated_column($column, $val);
238 my $frag = join " AND ", @frags;
240 $frag .= " ORDER BY $search_opts->{order_by}"
241 if $search_opts->{order_by};
242 $frag .= " LIMIT $search_opts->{limit}"
243 if $search_opts->{limit};
244 $frag .= " OFFSET $search_opts->{offset}"
245 if ($search_opts->{limit} && defined($search_opts->{offset}));
247 return $class->sth_to_objects($class->sql_Retrieve($frag), \@vals);