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>
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 =
81 30, // operations timeout
82 60, // session timeout
84 180, // Z39.50 session timeout
88 static void log_xml_doc(xmlDoc *doc)
90 FILE *lf = yaz_log_file();
93 #if LIBXML_VERSION >= 20600
94 xmlDocDumpFormatMemory(doc, &result, &len, 1);
96 xmlDocDumpMemory(doc, &result, &len);
100 fwrite(result, 1, len, lf);
106 // Recursively traverse query structure to extract terms.
107 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
119 pull_terms(nmem, n->u.p[0], termlist, num);
120 pull_terms(nmem, n->u.p[1], termlist, num);
123 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
124 for (i = 0; i < numwords; i++)
125 termlist[(*num)++] = words[i];
134 static void add_facet(struct session *s, const char *type, const char *value)
140 for (i = 0; i < s->num_termlists; i++)
141 if (!strcmp(s->termlists[i].name, type))
143 if (i == s->num_termlists)
145 if (i == SESSION_MAX_TERMLISTS)
147 yaz_log(YLOG_FATAL, "Too many termlists");
151 s->termlists[i].name = nmem_strdup(s->nmem, type);
152 s->termlists[i].termlist
153 = termlist_create(s->nmem, s->expected_maxrecs,
154 TERMLIST_HIGH_SCORE);
155 s->num_termlists = i + 1;
157 termlist_insert(s->termlists[i].termlist, value);
160 xmlDoc *record_to_xml(struct session_database *sdb, const char *rec)
162 struct database *db = sdb->database;
165 rdoc = xmlParseMemory(rec, strlen(rec));
169 yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s",
174 if (global_parameters.dump_records)
176 yaz_log(YLOG_LOG, "Un-normalized record from %s", db->url);
183 #define MAX_XSLT_ARGS 16
185 // Add static values from session database settings if applicable
186 static void insert_settings_parameters(struct session_database *sdb,
187 struct session *se, char **parms)
189 struct conf_service *service = se->service;
194 for (i = 0; i < service->num_metadata; i++)
196 struct conf_metadata *md = &service->metadata[i];
199 if (md->setting == Metadata_setting_parameter &&
200 (setting = settings_offset(service, md->name)) > 0)
202 const char *val = session_setting_oneval(sdb, setting);
203 if (val && nparms < MAX_XSLT_ARGS)
206 int len = strlen(val);
207 buf = nmem_malloc(se->nmem, len + 3);
209 strcpy(buf + 1, val);
212 parms[offset++] = md->name;
213 parms[offset++] = buf;
221 // Add static values from session database settings if applicable
222 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
223 struct conf_service *service)
227 for (i = 0; i < service->num_metadata; i++)
229 struct conf_metadata *md = &service->metadata[i];
232 if (md->setting == Metadata_setting_postproc &&
233 (offset = settings_offset(service, md->name)) > 0)
235 const char *val = session_setting_oneval(sdb, offset);
238 xmlNode *r = xmlDocGetRootElement(doc);
239 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
241 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
247 xmlDoc *normalize_record(struct session_database *sdb, struct session *se,
250 struct database_retrievalmap *m;
251 xmlDoc *rdoc = record_to_xml(sdb, rec);
254 for (m = sdb->map; m; m = m->next)
260 char *parms[MAX_XSLT_ARGS*2+1];
262 insert_settings_parameters(sdb, se, parms);
264 new = xsltApplyStylesheet(m->stylesheet, rdoc, (const char **) parms);
265 root= xmlDocGetRootElement(new);
266 if (!new || !root || !(root->children))
268 yaz_log(YLOG_WARN, "XSLT transformation failed from %s",
280 insert_settings_values(sdb, rdoc, se->service);
282 if (global_parameters.dump_records)
284 yaz_log(YLOG_LOG, "Normalized record from %s",
292 // Retrieve first defined value for 'name' for given database.
293 // Will be extended to take into account user associated with session
294 const char *session_setting_oneval(struct session_database *db, int offset)
296 if (!db->settings[offset])
298 return db->settings[offset]->value;
301 // Prepare XSLT stylesheets for record normalization
302 // Structures are allocated on the session_wide nmem to avoid having
303 // to recompute this for every search. This would lead
304 // to leaking if a single session was to repeatedly change the PZ_XSLT
305 // setting. However, this is not a realistic use scenario.
306 static int prepare_map(struct session *se, struct session_database *sdb)
312 yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url);
315 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
318 struct database_retrievalmap **m = &sdb->map;
320 char auto_stylesheet[256];
322 if (!strcmp(s, "auto"))
324 const char *request_syntax = session_setting_oneval(
325 sdb, PZ_REQUESTSYNTAX);
329 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
330 "%s.xsl", request_syntax);
331 for (cp = auto_stylesheet; *cp; cp++)
333 /* deliberately only consider ASCII */
334 if (*cp > 32 && *cp < 127)
341 yaz_log(YLOG_WARN, "No pz:requestsyntax for auto stylesheet");
344 nmem_strsplit(se->session_nmem, ",", s, &stylesheets, &num);
345 for (i = 0; i < num; i++)
347 (*m) = nmem_malloc(se->session_nmem, sizeof(**m));
349 if (!((*m)->stylesheet = conf_load_stylesheet(stylesheets[i])))
351 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
359 yaz_log(YLOG_WARN, "No Normalization stylesheet for target %s",
364 // This analyzes settings and recomputes any supporting data structures
366 static int prepare_session_database(struct session *se,
367 struct session_database *sdb)
372 "No settings associated with %s", sdb->database->url);
375 if (sdb->settings[PZ_XSLT] && !sdb->map)
377 if (prepare_map(se, sdb) < 0)
383 // called if watch should be removed because http_channel is to be destroyed
384 static void session_watch_cancel(void *data, struct http_channel *c,
387 struct session_watchentry *ent = data;
394 // set watch. Returns 0=OK, -1 if watch is already set
395 int session_set_watch(struct session *s, int what,
396 session_watchfun fun, void *data,
397 struct http_channel *chan)
399 if (s->watchlist[what].fun)
401 s->watchlist[what].fun = fun;
402 s->watchlist[what].data = data;
403 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
404 session_watch_cancel);
408 void session_alert_watch(struct session *s, int what)
410 if (s->watchlist[what].fun)
412 /* our watch is no longer associated with http_channel */
414 session_watchfun fun;
416 http_remove_observer(s->watchlist[what].obs);
417 fun = s->watchlist[what].fun;
418 data = s->watchlist[what].data;
420 /* reset watch before fun is invoked - in case fun wants to set
422 s->watchlist[what].fun = 0;
423 s->watchlist[what].data = 0;
424 s->watchlist[what].obs = 0;
430 //callback for grep_databases
431 static void select_targets_callback(void *context, struct session_database *db)
433 struct session *se = (struct session*) context;
434 struct client *cl = client_create();
435 client_set_database(cl, db);
436 client_set_session(cl, se);
439 // Associates a set of clients with a session;
440 // Note: Session-databases represent databases with per-session
442 int select_targets(struct session *se, struct database_criterion *crit)
445 client_destroy(se->clients);
447 return session_grep_databases(se, crit, select_targets_callback);
450 int session_active_clients(struct session *s)
455 for (c = s->clients; c; c = client_next_in_session(c))
456 if (client_is_active(c))
462 // parses crit1=val1,crit2=val2|val3,...
463 static struct database_criterion *parse_filter(NMEM m, const char *buf)
465 struct database_criterion *res = 0;
472 nmem_strsplit(m, ",", buf, &values, &num);
473 for (i = 0; i < num; i++)
478 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
479 char *eq = strchr(values[i], '=');
482 yaz_log(YLOG_WARN, "Missing equal-sign in filter");
486 new->name = values[i];
487 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
489 for (subi = 0; subi < subnum; subi++)
491 struct database_criterion_value *newv
492 = nmem_malloc(m, sizeof(*newv));
493 newv->value = subvalues[subi];
494 newv->next = new->values;
503 enum pazpar2_error_code search(struct session *se,
504 char *query, char *filter,
505 const char **addinfo)
507 int live_channels = 0;
511 struct database_criterion *criteria;
513 yaz_log(YLOG_DEBUG, "Search");
516 nmem_reset(se->nmem);
518 se->total_records = se->total_hits = se->total_merged = 0;
520 se->num_termlists = 0;
521 criteria = parse_filter(se->nmem, filter);
522 live_channels = select_targets(se, criteria);
525 int maxrecs = live_channels * global_parameters.toget; // This is buggy!!!
526 se->reclist = reclist_create(se->nmem, maxrecs);
527 se->expected_maxrecs = maxrecs;
530 return PAZPAR2_NO_TARGETS;
532 for (cl = se->clients; cl; cl = client_next_in_session(cl))
534 if (prepare_session_database(se, client_get_database(cl)) < 0)
536 *addinfo = client_get_database(cl)->database->url;
537 return PAZPAR2_CONFIG_TARGET;
539 // Parse query for target
540 if (client_parse_query(cl, query) < 0)
545 if (client_prep_connection(cl))
546 client_start_search(cl);
550 // If no queries could be mapped, we signal an error
554 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
556 return PAZPAR2_NO_ERROR;
559 // Creates a new session_database object for a database
560 static void session_init_databases_fun(void *context, struct database *db)
562 struct session *se = (struct session *) context;
563 struct conf_service *service = se->service;
564 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
565 int num = settings_num(service);
572 = nmem_malloc(se->session_nmem, sizeof(struct settings *) * num);
573 memset(new->settings, 0, sizeof(struct settings*) * num);
577 for (i = 0; i < num; i++)
578 new->settings[i] = db->settings[i];
580 new->next = se->databases;
584 // Doesn't free memory associated with sdb -- nmem takes care of that
585 static void session_database_destroy(struct session_database *sdb)
587 struct database_retrievalmap *m;
589 for (m = sdb->map; m; m = m->next)
590 xsltFreeStylesheet(m->stylesheet);
593 // Initialize session_database list -- this represents this session's view
594 // of the database list -- subject to modification by the settings ws command
595 void session_init_databases(struct session *se)
598 predef_grep_databases(se, se->service, 0, session_init_databases_fun);
601 // Probably session_init_databases_fun should be refactored instead of
603 static struct session_database *load_session_database(struct session *se,
606 struct database *db = find_database(id, 0, se->service);
608 session_init_databases_fun((void*) se, db);
609 // New sdb is head of se->databases list
610 return se->databases;
613 // Find an existing session database. If not found, load it
614 static struct session_database *find_session_database(struct session *se,
617 struct session_database *sdb;
619 for (sdb = se->databases; sdb; sdb = sdb->next)
620 if (!strcmp(sdb->database->url, id))
622 return load_session_database(se, id);
625 // Apply a session override to a database
626 void session_apply_setting(struct session *se, char *dbname, char *setting,
629 struct session_database *sdb = find_session_database(se, dbname);
630 struct conf_service *service = se->service;
631 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
632 int offset = settings_offset_cprefix(service, setting);
636 yaz_log(YLOG_WARN, "Unknown setting %s", setting);
639 // Jakub: This breaks the filter setting.
640 /*if (offset == PZ_ID)
642 yaz_log(YLOG_WARN, "No need to set pz:id setting. Ignoring");
646 new->target = dbname;
649 new->next = sdb->settings[offset];
650 sdb->settings[offset] = new;
652 // Force later recompute of settings-driven data structures
653 // (happens when a search starts and client connections are prepared)
659 struct database_retrievalmap *m;
660 // We don't worry about the map structure -- it's in nmem
661 for (m = sdb->map; m; m = m->next)
662 xsltFreeStylesheet(m->stylesheet);
669 void destroy_session(struct session *s)
671 struct session_database *sdb;
674 client_destroy(s->clients);
675 for (sdb = s->databases; sdb; sdb = sdb->next)
676 session_database_destroy(sdb);
677 nmem_destroy(s->nmem);
678 wrbuf_destroy(s->wrbuf);
681 struct session *new_session(NMEM nmem, struct conf_service *service)
684 struct session *session = nmem_malloc(nmem, sizeof(*session));
686 yaz_log(YLOG_DEBUG, "New Pazpar2 session");
688 session->service = service;
689 session->relevance = 0;
690 session->total_hits = 0;
691 session->total_records = 0;
692 session->number_of_warnings_unknown_elements = 0;
693 session->number_of_warnings_unknown_metadata = 0;
694 session->num_termlists = 0;
695 session->reclist = 0;
696 session->clients = 0;
697 session->expected_maxrecs = 0;
698 session->session_nmem = nmem;
699 session->nmem = nmem_create();
700 session->wrbuf = wrbuf_alloc();
701 session->databases = 0;
702 for (i = 0; i <= SESSION_WATCH_MAX; i++)
704 session->watchlist[i].data = 0;
705 session->watchlist[i].fun = 0;
710 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
712 struct hitsbytarget *res = 0;
716 for (cl = se->clients; cl; cl = client_next_in_session(cl))
719 res = nmem_malloc(nmem, sizeof(*res) * sz);
721 for (cl = se->clients; cl; cl = client_next_in_session(cl))
723 const char *name = session_setting_oneval(client_get_database(cl),
726 res[*count].id = client_get_database(cl)->database->url;
727 res[*count].name = *name ? name : "Unknown";
728 res[*count].hits = client_get_hits(cl);
729 res[*count].records = client_get_num_records(cl);
730 res[*count].diagnostic = client_get_diagnostic(cl);
731 res[*count].state = client_get_state_str(cl);
732 res[*count].connected = client_get_connection(cl) ? 1 : 0;
738 struct termlist_score **termlist(struct session *s, const char *name, int *num)
742 for (i = 0; i < s->num_termlists; i++)
743 if (!strcmp((const char *) s->termlists[i].name, name))
744 return termlist_highscore(s->termlists[i].termlist, num);
748 #ifdef MISSING_HEADERS
749 void report_nmem_stats(void)
751 size_t in_use, is_free;
753 nmem_get_memory_in_use(&in_use);
754 nmem_get_memory_free(&is_free);
756 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
757 (long) in_use, (long) is_free);
761 struct record_cluster *show_single(struct session *s, const char *id,
762 struct record_cluster **prev_r,
763 struct record_cluster **next_r)
765 struct record_cluster *r;
767 reclist_rewind(s->reclist);
770 while ((r = reclist_read_record(s->reclist)))
772 if (!strcmp(r->recid, id))
774 *next_r = reclist_read_record(s->reclist);
782 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp,
783 int start, int *num, int *total, int *sumhits,
786 struct record_cluster **recs = nmem_malloc(nmem_show, *num
787 * sizeof(struct record_cluster *));
788 struct reclist_sortparms *spp;
791 yaz_timing_t t = yaz_timing_create();
803 for (spp = sp; spp; spp = spp->next)
804 if (spp->type == Metadata_sortkey_relevance)
806 relevance_prepare_read(s->relevance, s->reclist);
809 reclist_sort(s->reclist, sp);
811 *total = s->reclist->num_records;
812 *sumhits = s->total_hits;
814 for (i = 0; i < start; i++)
815 if (!reclist_read_record(s->reclist))
822 for (i = 0; i < *num; i++)
824 struct record_cluster *r = reclist_read_record(s->reclist);
835 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
836 yaz_timing_get_real(t), yaz_timing_get_user(t),
837 yaz_timing_get_sys(t));
838 yaz_timing_destroy(&t);
843 void statistics(struct session *se, struct statistics *stat)
848 memset(stat, 0, sizeof(*stat));
849 for (cl = se->clients; cl; cl = client_next_in_session(cl))
851 if (!client_get_connection(cl))
852 stat->num_no_connection++;
853 switch (client_get_state(cl))
855 case Client_Connecting: stat->num_connecting++; break;
856 case Client_Working: stat->num_working++; break;
857 case Client_Idle: stat->num_idle++; break;
858 case Client_Failed: stat->num_failed++; break;
859 case Client_Error: stat->num_error++; break;
864 stat->num_hits = se->total_hits;
865 stat->num_records = se->total_records;
867 stat->num_clients = count;
870 int start_http_listener(struct conf_config *conf,
871 const char *listener_override,
872 const char *proxy_override)
874 struct conf_server *ser;
875 for (ser = conf->servers; ser; ser = ser->next)
877 WRBUF w = wrbuf_alloc();
879 if (listener_override)
881 wrbuf_puts(w, listener_override);
882 listener_override = 0; /* only first server is overriden */
887 wrbuf_puts(w, ser->host);
892 wrbuf_printf(w, "%d", ser->port);
895 r = http_init(wrbuf_cstr(w), ser);
902 wrbuf_puts(w, proxy_override);
903 else if (ser->proxy_host || ser->proxy_port)
906 wrbuf_puts(w, ser->proxy_host);
911 wrbuf_printf(w, "%d", ser->proxy_port);
915 http_set_proxyaddr(wrbuf_cstr(w), ser);
921 // Master list of connections we're handling events to
922 static IOCHAN channel_list = 0;
923 void pazpar2_add_channel(IOCHAN chan)
925 chan->next = channel_list;
929 void pazpar2_event_loop()
931 event_loop(&channel_list);
934 static struct record_metadata *record_metadata_init(
935 NMEM nmem, char *value, enum conf_metadata_type type)
937 struct record_metadata *rec_md = record_metadata_create(nmem);
938 if (type == Metadata_type_generic)
941 p = normalize7bit_generic(p, " ,/.:([");
943 rec_md->data.text.disp = nmem_strdup(nmem, p);
944 rec_md->data.text.sort = 0;
946 else if (type == Metadata_type_year || type == Metadata_type_date)
951 if (type == Metadata_type_date)
953 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
956 rec_md->data.number.min = first;
957 rec_md->data.number.max = last;
964 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
965 struct conf_service *service, NMEM nmem)
967 char *mergekey_norm = 0;
968 xmlNode *root = xmlDocGetRootElement(doc);
969 WRBUF norm_wr = wrbuf_alloc();
972 /* create mergekey based on mergekey attribute from XSL (if any) */
973 xmlChar *mergekey = xmlGetProp(root, (xmlChar *) "mergekey");
976 const char *norm_str;
977 pp2_relevance_token_t prt =
978 pp2_relevance_tokenize(
979 service->mergekey_pct,
980 (const char *) mergekey);
982 while ((norm_str = pp2_relevance_token_next(prt)))
986 if (wrbuf_len(norm_wr))
987 wrbuf_puts(norm_wr, " ");
988 wrbuf_puts(norm_wr, norm_str);
991 pp2_relevance_token_destroy(prt);
994 /* append (if any) mergekey=yes metadata values */
995 for (n = root->children; n; n = n->next)
997 if (n->type != XML_ELEMENT_NODE)
999 if (!strcmp((const char *) n->name, "metadata"))
1001 struct conf_metadata *ser_md = 0;
1002 int md_field_id = -1;
1004 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1010 = conf_service_metadata_field_id(service,
1011 (const char *) type);
1012 if (md_field_id >= 0)
1014 ser_md = &service->metadata[md_field_id];
1015 if (ser_md->mergekey == Metadata_mergekey_yes)
1017 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
1020 const char *norm_str;
1021 pp2_relevance_token_t prt =
1022 pp2_relevance_tokenize(
1023 service->mergekey_pct,
1024 (const char *) value);
1026 while ((norm_str = pp2_relevance_token_next(prt)))
1030 if (wrbuf_len(norm_wr))
1031 wrbuf_puts(norm_wr, " ");
1032 wrbuf_puts(norm_wr, norm_str);
1036 pp2_relevance_token_destroy(prt);
1044 /* generate unique key if none is not generated already or is empty */
1045 if (wrbuf_len(norm_wr) == 0)
1047 wrbuf_printf(norm_wr, "%s-%d",
1048 client_get_database(cl)->database->url, record_no);
1050 if (wrbuf_len(norm_wr) > 0)
1051 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1052 wrbuf_destroy(norm_wr);
1053 return mergekey_norm;
1058 /** \brief ingest XML record
1059 \param cl client holds the result set for record
1060 \param rec record buffer (0 terminated)
1061 \param record_no record position (1, 2, ..)
1062 \returns resulting record or NULL on failure
1064 struct record *ingest_record(struct client *cl, const char *rec,
1067 xmlDoc *xdoc = normalize_record(client_get_database(cl),
1068 client_get_session(cl), rec);
1070 struct record *record;
1071 struct record_cluster *cluster;
1072 struct session *se = client_get_session(cl);
1073 const char *mergekey_norm;
1076 struct conf_service *service = se->service;
1081 root = xmlDocGetRootElement(xdoc);
1083 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, se->nmem);
1086 yaz_log(YLOG_WARN, "Got no mergekey");
1090 record = record_create(se->nmem,
1091 service->num_metadata, service->num_sortkeys, cl,
1094 cluster = reclist_insert(se->reclist,
1096 record, (char *) mergekey_norm,
1098 if (global_parameters.dump_records)
1099 yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1100 client_get_database(cl)->database->url, record_no);
1103 /* no room for record */
1107 relevance_newrec(se->relevance, cluster);
1109 // now parsing XML record and adding data to cluster or record metadata
1110 for (n = root->children; n; n = n->next)
1112 pp2_relevance_token_t prt;
1119 if (n->type != XML_ELEMENT_NODE)
1121 if (!strcmp((const char *) n->name, "metadata"))
1123 struct conf_metadata *ser_md = 0;
1124 struct conf_sortkey *ser_sk = 0;
1125 struct record_metadata **wheretoput = 0;
1126 struct record_metadata *rec_md = 0;
1127 int md_field_id = -1;
1128 int sk_field_id = -1;
1130 type = xmlGetProp(n, (xmlChar *) "type");
1131 value = xmlNodeListGetString(xdoc, n->children, 1);
1133 if (!type || !value || !*value)
1137 = conf_service_metadata_field_id(service, (const char *) type);
1138 if (md_field_id < 0)
1140 if (se->number_of_warnings_unknown_metadata == 0)
1143 "Ignoring unknown metadata element: %s", type);
1145 se->number_of_warnings_unknown_metadata++;
1149 ser_md = &service->metadata[md_field_id];
1151 if (ser_md->sortkey_offset >= 0){
1152 sk_field_id = ser_md->sortkey_offset;
1153 ser_sk = &service->sortkeys[sk_field_id];
1156 // non-merged metadata
1157 rec_md = record_metadata_init(se->nmem, (char *) value,
1161 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1165 wheretoput = &record->metadata[md_field_id];
1167 wheretoput = &(*wheretoput)->next;
1168 *wheretoput = rec_md;
1171 rec_md = record_metadata_init(se->nmem, (char *) value,
1173 wheretoput = &cluster->metadata[md_field_id];
1175 // and polulate with data:
1176 // assign cluster or record based on merge action
1177 if (ser_md->merge == Metadata_merge_unique)
1179 struct record_metadata *mnode;
1180 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1181 if (!strcmp((const char *) mnode->data.text.disp,
1182 rec_md->data.text.disp))
1186 rec_md->next = *wheretoput;
1187 *wheretoput = rec_md;
1190 else if (ser_md->merge == Metadata_merge_longest)
1193 || strlen(rec_md->data.text.disp)
1194 > strlen((*wheretoput)->data.text.disp))
1196 *wheretoput = rec_md;
1199 const char *sort_str = 0;
1201 ser_sk->type == Metadata_sortkey_skiparticle;
1203 if (!cluster->sortkeys[sk_field_id])
1204 cluster->sortkeys[sk_field_id] =
1205 nmem_malloc(se->nmem,
1206 sizeof(union data_types));
1208 prt = pp2_relevance_tokenize(
1210 rec_md->data.text.disp);
1212 pp2_relevance_token_next(prt);
1214 sort_str = pp2_get_sort(prt, skip_article);
1216 cluster->sortkeys[sk_field_id]->text.disp =
1217 rec_md->data.text.disp;
1220 sort_str = rec_md->data.text.disp;
1222 "Could not make sortkey. Bug #1858");
1224 cluster->sortkeys[sk_field_id]->text.sort =
1225 nmem_strdup(se->nmem, sort_str);
1227 yaz_log(YLOG_LOG, "text disp=%s",
1228 cluster->sortkeys[sk_field_id]->text.disp);
1229 yaz_log(YLOG_LOG, "text sort=%s",
1230 cluster->sortkeys[sk_field_id]->text.sort);
1232 pp2_relevance_token_destroy(prt);
1236 else if (ser_md->merge == Metadata_merge_all)
1238 rec_md->next = *wheretoput;
1239 *wheretoput = rec_md;
1241 else if (ser_md->merge == Metadata_merge_range)
1245 *wheretoput = rec_md;
1247 cluster->sortkeys[sk_field_id]
1252 int this_min = rec_md->data.number.min;
1253 int this_max = rec_md->data.number.max;
1254 if (this_min < (*wheretoput)->data.number.min)
1255 (*wheretoput)->data.number.min = this_min;
1256 if (this_max > (*wheretoput)->data.number.max)
1257 (*wheretoput)->data.number.max = this_max;
1262 // ranking of _all_ fields enabled ...
1264 relevance_countwords(se->relevance, cluster,
1265 (char *) value, ser_md->rank);
1267 // construct facets ...
1268 if (ser_md->termlist)
1270 if (ser_md->type == Metadata_type_year)
1273 sprintf(year, "%d", rec_md->data.number.max);
1274 add_facet(se, (char *) type, year);
1275 if (rec_md->data.number.max != rec_md->data.number.min)
1277 sprintf(year, "%d", rec_md->data.number.min);
1278 add_facet(se, (char *) type, year);
1282 add_facet(se, (char *) type, (char *) value);
1292 if (se->number_of_warnings_unknown_elements == 0)
1294 "Unexpected element in internal record: %s", n->name);
1295 se->number_of_warnings_unknown_elements++;
1305 relevance_donerecord(se->relevance, cluster);
1306 se->total_records++;
1316 * c-file-style: "Stroustrup"
1317 * indent-tabs-mode: nil
1319 * vim: shiftwidth=4 tabstop=8 expandtab