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 die "You must override me somewhere!";
17 $term =~ s/(?:&[^;]+;)//go;
19 $term =~ s/(^|\s+)-(\w+)/$1!$2/go;
20 $term =~ s/\b(\+)(\w+)/$2/go;
21 $term =~ s/^\s*\b(.+)\b\s*$/$1/o;
22 $term =~ s/^(?:an?|the)\b(.*)/$1/o;
24 OpenILS::Utils::Logger->debug("Stripped search term string is [$term]",DEBUG);
26 my @words = $term =~ /\b((?<!!)\w+)\b/go;
27 my @nots = $term =~ /\b(?<=!)(\w+)\b/go;
30 while ($term =~ s/ ("+) (.*?) ((?<!\\)"){1} //x) {
35 push @parts, lc($part);
38 $self->{ fts_op } = 'ILIKE';
40 for my $part ( @words, @parts ) {
41 $part = OpenILS::Application::Storage::CDBI->quote($part);
42 push @{ $self->{ fts_query } }, "'\%$part\%'";
45 for my $part ( @nots ) {
46 $part = OpenILS::Application::Storage::CDBI->quote($part);
47 push @{ $self->{ fts_query_not } }, "'\%$part\%'";
50 $self->{ raw } = $term;
51 $self->{ words } = \@words;
52 $self->{ nots } = \@nots;
53 $self->{ phrases } = \@parts;
60 return wantarray ? @{ $self->{fts_query_not} } : $self->{fts_query_not};
65 return wantarray ? @{ $self->{fts_query} } : $self->{fts_query};
75 return wantarray ? @{ $self->{phrases} } : $self->{phrases};
80 return wantarray ? @{ $self->{words} } : $self->{words};
85 return wantarray ? @{ $self->{nots} } : $self->{nots};
88 sub sql_exact_phrase_match {
92 for my $phrase ( $self->phrases ) {
93 $phrase =~ s/%/\\%/go;
94 $phrase =~ s/_/\\_/go;
95 $phrase =~ s/'/\\_/go;
96 $log->debug("Adding phrase [$phrase] to the match list", DEBUG);
97 $output .= " AND $column ILIKE '\%$phrase\%'";
99 $log->debug("Phrase list is [$output]", DEBUG);
103 sub sql_exact_word_bump {
106 my $bump = shift || '0.1';
108 for my $word ( $self->words ) {
112 $log->debug("Adding word [$word] to the relevancy bump list", DEBUG);
113 $output .= " + CASE WHEN $column ILIKE '\%$word\%' THEN $bump ELSE 0 END";
115 $log->debug("Word bump list is [$output]", DEBUG);
119 sub sql_where_clause {
124 for my $fts ( $self->fts_query ) {
125 push @output, join(' ', $column, $self->{fts_op}, $fts);
128 for my $fts ( $self->fts_query_nots ) {
129 push @output, 'NOT (' . join(' ', $column, $self->{fts_op}, $fts) . ')';
132 return join(' AND ', @output);
135 #-------------------------------------------------------------------------------
144 my ($proto, $search_type, @args) = @_;
145 my $class = ref $proto || $proto;
147 @args = %{ $args[0] } if ref $args[0] eq "HASH";
150 my $search_opts = @args % 2 ? pop @args : {};
152 $search_opts->{offset} = int($search_opts->{page}) * int($search_opts->{page_size}) if ($search_opts->{page_size});
153 $search_opts->{_placeholder} ||= '?';
155 while (my ($col, $val) = splice @args, 0, 2) {
156 my $column = $class->find_column($col)
157 || (List::Util::first { $_->accessor eq $col } $class->columns)
158 || $class->_croak("$col is not a column of $class");
161 push @vals, $class->_deflated_column($column, $val);
164 my $frag = join " AND ",
165 map defined($vals[$_]) ? "$cols[$_] $search_type $$search_opts{_placeholder}" : "$cols[$_] IS NULL",
168 $frag .= " ORDER BY $search_opts->{order_by}"
169 if $search_opts->{order_by};
170 $frag .= " LIMIT $search_opts->{limit}"
171 if $search_opts->{limit};
172 $frag .= " OFFSET $search_opts->{offset}"
173 if ($search_opts->{limit} && defined($search_opts->{offset}));
175 return $class->sth_to_objects($class->sql_Retrieve($frag),
176 [ grep defined, @vals ]);