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