]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/FTS.pm
adjusting backdate so it does not clear fines for "today" (the backdate day); fine...
[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/\s+--\s+/ /go;
37         $term =~ s/(?:&[^;]+;)//go;
38         $term =~ s/\s+/ /go;
39         $term =~ s/(^|\s+)-(\w+)/$1!$2/go;
40         $term =~ s/\b(\+)(\w+)/$2/go;
41         $term =~ s/^\s*\b(.+)\b\s*$/$1/o;
42         $term =~ s/^(?:an?|the)\b(.*)/$1/o;
43
44         $log->debug("Stripped search term string is [$term]",DEBUG);
45
46         my @words = $term =~ /\b((?<!!)\w+)\b/go;
47         my @nots = $term =~ /\b(?<=!)(\w+)\b/go;
48
49         $log->debug("Stripped words are[".join(', ',@words)."]",DEBUG);
50         $log->debug("Stripped nots are[".join(', ',@nots)."]",DEBUG);
51
52         my @parts;
53         while ($term =~ s/ ((?<!\\)"{1}) (.*?) ((?<!\\)"){1} //x) {
54                 my $part = $2;
55                 $part =~ s/^\s*//o;
56                 $part =~ s/\s*$//o;
57                 next unless $part;
58                 push @parts, lc($part);
59         }
60
61         $self->{ fts_op } = 'ILIKE';
62         $self->{ fts_col } = $self->{ text_col } = 'value';
63         $self->{ raw } = $term;
64         $self->{ words } = \@words;
65         $self->{ nots } = \@nots;
66         $self->{ phrases } = \@parts;
67
68         return $self;
69 }
70
71 sub fts_query_not {
72         my $self = shift;
73         return wantarray ? @{ $self->{fts_query_not} } : $self->{fts_query_not};
74 }
75
76 sub fts_rank {
77         my $self = shift;
78         return wantarray ? @{ $self->{fts_rank} } : $self->{fts_rank};
79 }
80
81 sub fts_query {
82         my $self = shift;
83         return wantarray ? @{ $self->{fts_query} } : $self->{fts_query};
84 }
85
86 sub raw {
87         my $self = shift;
88         return $self->{raw};
89 }
90
91 sub fts_col {
92         my $self = shift;
93         return $self->{fts_col};
94 }
95
96 sub text_col {
97         my $self = shift;
98         return $self->{text_col};
99 }
100
101 sub phrases {
102         my $self = shift;
103         return wantarray ? @{ $self->{phrases} } : $self->{phrases};
104 }
105
106 sub words {
107         my $self = shift;
108         return wantarray ? @{ $self->{words} } : $self->{words};
109 }
110
111 sub nots {
112         my $self = shift;
113         return wantarray ? @{ $self->{nots} } : $self->{nots};
114 }
115
116 sub sql_exact_phrase_match {
117         my $self = shift;
118         my $column = $self->text_col;
119         my $output = '';
120         for my $phrase ( $self->phrases ) {
121                 $phrase =~ s/%/\\%/go;
122                 $phrase =~ s/_/\\_/go;
123                 $phrase =~ s/'/\\'/go;
124                 $log->debug("Adding phrase [$phrase] to the match list", DEBUG);
125                 $output .= " AND $column ILIKE '\%$phrase\%'";
126         }
127         $log->debug("Phrase list is [$output]", DEBUG);
128         return $output;
129 }
130
131 sub sql_exact_word_bump {
132         my $self = shift;
133         my $bump = shift || '0.1';
134
135         my $column = $self->text_col;
136         my $output = '';
137         for my $word ( $self->words ) {
138                 $word =~ s/%/\\%/go;
139                 $word =~ s/_/\\_/go;
140                 $word =~ s/'/''/go;
141                 $log->debug("Adding word [$word] to the relevancy bump list", DEBUG);
142                 $output .= " + CASE WHEN $column ILIKE '\%$word\%' THEN $bump ELSE 0 END";
143         }
144         $log->debug("Word bump list is [$output]", DEBUG);
145         return $output;
146 }
147
148 sub sql_where_clause {
149         my $self = shift;
150         my @output;
151
152         for my $fts ( $self->fts_query ) {
153                 push @output, join(' ', $self->fts_col, $self->{fts_op}, $fts);
154         }
155
156         for my $fts ( $self->fts_query_not ) {
157                 push @output, 'NOT (' . join(' ', $self->fts_col, $self->{fts_op}, $fts) . ')';
158         }
159
160         my $phrase_match = $self->sql_exact_phrase_match();
161         return join(' AND ', @output); 
162 }
163
164 #-------------------------------------------------------------------------------
165 use Class::DBI;
166
167 package Class::DBI;
168
169 {
170         no warnings;
171         no strict;
172         sub _do_search {
173                 my ($proto, $search_type, @args) = @_;
174                 my $class = ref $proto || $proto;
175                 
176                 my (@cols, @vals);
177                 my $search_opts = (@args > 1 and ref($args[-1]) eq 'HASH') ? pop @args : {};
178
179                 @args = %{ $args[0] } if ref $args[0] eq "HASH";
180
181                 $search_opts->{offset} = int($search_opts->{page} - 1) * int($search_opts->{page_size})  if ($search_opts->{page_size});
182                 $search_opts->{_placeholder} ||= '?';
183
184                 my @frags;
185                 while (my ($col, $val) = splice @args, 0, 2) {
186                         my $column = $class->find_column($col)
187                                 || (List::Util::first { $_->accessor eq $col } $class->columns)
188                                 || $class->_croak("$col is not a column of $class");
189
190                         if (!defined($val)) {
191                                 push @frags, "$col IS NULL";
192                         } elsif (ref($val) and ref($val) eq 'ARRAY') {
193                                 push @frags, "$col IN (".join(',',map{'?'}@$val).")";
194                                 for my $v (@$val) {
195                                         push @vals, ''.$class->_deflated_column($column, $v);
196                                 }
197                         } else {
198                                 push @frags, "$col $search_type $$search_opts{_placeholder}";
199                                 push @vals, $class->_deflated_column($column, $val);
200                         }
201                 }
202
203                 my $frag = join " AND ", @frags;
204
205                 $frag .= " ORDER BY $search_opts->{order_by}"
206                         if $search_opts->{order_by};
207                 $frag .= " LIMIT $search_opts->{limit}"
208                         if $search_opts->{limit};
209                 $frag .= " OFFSET $search_opts->{offset}"
210                         if ($search_opts->{limit} && defined($search_opts->{offset}));
211
212                 return $class->sth_to_objects($class->sql_Retrieve($frag), \@vals);
213         }
214 }
215
216 1;