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/\s+--\s+/ /go;
37 $term =~ s/(?:&[^;]+;)//go;
39 $term =~ s/(^|\s+)-(\w+)/$1!$2/go;
40 $term =~ s/\b(\+)(\w+)/$2/go;
41 $term =~ s/^\s*\b(.+)\b\s*$/$1/o;
42 $term =~ s/^(?:an?|the)\b(.*)/$1/o;
44 $log->debug("Stripped search term string is [$term]",DEBUG);
46 my @words = $term =~ /\b((?<!!)\w+)\b/go;
47 my @nots = $term =~ /\b(?<=!)(\w+)\b/go;
49 $log->debug("Stripped words are[".join(', ',@words)."]",DEBUG);
50 $log->debug("Stripped nots are[".join(', ',@nots)."]",DEBUG);
53 while ($term =~ s/ ((?<!\\)"{1}) (.*?) ((?<!\\)"){1} //x) {
58 push @parts, lc($part);
61 $self->{ fts_op } = 'ILIKE';
62 $self->{ fts_col } = $self->{ text_col } = 'value';
63 $self->{ raw } = $term;
64 $self->{ words } = \@words;
65 $self->{ nots } = \@nots;
66 $self->{ phrases } = \@parts;
73 return wantarray ? @{ $self->{fts_query_not} } : $self->{fts_query_not};
78 return wantarray ? @{ $self->{fts_rank} } : $self->{fts_rank};
83 return wantarray ? @{ $self->{fts_query} } : $self->{fts_query};
93 return $self->{fts_col};
98 return $self->{text_col};
103 return wantarray ? @{ $self->{phrases} } : $self->{phrases};
108 return wantarray ? @{ $self->{words} } : $self->{words};
113 return wantarray ? @{ $self->{nots} } : $self->{nots};
116 sub sql_exact_phrase_match {
118 my $column = $self->text_col;
120 for my $phrase ( $self->phrases ) {
121 $phrase =~ s/%/\\%/go;
122 $phrase =~ s/_/\\_/go;
123 $phrase =~ s/'/\\'/go;
124 $log->debug("Adding phrase [$phrase] to the match list", DEBUG);
125 $output .= " AND $column ILIKE '\%$phrase\%'";
127 $log->debug("Phrase list is [$output]", DEBUG);
131 sub sql_exact_word_bump {
133 my $bump = shift || '0.1';
135 my $column = $self->text_col;
137 for my $word ( $self->words ) {
141 $log->debug("Adding word [$word] to the relevancy bump list", DEBUG);
142 $output .= " + CASE WHEN $column ILIKE '\%$word\%' THEN $bump ELSE 0 END";
144 $log->debug("Word bump list is [$output]", DEBUG);
148 sub sql_where_clause {
152 for my $fts ( $self->fts_query ) {
153 push @output, join(' ', $self->fts_col, $self->{fts_op}, $fts);
156 for my $fts ( $self->fts_query_not ) {
157 push @output, 'NOT (' . join(' ', $self->fts_col, $self->{fts_op}, $fts) . ')';
160 my $phrase_match = $self->sql_exact_phrase_match();
161 return join(' AND ', @output);
164 #-------------------------------------------------------------------------------
173 my ($proto, $search_type, @args) = @_;
174 my $class = ref $proto || $proto;
177 my $search_opts = (@args > 1 and ref($args[-1]) eq 'HASH') ? pop @args : {};
179 @args = %{ $args[0] } if ref $args[0] eq "HASH";
181 $search_opts->{offset} = int($search_opts->{page} - 1) * int($search_opts->{page_size}) if ($search_opts->{page_size});
182 $search_opts->{_placeholder} ||= '?';
185 while (my ($col, $val) = splice @args, 0, 2) {
186 my $column = $class->find_column($col)
187 || (List::Util::first { $_->accessor eq $col } $class->columns)
188 || $class->_croak("$col is not a column of $class");
190 if (!defined($val)) {
191 push @frags, "$col IS NULL";
192 } elsif (ref($val) and ref($val) eq 'ARRAY') {
193 push @frags, "$col IN (".join(',',map{'?'}@$val).")";
195 push @vals, ''.$class->_deflated_column($column, $v);
198 push @frags, "$col $search_type $$search_opts{_placeholder}";
199 push @vals, $class->_deflated_column($column, $val);
203 my $frag = join " AND ", @frags;
205 $frag .= " ORDER BY $search_opts->{order_by}"
206 if $search_opts->{order_by};
207 $frag .= " LIMIT $search_opts->{limit}"
208 if $search_opts->{limit};
209 $frag .= " OFFSET $search_opts->{offset}"
210 if ($search_opts->{limit} && defined($search_opts->{offset}));
212 return $class->sth_to_objects($class->sql_Retrieve($frag), \@vals);