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