]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/FTS.pm
treat full birth/death dates specially, as tsearch2 is inconsistent between 8.1 and 8.2
[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 use Parse::RecDescent;
8 use Unicode::Normalize;
9
10 my $_default_grammar_parser = new Parse::RecDescent ( <<'GRAMMAR' );
11
12 <autotree>
13
14 search_expression: or_expr(s) | and_expr(s) | expr(s)
15 or_expr: lexpr '||' rexpr
16 and_expr: lexpr '&&' rexpr
17 lexpr: expr
18 rexpr: expr
19 expr: phrase(s) | group(s) | word(s)
20 joiner: '||' | '&&'
21 phrase: '"' token(s) '"'
22 group : '(' search_expression ')'
23 word: numeric_range | negative_token | token
24 negative_token: '-' .../\D+/ token
25 token: /[-\w]+/
26 numeric_range: /\d+-\d*/
27
28 GRAMMAR
29
30 sub naco_normalize {
31
32     my $txt = lc(shift);
33     my $sf = shift;
34
35     $txt = NFD($txt);
36     $txt =~ s/\pM+//go; # Remove diacritics
37
38     $txt =~ s/\xE6/AE/go;   # Convert ae digraph
39     $txt =~ s/\x{153}/OE/go;# Convert oe digraph
40     $txt =~ s/\xFE/TH/go;   # Convert Icelandic thorn
41
42     $txt =~ tr/\x{2070}\x{2071}\x{2072}\x{2073}\x{2074}\x{2075}\x{2076}\x{2077}\x{2078}\x{2079}\x{207A}\x{207B}/0123456789+-/;# Convert superscript numbers
43     $txt =~ tr/\x{2080}\x{2081}\x{2082}\x{2083}\x{2084}\x{2085}\x{2086}\x{2087}\x{2088}\x{2089}\x{208A}\x{208B}/0123456889+-/;# Convert subscript numbers
44
45     $txt =~ tr/\x{0251}\x{03B1}\x{03B2}\x{0262}\x{03B3}/AABGG/;     # Convert Latin and Greek
46     $txt =~ tr/\x{2113}\xF0\!\"\(\)\-\{\}\<\>\;\:\.\?\xA1\xBF\/\\\@\*\%\=\xB1\+\xAE\xA9\x{2117}\$\xA3\x{FFE1}\xB0\^\_\~\`/LD /; # Convert Misc
47     $txt =~ tr/\'\[\]\|//d;                         # Remove Misc
48
49     if ($sf && $sf =~ /^a/o) {
50         my $commapos = index($txt,',');
51         if ($commapos > -1) {
52             if ($commapos != length($txt) - 1) {
53                 my @list = split /,/, $txt;
54                 my $first = shift @list;
55                 $txt = $first . ',' . join(' ', @list);
56             } else {
57                 $txt =~ s/,/ /go;
58             }
59         }
60     } else {
61         $txt =~ s/,/ /go;
62     }
63
64     $txt =~ s/\s+/ /go; # Compress multiple spaces
65     $txt =~ s/^\s+//o;  # Remove leading space
66     $txt =~ s/\s+$//o;  # Remove trailing space
67
68     return $txt;
69 }
70
71 #' stupid vim syntax highlighting ...
72
73 sub compile {
74
75         $log->debug("You must override me somewhere, or I will make searching really slow!!!!",ERROR);;
76
77         my $self = shift;
78         my $class = shift;
79         my $term = shift;
80
81         $self = ref($self) || $self;
82         $self = bless {} => $self;
83
84         $self->decompose($term);
85
86         for my $part ( $self->words, $self->phrases ) {
87                 $part = OpenILS::Application::Storage::CDBI->quote($part);
88                 push @{ $self->{ fts_query } },   "'\%$part\%'";
89         }
90
91         for my $part ( $self->nots ) {
92                 $part = OpenILS::Application::Storage::CDBI->quote($part);
93                 push @{ $self->{ fts_query_not } },   "'\%$part\%'";
94         }
95 }
96
97 sub decompose {
98         my $self = shift;
99         my $term = shift;
100         my $parser = shift || $_default_grammar_parser;
101
102         $term =~ s/:/ /go;
103         $term =~ s/\s+--\s+/ /go;
104         $term =~ s/(?:&[^;]+;)//go;
105         $term =~ s/\s+/ /go;
106         $term =~ s/(^|\s+)-(\w+)/$1!$2/go;
107         $term =~ s/\b(\+)(\w+)/$2/go;
108         $term =~ s/^\s*\b(.+)\b\s*$/$1/o;
109         $term =~ s/(\d{4})-(\d{4})/$1 $2/go;
110         #$term =~ s/^(?:an?|the)\b(.*)/$1/o;
111
112         $log->debug("Stripped search term string is [$term]",DEBUG);
113
114         my $parsetree = $parser->search_expression( $term );
115         my @words = $term =~ /\b((?<!!)\w+)\b/go;
116         my @nots = $term =~ /\b(?<=!)(\w+)\b/go;
117
118         $log->debug("Stripped words are[".join(', ',@words)."]",DEBUG);
119         $log->debug("Stripped nots are[".join(', ',@nots)."]",DEBUG);
120
121         my @parts;
122         while ($term =~ s/ ((?<!\\)"{1}) (.*?) ((?<!\\)"){1} //x) {
123                 my $part = $2;
124                 $part =~ s/^\s*//o;
125                 $part =~ s/\s*$//o;
126                 next unless $part;
127                 push @parts, lc($part);
128         }
129
130         $self->{ fts_op } = 'ILIKE';
131         $self->{ fts_col } = $self->{ text_col } = 'value';
132         $self->{ raw } = $term;
133         $self->{ parsetree } = $parsetree;
134         $self->{ words } = \@words;
135         $self->{ nots } = \@nots;
136         $self->{ phrases } = \@parts;
137
138         return $self;
139 }
140
141 sub fts_query_not {
142         my $self = shift;
143         return wantarray ? @{ $self->{fts_query_not} } : $self->{fts_query_not};
144 }
145
146 sub fts_rank {
147         my $self = shift;
148         return wantarray ? @{ $self->{fts_rank} } : $self->{fts_rank};
149 }
150
151 sub fts_query {
152         my $self = shift;
153         return wantarray ? @{ $self->{fts_query} } : $self->{fts_query};
154 }
155
156 sub raw {
157         my $self = shift;
158         return $self->{raw};
159 }
160
161 sub parse_tree {
162         my $self = shift;
163         return $self->{parsetree};
164 }
165
166 sub fts_col {
167         my $self = shift;
168         return $self->{fts_col};
169 }
170
171 sub text_col {
172         my $self = shift;
173         return $self->{text_col};
174 }
175
176 sub phrases {
177         my $self = shift;
178         return wantarray ? @{ $self->{phrases} } : $self->{phrases};
179 }
180
181 sub words {
182         my $self = shift;
183         return wantarray ? @{ $self->{words} } : $self->{words};
184 }
185
186 sub nots {
187         my $self = shift;
188         return wantarray ? @{ $self->{nots} } : $self->{nots};
189 }
190
191 sub sql_exact_phrase_match {
192         my $self = shift;
193         my $column = $self->text_col;
194         my $output = '';
195         for my $phrase ( $self->phrases ) {
196                 $phrase =~ s/%/\\%/go;
197                 $phrase =~ s/_/\\_/go;
198                 $phrase =~ s/'/\\'/go;
199                 $log->debug("Adding phrase [$phrase] to the match list", DEBUG);
200                 $output .= " AND $column ILIKE '\%$phrase\%'";
201         }
202         $log->debug("Phrase list is [$output]", DEBUG);
203         return $output;
204 }
205
206 sub sql_exact_word_bump {
207         my $self = shift;
208         my $bump = shift || '0.1';
209
210         my $column = $self->text_col;
211         my $output = '';
212         for my $word ( $self->words ) {
213                 $word =~ s/%/\\%/go;
214                 $word =~ s/_/\\_/go;
215                 $word =~ s/'/''/go;
216                 $log->debug("Adding word [$word] to the relevancy bump list", DEBUG);
217                 $output .= " + CASE WHEN $column ILIKE '\%$word\%' THEN $bump ELSE 0 END";
218         }
219         $log->debug("Word bump list is [$output]", DEBUG);
220         return $output;
221 }
222
223 sub sql_where_clause {
224         my $self = shift;
225         my @output;
226
227         for my $fts ( $self->fts_query ) {
228                 push @output, join(' ', $self->fts_col, $self->{fts_op}, $fts);
229         }
230
231         for my $fts ( $self->fts_query_not ) {
232                 push @output, 'NOT (' . join(' ', $self->fts_col, $self->{fts_op}, $fts) . ')';
233         }
234
235         my $phrase_match = $self->sql_exact_phrase_match();
236         return join(' AND ', @output); 
237 }
238
239 #-------------------------------------------------------------------------------
240 use Class::DBI;
241
242 package Class::DBI;
243
244 {
245         no warnings;
246         no strict;
247         sub _do_search {
248                 my ($proto, $search_type, @args) = @_;
249                 my $class = ref $proto || $proto;
250                 
251                 my (@cols, @vals);
252                 my $search_opts = (@args > 1 and ref($args[-1]) eq 'HASH') ? pop @args : {};
253
254                 @args = %{ $args[0] } if ref $args[0] eq "HASH";
255
256                 $search_opts->{offset} = int($search_opts->{page} - 1) * int($search_opts->{page_size})  if ($search_opts->{page_size});
257                 $search_opts->{_placeholder} ||= '?';
258
259                 my @frags;
260                 while (my ($col, $val) = splice @args, 0, 2) {
261                         my $column = $class->find_column($col)
262                                 || (List::Util::first { $_->accessor eq $col } $class->columns)
263                                 || $class->_croak("$col is not a column of $class");
264
265                         if (!defined($val)) {
266                                 push @frags, "$col IS NULL";
267                         } elsif (ref($val) and ref($val) eq 'ARRAY') {
268                                 push @frags, "$col IN (".join(',',map{'?'}@$val).")";
269                                 for my $v (@$val) {
270                                         push @vals, ''.$class->_deflated_column($column, $v);
271                                 }
272                         } else {
273                                 push @frags, "$col $search_type $$search_opts{_placeholder}";
274                                 push @vals, $class->_deflated_column($column, $val);
275                         }
276                 }
277
278                 my $frag = join " AND ", @frags;
279
280                 $frag .= " ORDER BY $search_opts->{order_by}"
281                         if $search_opts->{order_by};
282                 $frag .= " LIMIT $search_opts->{limit}"
283                         if $search_opts->{limit};
284                 $frag .= " OFFSET $search_opts->{offset}"
285                         if ($search_opts->{limit} && defined($search_opts->{offset}));
286
287                 return $class->sth_to_objects($class->sql_Retrieve($frag), \@vals);
288         }
289 }
290
291 1;
292