]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/FTS.pm
"grocery" catchall billing
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / FTS.pm
1 use OpenSRF::Utils::Logger qw/:level/;
2 my $log = 'OpenSRF::Utils::Logger';
3
4 #-------------------------------------------------------------------------------
5 package OpenILS::Application::Storage::FTS;
6 use OpenSRF::Utils::Logger qw/:level/;
7
8 sub compile {
9
10         $log->debug("You must override me somewhere, or I will make searching really slow!!!!",ERROR);;
11
12         my $self = shift;
13         my $term = shift;
14
15         $self = ref($self) || $self;
16         $self = bless {} => $self;
17
18         $self->decompose($term);
19
20         for my $part ( $self->words, $self->phrases ) {
21                 $part = OpenILS::Application::Storage::CDBI->quote($part);
22                 push @{ $self->{ fts_query } },   "'\%$part\%'";
23         }
24
25         for my $part ( $self->nots ) {
26                 $part = OpenILS::Application::Storage::CDBI->quote($part);
27                 push @{ $self->{ fts_query_not } },   "'\%$part\%'";
28         }
29 }
30
31 sub decompose {
32         my $self = shift;
33         my $term = shift;
34
35         $term =~ s/:/ /go;
36         $term =~ s/(?:&[^;]+;)//go;
37         $term =~ s/\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;
42
43         $log->debug("Stripped search term string is [$term]",DEBUG);
44
45         my @words = $term =~ /\b((?<!!)\w+)\b/go;
46         my @nots = $term =~ /\b(?<=!)(\w+)\b/go;
47
48         $log->debug("Stripped words are[".join(', ',@words)."]",DEBUG);
49         $log->debug("Stripped nots are[".join(', ',@nots)."]",DEBUG);
50
51         my @parts;
52         while ($term =~ s/ ((?<!\\)"{1}) (.*?) ((?<!\\)"){1} //x) {
53                 my $part = $2;
54                 $part =~ s/^\s*//o;
55                 $part =~ s/\s*$//o;
56                 next unless $part;
57                 push @parts, lc($part);
58         }
59
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;
66
67         return $self;
68 }
69
70 sub fts_query_not {
71         my $self = shift;
72         return wantarray ? @{ $self->{fts_query_not} } : $self->{fts_query_not};
73 }
74
75 sub fts_rank {
76         my $self = shift;
77         return wantarray ? @{ $self->{fts_rank} } : $self->{fts_rank};
78 }
79
80 sub fts_query {
81         my $self = shift;
82         return wantarray ? @{ $self->{fts_query} } : $self->{fts_query};
83 }
84
85 sub raw {
86         my $self = shift;
87         return $self->{raw};
88 }
89
90 sub fts_col {
91         my $self = shift;
92         return $self->{fts_col};
93 }
94
95 sub text_col {
96         my $self = shift;
97         return $self->{text_col};
98 }
99
100 sub phrases {
101         my $self = shift;
102         return wantarray ? @{ $self->{phrases} } : $self->{phrases};
103 }
104
105 sub words {
106         my $self = shift;
107         return wantarray ? @{ $self->{words} } : $self->{words};
108 }
109
110 sub nots {
111         my $self = shift;
112         return wantarray ? @{ $self->{nots} } : $self->{nots};
113 }
114
115 sub sql_exact_phrase_match {
116         my $self = shift;
117         my $column = $self->text_col;
118         my $output = '';
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\%'";
125         }
126         $log->debug("Phrase list is [$output]", DEBUG);
127         return $output;
128 }
129
130 sub sql_exact_word_bump {
131         my $self = shift;
132         my $bump = shift || '0.1';
133
134         my $column = $self->text_col;
135         my $output = '';
136         for my $word ( $self->words ) {
137                 $word =~ s/%/\\%/go;
138                 $word =~ s/_/\\_/go;
139                 $word =~ s/'/''/go;
140                 $log->debug("Adding word [$word] to the relevancy bump list", DEBUG);
141                 $output .= " + CASE WHEN $column ILIKE '\%$word\%' THEN $bump ELSE 0 END";
142         }
143         $log->debug("Word bump list is [$output]", DEBUG);
144         return $output;
145 }
146
147 sub sql_where_clause {
148         my $self = shift;
149         my @output;
150
151         for my $fts ( $self->fts_query ) {
152                 push @output, join(' ', $self->fts_col, $self->{fts_op}, $fts);
153         }
154
155         for my $fts ( $self->fts_query_not ) {
156                 push @output, 'NOT (' . join(' ', $self->fts_col, $self->{fts_op}, $fts) . ')';
157         }
158
159         my $phrase_match = $self->sql_exact_phrase_match();
160         return join(' AND ', @output); 
161 }
162
163 #-------------------------------------------------------------------------------
164 use Class::DBI;
165
166 package Class::DBI;
167
168 {
169         no warnings;
170         no strict;
171         sub _do_search {
172                 my ($proto, $search_type, @args) = @_;
173                 my $class = ref $proto || $proto;
174                 
175                 my (@cols, @vals);
176                 my $search_opts = (@args > 1 and ref($args[-1]) eq 'HASH') ? pop @args : {};
177
178                 @args = %{ $args[0] } if ref $args[0] eq "HASH";
179
180                 $search_opts->{offset} = int($search_opts->{page} - 1) * int($search_opts->{page_size})  if ($search_opts->{page_size});
181                 $search_opts->{_placeholder} ||= '?';
182
183                 my @frags;
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");
188
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).")";
193                                 for my $v (@$val) {
194                                         push @vals, ''.$class->_deflated_column($column, $v);
195                                 }
196                         } else {
197                                 push @frags, "$col $search_type $$search_opts{_placeholder}";
198                                 push @vals, $class->_deflated_column($column, $val);
199                         }
200                 }
201
202                 my $frag = join " AND ", @frags;
203
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}));
210
211                 return $class->sth_to_objects($class->sql_Retrieve($frag), \@vals);
212         }
213 }
214
215 1;