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);;
16 $self = ref($self) || $self;
17 $self = bless {} => $self;
19 $self->decompose($term);
21 for my $part ( $self->words, $self->phrases ) {
22 $part = OpenILS::Application::Storage::CDBI->quote($part);
23 push @{ $self->{ fts_query } }, "'\%$part\%'";
26 for my $part ( $self->nots ) {
27 $part = OpenILS::Application::Storage::CDBI->quote($part);
28 push @{ $self->{ fts_query_not } }, "'\%$part\%'";
37 $term =~ s/\s+--\s+/ /go;
38 $term =~ s/(?:&[^;]+;)//go;
40 $term =~ s/(^|\s+)-(\w+)/$1!$2/go;
41 $term =~ s/\b(\+)(\w+)/$2/go;
42 $term =~ s/^\s*\b(.+)\b\s*$/$1/o;
43 #$term =~ s/^(?:an?|the)\b(.*)/$1/o;
45 $log->debug("Stripped search term string is [$term]",DEBUG);
47 my @words = $term =~ /\b((?<!!)\w+)\b/go;
48 my @nots = $term =~ /\b(?<=!)(\w+)\b/go;
50 $log->debug("Stripped words are[".join(', ',@words)."]",DEBUG);
51 $log->debug("Stripped nots are[".join(', ',@nots)."]",DEBUG);
54 while ($term =~ s/ ((?<!\\)"{1}) (.*?) ((?<!\\)"){1} //x) {
59 push @parts, lc($part);
62 $self->{ fts_op } = 'ILIKE';
63 $self->{ fts_col } = $self->{ text_col } = 'value';
64 $self->{ raw } = $term;
65 $self->{ words } = \@words;
66 $self->{ nots } = \@nots;
67 $self->{ phrases } = \@parts;
74 return wantarray ? @{ $self->{fts_query_not} } : $self->{fts_query_not};
79 return wantarray ? @{ $self->{fts_rank} } : $self->{fts_rank};
84 return wantarray ? @{ $self->{fts_query} } : $self->{fts_query};
94 return $self->{fts_col};
99 return $self->{text_col};
104 return wantarray ? @{ $self->{phrases} } : $self->{phrases};
109 return wantarray ? @{ $self->{words} } : $self->{words};
114 return wantarray ? @{ $self->{nots} } : $self->{nots};
117 sub sql_exact_phrase_match {
119 my $column = $self->text_col;
121 for my $phrase ( $self->phrases ) {
122 $phrase =~ s/%/\\%/go;
123 $phrase =~ s/_/\\_/go;
124 $phrase =~ s/'/\\'/go;
125 $log->debug("Adding phrase [$phrase] to the match list", DEBUG);
126 $output .= " AND $column ILIKE '\%$phrase\%'";
128 $log->debug("Phrase list is [$output]", DEBUG);
132 sub sql_exact_word_bump {
134 my $bump = shift || '0.1';
136 my $column = $self->text_col;
138 for my $word ( $self->words ) {
142 $log->debug("Adding word [$word] to the relevancy bump list", DEBUG);
143 $output .= " + CASE WHEN $column ILIKE '\%$word\%' THEN $bump ELSE 0 END";
145 $log->debug("Word bump list is [$output]", DEBUG);
149 sub sql_where_clause {
153 for my $fts ( $self->fts_query ) {
154 push @output, join(' ', $self->fts_col, $self->{fts_op}, $fts);
157 for my $fts ( $self->fts_query_not ) {
158 push @output, 'NOT (' . join(' ', $self->fts_col, $self->{fts_op}, $fts) . ')';
161 my $phrase_match = $self->sql_exact_phrase_match();
162 return join(' AND ', @output);
165 #-------------------------------------------------------------------------------
174 my ($proto, $search_type, @args) = @_;
175 my $class = ref $proto || $proto;
178 my $search_opts = (@args > 1 and ref($args[-1]) eq 'HASH') ? pop @args : {};
180 @args = %{ $args[0] } if ref $args[0] eq "HASH";
182 $search_opts->{offset} = int($search_opts->{page} - 1) * int($search_opts->{page_size}) if ($search_opts->{page_size});
183 $search_opts->{_placeholder} ||= '?';
186 while (my ($col, $val) = splice @args, 0, 2) {
187 my $column = $class->find_column($col)
188 || (List::Util::first { $_->accessor eq $col } $class->columns)
189 || $class->_croak("$col is not a column of $class");
191 if (!defined($val)) {
192 push @frags, "$col IS NULL";
193 } elsif (ref($val) and ref($val) eq 'ARRAY') {
194 push @frags, "$col IN (".join(',',map{'?'}@$val).")";
196 push @vals, ''.$class->_deflated_column($column, $v);
199 push @frags, "$col $search_type $$search_opts{_placeholder}";
200 push @vals, $class->_deflated_column($column, $val);
204 my $frag = join " AND ", @frags;
206 $frag .= " ORDER BY $search_opts->{order_by}"
207 if $search_opts->{order_by};
208 $frag .= " LIMIT $search_opts->{limit}"
209 if $search_opts->{limit};
210 $frag .= " OFFSET $search_opts->{offset}"
211 if ($search_opts->{limit} && defined($search_opts->{offset}));
213 return $class->sth_to_objects($class->sql_Retrieve($frag), \@vals);