]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/FTS.pm
d9c4ee452d2fa53c570c040fef955c99f38423f5
[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 Parser::RecDescent;
8
9 my $_default_grammar_parser = new Parse::RecDescent ( join '', (<DATA>) );
10
11 sub compile {
12
13         $log->debug("You must override me somewhere, or I will make searching really slow!!!!",ERROR);;
14
15         my $self = shift;
16         my $class = shift;
17         my $term = shift;
18
19         $self = ref($self) || $self;
20         $self = bless {} => $self;
21
22         $self->decompose($term);
23
24         for my $part ( $self->words, $self->phrases ) {
25                 $part = OpenILS::Application::Storage::CDBI->quote($part);
26                 push @{ $self->{ fts_query } },   "'\%$part\%'";
27         }
28
29         for my $part ( $self->nots ) {
30                 $part = OpenILS::Application::Storage::CDBI->quote($part);
31                 push @{ $self->{ fts_query_not } },   "'\%$part\%'";
32         }
33 }
34
35 sub decompose {
36         my $self = shift;
37         my $term = shift;
38         my $parser = shift || $_default_grammar_parser;
39
40         $term =~ s/:/ /go;
41         $term =~ s/\s+--\s+/ /go;
42         $term =~ s/(?:&[^;]+;)//go;
43         $term =~ s/\s+/ /go;
44         $term =~ s/(^|\s+)-(\w+)/$1!$2/go;
45         $term =~ s/\b(\+)(\w+)/$2/go;
46         $term =~ s/^\s*\b(.+)\b\s*$/$1/o;
47         #$term =~ s/^(?:an?|the)\b(.*)/$1/o;
48
49         $log->debug("Stripped search term string is [$term]",DEBUG);
50
51         my $parsetree = $parser->search_expression( $term );
52         my @words = $term =~ /\b((?<!!)\w+)\b/go;
53         my @nots = $term =~ /\b(?<=!)(\w+)\b/go;
54
55         $log->debug("Stripped words are[".join(', ',@words)."]",DEBUG);
56         $log->debug("Stripped nots are[".join(', ',@nots)."]",DEBUG);
57
58         my @parts;
59         while ($term =~ s/ ((?<!\\)"{1}) (.*?) ((?<!\\)"){1} //x) {
60                 my $part = $2;
61                 $part =~ s/^\s*//o;
62                 $part =~ s/\s*$//o;
63                 next unless $part;
64                 push @parts, lc($part);
65         }
66
67         $self->{ fts_op } = 'ILIKE';
68         $self->{ fts_col } = $self->{ text_col } = 'value';
69         $self->{ raw } = $term;
70         $self->{ parsetree } = $parsetree;
71         $self->{ words } = \@words;
72         $self->{ nots } = \@nots;
73         $self->{ phrases } = \@parts;
74
75         return $self;
76 }
77
78 sub fts_query_not {
79         my $self = shift;
80         return wantarray ? @{ $self->{fts_query_not} } : $self->{fts_query_not};
81 }
82
83 sub fts_rank {
84         my $self = shift;
85         return wantarray ? @{ $self->{fts_rank} } : $self->{fts_rank};
86 }
87
88 sub fts_query {
89         my $self = shift;
90         return wantarray ? @{ $self->{fts_query} } : $self->{fts_query};
91 }
92
93 sub raw {
94         my $self = shift;
95         return $self->{raw};
96 }
97
98 sub fts_col {
99         my $self = shift;
100         return $self->{fts_col};
101 }
102
103 sub text_col {
104         my $self = shift;
105         return $self->{text_col};
106 }
107
108 sub phrases {
109         my $self = shift;
110         return wantarray ? @{ $self->{phrases} } : $self->{phrases};
111 }
112
113 sub words {
114         my $self = shift;
115         return wantarray ? @{ $self->{words} } : $self->{words};
116 }
117
118 sub nots {
119         my $self = shift;
120         return wantarray ? @{ $self->{nots} } : $self->{nots};
121 }
122
123 sub sql_exact_phrase_match {
124         my $self = shift;
125         my $column = $self->text_col;
126         my $output = '';
127         for my $phrase ( $self->phrases ) {
128                 $phrase =~ s/%/\\%/go;
129                 $phrase =~ s/_/\\_/go;
130                 $phrase =~ s/'/\\'/go;
131                 $log->debug("Adding phrase [$phrase] to the match list", DEBUG);
132                 $output .= " AND $column ILIKE '\%$phrase\%'";
133         }
134         $log->debug("Phrase list is [$output]", DEBUG);
135         return $output;
136 }
137
138 sub sql_exact_word_bump {
139         my $self = shift;
140         my $bump = shift || '0.1';
141
142         my $column = $self->text_col;
143         my $output = '';
144         for my $word ( $self->words ) {
145                 $word =~ s/%/\\%/go;
146                 $word =~ s/_/\\_/go;
147                 $word =~ s/'/''/go;
148                 $log->debug("Adding word [$word] to the relevancy bump list", DEBUG);
149                 $output .= " + CASE WHEN $column ILIKE '\%$word\%' THEN $bump ELSE 0 END";
150         }
151         $log->debug("Word bump list is [$output]", DEBUG);
152         return $output;
153 }
154
155 sub sql_where_clause {
156         my $self = shift;
157         my @output;
158
159         for my $fts ( $self->fts_query ) {
160                 push @output, join(' ', $self->fts_col, $self->{fts_op}, $fts);
161         }
162
163         for my $fts ( $self->fts_query_not ) {
164                 push @output, 'NOT (' . join(' ', $self->fts_col, $self->{fts_op}, $fts) . ')';
165         }
166
167         my $phrase_match = $self->sql_exact_phrase_match();
168         return join(' AND ', @output); 
169 }
170
171 #-------------------------------------------------------------------------------
172 use Class::DBI;
173
174 package Class::DBI;
175
176 {
177         no warnings;
178         no strict;
179         sub _do_search {
180                 my ($proto, $search_type, @args) = @_;
181                 my $class = ref $proto || $proto;
182                 
183                 my (@cols, @vals);
184                 my $search_opts = (@args > 1 and ref($args[-1]) eq 'HASH') ? pop @args : {};
185
186                 @args = %{ $args[0] } if ref $args[0] eq "HASH";
187
188                 $search_opts->{offset} = int($search_opts->{page} - 1) * int($search_opts->{page_size})  if ($search_opts->{page_size});
189                 $search_opts->{_placeholder} ||= '?';
190
191                 my @frags;
192                 while (my ($col, $val) = splice @args, 0, 2) {
193                         my $column = $class->find_column($col)
194                                 || (List::Util::first { $_->accessor eq $col } $class->columns)
195                                 || $class->_croak("$col is not a column of $class");
196
197                         if (!defined($val)) {
198                                 push @frags, "$col IS NULL";
199                         } elsif (ref($val) and ref($val) eq 'ARRAY') {
200                                 push @frags, "$col IN (".join(',',map{'?'}@$val).")";
201                                 for my $v (@$val) {
202                                         push @vals, ''.$class->_deflated_column($column, $v);
203                                 }
204                         } else {
205                                 push @frags, "$col $search_type $$search_opts{_placeholder}";
206                                 push @vals, $class->_deflated_column($column, $val);
207                         }
208                 }
209
210                 my $frag = join " AND ", @frags;
211
212                 $frag .= " ORDER BY $search_opts->{order_by}"
213                         if $search_opts->{order_by};
214                 $frag .= " LIMIT $search_opts->{limit}"
215                         if $search_opts->{limit};
216                 $frag .= " OFFSET $search_opts->{offset}"
217                         if ($search_opts->{limit} && defined($search_opts->{offset}));
218
219                 return $class->sth_to_objects($class->sql_Retrieve($frag), \@vals);
220         }
221 }
222
223 1;
224
225 __DATA__
226 <autotree>
227
228 search_expression: or_expr(s) | and_expr(s) | expr(s)
229 or_expr: lexpr '||' rexpr
230 and_expr: lexpr '&&' rexpr
231 lexpr: expr
232 rexpr: expr
233 expr: phrase(s) | group(s) | word(s)
234 joiner: '||' | '&&'
235 phrase: '"' token(s) '"'
236 group : '(' search_expression ')'
237 word: numeric_range | negative_token | token
238 negative_token: '-' .../\D+/ token
239 token: /[-\w]+/
240 numeric_range: /\d+-\d*/
241