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/;
10 $log->debug("You must override me somewhere, or I will make searching really slow!!!!",ERROR);;
15 $self = ref($self) || $self;
16 $self = bless {} => $self;
18 $self->decompose($term);
20 for my $part ( $self->words, $self->phrases ) {
21 $part = OpenILS::Application::Storage::CDBI->quote($part);
22 push @{ $self->{ fts_query } }, "'\%$part\%'";
25 for my $part ( $self->nots ) {
26 $part = OpenILS::Application::Storage::CDBI->quote($part);
27 push @{ $self->{ fts_query_not } }, "'\%$part\%'";
36 $term =~ s/(?:&[^;]+;)//go;
38 $term =~ s/(^|\s+)-(\w+)/$1!$2/go;
39 $term =~ s/\b(\+)(\w+)/$2/go;
40 $term =~ s/^\s*\b(.+)\b\s*$/$1/o;
41 $term =~ s/^(?:an?|the)\b(.*)/$1/o;
43 $log->debug("Stripped search term string is [$term]",DEBUG);
45 my @words = $term =~ /\b((?<!!)\w+)\b/go;
46 my @nots = $term =~ /\b(?<=!)(\w+)\b/go;
48 $log->debug("Stripped words are[".join(', ',@words)."]",DEBUG);
49 $log->debug("Stripped nots are[".join(', ',@nots)."]",DEBUG);
52 while ($term =~ s/ ((?<!\\)"{1}) (.*?) ((?<!\\)"){1} //x) {
57 push @parts, lc($part);
60 $self->{ fts_op } = 'ILIKE';
61 $self->{ fts_col } = $self->{ text_col } = 'value';
62 $self->{ raw } = $term;
63 $self->{ words } = \@words;
64 $self->{ nots } = \@nots;
65 $self->{ phrases } = \@parts;
72 return wantarray ? @{ $self->{fts_query_not} } : $self->{fts_query_not};
77 return wantarray ? @{ $self->{fts_rank} } : $self->{fts_rank};
82 return wantarray ? @{ $self->{fts_query} } : $self->{fts_query};
92 return $self->{fts_col};
97 return $self->{text_col};
102 return wantarray ? @{ $self->{phrases} } : $self->{phrases};
107 return wantarray ? @{ $self->{words} } : $self->{words};
112 return wantarray ? @{ $self->{nots} } : $self->{nots};
115 sub sql_exact_phrase_match {
117 my $column = $self->text_col;
119 for my $phrase ( $self->phrases ) {
120 $phrase =~ s/%/\\%/go;
121 $phrase =~ s/_/\\_/go;
122 $phrase =~ s/'/\\'/go;
123 $log->debug("Adding phrase [$phrase] to the match list", DEBUG);
124 $output .= " AND $column ILIKE '\%$phrase\%'";
126 $log->debug("Phrase list is [$output]", DEBUG);
130 sub sql_exact_word_bump {
132 my $bump = shift || '0.1';
134 my $column = $self->text_col;
136 for my $word ( $self->words ) {
140 $log->debug("Adding word [$word] to the relevancy bump list", DEBUG);
141 $output .= " + CASE WHEN $column ILIKE '\%$word\%' THEN $bump ELSE 0 END";
143 $log->debug("Word bump list is [$output]", DEBUG);
147 sub sql_where_clause {
151 for my $fts ( $self->fts_query ) {
152 push @output, join(' ', $self->fts_col, $self->{fts_op}, $fts);
155 for my $fts ( $self->fts_query_not ) {
156 push @output, 'NOT (' . join(' ', $self->fts_col, $self->{fts_op}, $fts) . ')';
159 my $phrase_match = $self->sql_exact_phrase_match();
160 return join(' AND ', @output);
163 #-------------------------------------------------------------------------------
172 my ($proto, $search_type, @args) = @_;
173 my $class = ref $proto || $proto;
176 my $search_opts = (@args > 1 and ref($args[-1]) eq 'HASH') ? pop @args : {};
178 @args = %{ $args[0] } if ref $args[0] eq "HASH";
180 $search_opts->{offset} = int($search_opts->{page} - 1) * int($search_opts->{page_size}) if ($search_opts->{page_size});
181 $search_opts->{_placeholder} ||= '?';
184 while (my ($col, $val) = splice @args, 0, 2) {
185 my $column = $class->find_column($col)
186 || (List::Util::first { $_->accessor eq $col } $class->columns)
187 || $class->_croak("$col is not a column of $class");
189 if (!defined($val)) {
190 push @frags, "$col IS NULL";
191 } elsif (ref($val) and ref($val) eq 'ARRAY') {
192 push @frags, "$col IN (".join(',',map{'?'}@$val).")";
194 push @vals, ''.$class->_deflated_column($column, $v);
197 push @frags, "$col $search_type $$search_opts{_placeholder}";
198 push @vals, $class->_deflated_column($column, $val);
202 my $frag = join " AND ", @frags;
204 $frag .= " ORDER BY $search_opts->{order_by}"
205 if $search_opts->{order_by};
206 $frag .= " LIMIT $search_opts->{limit}"
207 if $search_opts->{limit};
208 $frag .= " OFFSET $search_opts->{offset}"
209 if ($search_opts->{limit} && defined($search_opts->{offset}));
211 return $class->sth_to_objects($class->sql_Retrieve($frag), \@vals);