4 ALTER TABLE config.metabib_field
5 ADD COLUMN display_xpath TEXT,
6 ADD COLUMN display_field BOOL NOT NULL DEFAULT FALSE;
8 CREATE TABLE config.display_field_map (
10 field INTEGER REFERENCES config.metabib_field (id),
11 multi BOOLEAN DEFAULT FALSE
14 CREATE TABLE metabib.display_entry (
15 id BIGSERIAL PRIMARY KEY,
16 source BIGINT NOT NULL REFERENCES biblio.record_entry (id),
17 field INT NOT NULL REFERENCES config.metabib_field (id),
21 CREATE INDEX metabib_display_entry_field_idx ON metabib.display_entry (field);
22 CREATE INDEX metabib_display_entry_source_idx ON metabib.display_entry (source);
24 -- one row per display entry fleshed with field info
25 CREATE VIEW metabib.flat_display_entry AS
33 FROM metabib.display_entry mde
34 JOIN config.metabib_field cmf ON (cmf.id = mde.field)
35 JOIN config.display_field_map cdfm ON (cdfm.field = mde.field)
38 -- like flat_display_entry except values are compressed
39 -- into one row per display_field_map and JSON-ified.
40 CREATE VIEW metabib.compressed_display_entry AS
48 TO_JSON(ARRAY_AGG(value))
52 FROM metabib.flat_display_entry
53 GROUP BY 1, 2, 3, 4, 5
56 -- TODO: expand to encompass all well-known fields
57 CREATE VIEW metabib.wide_display_entry AS
60 COALESCE(mcde_title.value, 'null') AS title,
61 COALESCE(mcde_author.value, 'null') AS author,
62 COALESCE(mcde_subject.value, 'null') AS subject,
63 COALESCE(mcde_topic_subject.value, 'null') AS topic_subject,
64 COALESCE(mcde_isbn.value, 'null') AS isbn
65 -- ensure one row per bre regardless of any display fields
66 FROM biblio.record_entry bre
67 LEFT JOIN metabib.compressed_display_entry mcde_title
68 ON (bre.id = mcde_title.source AND mcde_title.name = 'title')
69 LEFT JOIN metabib.compressed_display_entry mcde_author
70 ON (bre.id = mcde_author.source AND mcde_author.name = 'author')
71 LEFT JOIN metabib.compressed_display_entry mcde_subject
72 ON (bre.id = mcde_subject.source AND mcde_subject.name = 'subject')
73 LEFT JOIN metabib.compressed_display_entry mcde_topic_subject
74 ON (bre.id = mcde_topic_subject.source AND mcde_topic_subject.name = 'topic_subject')
75 LEFT JOIN metabib.compressed_display_entry mcde_isbn
76 ON (bre.id = mcde_isbn.source AND mcde_isbn.name = 'isbn')
80 CREATE OR REPLACE FUNCTION metabib.display_field_normalize_trigger ()
84 display_field_text TEXT;
86 display_field_text := NEW.value;
89 SELECT n.func AS func,
90 n.param_count AS param_count,
92 FROM config.index_normalizer n
93 JOIN config.metabib_field_index_norm_map m ON (m.norm = n.id)
94 WHERE m.field = NEW.field AND m.pos < 0
97 EXECUTE 'SELECT ' || normalizer.func || '(' ||
98 quote_literal( display_field_text ) ||
100 WHEN normalizer.param_count > 0
101 THEN ',' || REPLACE(REPLACE(BTRIM(
102 normalizer.params,'[]'),E'\'',E'\\\''),E'"',E'\'')
105 ')' INTO display_field_text;
109 NEW.value = display_field_text;
115 CREATE TRIGGER display_field_normalize_tgr
116 BEFORE UPDATE OR INSERT ON metabib.display_entry
117 FOR EACH ROW EXECUTE PROCEDURE metabib.display_field_normalize_trigger();
119 CREATE OR REPLACE FUNCTION evergreen.display_field_force_nfc()
120 RETURNS TRIGGER AS $$
122 NEW.value := force_unicode_normal_form(NEW.value,'NFC');
127 CREATE TRIGGER display_field_force_nfc_tgr
128 BEFORE UPDATE OR INSERT ON metabib.display_entry
129 FOR EACH ROW EXECUTE PROCEDURE evergreen.display_field_force_nfc();
131 ALTER TYPE metabib.field_entry_template ADD ATTRIBUTE display_field BOOL;
133 CREATE OR REPLACE FUNCTION biblio.extract_metabib_field_entry ( rid BIGINT, default_joiner TEXT ) RETURNS SETOF metabib.field_entry_template AS $func$
135 bib biblio.record_entry%ROWTYPE;
136 idx config.metabib_field%ROWTYPE;
137 xfrm config.xml_transform%ROWTYPE;
139 transformed_xml TEXT;
141 xml_node_list TEXT[];
148 joiner TEXT := default_joiner; -- XXX will index defs supply a joiner?
150 authority_link BIGINT;
151 output_row metabib.field_entry_template%ROWTYPE;
154 -- Start out with no field-use bools set
155 output_row.browse_field = FALSE;
156 output_row.facet_field = FALSE;
157 output_row.display_field = FALSE;
158 output_row.search_field = FALSE;
161 SELECT INTO bib * FROM biblio.record_entry WHERE id = rid;
163 -- Loop over the indexing entries
164 FOR idx IN SELECT * FROM config.metabib_field ORDER BY format LOOP
166 joiner := COALESCE(idx.joiner, default_joiner);
168 SELECT INTO xfrm * from config.xml_transform WHERE name = idx.format;
170 -- See if we can skip the XSLT ... it's expensive
171 IF prev_xfrm IS NULL OR prev_xfrm <> xfrm.name THEN
172 -- Can't skip the transform
173 IF xfrm.xslt <> '---' THEN
174 transformed_xml := oils_xslt_process(bib.marc,xfrm.xslt);
176 transformed_xml := bib.marc;
179 prev_xfrm := xfrm.name;
182 xml_node_list := oils_xpath( idx.xpath, transformed_xml, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
185 FOR xml_node IN SELECT x FROM unnest(xml_node_list) AS x LOOP
186 CONTINUE WHEN xml_node !~ E'^\\s*<';
188 -- XXX much of this should be moved into oils_xpath_string...
189 curr_text := ARRAY_TO_STRING(evergreen.array_remove_item_by_value(evergreen.array_remove_item_by_value(
190 oils_xpath( '//text()', -- get the content of all the nodes within the main selected node
191 REGEXP_REPLACE( xml_node, E'\\s+', ' ', 'g' ) -- Translate adjacent whitespace to a single space
192 ), ' '), ''), -- throw away morally empty (bankrupt?) strings
196 CONTINUE WHEN curr_text IS NULL OR curr_text = '';
198 IF raw_text IS NOT NULL THEN
199 raw_text := raw_text || joiner;
202 raw_text := COALESCE(raw_text,'') || curr_text;
204 -- autosuggest/metabib.browse_entry
205 IF idx.browse_field THEN
207 IF idx.browse_xpath IS NOT NULL AND idx.browse_xpath <> '' THEN
208 browse_text := oils_xpath_string( idx.browse_xpath, xml_node, joiner, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
210 browse_text := curr_text;
213 IF idx.browse_sort_xpath IS NOT NULL AND
214 idx.browse_sort_xpath <> '' THEN
216 sort_value := oils_xpath_string(
217 idx.browse_sort_xpath, xml_node, joiner,
218 ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]]
221 sort_value := browse_text;
224 output_row.field_class = idx.field_class;
225 output_row.field = idx.id;
226 output_row.source = rid;
227 output_row.value = BTRIM(REGEXP_REPLACE(browse_text, E'\\s+', ' ', 'g'));
228 output_row.sort_value :=
229 public.naco_normalize(sort_value);
231 output_row.authority := NULL;
233 IF idx.authority_xpath IS NOT NULL AND idx.authority_xpath <> '' THEN
234 authority_text := oils_xpath_string(
235 idx.authority_xpath, xml_node, joiner,
237 ARRAY[xfrm.prefix, xfrm.namespace_uri],
238 ARRAY['xlink','http://www.w3.org/1999/xlink']
242 IF authority_text ~ '^\d+$' THEN
243 authority_link := authority_text::BIGINT;
244 PERFORM * FROM authority.record_entry WHERE id = authority_link;
246 output_row.authority := authority_link;
252 output_row.browse_field = TRUE;
253 -- Returning browse rows with search_field = true for search+browse
254 -- configs allows us to retain granularity of being able to search
255 -- browse fields with "starts with" type operators (for example, for
256 -- titles of songs in music albums)
257 IF idx.search_field THEN
258 output_row.search_field = TRUE;
260 RETURN NEXT output_row;
261 output_row.browse_field = FALSE;
262 output_row.search_field = FALSE;
263 output_row.sort_value := NULL;
266 -- insert raw node text for faceting
267 IF idx.facet_field THEN
269 IF idx.facet_xpath IS NOT NULL AND idx.facet_xpath <> '' THEN
270 facet_text := oils_xpath_string( idx.facet_xpath, xml_node, joiner, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
272 facet_text := curr_text;
275 output_row.field_class = idx.field_class;
276 output_row.field = -1 * idx.id;
277 output_row.source = rid;
278 output_row.value = BTRIM(REGEXP_REPLACE(facet_text, E'\\s+', ' ', 'g'));
280 output_row.facet_field = TRUE;
281 RETURN NEXT output_row;
282 output_row.facet_field = FALSE;
285 -- insert raw node text for display
286 IF idx.display_field THEN
288 IF idx.display_xpath IS NOT NULL AND idx.display_xpath <> '' THEN
289 display_text := oils_xpath_string( idx.display_xpath, xml_node, joiner, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
291 display_text := curr_text;
294 output_row.field_class = idx.field_class;
295 output_row.field = -1 * idx.id;
296 output_row.source = rid;
297 output_row.value = BTRIM(REGEXP_REPLACE(display_text, E'\\s+', ' ', 'g'));
299 output_row.display_field = TRUE;
300 RETURN NEXT output_row;
301 output_row.display_field = FALSE;
306 CONTINUE WHEN raw_text IS NULL OR raw_text = '';
308 -- insert combined node text for searching
309 IF idx.search_field THEN
310 output_row.field_class = idx.field_class;
311 output_row.field = idx.id;
312 output_row.source = rid;
313 output_row.value = BTRIM(REGEXP_REPLACE(raw_text, E'\\s+', ' ', 'g'));
315 output_row.search_field = TRUE;
316 RETURN NEXT output_row;
317 output_row.search_field = FALSE;
324 $func$ LANGUAGE PLPGSQL;
326 DROP FUNCTION metabib.reingest_metabib_field_entries(BIGINT, BOOL, BOOL, BOOL);
328 CREATE OR REPLACE FUNCTION metabib.reingest_metabib_field_entries(
329 bib_id BIGINT, skip_facet BOOL DEFAULT FALSE,
330 skip_display BOOL DEFAULT FALSE, skip_browse BOOL DEFAULT FALSE,
331 skip_search BOOL DEFAULT FALSE ) RETURNS VOID AS $func$
334 ind_data metabib.field_entry_template%ROWTYPE;
335 mbe_row metabib.browse_entry%ROWTYPE;
344 SELECT COALESCE(NULLIF(skip_facet, FALSE), EXISTS (SELECT enabled FROM config.internal_flag WHERE name = 'ingest.skip_facet_indexing' AND enabled)) INTO b_skip_facet;
345 SELECT COALESCE(NULLIF(skip_display, FALSE), EXISTS (SELECT enabled FROM config.internal_flag WHERE name = 'ingest.skip_display_indexing' AND enabled)) INTO b_skip_display;
346 SELECT COALESCE(NULLIF(skip_browse, FALSE), EXISTS (SELECT enabled FROM config.internal_flag WHERE name = 'ingest.skip_browse_indexing' AND enabled)) INTO b_skip_browse;
347 SELECT COALESCE(NULLIF(skip_search, FALSE), EXISTS (SELECT enabled FROM config.internal_flag WHERE name = 'ingest.skip_search_indexing' AND enabled)) INTO b_skip_search;
349 PERFORM * FROM config.internal_flag WHERE name = 'ingest.assume_inserts_only' AND enabled;
351 IF NOT b_skip_search THEN
352 FOR fclass IN SELECT * FROM config.metabib_class LOOP
353 -- RAISE NOTICE 'Emptying out %', fclass.name;
354 EXECUTE $$DELETE FROM metabib.$$ || fclass.name || $$_field_entry WHERE source = $$ || bib_id;
357 IF NOT b_skip_facet THEN
358 DELETE FROM metabib.facet_entry WHERE source = bib_id;
360 IF NOT b_skip_display THEN
361 DELETE FROM metabib.display_entry WHERE source = bib_id;
363 IF NOT b_skip_browse THEN
364 DELETE FROM metabib.browse_entry_def_map WHERE source = bib_id;
368 FOR ind_data IN SELECT * FROM biblio.extract_metabib_field_entry( bib_id ) LOOP
370 -- don't store what has been normalized away
371 CONTINUE WHEN ind_data.value IS NULL;
373 IF ind_data.field < 0 THEN
374 ind_data.field = -1 * ind_data.field;
377 IF ind_data.facet_field AND NOT b_skip_facet THEN
378 INSERT INTO metabib.facet_entry (field, source, value)
379 VALUES (ind_data.field, ind_data.source, ind_data.value);
382 IF ind_data.display_field AND NOT b_skip_display THEN
383 INSERT INTO metabib.display_entry (field, source, value)
384 VALUES (ind_data.field, ind_data.source, ind_data.value);
388 IF ind_data.browse_field AND NOT b_skip_browse THEN
389 -- A caveat about this SELECT: this should take care of replacing
390 -- old mbe rows when data changes, but not if normalization (by
391 -- which I mean specifically the output of
392 -- evergreen.oils_tsearch2()) changes. It may or may not be
393 -- expensive to add a comparison of index_vector to index_vector
394 -- to the WHERE clause below.
396 CONTINUE WHEN ind_data.sort_value IS NULL;
398 value_prepped := metabib.browse_normalize(ind_data.value, ind_data.field);
399 SELECT INTO mbe_row * FROM metabib.browse_entry
400 WHERE value = value_prepped AND sort_value = ind_data.sort_value;
403 mbe_id := mbe_row.id;
405 INSERT INTO metabib.browse_entry
406 ( value, sort_value ) VALUES
407 ( value_prepped, ind_data.sort_value );
409 mbe_id := CURRVAL('metabib.browse_entry_id_seq'::REGCLASS);
412 INSERT INTO metabib.browse_entry_def_map (entry, def, source, authority)
413 VALUES (mbe_id, ind_data.field, ind_data.source, ind_data.authority);
416 IF ind_data.search_field AND NOT b_skip_search THEN
417 -- Avoid inserting duplicate rows
418 EXECUTE 'SELECT 1 FROM metabib.' || ind_data.field_class ||
419 '_field_entry WHERE field = $1 AND source = $2 AND value = $3'
420 INTO mbe_id USING ind_data.field, ind_data.source, ind_data.value;
421 -- RAISE NOTICE 'Search for an already matching row returned %', mbe_id;
422 IF mbe_id IS NULL THEN
424 INSERT INTO metabib.$$ || ind_data.field_class || $$_field_entry (field, source, value)
426 quote_literal(ind_data.field) || $$, $$ ||
427 quote_literal(ind_data.source) || $$, $$ ||
428 quote_literal(ind_data.value) ||
435 IF NOT b_skip_search THEN
436 PERFORM metabib.update_combined_index_vectors(bib_id);
441 $func$ LANGUAGE PLPGSQL;