2 * Copyright (C) 2016 Equinox Software, Inc.
3 * Mike Rylander <miker@esilibrary.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
20 ALTER TYPE search.search_result ADD ATTRIBUTE badges TEXT, ADD ATTRIBUTE popularity NUMERIC;
22 CREATE OR REPLACE FUNCTION search.query_parser_fts (
28 param_locations INT[],
35 param_pref_ou INT DEFAULT NULL
36 ) RETURNS SETOF search.search_result AS $func$
39 current_res search.search_result%ROWTYPE;
40 search_org_list INT[];
50 core_cursor REFCURSOR;
55 deleted_count INT := 0;
56 visible_count INT := 0;
57 excluded_count INT := 0;
62 check_limit := COALESCE( param_check, 1000 );
63 core_limit := COALESCE( param_limit, 25000 );
64 core_offset := COALESCE( param_offset, 0 );
66 SELECT COALESCE( enabled, FALSE ) INTO luri_as_copy FROM config.global_flag WHERE name = 'opac.located_uri.act_as_copy';
68 -- core_skip_chk := COALESCE( param_skip_chk, 1 );
70 IF param_search_ou > 0 THEN
71 IF param_depth IS NOT NULL THEN
72 SELECT ARRAY_AGG(distinct id) INTO search_org_list FROM actor.org_unit_descendants( param_search_ou, param_depth );
74 SELECT ARRAY_AGG(distinct id) INTO search_org_list FROM actor.org_unit_descendants( param_search_ou );
78 SELECT ARRAY_AGG(distinct id) INTO luri_org_list FROM actor.org_unit_full_path( param_search_ou );
80 SELECT ARRAY_AGG(distinct id) INTO luri_org_list FROM actor.org_unit_ancestors( param_search_ou );
83 ELSIF param_search_ou < 0 THEN
84 SELECT ARRAY_AGG(distinct org_unit) INTO search_org_list FROM actor.org_lasso_map WHERE lasso = -param_search_ou;
86 FOR tmp_int IN SELECT * FROM UNNEST(search_org_list) LOOP
89 SELECT ARRAY_AGG(distinct id) INTO tmp_int_list FROM actor.org_unit_full_path( tmp_int );
91 SELECT ARRAY_AGG(distinct id) INTO tmp_int_list FROM actor.org_unit_ancestors( tmp_int );
94 luri_org_list := luri_org_list || tmp_int_list;
97 SELECT ARRAY_AGG(DISTINCT x.id) INTO luri_org_list FROM UNNEST(luri_org_list) x(id);
99 ELSIF param_search_ou = 0 THEN
100 -- reserved for user lassos (ou_buckets/type='lasso') with ID passed in depth ... hack? sure.
103 IF param_pref_ou IS NOT NULL THEN
105 SELECT ARRAY_AGG(distinct id) INTO tmp_int_list FROM actor.org_unit_full_path( param_pref_ou );
107 SELECT ARRAY_AGG(distinct id) INTO tmp_int_list FROM actor.org_unit_ancestors( param_pref_ou );
110 luri_org_list := luri_org_list || tmp_int_list;
113 OPEN core_cursor FOR EXECUTE param_query;
117 FETCH core_cursor INTO core_result;
119 EXIT WHEN total_count >= core_limit;
121 total_count := total_count + 1;
123 CONTINUE WHEN total_count NOT BETWEEN core_offset + 1 AND check_limit + core_offset;
125 check_count := check_count + 1;
127 IF NOT deleted_search THEN
129 PERFORM 1 FROM biblio.record_entry b WHERE NOT b.deleted AND b.id IN ( SELECT * FROM unnest( core_result.records ) );
131 -- RAISE NOTICE ' % were all deleted ... ', core_result.records;
132 deleted_count := deleted_count + 1;
137 FROM biblio.record_entry b
138 JOIN config.bib_source s ON (b.source = s.id)
140 AND b.id IN ( SELECT * FROM unnest( core_result.records ) );
143 -- RAISE NOTICE ' % were all transcendant ... ', core_result.records;
144 visible_count := visible_count + 1;
146 current_res.id = core_result.id;
147 current_res.rel = core_result.rel;
148 current_res.badges = core_result.badges;
149 current_res.popularity = core_result.popularity;
153 SELECT COUNT(DISTINCT s.source) INTO tmp_int FROM metabib.metarecord_source_map s WHERE s.metarecord = core_result.id;
157 current_res.record = core_result.records[1];
159 current_res.record = NULL;
162 RETURN NEXT current_res;
168 FROM asset.call_number cn
169 JOIN asset.uri_call_number_map map ON (map.call_number = cn.id)
170 JOIN asset.uri uri ON (map.uri = uri.id)
172 AND cn.label = '##URI##'
174 AND ( param_locations IS NULL OR array_upper(param_locations, 1) IS NULL )
175 AND cn.record IN ( SELECT * FROM unnest( core_result.records ) )
176 AND cn.owning_lib IN ( SELECT * FROM unnest( luri_org_list ) )
180 -- RAISE NOTICE ' % have at least one URI ... ', core_result.records;
181 visible_count := visible_count + 1;
183 current_res.id = core_result.id;
184 current_res.rel = core_result.rel;
185 current_res.badges = core_result.badges;
186 current_res.popularity = core_result.popularity;
190 SELECT COUNT(DISTINCT s.source) INTO tmp_int FROM metabib.metarecord_source_map s WHERE s.metarecord = core_result.id;
194 current_res.record = core_result.records[1];
196 current_res.record = NULL;
199 RETURN NEXT current_res;
204 IF param_statuses IS NOT NULL AND array_upper(param_statuses, 1) > 0 THEN
207 FROM asset.call_number cn
208 JOIN asset.copy cp ON (cp.call_number = cn.id)
211 AND cp.status IN ( SELECT * FROM unnest( param_statuses ) )
212 AND cn.record IN ( SELECT * FROM unnest( core_result.records ) )
213 AND cp.circ_lib IN ( SELECT * FROM unnest( search_org_list ) )
218 FROM biblio.peer_bib_copy_map pr
219 JOIN asset.copy cp ON (cp.id = pr.target_copy)
221 AND cp.status IN ( SELECT * FROM unnest( param_statuses ) )
222 AND pr.peer_record IN ( SELECT * FROM unnest( core_result.records ) )
223 AND cp.circ_lib IN ( SELECT * FROM unnest( search_org_list ) )
227 -- RAISE NOTICE ' % and multi-home linked records were all status-excluded ... ', core_result.records;
228 excluded_count := excluded_count + 1;
235 IF param_locations IS NOT NULL AND array_upper(param_locations, 1) > 0 THEN
238 FROM asset.call_number cn
239 JOIN asset.copy cp ON (cp.call_number = cn.id)
242 AND cp.location IN ( SELECT * FROM unnest( param_locations ) )
243 AND cn.record IN ( SELECT * FROM unnest( core_result.records ) )
244 AND cp.circ_lib IN ( SELECT * FROM unnest( search_org_list ) )
249 FROM biblio.peer_bib_copy_map pr
250 JOIN asset.copy cp ON (cp.id = pr.target_copy)
252 AND cp.location IN ( SELECT * FROM unnest( param_locations ) )
253 AND pr.peer_record IN ( SELECT * FROM unnest( core_result.records ) )
254 AND cp.circ_lib IN ( SELECT * FROM unnest( search_org_list ) )
258 -- RAISE NOTICE ' % and multi-home linked records were all copy_location-excluded ... ', core_result.records;
259 excluded_count := excluded_count + 1;
266 IF staff IS NULL OR NOT staff THEN
269 FROM asset.opac_visible_copies
270 WHERE circ_lib IN ( SELECT * FROM unnest( search_org_list ) )
271 AND record IN ( SELECT * FROM unnest( core_result.records ) )
276 FROM biblio.peer_bib_copy_map pr
277 JOIN asset.opac_visible_copies cp ON (cp.copy_id = pr.target_copy)
278 WHERE cp.circ_lib IN ( SELECT * FROM unnest( search_org_list ) )
279 AND pr.peer_record IN ( SELECT * FROM unnest( core_result.records ) )
284 -- RAISE NOTICE ' % and multi-home linked records were all visibility-excluded ... ', core_result.records;
285 excluded_count := excluded_count + 1;
293 FROM asset.call_number cn
294 JOIN asset.copy cp ON (cp.call_number = cn.id)
297 AND cp.circ_lib IN ( SELECT * FROM unnest( search_org_list ) )
298 AND cn.record IN ( SELECT * FROM unnest( core_result.records ) )
304 FROM biblio.peer_bib_copy_map pr
305 JOIN asset.copy cp ON (cp.id = pr.target_copy)
307 AND cp.circ_lib IN ( SELECT * FROM unnest( search_org_list ) )
308 AND pr.peer_record IN ( SELECT * FROM unnest( core_result.records ) )
314 FROM asset.call_number cn
315 JOIN asset.copy cp ON (cp.call_number = cn.id)
316 WHERE cn.record IN ( SELECT * FROM unnest( core_result.records ) )
321 -- Recheck Located URI visibility in the case of no "foreign" copies
323 FROM asset.call_number cn
324 JOIN asset.uri_call_number_map map ON (map.call_number = cn.id)
325 JOIN asset.uri uri ON (map.uri = uri.id)
327 AND cn.label = '##URI##'
329 AND cn.record IN ( SELECT * FROM unnest( core_result.records ) )
330 AND cn.owning_lib NOT IN ( SELECT * FROM unnest( luri_org_list ) )
334 -- RAISE NOTICE ' % were excluded for foreign located URIs... ', core_result.records;
335 excluded_count := excluded_count + 1;
339 -- RAISE NOTICE ' % and multi-home linked records were all visibility-excluded ... ', core_result.records;
340 excluded_count := excluded_count + 1;
351 visible_count := visible_count + 1;
353 current_res.id = core_result.id;
354 current_res.rel = core_result.rel;
355 current_res.badges = core_result.badges;
356 current_res.popularity = core_result.popularity;
360 SELECT COUNT(DISTINCT s.source) INTO tmp_int FROM metabib.metarecord_source_map s WHERE s.metarecord = core_result.id;
364 current_res.record = core_result.records[1];
366 current_res.record = NULL;
369 RETURN NEXT current_res;
371 IF visible_count % 1000 = 0 THEN
372 -- RAISE NOTICE ' % visible so far ... ', visible_count;
377 current_res.id = NULL;
378 current_res.rel = NULL;
379 current_res.record = NULL;
380 current_res.badges = NULL;
381 current_res.popularity = NULL;
382 current_res.total = total_count;
383 current_res.checked = check_count;
384 current_res.deleted = deleted_count;
385 current_res.visible = visible_count;
386 current_res.excluded = excluded_count;
390 RETURN NEXT current_res;
393 $func$ LANGUAGE PLPGSQL;
395 CREATE OR REPLACE FUNCTION metabib.staged_browse(
399 context_locations INT[],
401 browse_superpage_size INT,
402 count_up_from_zero BOOL, -- if false, count down from -1
405 ) RETURNS SETOF metabib.flat_browse_entry_appearance AS $p$
413 result_row metabib.flat_browse_entry_appearance%ROWTYPE;
414 results_skipped INT := 0;
415 row_counter INT := 0;
420 all_records BIGINT[];
421 all_brecords BIGINT[];
422 all_arecords BIGINT[];
423 superpage_of_records BIGINT[];
426 IF count_up_from_zero THEN
432 OPEN curs FOR EXECUTE query;
437 IF result_row.pivot_point IS NOT NULL THEN
438 RETURN NEXT result_row;
444 -- Gather aggregate data based on the MBE row we're looking at now, authority axis
445 SELECT INTO all_arecords, result_row.sees, afields
446 ARRAY_AGG(DISTINCT abl.bib), -- bibs to check for visibility
447 STRING_AGG(DISTINCT aal.source::TEXT, $$,$$), -- authority record ids
448 ARRAY_AGG(DISTINCT map.metabib_field) -- authority-tag-linked CMF rows
450 FROM metabib.browse_entry_simple_heading_map mbeshm
451 JOIN authority.simple_heading ash ON ( mbeshm.simple_heading = ash.id )
452 JOIN authority.authority_linking aal ON ( ash.record = aal.source )
453 JOIN authority.bib_linking abl ON ( aal.target = abl.authority )
454 JOIN authority.control_set_auth_field_metabib_field_map_refs map ON (
455 ash.atag = map.authority_field
456 AND map.metabib_field = ANY(fields)
458 WHERE mbeshm.entry = rec.id;
461 -- Gather aggregate data based on the MBE row we're looking at now, bib axis
462 SELECT INTO all_brecords, result_row.authorities, bfields
463 ARRAY_AGG(DISTINCT source),
464 STRING_AGG(DISTINCT authority::TEXT, $$,$$),
465 ARRAY_AGG(DISTINCT def)
466 FROM metabib.browse_entry_def_map
468 AND def = ANY(fields);
470 SELECT INTO result_row.fields STRING_AGG(DISTINCT x::TEXT, $$,$$) FROM UNNEST(afields || bfields) x;
472 result_row.sources := 0;
473 result_row.asources := 0;
475 -- Bib-linked vis checking
476 IF ARRAY_UPPER(all_brecords,1) IS NOT NULL THEN
478 full_end := ARRAY_LENGTH(all_brecords, 1);
479 superpage_size := COALESCE(browse_superpage_size, full_end);
481 slice_end := superpage_size;
483 WHILE result_row.sources = 0 AND slice_start <= full_end LOOP
484 superpage_of_records := all_brecords[slice_start:slice_end];
486 'SELECT NULL::BIGINT AS id, ARRAY[r] AS records, ' ||
487 'NULL AS badges, NULL::NUMERIC AS popularity, ' ||
488 '1::NUMERIC AS rel FROM (SELECT UNNEST(' ||
489 quote_literal(superpage_of_records) || '::BIGINT[]) AS r) rr';
491 -- We use search.query_parser_fts() for visibility testing.
492 -- We're calling it once per browse-superpage worth of records
493 -- out of the set of records related to a given mbe, until we've
494 -- either exhausted that set of records or found at least 1
497 SELECT INTO result_row.sources visible
498 FROM search.query_parser_fts(
499 context_org, NULL, qpfts_query, NULL,
500 context_locations, 0, NULL, NULL, FALSE, staff, FALSE
502 WHERE qpfts.rel IS NULL;
504 slice_start := slice_start + superpage_size;
505 slice_end := slice_end + superpage_size;
508 -- Accurate? Well, probably.
509 result_row.accurate := browse_superpage_size IS NULL OR
510 browse_superpage_size >= full_end;
514 -- Authority-linked vis checking
515 IF ARRAY_UPPER(all_arecords,1) IS NOT NULL THEN
517 full_end := ARRAY_LENGTH(all_arecords, 1);
518 superpage_size := COALESCE(browse_superpage_size, full_end);
520 slice_end := superpage_size;
522 WHILE result_row.asources = 0 AND slice_start <= full_end LOOP
523 superpage_of_records := all_arecords[slice_start:slice_end];
525 'SELECT NULL::BIGINT AS id, ARRAY[r] AS records, ' ||
526 'NULL AS badges, NULL::NUMERIC AS popularity, ' ||
527 '1::NUMERIC AS rel FROM (SELECT UNNEST(' ||
528 quote_literal(superpage_of_records) || '::BIGINT[]) AS r) rr';
530 -- We use search.query_parser_fts() for visibility testing.
531 -- We're calling it once per browse-superpage worth of records
532 -- out of the set of records related to a given mbe, via
533 -- authority until we've either exhausted that set of records
534 -- or found at least 1 visible record.
536 SELECT INTO result_row.asources visible
537 FROM search.query_parser_fts(
538 context_org, NULL, qpfts_query, NULL,
539 context_locations, 0, NULL, NULL, FALSE, staff, FALSE
541 WHERE qpfts.rel IS NULL;
543 slice_start := slice_start + superpage_size;
544 slice_end := slice_end + superpage_size;
548 -- Accurate? Well, probably.
549 result_row.aaccurate := browse_superpage_size IS NULL OR
550 browse_superpage_size >= full_end;
554 IF result_row.sources > 0 OR result_row.asources > 0 THEN
556 -- The function that calls this function needs row_number in order
557 -- to correctly order results from two different runs of this
559 result_row.row_number := row_number;
561 -- Now, if row_counter is still less than limit, return a row. If
562 -- not, but it is less than next_pivot_pos, continue on without
563 -- returning actual result rows until we find
564 -- that next pivot, and return it.
566 IF row_counter < result_limit THEN
567 result_row.browse_entry := rec.id;
568 result_row.value := rec.value;
570 RETURN NEXT result_row;
572 result_row.browse_entry := NULL;
573 result_row.authorities := NULL;
574 result_row.fields := NULL;
575 result_row.value := NULL;
576 result_row.sources := NULL;
577 result_row.sees := NULL;
578 result_row.accurate := NULL;
579 result_row.aaccurate := NULL;
580 result_row.pivot_point := rec.id;
582 IF row_counter >= next_pivot_pos THEN
583 RETURN NEXT result_row;
588 IF count_up_from_zero THEN
589 row_number := row_number + 1;
591 row_number := row_number - 1;
594 -- row_counter is different from row_number.
595 -- It simply counts up from zero so that we know when
596 -- we've reached our limit.
597 row_counter := row_counter + 1;
601 $p$ LANGUAGE PLPGSQL;