1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 \brief high-level logic; mostly user sessions and settings
41 #include <yaz/marcdisp.h>
42 #include <yaz/comstack.h>
43 #include <yaz/tcpip.h>
44 #include <yaz/proto.h>
45 #include <yaz/readconf.h>
46 #include <yaz/pquery.h>
47 #include <yaz/otherinfo.h>
48 #include <yaz/yaz-util.h>
50 #include <yaz/query-charset.h>
51 #include <yaz/querytowrbuf.h>
52 #include <yaz/oid_db.h>
53 #include <yaz/snprintf.h>
57 #include <yaz/timing.h>
60 #include "parameters.h"
64 #include "termlists.h"
66 #include "relevance.h"
70 #include "normalize7bit.h"
72 #define TERMLIST_HIGH_SCORE 25
76 // Note: Some things in this structure will eventually move to configuration
77 struct parameters global_parameters =
84 static void log_xml_doc(xmlDoc *doc)
86 FILE *lf = yaz_log_file();
89 #if LIBXML_VERSION >= 20600
90 xmlDocDumpFormatMemory(doc, &result, &len, 1);
92 xmlDocDumpMemory(doc, &result, &len);
96 fwrite(result, 1, len, lf);
102 // Recursively traverse query structure to extract terms.
103 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
115 pull_terms(nmem, n->u.p[0], termlist, num);
116 pull_terms(nmem, n->u.p[1], termlist, num);
119 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
120 for (i = 0; i < numwords; i++)
121 termlist[(*num)++] = words[i];
130 static void add_facet(struct session *s, const char *type, const char *value)
136 for (i = 0; i < s->num_termlists; i++)
137 if (!strcmp(s->termlists[i].name, type))
139 if (i == s->num_termlists)
141 if (i == SESSION_MAX_TERMLISTS)
143 yaz_log(YLOG_FATAL, "Too many termlists");
147 s->termlists[i].name = nmem_strdup(s->nmem, type);
148 s->termlists[i].termlist
149 = termlist_create(s->nmem, s->expected_maxrecs,
150 TERMLIST_HIGH_SCORE);
151 s->num_termlists = i + 1;
153 termlist_insert(s->termlists[i].termlist, value);
156 static xmlDoc *record_to_xml(struct session_database *sdb, const char *rec)
158 struct database *db = sdb->database;
161 rdoc = xmlParseMemory(rec, strlen(rec));
165 yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s",
170 if (global_parameters.dump_records)
172 yaz_log(YLOG_LOG, "Un-normalized record from %s", db->url);
179 #define MAX_XSLT_ARGS 16
181 // Add static values from session database settings if applicable
182 static void insert_settings_parameters(struct session_database *sdb,
183 struct session *se, char **parms)
185 struct conf_service *service = se->service;
190 for (i = 0; i < service->num_metadata; i++)
192 struct conf_metadata *md = &service->metadata[i];
195 if (md->setting == Metadata_setting_parameter &&
196 (setting = settings_offset(service, md->name)) > 0)
198 const char *val = session_setting_oneval(sdb, setting);
199 if (val && nparms < MAX_XSLT_ARGS)
202 int len = strlen(val);
203 buf = nmem_malloc(se->nmem, len + 3);
205 strcpy(buf + 1, val);
208 parms[offset++] = md->name;
209 parms[offset++] = buf;
217 // Add static values from session database settings if applicable
218 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
219 struct conf_service *service)
223 for (i = 0; i < service->num_metadata; i++)
225 struct conf_metadata *md = &service->metadata[i];
228 if (md->setting == Metadata_setting_postproc &&
229 (offset = settings_offset(service, md->name)) > 0)
231 const char *val = session_setting_oneval(sdb, offset);
234 xmlNode *r = xmlDocGetRootElement(doc);
235 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
237 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
243 static xmlDoc *normalize_record(struct session_database *sdb,
247 xmlDoc *rdoc = record_to_xml(sdb, rec);
251 char *parms[MAX_XSLT_ARGS*2+1];
253 insert_settings_parameters(sdb, se, parms);
255 if (normalize_record_transform(sdb->map, &rdoc, (const char **)parms))
257 yaz_log(YLOG_WARN, "Normalize failed from %s", sdb->database->url);
261 insert_settings_values(sdb, rdoc, se->service);
263 if (global_parameters.dump_records)
265 yaz_log(YLOG_LOG, "Normalized record from %s",
274 // Retrieve first defined value for 'name' for given database.
275 // Will be extended to take into account user associated with session
276 const char *session_setting_oneval(struct session_database *db, int offset)
278 if (!db->settings[offset])
280 return db->settings[offset]->value;
283 // Prepare XSLT stylesheets for record normalization
284 // Structures are allocated on the session_wide nmem to avoid having
285 // to recompute this for every search. This would lead
286 // to leaking if a single session was to repeatedly change the PZ_XSLT
287 // setting. However, this is not a realistic use scenario.
288 static int prepare_map(struct session *se, struct session_database *sdb)
294 yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url);
297 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
299 char auto_stylesheet[256];
301 if (!strcmp(s, "auto"))
303 const char *request_syntax = session_setting_oneval(
304 sdb, PZ_REQUESTSYNTAX);
308 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
309 "%s.xsl", request_syntax);
310 for (cp = auto_stylesheet; *cp; cp++)
312 /* deliberately only consider ASCII */
313 if (*cp > 32 && *cp < 127)
320 yaz_log(YLOG_WARN, "No pz:requestsyntax for auto stylesheet");
323 sdb->map = normalize_cache_get(se->normalize_cache,
331 // This analyzes settings and recomputes any supporting data structures
333 static int prepare_session_database(struct session *se,
334 struct session_database *sdb)
339 "No settings associated with %s", sdb->database->url);
342 if (sdb->settings[PZ_XSLT] && !sdb->map)
344 if (prepare_map(se, sdb) < 0)
350 // called if watch should be removed because http_channel is to be destroyed
351 static void session_watch_cancel(void *data, struct http_channel *c,
354 struct session_watchentry *ent = data;
361 // set watch. Returns 0=OK, -1 if watch is already set
362 int session_set_watch(struct session *s, int what,
363 session_watchfun fun, void *data,
364 struct http_channel *chan)
366 if (s->watchlist[what].fun)
368 s->watchlist[what].fun = fun;
369 s->watchlist[what].data = data;
370 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
371 session_watch_cancel);
375 void session_alert_watch(struct session *s, int what)
377 if (s->watchlist[what].fun)
379 /* our watch is no longer associated with http_channel */
381 session_watchfun fun;
383 http_remove_observer(s->watchlist[what].obs);
384 fun = s->watchlist[what].fun;
385 data = s->watchlist[what].data;
387 /* reset watch before fun is invoked - in case fun wants to set
389 s->watchlist[what].fun = 0;
390 s->watchlist[what].data = 0;
391 s->watchlist[what].obs = 0;
397 //callback for grep_databases
398 static void select_targets_callback(void *context, struct session_database *db)
400 struct session *se = (struct session*) context;
401 struct client *cl = client_create();
402 client_set_database(cl, db);
403 client_set_session(cl, se);
406 // Associates a set of clients with a session;
407 // Note: Session-databases represent databases with per-session
409 int select_targets(struct session *se, struct database_criterion *crit)
412 client_destroy(se->clients);
414 return session_grep_databases(se, crit, select_targets_callback);
417 int session_active_clients(struct session *s)
422 for (c = s->clients; c; c = client_next_in_session(c))
423 if (client_is_active(c))
429 // parses crit1=val1,crit2=val2|val3,...
430 static struct database_criterion *parse_filter(NMEM m, const char *buf)
432 struct database_criterion *res = 0;
439 nmem_strsplit(m, ",", buf, &values, &num);
440 for (i = 0; i < num; i++)
445 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
447 if ((eq = strchr(values[i], '=')))
448 new->type = PAZPAR2_STRING_MATCH;
449 if ((eq = strchr(values[i], '~')))
450 new->type = PAZPAR2_SUBSTRING_MATCH;
453 yaz_log(YLOG_WARN, "Missing equal-signi/tilde in filter");
457 new->name = values[i];
458 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
460 for (subi = 0; subi < subnum; subi++)
462 struct database_criterion_value *newv
463 = nmem_malloc(m, sizeof(*newv));
464 newv->value = subvalues[subi];
465 newv->next = new->values;
474 enum pazpar2_error_code search(struct session *se,
475 const char *query, const char *filter,
476 const char **addinfo)
478 int live_channels = 0;
482 struct database_criterion *criteria;
484 yaz_log(YLOG_DEBUG, "Search");
487 nmem_reset(se->nmem);
489 se->total_records = se->total_hits = se->total_merged = 0;
491 se->num_termlists = 0;
492 criteria = parse_filter(se->nmem, filter);
493 live_channels = select_targets(se, criteria);
496 int maxrecs = live_channels * global_parameters.toget; // This is buggy!!!
497 se->reclist = reclist_create(se->nmem, maxrecs);
498 se->expected_maxrecs = maxrecs;
501 return PAZPAR2_NO_TARGETS;
503 for (cl = se->clients; cl; cl = client_next_in_session(cl))
505 if (prepare_session_database(se, client_get_database(cl)) < 0)
507 // Parse query for target
508 if (client_parse_query(cl, query) < 0)
513 if (client_prep_connection(cl, se->service->z3950_operation_timeout,
514 se->service->z3950_session_timeout))
515 client_start_search(cl);
523 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
526 return PAZPAR2_NO_TARGETS;
528 return PAZPAR2_NO_ERROR;
531 // Creates a new session_database object for a database
532 static void session_init_databases_fun(void *context, struct database *db)
534 struct session *se = (struct session *) context;
535 struct conf_service *service = se->service;
536 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
537 int num = settings_num(service);
544 = nmem_malloc(se->session_nmem, sizeof(struct settings *) * num);
545 memset(new->settings, 0, sizeof(struct settings*) * num);
549 for (i = 0; i < num; i++)
550 new->settings[i] = db->settings[i];
552 new->next = se->databases;
556 // Doesn't free memory associated with sdb -- nmem takes care of that
557 static void session_database_destroy(struct session_database *sdb)
562 // Initialize session_database list -- this represents this session's view
563 // of the database list -- subject to modification by the settings ws command
564 void session_init_databases(struct session *se)
567 predef_grep_databases(se, se->service, 0, session_init_databases_fun);
570 // Probably session_init_databases_fun should be refactored instead of
572 static struct session_database *load_session_database(struct session *se,
575 struct database *db = find_database(id, 0, se->service);
577 resolve_database(db);
579 session_init_databases_fun((void*) se, db);
580 // New sdb is head of se->databases list
581 return se->databases;
584 // Find an existing session database. If not found, load it
585 static struct session_database *find_session_database(struct session *se,
588 struct session_database *sdb;
590 for (sdb = se->databases; sdb; sdb = sdb->next)
591 if (!strcmp(sdb->database->url, id))
593 return load_session_database(se, id);
596 // Apply a session override to a database
597 void session_apply_setting(struct session *se, char *dbname, char *setting,
600 struct session_database *sdb = find_session_database(se, dbname);
601 struct conf_service *service = se->service;
602 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
603 int offset = settings_offset_cprefix(service, setting);
607 yaz_log(YLOG_WARN, "Unknown setting %s", setting);
610 // Jakub: This breaks the filter setting.
611 /*if (offset == PZ_ID)
613 yaz_log(YLOG_WARN, "No need to set pz:id setting. Ignoring");
617 new->target = dbname;
620 new->next = sdb->settings[offset];
621 sdb->settings[offset] = new;
623 // Force later recompute of settings-driven data structures
624 // (happens when a search starts and client connections are prepared)
636 void destroy_session(struct session *s)
638 struct session_database *sdb;
641 client_destroy(s->clients);
642 for (sdb = s->databases; sdb; sdb = sdb->next)
643 session_database_destroy(sdb);
644 normalize_cache_destroy(s->normalize_cache);
645 nmem_destroy(s->nmem);
646 service_destroy(s->service);
647 wrbuf_destroy(s->wrbuf);
650 struct session *new_session(NMEM nmem, struct conf_service *service)
653 struct session *session = nmem_malloc(nmem, sizeof(*session));
655 yaz_log(YLOG_DEBUG, "New Pazpar2 session");
657 session->service = service;
658 session->relevance = 0;
659 session->total_hits = 0;
660 session->total_records = 0;
661 session->number_of_warnings_unknown_elements = 0;
662 session->number_of_warnings_unknown_metadata = 0;
663 session->num_termlists = 0;
664 session->reclist = 0;
665 session->clients = 0;
666 session->expected_maxrecs = 0;
667 session->session_nmem = nmem;
668 session->nmem = nmem_create();
669 session->wrbuf = wrbuf_alloc();
670 session->databases = 0;
671 for (i = 0; i <= SESSION_WATCH_MAX; i++)
673 session->watchlist[i].data = 0;
674 session->watchlist[i].fun = 0;
676 session->normalize_cache = normalize_cache_create();
681 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
683 struct hitsbytarget *res = 0;
687 for (cl = se->clients; cl; cl = client_next_in_session(cl))
690 res = nmem_malloc(nmem, sizeof(*res) * sz);
692 for (cl = se->clients; cl; cl = client_next_in_session(cl))
694 const char *name = session_setting_oneval(client_get_database(cl),
697 res[*count].id = client_get_database(cl)->database->url;
698 res[*count].name = *name ? name : "Unknown";
699 res[*count].hits = client_get_hits(cl);
700 res[*count].records = client_get_num_records(cl);
701 res[*count].diagnostic = client_get_diagnostic(cl);
702 res[*count].state = client_get_state_str(cl);
703 res[*count].connected = client_get_connection(cl) ? 1 : 0;
709 struct termlist_score **termlist(struct session *s, const char *name, int *num)
713 for (i = 0; i < s->num_termlists; i++)
714 if (!strcmp((const char *) s->termlists[i].name, name))
715 return termlist_highscore(s->termlists[i].termlist, num);
719 #ifdef MISSING_HEADERS
720 void report_nmem_stats(void)
722 size_t in_use, is_free;
724 nmem_get_memory_in_use(&in_use);
725 nmem_get_memory_free(&is_free);
727 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
728 (long) in_use, (long) is_free);
732 struct record_cluster *show_single(struct session *s, const char *id,
733 struct record_cluster **prev_r,
734 struct record_cluster **next_r)
736 struct record_cluster *r;
738 reclist_rewind(s->reclist);
741 while ((r = reclist_read_record(s->reclist)))
743 if (!strcmp(r->recid, id))
745 *next_r = reclist_read_record(s->reclist);
753 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp,
754 int start, int *num, int *total, int *sumhits,
757 struct record_cluster **recs = nmem_malloc(nmem_show, *num
758 * sizeof(struct record_cluster *));
759 struct reclist_sortparms *spp;
762 yaz_timing_t t = yaz_timing_create();
774 for (spp = sp; spp; spp = spp->next)
775 if (spp->type == Metadata_sortkey_relevance)
777 relevance_prepare_read(s->relevance, s->reclist);
780 reclist_sort(s->reclist, sp);
782 *total = s->reclist->num_records;
783 *sumhits = s->total_hits;
785 for (i = 0; i < start; i++)
786 if (!reclist_read_record(s->reclist))
793 for (i = 0; i < *num; i++)
795 struct record_cluster *r = reclist_read_record(s->reclist);
806 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
807 yaz_timing_get_real(t), yaz_timing_get_user(t),
808 yaz_timing_get_sys(t));
809 yaz_timing_destroy(&t);
814 void statistics(struct session *se, struct statistics *stat)
819 memset(stat, 0, sizeof(*stat));
820 for (cl = se->clients; cl; cl = client_next_in_session(cl))
822 if (!client_get_connection(cl))
823 stat->num_no_connection++;
824 switch (client_get_state(cl))
826 case Client_Connecting: stat->num_connecting++; break;
827 case Client_Working: stat->num_working++; break;
828 case Client_Idle: stat->num_idle++; break;
829 case Client_Failed: stat->num_failed++; break;
830 case Client_Error: stat->num_error++; break;
835 stat->num_hits = se->total_hits;
836 stat->num_records = se->total_records;
838 stat->num_clients = count;
842 // Master list of connections we're handling events to
843 static IOCHAN channel_list = 0; /* thread pr */
845 void pazpar2_add_channel(IOCHAN chan)
847 chan->next = channel_list;
851 void pazpar2_event_loop()
853 event_loop(&channel_list);
856 static struct record_metadata *record_metadata_init(
857 NMEM nmem, char *value, enum conf_metadata_type type)
859 struct record_metadata *rec_md = record_metadata_create(nmem);
860 if (type == Metadata_type_generic)
863 p = normalize7bit_generic(p, " ,/.:([");
865 rec_md->data.text.disp = nmem_strdup(nmem, p);
866 rec_md->data.text.sort = 0;
868 else if (type == Metadata_type_year || type == Metadata_type_date)
873 if (type == Metadata_type_date)
875 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
878 rec_md->data.number.min = first;
879 rec_md->data.number.max = last;
886 static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
887 struct conf_service *service, WRBUF norm_wr)
891 for (n = root->children; n; n = n->next)
893 if (n->type != XML_ELEMENT_NODE)
895 if (!strcmp((const char *) n->name, "metadata"))
897 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
898 if (!strcmp(name, (const char *) type))
900 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
903 const char *norm_str;
904 pp2_relevance_token_t prt =
905 pp2_relevance_tokenize(
906 service->mergekey_pct,
907 (const char *) value);
909 wrbuf_puts(norm_wr, name);
910 wrbuf_puts(norm_wr, "=");
912 pp2_relevance_token_next(prt)))
916 if (wrbuf_len(norm_wr))
917 wrbuf_puts(norm_wr, " ");
918 wrbuf_puts(norm_wr, norm_str);
922 pp2_relevance_token_destroy(prt);
932 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
933 struct conf_service *service, NMEM nmem)
935 char *mergekey_norm = 0;
936 xmlNode *root = xmlDocGetRootElement(doc);
937 WRBUF norm_wr = wrbuf_alloc();
939 /* consider mergekey from XSL first */
940 xmlChar *mergekey = xmlGetProp(root, (xmlChar *) "mergekey");
943 const char *norm_str;
944 pp2_relevance_token_t prt =
945 pp2_relevance_tokenize(
946 service->mergekey_pct,
947 (const char *) mergekey);
949 while ((norm_str = pp2_relevance_token_next(prt)))
953 if (wrbuf_len(norm_wr))
954 wrbuf_puts(norm_wr, " ");
955 wrbuf_puts(norm_wr, norm_str);
958 pp2_relevance_token_destroy(prt);
963 /* no mergekey defined in XSL. Look for mergekey metadata instead */
965 for (field_id = 0; field_id < service->num_metadata; field_id++)
967 struct conf_metadata *ser_md = &service->metadata[field_id];
968 if (ser_md->mergekey != Metadata_mergekey_no)
970 int r = get_mergekey_from_doc(doc, root, ser_md->name,
972 if (r == 0 && ser_md->mergekey == Metadata_mergekey_required)
974 /* no mergekey on this one and it is required..
975 Generate unique key instead */
976 wrbuf_rewind(norm_wr);
983 /* generate unique key if none is not generated already or is empty */
984 if (wrbuf_len(norm_wr) == 0)
986 wrbuf_printf(norm_wr, "%s-%d",
987 client_get_database(cl)->database->url, record_no);
989 if (wrbuf_len(norm_wr) > 0)
990 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
991 wrbuf_destroy(norm_wr);
992 return mergekey_norm;
995 /** \brief see if metadata for pz:recordfilter exists
996 \param root xml root element of normalized record
997 \param sdb session database for client
998 \retval 0 if there is no metadata for pz:recordfilter
999 \retval 1 if there is metadata for pz:recordfilter
1001 If there is no pz:recordfilter defined, this function returns 1
1005 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1010 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1015 for (n = root->children; n; n = n->next)
1017 if (n->type != XML_ELEMENT_NODE)
1019 if (!strcmp((const char *) n->name, "metadata"))
1021 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1025 const char *eq = strchr(s, '=');
1030 if (len == strlen((const char *)type) &&
1031 !memcmp((const char *) type, s, len))
1033 xmlChar *value = xmlNodeGetContent(n);
1034 if (value && *value)
1036 if (!eq || strstr((const char *) value, eq+1))
1049 /** \brief ingest XML record
1050 \param cl client holds the result set for record
1051 \param rec record buffer (0 terminated)
1052 \param record_no record position (1, 2, ..)
1053 \returns resulting record or NULL on failure
1055 struct record *ingest_record(struct client *cl, const char *rec,
1058 struct session_database *sdb = client_get_database(cl);
1059 struct session *se = client_get_session(cl);
1060 xmlDoc *xdoc = normalize_record(sdb, se, rec);
1062 struct record *record;
1063 struct record_cluster *cluster;
1064 const char *mergekey_norm;
1067 struct conf_service *service = se->service;
1072 root = xmlDocGetRootElement(xdoc);
1074 if (!check_record_filter(root, sdb))
1076 yaz_log(YLOG_WARN, "Filtered out record no %d from %s", record_no,
1077 sdb->database->url);
1082 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, se->nmem);
1085 yaz_log(YLOG_WARN, "Got no mergekey");
1089 record = record_create(se->nmem,
1090 service->num_metadata, service->num_sortkeys, cl,
1093 cluster = reclist_insert(se->reclist,
1095 record, (char *) mergekey_norm,
1097 if (global_parameters.dump_records)
1098 yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1099 sdb->database->url, record_no);
1102 /* no room for record */
1106 relevance_newrec(se->relevance, cluster);
1108 // now parsing XML record and adding data to cluster or record metadata
1109 for (n = root->children; n; n = n->next)
1111 pp2_relevance_token_t prt;
1118 if (n->type != XML_ELEMENT_NODE)
1120 if (!strcmp((const char *) n->name, "metadata"))
1122 struct conf_metadata *ser_md = 0;
1123 struct conf_sortkey *ser_sk = 0;
1124 struct record_metadata **wheretoput = 0;
1125 struct record_metadata *rec_md = 0;
1126 int md_field_id = -1;
1127 int sk_field_id = -1;
1129 type = xmlGetProp(n, (xmlChar *) "type");
1130 value = xmlNodeListGetString(xdoc, n->children, 1);
1132 if (!type || !value || !*value)
1136 = conf_service_metadata_field_id(service, (const char *) type);
1137 if (md_field_id < 0)
1139 if (se->number_of_warnings_unknown_metadata == 0)
1142 "Ignoring unknown metadata element: %s", type);
1144 se->number_of_warnings_unknown_metadata++;
1148 ser_md = &service->metadata[md_field_id];
1150 if (ser_md->sortkey_offset >= 0){
1151 sk_field_id = ser_md->sortkey_offset;
1152 ser_sk = &service->sortkeys[sk_field_id];
1155 // non-merged metadata
1156 rec_md = record_metadata_init(se->nmem, (char *) value,
1160 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1164 wheretoput = &record->metadata[md_field_id];
1166 wheretoput = &(*wheretoput)->next;
1167 *wheretoput = rec_md;
1170 rec_md = record_metadata_init(se->nmem, (char *) value,
1172 wheretoput = &cluster->metadata[md_field_id];
1174 // and polulate with data:
1175 // assign cluster or record based on merge action
1176 if (ser_md->merge == Metadata_merge_unique)
1178 struct record_metadata *mnode;
1179 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1180 if (!strcmp((const char *) mnode->data.text.disp,
1181 rec_md->data.text.disp))
1185 rec_md->next = *wheretoput;
1186 *wheretoput = rec_md;
1189 else if (ser_md->merge == Metadata_merge_longest)
1192 || strlen(rec_md->data.text.disp)
1193 > strlen((*wheretoput)->data.text.disp))
1195 *wheretoput = rec_md;
1198 const char *sort_str = 0;
1200 ser_sk->type == Metadata_sortkey_skiparticle;
1202 if (!cluster->sortkeys[sk_field_id])
1203 cluster->sortkeys[sk_field_id] =
1204 nmem_malloc(se->nmem,
1205 sizeof(union data_types));
1207 prt = pp2_relevance_tokenize(
1209 rec_md->data.text.disp);
1211 pp2_relevance_token_next(prt);
1213 sort_str = pp2_get_sort(prt, skip_article);
1215 cluster->sortkeys[sk_field_id]->text.disp =
1216 rec_md->data.text.disp;
1219 sort_str = rec_md->data.text.disp;
1221 "Could not make sortkey. Bug #1858");
1223 cluster->sortkeys[sk_field_id]->text.sort =
1224 nmem_strdup(se->nmem, sort_str);
1226 yaz_log(YLOG_LOG, "text disp=%s",
1227 cluster->sortkeys[sk_field_id]->text.disp);
1228 yaz_log(YLOG_LOG, "text sort=%s",
1229 cluster->sortkeys[sk_field_id]->text.sort);
1231 pp2_relevance_token_destroy(prt);
1235 else if (ser_md->merge == Metadata_merge_all)
1237 rec_md->next = *wheretoput;
1238 *wheretoput = rec_md;
1240 else if (ser_md->merge == Metadata_merge_range)
1244 *wheretoput = rec_md;
1246 cluster->sortkeys[sk_field_id]
1251 int this_min = rec_md->data.number.min;
1252 int this_max = rec_md->data.number.max;
1253 if (this_min < (*wheretoput)->data.number.min)
1254 (*wheretoput)->data.number.min = this_min;
1255 if (this_max > (*wheretoput)->data.number.max)
1256 (*wheretoput)->data.number.max = this_max;
1261 // ranking of _all_ fields enabled ...
1263 relevance_countwords(se->relevance, cluster,
1264 (char *) value, ser_md->rank);
1266 // construct facets ...
1267 if (ser_md->termlist)
1269 if (ser_md->type == Metadata_type_year)
1272 sprintf(year, "%d", rec_md->data.number.max);
1273 add_facet(se, (char *) type, year);
1274 if (rec_md->data.number.max != rec_md->data.number.min)
1276 sprintf(year, "%d", rec_md->data.number.min);
1277 add_facet(se, (char *) type, year);
1281 add_facet(se, (char *) type, (char *) value);
1291 if (se->number_of_warnings_unknown_elements == 0)
1293 "Unexpected element in internal record: %s", n->name);
1294 se->number_of_warnings_unknown_elements++;
1304 relevance_donerecord(se->relevance, cluster);
1305 se->total_records++;
1315 * c-file-style: "Stroustrup"
1316 * indent-tabs-mode: nil
1318 * vim: shiftwidth=4 tabstop=8 expandtab