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 ( join '', (<DATA>) );
13 $log->debug("You must override me somewhere, or I will make searching really slow!!!!",ERROR);;
19 $self = ref($self) || $self;
20 $self = bless {} => $self;
22 $self->decompose($term);
24 for my $part ( $self->words, $self->phrases ) {
25 $part = OpenILS::Application::Storage::CDBI->quote($part);
26 push @{ $self->{ fts_query } }, "'\%$part\%'";
29 for my $part ( $self->nots ) {
30 $part = OpenILS::Application::Storage::CDBI->quote($part);
31 push @{ $self->{ fts_query_not } }, "'\%$part\%'";
38 my $parser = shift || $_default_grammar_parser;
41 $term =~ s/\s+--\s+/ /go;
42 $term =~ s/(?:&[^;]+;)//go;
44 $term =~ s/(^|\s+)-(\w+)/$1!$2/go;
45 $term =~ s/\b(\+)(\w+)/$2/go;
46 $term =~ s/^\s*\b(.+)\b\s*$/$1/o;
47 #$term =~ s/^(?:an?|the)\b(.*)/$1/o;
49 $log->debug("Stripped search term string is [$term]",DEBUG);
51 my $parsetree = $parser->search_expression( $term );
52 my @words = $term =~ /\b((?<!!)\w+)\b/go;
53 my @nots = $term =~ /\b(?<=!)(\w+)\b/go;
55 $log->debug("Stripped words are[".join(', ',@words)."]",DEBUG);
56 $log->debug("Stripped nots are[".join(', ',@nots)."]",DEBUG);
59 while ($term =~ s/ ((?<!\\)"{1}) (.*?) ((?<!\\)"){1} //x) {
64 push @parts, lc($part);
67 $self->{ fts_op } = 'ILIKE';
68 $self->{ fts_col } = $self->{ text_col } = 'value';
69 $self->{ raw } = $term;
70 $self->{ parsetree } = $parsetree;
71 $self->{ words } = \@words;
72 $self->{ nots } = \@nots;
73 $self->{ phrases } = \@parts;
80 return wantarray ? @{ $self->{fts_query_not} } : $self->{fts_query_not};
85 return wantarray ? @{ $self->{fts_rank} } : $self->{fts_rank};
90 return wantarray ? @{ $self->{fts_query} } : $self->{fts_query};
100 return $self->{fts_col};
105 return $self->{text_col};
110 return wantarray ? @{ $self->{phrases} } : $self->{phrases};
115 return wantarray ? @{ $self->{words} } : $self->{words};
120 return wantarray ? @{ $self->{nots} } : $self->{nots};
123 sub sql_exact_phrase_match {
125 my $column = $self->text_col;
127 for my $phrase ( $self->phrases ) {
128 $phrase =~ s/%/\\%/go;
129 $phrase =~ s/_/\\_/go;
130 $phrase =~ s/'/\\'/go;
131 $log->debug("Adding phrase [$phrase] to the match list", DEBUG);
132 $output .= " AND $column ILIKE '\%$phrase\%'";
134 $log->debug("Phrase list is [$output]", DEBUG);
138 sub sql_exact_word_bump {
140 my $bump = shift || '0.1';
142 my $column = $self->text_col;
144 for my $word ( $self->words ) {
148 $log->debug("Adding word [$word] to the relevancy bump list", DEBUG);
149 $output .= " + CASE WHEN $column ILIKE '\%$word\%' THEN $bump ELSE 0 END";
151 $log->debug("Word bump list is [$output]", DEBUG);
155 sub sql_where_clause {
159 for my $fts ( $self->fts_query ) {
160 push @output, join(' ', $self->fts_col, $self->{fts_op}, $fts);
163 for my $fts ( $self->fts_query_not ) {
164 push @output, 'NOT (' . join(' ', $self->fts_col, $self->{fts_op}, $fts) . ')';
167 my $phrase_match = $self->sql_exact_phrase_match();
168 return join(' AND ', @output);
171 #-------------------------------------------------------------------------------
180 my ($proto, $search_type, @args) = @_;
181 my $class = ref $proto || $proto;
184 my $search_opts = (@args > 1 and ref($args[-1]) eq 'HASH') ? pop @args : {};
186 @args = %{ $args[0] } if ref $args[0] eq "HASH";
188 $search_opts->{offset} = int($search_opts->{page} - 1) * int($search_opts->{page_size}) if ($search_opts->{page_size});
189 $search_opts->{_placeholder} ||= '?';
192 while (my ($col, $val) = splice @args, 0, 2) {
193 my $column = $class->find_column($col)
194 || (List::Util::first { $_->accessor eq $col } $class->columns)
195 || $class->_croak("$col is not a column of $class");
197 if (!defined($val)) {
198 push @frags, "$col IS NULL";
199 } elsif (ref($val) and ref($val) eq 'ARRAY') {
200 push @frags, "$col IN (".join(',',map{'?'}@$val).")";
202 push @vals, ''.$class->_deflated_column($column, $v);
205 push @frags, "$col $search_type $$search_opts{_placeholder}";
206 push @vals, $class->_deflated_column($column, $val);
210 my $frag = join " AND ", @frags;
212 $frag .= " ORDER BY $search_opts->{order_by}"
213 if $search_opts->{order_by};
214 $frag .= " LIMIT $search_opts->{limit}"
215 if $search_opts->{limit};
216 $frag .= " OFFSET $search_opts->{offset}"
217 if ($search_opts->{limit} && defined($search_opts->{offset}));
219 return $class->sth_to_objects($class->sql_Retrieve($frag), \@vals);
228 search_expression: or_expr(s) | and_expr(s) | expr(s)
229 or_expr: lexpr '||' rexpr
230 and_expr: lexpr '&&' rexpr
233 expr: phrase(s) | group(s) | word(s)
235 phrase: '"' token(s) '"'
236 group : '(' search_expression ')'
237 word: numeric_range | negative_token | token
238 negative_token: '-' .../\D+/ token
240 numeric_range: /\d+-\d*/