]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/metabib.pm
adding rank bump for search string word order
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher / metabib.pm
1 package OpenILS::Application::Storage::Publisher::metabib;
2 use base qw/OpenILS::Application::Storage::Publisher/;
3 use vars qw/$VERSION/;
4 use OpenSRF::EX qw/:try/;
5 use OpenILS::Application::Storage::FTS;
6 use OpenILS::Utils::Fieldmapper;
7 use OpenSRF::Utils::Logger qw/:level/;
8 use OpenSRF::Utils::Cache;
9 use Data::Dumper;
10 use Digest::MD5 qw/md5_hex/;
11
12 my $log = 'OpenSRF::Utils::Logger';
13
14 $VERSION = 1;
15
16 sub metarecord_copy_count {
17         my $self = shift;
18         my $client = shift;
19
20         my %args = @_;
21
22         my $sm_table = metabib::metarecord_source_map->table;
23         my $cn_table = asset::call_number->table;
24         my $cp_table = asset::copy->table;
25         my $out_table = actor::org_unit_type->table;
26         my $descendants = "actor.org_unit_descendants(u.id)";
27         my $ancestors = "actor.org_unit_ancestors(?)";
28
29         my $copies_visible = 'AND cp.opac_visible IS TRUE';
30         $copies_visible = '' if ($self->api_name =~ /staff/o);
31
32         my $sql = <<"   SQL";
33                 SELECT  t.depth,
34                         u.id AS org_unit,
35                         sum(
36                                 (SELECT count(cp.id)
37                                   FROM  $sm_table r
38                                         JOIN $cn_table cn ON (cn.record = r.source)
39                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
40                                         JOIN $descendants a ON (cp.circ_lib = a.id)
41                                   WHERE r.metarecord = ?
42                                         $copies_visible
43                                 )
44                         ) AS count,
45                         sum(
46                                 (SELECT count(cp.id)
47                                   FROM  $sm_table r
48                                         JOIN $cn_table cn ON (cn.record = r.source)
49                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
50                                         JOIN $descendants a ON (cp.circ_lib = a.id)
51                                   WHERE r.metarecord = ?
52                                         AND cp.status = 0
53                                         $copies_visible
54                                 )
55                         ) AS available
56
57                   FROM  $ancestors u
58                         JOIN $out_table t ON (u.ou_type = t.id)
59                   GROUP BY 1,2
60         SQL
61
62         my $sth = metabib::metarecord_source_map->db_Main->prepare_cached($sql);
63         $sth->execute(''.$args{metarecord}, ''.$args{metarecord}, ''.$args{org_unit});
64         while ( my $row = $sth->fetchrow_hashref ) {
65                 $client->respond( $row );
66         }
67         return undef;
68 }
69 __PACKAGE__->register_method(
70         api_name        => 'open-ils.storage.metabib.metarecord.copy_count',
71         method          => 'metarecord_copy_count',
72         api_level       => 1,
73         stream          => 1,
74         cachable        => 1,
75 );
76 __PACKAGE__->register_method(
77         api_name        => 'open-ils.storage.metabib.metarecord.copy_count.staff',
78         method          => 'metarecord_copy_count',
79         api_level       => 1,
80         stream          => 1,
81         cachable        => 1,
82 );
83
84 sub search_full_rec {
85         my $self = shift;
86         my $client = shift;
87
88         my %args = @_;
89         
90         my $term = $args{term};
91         my $limiters = $args{restrict};
92
93         my ($index_col) = metabib::full_rec->columns('FTS');
94         $index_col ||= 'value';
95         my $search_table = metabib::full_rec->table;
96
97         my $fts = OpenILS::Application::Storage::FTS->compile($term, 'value',"$index_col");
98
99         my $fts_where = $fts->sql_where_clause();
100         my @fts_ranks = $fts->fts_rank;
101
102         my $rank = join(' + ', @fts_ranks);
103
104         my @binds;
105         my @wheres;
106         for my $limit (@$limiters) {
107                 push @wheres, "( tag = ? AND subfield LIKE ? AND $fts_where )";
108                 push @binds, $$limit{tag}, $$limit{subfield};
109                 $log->debug("Limiting query using { tag => $$limit{tag}, subfield => $$limit{subfield} }", DEBUG);
110         }
111         my $where = join(' OR ', @wheres);
112
113         my $select = "SELECT record, sum($rank) FROM $search_table WHERE $where GROUP BY 1 ORDER BY 2 DESC;";
114
115         $log->debug("Search SQL :: [$select]",DEBUG);
116
117         my $recs = metabib::full_rec->db_Main->selectall_arrayref($select, {}, @binds);
118         $log->debug("Search yielded ".scalar(@$recs)." results.",DEBUG);
119
120         $client->respond($_) for (@$recs);
121         return undef;
122 }
123 __PACKAGE__->register_method(
124         api_name        => 'open-ils.storage.direct.metabib.full_rec.search_fts.value',
125         method          => 'search_full_rec',
126         api_level       => 1,
127         stream          => 1,
128         cachable        => 1,
129 );
130 __PACKAGE__->register_method(
131         api_name        => 'open-ils.storage.direct.metabib.full_rec.search_fts.index_vector',
132         method          => 'search_full_rec',
133         api_level       => 1,
134         stream          => 1,
135         cachable        => 1,
136 );
137
138
139 # XXX factored most of the PG dependant stuff out of here... need to find a way to do "dependants".
140 sub search_class_fts {
141         my $self = shift;
142         my $client = shift;
143         my %args = @_;
144         
145         my $term = $args{term};
146         my $ou = $args{org_unit};
147         my $ou_type = $args{depth};
148         my $limit = $args{limit};
149         my $offset = $args{offset};
150
151         my $limit_clause = '';
152         my $offset_clause = '';
153
154         $limit_clause = "LIMIT $limit" if (defined $limit and int($limit) > 0);
155         $offset_clause = "OFFSET $offset" if (defined $offset and int($offset) > 0);
156
157
158         my $descendants = defined($ou_type) ?
159                                 "actor.org_unit_descendants($ou, $ou_type)" :
160                                 "actor.org_unit_descendants($ou)";
161
162         my $class = $self->{cdbi};
163         my $search_table = $class->table;
164
165         my $metabib_metarecord = metabib::metarecord->table;
166         my $metabib_full_rec = metabib::full_rec->table;
167         my $metabib_metarecord_source_map_table = metabib::metarecord_source_map->table;
168         my $asset_call_number_table = asset::call_number->table;
169         my $asset_copy_table = asset::copy->table;
170
171         my ($index_col) = $class->columns('FTS');
172         $index_col ||= 'value';
173
174         my $fts = OpenILS::Application::Storage::FTS->compile($term, 'f.value', "f.$index_col");
175
176         my $fts_where = $fts->sql_where_clause;
177         my @fts_ranks = $fts->fts_rank;
178
179         my $rank = join(' + ', @fts_ranks);
180
181         my $has_vols = 'AND cn.owning_lib = d.id';
182         my $has_copies = 'AND cp.call_number = cn.id';
183         my $copies_visible = 'AND cp.opac_visible IS TRUE';
184
185         my $visible_count = ', count(DISTINCT cp.id)';
186         my $visible_count_test = 'HAVING count(DISTINCT cp.id) > 0';
187
188         if ($self->api_name =~ /staff/o) {
189                 $copies_visible = '';
190                 $visible_count_test = '';
191                 $has_copies = '' if ($ou_type == 0);
192                 $has_vols = '' if ($ou_type == 0);
193         }
194
195         my $rank_calc = ", sum($rank + CASE WHEN f.value ILIKE ? THEN 1 ELSE 0 END)/count(m.source)";
196         my $rank_order = "ORDER BY 2 DESC";
197         $rank_calc = ',sum(1 + CASE WHEN f.value ILIKE ? THEN 1 ELSE 0 END)' if ($self->api_name =~ /unordered/o);
198         $rank_order = '' if ($self->api_name =~ /unordered/o);
199
200         my $select = <<"        SQL";
201                 SELECT  m.metarecord $rank_calc $visible_count
202                   FROM  $search_table f,
203                         $metabib_metarecord_source_map_table m,
204                         $asset_call_number_table cn,
205                         $asset_copy_table cp,
206                         $descendants d
207                   WHERE $fts_where
208                         AND m.source = f.source
209                         AND cn.record = m.source
210                         $has_vols
211                         $has_copies
212                         $copies_visible
213                   GROUP BY m.metarecord $visible_count_test
214                   $rank_order
215                   $limit_clause $offset_clause
216         SQL
217
218         $log->debug("Field Search SQL :: [$select]",DEBUG);
219
220         my $string = '%'.join('%',$fts->words).'%';
221         my $recs = $class->db_Main->selectall_arrayref($select, {}, lc($string));
222         
223         $log->debug("Search yielded ".scalar(@$recs)." results.",DEBUG);
224
225         $client->respond($_) for (@$recs);
226         return undef;
227 }
228
229 for my $class ( qw/title author subject keyword series/ ) {
230         __PACKAGE__->register_method(
231                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord",
232                 method          => 'search_class_fts',
233                 api_level       => 1,
234                 stream          => 1,
235                 cdbi            => "metabib::${class}_field_entry",
236                 cachable        => 1,
237         );
238         __PACKAGE__->register_method(
239                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord.unordered",
240                 method          => 'search_class_fts',
241                 api_level       => 1,
242                 stream          => 1,
243                 cdbi            => "metabib::${class}_field_entry",
244                 cachable        => 1,
245         );
246         __PACKAGE__->register_method(
247                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord.staff",
248                 method          => 'search_class_fts',
249                 api_level       => 1,
250                 stream          => 1,
251                 cdbi            => "metabib::${class}_field_entry",
252                 cachable        => 1,
253         );
254         __PACKAGE__->register_method(
255                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord.staff.unordered",
256                 method          => 'search_class_fts',
257                 api_level       => 1,
258                 stream          => 1,
259                 cdbi            => "metabib::${class}_field_entry",
260                 cachable        => 1,
261         );
262 }
263
264 # XXX factored most of the PG dependant stuff out of here... need to find a way to do "dependants".
265 sub search_class_fts_count {
266         my $self = shift;
267         my $client = shift;
268         my %args = @_;
269         
270         my $term = $args{term};
271         my $ou = $args{org_unit};
272         my $ou_type = $args{depth};
273         my $limit = $args{limit} || 100;
274         my $offset = $args{offset} || 0;
275
276         my $descendants = defined($ou_type) ?
277                                 "actor.org_unit_descendants($ou, $ou_type)" :
278                                 "actor.org_unit_descendants($ou)";
279                 
280
281         (my $search_class = $self->api_name) =~ s/.*metabib.(\w+).search_fts.*/$1/o;
282
283         my $class = $self->{cdbi};
284         my $search_table = $class->table;
285
286         my $metabib_metarecord_source_map_table = metabib::metarecord_source_map->table;
287         my $asset_call_number_table = asset::call_number->table;
288         my $asset_copy_table = asset::copy->table;
289
290         my ($index_col) = $class->columns('FTS');
291         $index_col ||= 'value';
292
293         my $fts = OpenILS::Application::Storage::FTS->compile($term, 'value',"$index_col");
294
295         my $fts_where = $fts->sql_where_clause;
296
297         my $has_vols = 'AND cn.owning_lib = d.id';
298         my $has_copies = 'AND cp.call_number = cn.id';
299         my $copies_visible = 'AND cp.opac_visible IS TRUE';
300         if ($self->api_name =~ /staff/o) {
301                 $copies_visible = '';
302                 $has_vols = '' if ($ou_type == 0);
303                 $has_copies = '' if ($ou_type == 0);
304         }
305
306         # XXX test an "EXISTS version of descendant checking...
307         my $select = <<"        SQL";
308                 SELECT  count(distinct  m.metarecord)
309                   FROM  $search_table f,
310                         $metabib_metarecord_source_map_table m,
311                         $asset_call_number_table cn,
312                         $asset_copy_table cp,
313                         $descendants d
314                   WHERE $fts_where
315                         AND m.source = f.source
316                         AND cn.record = m.source
317                         $has_vols
318                         $has_copies
319                         $copies_visible
320         SQL
321
322         $log->debug("Field Search Count SQL :: [$select]",DEBUG);
323
324         my $recs = $class->db_Main->selectrow_arrayref($select)->[0];
325         
326         $log->debug("Count Search yielded $recs results.",DEBUG);
327
328         return $recs;
329
330 }
331 for my $class ( qw/title author subject keyword series/ ) {
332         __PACKAGE__->register_method(
333                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord_count",
334                 method          => 'search_class_fts_count',
335                 api_level       => 1,
336                 stream          => 1,
337                 cdbi            => "metabib::${class}_field_entry",
338                 cachable        => 1,
339         );
340         __PACKAGE__->register_method(
341                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord_count.staff",
342                 method          => 'search_class_fts_count',
343                 api_level       => 1,
344                 stream          => 1,
345                 cdbi            => "metabib::${class}_field_entry",
346                 cachable        => 1,
347         );
348 }
349
350
351 1;