JBAS-1929 Filter bib links at vis check time
authorBill Erickson <berickxx@gmail.com>
Thu, 11 Jan 2018 16:46:26 +0000 (11:46 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Filter all bibs that don't match the requested mattype during the
visibility checking stage instead of the link-finding stage to ensure
all related bib links are scanned for viability.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
KCLS/sql/schema/deploy/browse-mattype-filter.sql

index 29cc151..af17020 100644 (file)
@@ -316,18 +316,7 @@ BEGIN
                     ARRAY_TO_STRING(ARRAY_AGG(DISTINCT authority), $$,$$),
                     ARRAY_AGG(DISTINCT def)
                 FROM  metabib.browse_author_entry_def_map map
-                WHERE entry = rec.id
-                    AND (item_type IS NULL OR (
-                        source IN (
-                            SELECT id FROM metabib.record_attr rattr
-                            /* The seemingly superfluous rattr.id = map.source
-                               helps to ensure we're not filtering on every
-                               record with the selected mattype -- i.e.
-                               it makes this optional sub-filter faster */
-                            WHERE rattr.id = map.source AND attrs->'mattype' = item_type
-                        )
-                    ))
-                    AND def = ANY(fields);
+                WHERE entry = rec.id;
 
             WHEN 'title' THEN
                 -- Gather aggregate data based on the MBE row we're looking at now, authority axis
@@ -351,14 +340,7 @@ BEGIN
                     ARRAY_TO_STRING(ARRAY_AGG(DISTINCT authority), $$,$$),
                     ARRAY_AGG(DISTINCT def)
                 FROM  metabib.browse_title_entry_def_map map
-                WHERE entry = rec.id
-                    AND (item_type IS NULL OR (
-                        source IN (
-                            SELECT id FROM metabib.record_attr rattr
-                            WHERE rattr.id = map.source AND attrs->'mattype' = item_type
-                        )
-                    ))
-                    AND def = ANY(fields);
+                WHERE entry = rec.id;
 
             WHEN 'subject' THEN
                                --Is unauthorized, i.e., 4xx on an auth record?
@@ -403,14 +385,7 @@ BEGIN
                     ARRAY_TO_STRING(ARRAY_AGG(DISTINCT authority), $$,$$),
                     ARRAY_AGG(DISTINCT def)
                 FROM  metabib.browse_subject_entry_def_map map
-                WHERE entry = rec.id
-                    AND (item_type IS NULL OR (
-                        source IN (
-                            SELECT id FROM metabib.record_attr rattr
-                            WHERE rattr.id = map.source AND attrs->'mattype' = item_type
-                        )
-                    ))
-                    AND def = ANY(fields);
+                WHERE entry = rec.id;
 
             WHEN 'series' THEN
                                --Is unauthorized, i.e., 4xx on an auth record?
@@ -456,14 +431,7 @@ BEGIN
                     ARRAY_TO_STRING(ARRAY_AGG(DISTINCT authority), $$,$$),
                     ARRAY_AGG(DISTINCT def)
                 FROM  metabib.browse_series_entry_def_map map
-                WHERE entry = rec.id
-                    AND (item_type IS NULL OR (
-                        source IN (
-                            SELECT id FROM metabib.record_attr rattr
-                            WHERE rattr.id = map.source AND attrs->'mattype' = item_type
-                        )
-                    ))
-                    AND def = ANY(fields);
+                WHERE entry = rec.id;
 
             WHEN 'call_number' THEN
                 -- Gather aggregate data based on the MBE row we're looking at now, bib axis
@@ -472,15 +440,7 @@ BEGIN
                     ARRAY_TO_STRING(ARRAY_AGG(DISTINCT authority), $$,$$),
                     ARRAY_AGG(DISTINCT def)
                 FROM  metabib.browse_call_number_entry_def_map
-                WHERE entry = rec.id
-                    AND (item_type IS NULL OR (
-                        source IN (
-                            SELECT id FROM metabib.record_attr rattr
-                            WHERE rattr.id = map.source AND attrs->'mattype' = item_type
-                        )
-                    ))
-                    AND def = ANY(fields);
-
+                WHERE entry = rec.id;
             ELSE
 
         END CASE;
@@ -492,6 +452,47 @@ BEGIN
         result_row.sources := 0;
         result_row.asources := 0;
 
+        /** JBAS-1929 Remove references to any bib records that do not 
+            match the requested item_type (mattype).  */
+        IF item_type IS NOT NULL THEN
+
+
+            -- bib-linked bibs
+            IF ARRAY_UPPER(all_brecords, 1) IS NOT NULL THEN
+                -- RAISE NOTICE 'Checking bib links on %', all_brecords;
+
+                SELECT INTO all_brecords ARRAY_AGG(DISTINCT vec.source)
+                FROM metabib.record_attr_vector_list vec
+                JOIN config.coded_value_map ccvm
+                    ON (
+                        ccvm.ctype = 'mattype' 
+                        AND ccvm.code = item_type
+                        AND ccvm.id = ANY (vec.vlist)
+                    )
+                WHERE vec.source = ANY(all_brecords);
+
+                -- RAISE NOTICE 'Bib links trimmed to %', all_brecords;
+            END IF;
+
+            -- auth-linked bibs
+            IF ARRAY_UPPER(all_arecords, 1) IS NOT NULL THEN
+                -- RAISE NOTICE 'Checking auth links on %', all_arecords;
+
+                SELECT INTO all_arecords ARRAY_AGG(DISTINCT vec.source)
+                FROM metabib.record_attr_vector_list vec
+                JOIN config.coded_value_map ccvm
+                    ON (
+                        ccvm.ctype = 'mattype' 
+                        AND ccvm.code = item_type
+                        AND ccvm.id = ANY (vec.vlist)
+                    )
+                WHERE vec.source = ANY(all_arecords);
+
+                -- RAISE NOTICE 'Auth links trimmed to %', all_arecords;
+            END IF;
+
+        END IF;
+
         -- Bib-linked vis checking
         IF ARRAY_UPPER(all_brecords,1) IS NOT NULL THEN