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