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