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/;
9 my $_default_grammar_parser = new Parse::RecDescent ( <<'GRAMMAR' );
13 search_expression: or_expr(s) | and_expr(s) | expr(s)
14 or_expr: lexpr '||' rexpr
15 and_expr: lexpr '&&' rexpr
18 expr: phrase(s) | group(s) | word(s)
20 phrase: '"' token(s) '"'
21 group : '(' search_expression ')'
22 word: numeric_range | negative_token | token
23 negative_token: '-' .../\D+/ token
25 numeric_range: /\d+-\d*/
31 $log->debug("You must override me somewhere, or I will make searching really slow!!!!",ERROR);;
37 $self = ref($self) || $self;
38 $self = bless {} => $self;
40 $self->decompose($term);
42 for my $part ( $self->words, $self->phrases ) {
43 $part = OpenILS::Application::Storage::CDBI->quote($part);
44 push @{ $self->{ fts_query } }, "'\%$part\%'";
47 for my $part ( $self->nots ) {
48 $part = OpenILS::Application::Storage::CDBI->quote($part);
49 push @{ $self->{ fts_query_not } }, "'\%$part\%'";
56 my $parser = shift || $_default_grammar_parser;
59 $term =~ s/\s+--\s+/ /go;
60 $term =~ s/(?:&[^;]+;)//go;
62 $term =~ s/(^|\s+)-(\w+)/$1!$2/go;
63 $term =~ s/\b(\+)(\w+)/$2/go;
64 $term =~ s/^\s*\b(.+)\b\s*$/$1/o;
65 #$term =~ s/^(?:an?|the)\b(.*)/$1/o;
67 $log->debug("Stripped search term string is [$term]",DEBUG);
69 my $parsetree = $parser->search_expression( $term );
70 my @words = $term =~ /\b((?<!!)\w+)\b/go;
71 my @nots = $term =~ /\b(?<=!)(\w+)\b/go;
73 $log->debug("Stripped words are[".join(', ',@words)."]",DEBUG);
74 $log->debug("Stripped nots are[".join(', ',@nots)."]",DEBUG);
77 while ($term =~ s/ ((?<!\\)"{1}) (.*?) ((?<!\\)"){1} //x) {
82 push @parts, lc($part);
85 $self->{ fts_op } = 'ILIKE';
86 $self->{ fts_col } = $self->{ text_col } = 'value';
87 $self->{ raw } = $term;
88 $self->{ parsetree } = $parsetree;
89 $self->{ words } = \@words;
90 $self->{ nots } = \@nots;
91 $self->{ phrases } = \@parts;
98 return wantarray ? @{ $self->{fts_query_not} } : $self->{fts_query_not};
103 return wantarray ? @{ $self->{fts_rank} } : $self->{fts_rank};
108 return wantarray ? @{ $self->{fts_query} } : $self->{fts_query};
118 return $self->{parsetree};
123 return $self->{fts_col};
128 return $self->{text_col};
133 return wantarray ? @{ $self->{phrases} } : $self->{phrases};
138 return wantarray ? @{ $self->{words} } : $self->{words};
143 return wantarray ? @{ $self->{nots} } : $self->{nots};
146 sub sql_exact_phrase_match {
148 my $column = $self->text_col;
150 for my $phrase ( $self->phrases ) {
151 $phrase =~ s/%/\\%/go;
152 $phrase =~ s/_/\\_/go;
153 $phrase =~ s/'/\\'/go;
154 $log->debug("Adding phrase [$phrase] to the match list", DEBUG);
155 $output .= " AND $column ILIKE '\%$phrase\%'";
157 $log->debug("Phrase list is [$output]", DEBUG);
161 sub sql_exact_word_bump {
163 my $bump = shift || '0.1';
165 my $column = $self->text_col;
167 for my $word ( $self->words ) {
171 $log->debug("Adding word [$word] to the relevancy bump list", DEBUG);
172 $output .= " + CASE WHEN $column ILIKE '\%$word\%' THEN $bump ELSE 0 END";
174 $log->debug("Word bump list is [$output]", DEBUG);
178 sub sql_where_clause {
182 for my $fts ( $self->fts_query ) {
183 push @output, join(' ', $self->fts_col, $self->{fts_op}, $fts);
186 for my $fts ( $self->fts_query_not ) {
187 push @output, 'NOT (' . join(' ', $self->fts_col, $self->{fts_op}, $fts) . ')';
190 my $phrase_match = $self->sql_exact_phrase_match();
191 return join(' AND ', @output);
194 #-------------------------------------------------------------------------------
203 my ($proto, $search_type, @args) = @_;
204 my $class = ref $proto || $proto;
207 my $search_opts = (@args > 1 and ref($args[-1]) eq 'HASH') ? pop @args : {};
209 @args = %{ $args[0] } if ref $args[0] eq "HASH";
211 $search_opts->{offset} = int($search_opts->{page} - 1) * int($search_opts->{page_size}) if ($search_opts->{page_size});
212 $search_opts->{_placeholder} ||= '?';
215 while (my ($col, $val) = splice @args, 0, 2) {
216 my $column = $class->find_column($col)
217 || (List::Util::first { $_->accessor eq $col } $class->columns)
218 || $class->_croak("$col is not a column of $class");
220 if (!defined($val)) {
221 push @frags, "$col IS NULL";
222 } elsif (ref($val) and ref($val) eq 'ARRAY') {
223 push @frags, "$col IN (".join(',',map{'?'}@$val).")";
225 push @vals, ''.$class->_deflated_column($column, $v);
228 push @frags, "$col $search_type $$search_opts{_placeholder}";
229 push @vals, $class->_deflated_column($column, $val);
233 my $frag = join " AND ", @frags;
235 $frag .= " ORDER BY $search_opts->{order_by}"
236 if $search_opts->{order_by};
237 $frag .= " LIMIT $search_opts->{limit}"
238 if $search_opts->{limit};
239 $frag .= " OFFSET $search_opts->{offset}"
240 if ($search_opts->{limit} && defined($search_opts->{offset}));
242 return $class->sth_to_objects($class->sql_Retrieve($frag), \@vals);