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 =
85 30, // operations timeout
89 60, // session timeout
94 180, // Z39.50 session timeout
98 static void log_xml_doc(xmlDoc *doc)
100 FILE *lf = yaz_log_file();
103 #if LIBXML_VERSION >= 20600
104 xmlDocDumpFormatMemory(doc, &result, &len, 1);
106 xmlDocDumpMemory(doc, &result, &len);
110 fwrite(result, 1, len, lf);
116 // Recursively traverse query structure to extract terms.
117 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
129 pull_terms(nmem, n->u.p[0], termlist, num);
130 pull_terms(nmem, n->u.p[1], termlist, num);
133 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
134 for (i = 0; i < numwords; i++)
135 termlist[(*num)++] = words[i];
144 static void add_facet(struct session *s, const char *type, const char *value)
150 for (i = 0; i < s->num_termlists; i++)
151 if (!strcmp(s->termlists[i].name, type))
153 if (i == s->num_termlists)
155 if (i == SESSION_MAX_TERMLISTS)
157 yaz_log(YLOG_FATAL, "Too many termlists");
161 s->termlists[i].name = nmem_strdup(s->nmem, type);
162 s->termlists[i].termlist
163 = termlist_create(s->nmem, s->expected_maxrecs,
164 TERMLIST_HIGH_SCORE);
165 s->num_termlists = i + 1;
167 termlist_insert(s->termlists[i].termlist, value);
170 xmlDoc *record_to_xml(struct session_database *sdb, const char *rec)
172 struct database *db = sdb->database;
175 rdoc = xmlParseMemory(rec, strlen(rec));
179 yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s",
184 if (global_parameters.dump_records)
186 yaz_log(YLOG_LOG, "Un-normalized record from %s", db->url);
193 #define MAX_XSLT_ARGS 16
195 // Add static values from session database settings if applicable
196 static void insert_settings_parameters(struct session_database *sdb,
197 struct session *se, char **parms)
199 struct conf_service *service = se->service;
204 for (i = 0; i < service->num_metadata; i++)
206 struct conf_metadata *md = &service->metadata[i];
209 if (md->setting == Metadata_setting_parameter &&
210 (setting = settings_offset(service, md->name)) > 0)
212 const char *val = session_setting_oneval(sdb, setting);
213 if (val && nparms < MAX_XSLT_ARGS)
216 int len = strlen(val);
217 buf = nmem_malloc(se->nmem, len + 3);
219 strcpy(buf + 1, val);
222 parms[offset++] = md->name;
223 parms[offset++] = buf;
231 // Add static values from session database settings if applicable
232 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
233 struct conf_service *service)
237 for (i = 0; i < service->num_metadata; i++)
239 struct conf_metadata *md = &service->metadata[i];
242 if (md->setting == Metadata_setting_postproc &&
243 (offset = settings_offset(service, md->name)) > 0)
245 const char *val = session_setting_oneval(sdb, offset);
248 xmlNode *r = xmlDocGetRootElement(doc);
249 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
251 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
257 xmlDoc *normalize_record(struct session_database *sdb, struct session *se,
260 struct database_retrievalmap *m;
261 xmlDoc *rdoc = record_to_xml(sdb, rec);
264 for (m = sdb->map; m; m = m->next)
270 char *parms[MAX_XSLT_ARGS*2+1];
272 insert_settings_parameters(sdb, se, parms);
274 new = xsltApplyStylesheet(m->stylesheet, rdoc, (const char **) parms);
275 root= xmlDocGetRootElement(new);
276 if (!new || !root || !(root->children))
278 yaz_log(YLOG_WARN, "XSLT transformation failed from %s",
290 insert_settings_values(sdb, rdoc, se->service);
292 if (global_parameters.dump_records)
294 yaz_log(YLOG_LOG, "Normalized record from %s",
302 // Retrieve first defined value for 'name' for given database.
303 // Will be extended to take into account user associated with session
304 const char *session_setting_oneval(struct session_database *db, int offset)
306 if (!db->settings[offset])
308 return db->settings[offset]->value;
311 // Prepare XSLT stylesheets for record normalization
312 // Structures are allocated on the session_wide nmem to avoid having
313 // to recompute this for every search. This would lead
314 // to leaking if a single session was to repeatedly change the PZ_XSLT
315 // setting. However, this is not a realistic use scenario.
316 static int prepare_map(struct session *se, struct session_database *sdb)
322 yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url);
325 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
328 struct database_retrievalmap **m = &sdb->map;
330 char auto_stylesheet[256];
332 if (!strcmp(s, "auto"))
334 const char *request_syntax = session_setting_oneval(
335 sdb, PZ_REQUESTSYNTAX);
339 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
340 "%s.xsl", request_syntax);
341 for (cp = auto_stylesheet; *cp; cp++)
343 /* deliberately only consider ASCII */
344 if (*cp > 32 && *cp < 127)
351 yaz_log(YLOG_WARN, "No pz:requestsyntax for auto stylesheet");
354 nmem_strsplit(se->session_nmem, ",", s, &stylesheets, &num);
355 for (i = 0; i < num; i++)
357 (*m) = nmem_malloc(se->session_nmem, sizeof(**m));
359 if (!((*m)->stylesheet = conf_load_stylesheet(stylesheets[i])))
361 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
369 yaz_log(YLOG_WARN, "No Normalization stylesheet for target %s",
374 // This analyzes settings and recomputes any supporting data structures
376 static int prepare_session_database(struct session *se,
377 struct session_database *sdb)
382 "No settings associated with %s", sdb->database->url);
385 if (sdb->settings[PZ_XSLT] && !sdb->map)
387 if (prepare_map(se, sdb) < 0)
393 // called if watch should be removed because http_channel is to be destroyed
394 static void session_watch_cancel(void *data, struct http_channel *c,
397 struct session_watchentry *ent = data;
404 // set watch. Returns 0=OK, -1 if watch is already set
405 int session_set_watch(struct session *s, int what,
406 session_watchfun fun, void *data,
407 struct http_channel *chan)
409 if (s->watchlist[what].fun)
411 s->watchlist[what].fun = fun;
412 s->watchlist[what].data = data;
413 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
414 session_watch_cancel);
418 void session_alert_watch(struct session *s, int what)
420 if (s->watchlist[what].fun)
422 /* our watch is no longer associated with http_channel */
424 session_watchfun fun;
426 http_remove_observer(s->watchlist[what].obs);
427 fun = s->watchlist[what].fun;
428 data = s->watchlist[what].data;
430 /* reset watch before fun is invoked - in case fun wants to set
432 s->watchlist[what].fun = 0;
433 s->watchlist[what].data = 0;
434 s->watchlist[what].obs = 0;
440 //callback for grep_databases
441 static void select_targets_callback(void *context, struct session_database *db)
443 struct session *se = (struct session*) context;
444 struct client *cl = client_create();
445 client_set_database(cl, db);
446 client_set_session(cl, se);
449 // Associates a set of clients with a session;
450 // Note: Session-databases represent databases with per-session
452 int select_targets(struct session *se, struct database_criterion *crit)
455 client_destroy(se->clients);
457 return session_grep_databases(se, crit, select_targets_callback);
460 int session_active_clients(struct session *s)
465 for (c = s->clients; c; c = client_next_in_session(c))
466 if (client_is_active(c))
472 // parses crit1=val1,crit2=val2|val3,...
473 static struct database_criterion *parse_filter(NMEM m, const char *buf)
475 struct database_criterion *res = 0;
482 nmem_strsplit(m, ",", buf, &values, &num);
483 for (i = 0; i < num; i++)
488 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
489 char *eq = strchr(values[i], '=');
492 yaz_log(YLOG_WARN, "Missing equal-sign in filter");
496 new->name = values[i];
497 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
499 for (subi = 0; subi < subnum; subi++)
501 struct database_criterion_value *newv
502 = nmem_malloc(m, sizeof(*newv));
503 newv->value = subvalues[subi];
504 newv->next = new->values;
513 enum pazpar2_error_code search(struct session *se,
514 char *query, char *filter,
515 const char **addinfo)
517 int live_channels = 0;
521 struct database_criterion *criteria;
523 yaz_log(YLOG_DEBUG, "Search");
526 nmem_reset(se->nmem);
528 se->total_records = se->total_hits = se->total_merged = 0;
530 se->num_termlists = 0;
531 criteria = parse_filter(se->nmem, filter);
532 live_channels = select_targets(se, criteria);
535 int maxrecs = live_channels * global_parameters.toget;
536 se->reclist = reclist_create(se->nmem, maxrecs);
537 se->expected_maxrecs = maxrecs;
540 return PAZPAR2_NO_TARGETS;
542 for (cl = se->clients; cl; cl = client_next_in_session(cl))
544 if (prepare_session_database(se, client_get_database(cl)) < 0)
546 *addinfo = client_get_database(cl)->database->url;
547 return PAZPAR2_CONFIG_TARGET;
549 // Parse query for target
550 if (client_parse_query(cl, query) < 0)
555 if (client_prep_connection(cl))
556 client_start_search(cl);
560 // If no queries could be mapped, we signal an error
564 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
566 return PAZPAR2_NO_ERROR;
569 // Creates a new session_database object for a database
570 static void session_init_databases_fun(void *context, struct database *db)
572 struct session *se = (struct session *) context;
573 struct conf_service *service = se->service;
574 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
575 int num = settings_num(service);
582 = nmem_malloc(se->session_nmem, sizeof(struct settings *) * num);
583 memset(new->settings, 0, sizeof(struct settings*) * num);
587 for (i = 0; i < num; i++)
588 new->settings[i] = db->settings[i];
590 new->next = se->databases;
594 // Doesn't free memory associated with sdb -- nmem takes care of that
595 static void session_database_destroy(struct session_database *sdb)
597 struct database_retrievalmap *m;
599 for (m = sdb->map; m; m = m->next)
600 xsltFreeStylesheet(m->stylesheet);
603 // Initialize session_database list -- this represents this session's view
604 // of the database list -- subject to modification by the settings ws command
605 void session_init_databases(struct session *se)
608 predef_grep_databases(se, se->service, 0, session_init_databases_fun);
611 // Probably session_init_databases_fun should be refactored instead of
613 static struct session_database *load_session_database(struct session *se,
616 struct database *db = find_database(id, 0, se->service);
618 session_init_databases_fun((void*) se, db);
619 // New sdb is head of se->databases list
620 return se->databases;
623 // Find an existing session database. If not found, load it
624 static struct session_database *find_session_database(struct session *se,
627 struct session_database *sdb;
629 for (sdb = se->databases; sdb; sdb = sdb->next)
630 if (!strcmp(sdb->database->url, id))
632 return load_session_database(se, id);
635 // Apply a session override to a database
636 void session_apply_setting(struct session *se, char *dbname, char *setting,
639 struct session_database *sdb = find_session_database(se, dbname);
640 struct conf_service *service = se->service;
641 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
642 int offset = settings_offset_cprefix(service, setting);
646 yaz_log(YLOG_WARN, "Unknown setting %s", setting);
649 // Jakub: This breaks the filter setting.
650 /*if (offset == PZ_ID)
652 yaz_log(YLOG_WARN, "No need to set pz:id setting. Ignoring");
656 new->target = dbname;
659 new->next = sdb->settings[offset];
660 sdb->settings[offset] = new;
662 // Force later recompute of settings-driven data structures
663 // (happens when a search starts and client connections are prepared)
669 struct database_retrievalmap *m;
670 // We don't worry about the map structure -- it's in nmem
671 for (m = sdb->map; m; m = m->next)
672 xsltFreeStylesheet(m->stylesheet);
679 void destroy_session(struct session *s)
681 struct session_database *sdb;
684 client_destroy(s->clients);
685 for (sdb = s->databases; sdb; sdb = sdb->next)
686 session_database_destroy(sdb);
687 nmem_destroy(s->nmem);
688 wrbuf_destroy(s->wrbuf);
691 struct session *new_session(NMEM nmem, struct conf_service *service)
694 struct session *session = nmem_malloc(nmem, sizeof(*session));
696 yaz_log(YLOG_DEBUG, "New Pazpar2 session");
698 session->service = service;
699 session->relevance = 0;
700 session->total_hits = 0;
701 session->total_records = 0;
702 session->number_of_warnings_unknown_elements = 0;
703 session->number_of_warnings_unknown_metadata = 0;
704 session->num_termlists = 0;
705 session->reclist = 0;
706 session->clients = 0;
707 session->expected_maxrecs = 0;
708 session->session_nmem = nmem;
709 session->nmem = nmem_create();
710 session->wrbuf = wrbuf_alloc();
711 session->databases = 0;
712 for (i = 0; i <= SESSION_WATCH_MAX; i++)
714 session->watchlist[i].data = 0;
715 session->watchlist[i].fun = 0;
720 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
722 struct hitsbytarget *res = 0;
726 for (cl = se->clients; cl; cl = client_next_in_session(cl))
729 res = nmem_malloc(nmem, sizeof(*res) * sz);
731 for (cl = se->clients; cl; cl = client_next_in_session(cl))
733 const char *name = session_setting_oneval(client_get_database(cl),
736 res[*count].id = client_get_database(cl)->database->url;
737 res[*count].name = *name ? name : "Unknown";
738 res[*count].hits = client_get_hits(cl);
739 res[*count].records = client_get_num_records(cl);
740 res[*count].diagnostic = client_get_diagnostic(cl);
741 res[*count].state = client_get_state_str(cl);
742 res[*count].connected = client_get_connection(cl) ? 1 : 0;
748 struct termlist_score **termlist(struct session *s, const char *name, int *num)
752 for (i = 0; i < s->num_termlists; i++)
753 if (!strcmp((const char *) s->termlists[i].name, name))
754 return termlist_highscore(s->termlists[i].termlist, num);
758 #ifdef MISSING_HEADERS
759 void report_nmem_stats(void)
761 size_t in_use, is_free;
763 nmem_get_memory_in_use(&in_use);
764 nmem_get_memory_free(&is_free);
766 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
767 (long) in_use, (long) is_free);
771 struct record_cluster *show_single(struct session *s, const char *id,
772 struct record_cluster **prev_r,
773 struct record_cluster **next_r)
775 struct record_cluster *r;
777 reclist_rewind(s->reclist);
780 while ((r = reclist_read_record(s->reclist)))
782 if (!strcmp(r->recid, id))
784 *next_r = reclist_read_record(s->reclist);
792 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp,
793 int start, int *num, int *total, int *sumhits,
796 struct record_cluster **recs = nmem_malloc(nmem_show, *num
797 * sizeof(struct record_cluster *));
798 struct reclist_sortparms *spp;
801 yaz_timing_t t = yaz_timing_create();
813 for (spp = sp; spp; spp = spp->next)
814 if (spp->type == Metadata_sortkey_relevance)
816 relevance_prepare_read(s->relevance, s->reclist);
819 reclist_sort(s->reclist, sp);
821 *total = s->reclist->num_records;
822 *sumhits = s->total_hits;
824 for (i = 0; i < start; i++)
825 if (!reclist_read_record(s->reclist))
832 for (i = 0; i < *num; i++)
834 struct record_cluster *r = reclist_read_record(s->reclist);
845 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
846 yaz_timing_get_real(t), yaz_timing_get_user(t),
847 yaz_timing_get_sys(t));
848 yaz_timing_destroy(&t);
853 void statistics(struct session *se, struct statistics *stat)
858 memset(stat, 0, sizeof(*stat));
859 for (cl = se->clients; cl; cl = client_next_in_session(cl))
861 if (!client_get_connection(cl))
862 stat->num_no_connection++;
863 switch (client_get_state(cl))
865 case Client_Connecting: stat->num_connecting++; break;
866 case Client_Working: stat->num_working++; break;
867 case Client_Idle: stat->num_idle++; break;
868 case Client_Failed: stat->num_failed++; break;
869 case Client_Error: stat->num_error++; break;
874 stat->num_hits = se->total_hits;
875 stat->num_records = se->total_records;
877 stat->num_clients = count;
880 int start_http_listener(void)
883 struct conf_server *ser = global_parameters.server;
885 if (*global_parameters.listener_override)
886 strcpy(hp, global_parameters.listener_override);
889 strcpy(hp, ser->host ? ser->host : "");
894 sprintf(hp + strlen(hp), "%d", ser->port);
897 return http_init(hp);
900 void start_proxy(void)
903 struct conf_server *ser = global_parameters.server;
905 if (*global_parameters.proxy_override)
906 strcpy(hp, global_parameters.proxy_override);
907 else if (ser->proxy_host || ser->proxy_port)
909 strcpy(hp, ser->proxy_host ? ser->proxy_host : "");
914 sprintf(hp + strlen(hp), "%d", ser->proxy_port);
920 http_set_proxyaddr(hp, ser->myurl ? ser->myurl : "");
924 // Master list of connections we're handling events to
925 static IOCHAN channel_list = 0;
926 void pazpar2_add_channel(IOCHAN chan)
928 chan->next = channel_list;
932 void pazpar2_event_loop()
934 event_loop(&channel_list);
937 static struct record_metadata *record_metadata_init(
938 NMEM nmem, char *value, enum conf_metadata_type type)
940 struct record_metadata *rec_md = record_metadata_create(nmem);
941 if (type == Metadata_type_generic)
944 p = normalize7bit_generic(p, " ,/.:([");
946 rec_md->data.text.disp = nmem_strdup(nmem, p);
947 rec_md->data.text.sort = 0;
949 else if (type == Metadata_type_year || type == Metadata_type_date)
954 if (type == Metadata_type_date)
956 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
959 rec_md->data.number.min = first;
960 rec_md->data.number.max = last;
967 const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
968 struct conf_service *service, NMEM nmem)
970 char *mergekey_norm = 0;
971 xmlNode *root = xmlDocGetRootElement(doc);
972 WRBUF norm_wr = wrbuf_alloc();
975 /* create mergekey based on mergekey attribute from XSL (if any) */
976 xmlChar *mergekey = xmlGetProp(root, (xmlChar *) "mergekey");
979 const char *norm_str;
980 pp2_relevance_token_t prt =
981 pp2_relevance_tokenize(
982 global_parameters.server->mergekey_pct,
983 (const char *) mergekey);
985 while ((norm_str = pp2_relevance_token_next(prt)))
989 if (wrbuf_len(norm_wr))
990 wrbuf_puts(norm_wr, " ");
991 wrbuf_puts(norm_wr, norm_str);
994 pp2_relevance_token_destroy(prt);
997 /* append (if any) mergekey=yes metadata values */
998 for (n = root->children; n; n = n->next)
1000 if (n->type != XML_ELEMENT_NODE)
1002 if (!strcmp((const char *) n->name, "metadata"))
1004 struct conf_metadata *ser_md = 0;
1005 int md_field_id = -1;
1007 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1013 = conf_service_metadata_field_id(service,
1014 (const char *) type);
1015 if (md_field_id >= 0)
1017 ser_md = &service->metadata[md_field_id];
1018 if (ser_md->mergekey == Metadata_mergekey_yes)
1020 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
1023 const char *norm_str;
1024 pp2_relevance_token_t prt =
1025 pp2_relevance_tokenize(
1026 global_parameters.server->mergekey_pct,
1027 (const char *) value);
1029 while ((norm_str = pp2_relevance_token_next(prt)))
1033 if (wrbuf_len(norm_wr))
1034 wrbuf_puts(norm_wr, " ");
1035 wrbuf_puts(norm_wr, norm_str);
1039 pp2_relevance_token_destroy(prt);
1047 /* generate unique key if none is not generated already or is empty */
1048 if (wrbuf_len(norm_wr) == 0)
1050 wrbuf_printf(norm_wr, "%s-%d",
1051 client_get_database(cl)->database->url, record_no);
1053 if (wrbuf_len(norm_wr) > 0)
1054 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1055 wrbuf_destroy(norm_wr);
1056 return mergekey_norm;
1061 /** \brief ingest XML record
1062 \param cl client holds the result set for record
1063 \param rec record buffer (0 terminated)
1064 \param record_no record position (1, 2, ..)
1065 \returns resulting record or NULL on failure
1067 struct record *ingest_record(struct client *cl, const char *rec,
1070 xmlDoc *xdoc = normalize_record(client_get_database(cl),
1071 client_get_session(cl), rec);
1073 struct record *record;
1074 struct record_cluster *cluster;
1075 struct session *se = client_get_session(cl);
1076 const char *mergekey_norm;
1079 struct conf_service *service = se->service;
1084 root = xmlDocGetRootElement(xdoc);
1086 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, se->nmem);
1089 yaz_log(YLOG_WARN, "Got no mergekey");
1093 record = record_create(se->nmem,
1094 service->num_metadata, service->num_sortkeys, cl,
1097 cluster = reclist_insert(se->reclist,
1099 record, (char *) mergekey_norm,
1101 if (global_parameters.dump_records)
1102 yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1103 client_get_database(cl)->database->url, record_no);
1106 /* no room for record */
1110 relevance_newrec(se->relevance, cluster);
1112 // now parsing XML record and adding data to cluster or record metadata
1113 for (n = root->children; n; n = n->next)
1115 pp2_relevance_token_t prt;
1122 if (n->type != XML_ELEMENT_NODE)
1124 if (!strcmp((const char *) n->name, "metadata"))
1126 struct conf_metadata *ser_md = 0;
1127 struct conf_sortkey *ser_sk = 0;
1128 struct record_metadata **wheretoput = 0;
1129 struct record_metadata *rec_md = 0;
1130 int md_field_id = -1;
1131 int sk_field_id = -1;
1133 type = xmlGetProp(n, (xmlChar *) "type");
1134 value = xmlNodeListGetString(xdoc, n->children, 1);
1136 if (!type || !value || !*value)
1140 = conf_service_metadata_field_id(service, (const char *) type);
1141 if (md_field_id < 0)
1143 if (se->number_of_warnings_unknown_metadata == 0)
1146 "Ignoring unknown metadata element: %s", type);
1148 se->number_of_warnings_unknown_metadata++;
1152 ser_md = &service->metadata[md_field_id];
1154 if (ser_md->sortkey_offset >= 0){
1155 sk_field_id = ser_md->sortkey_offset;
1156 ser_sk = &service->sortkeys[sk_field_id];
1159 // non-merged metadata
1160 rec_md = record_metadata_init(se->nmem, (char *) value,
1164 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1168 wheretoput = &record->metadata[md_field_id];
1170 wheretoput = &(*wheretoput)->next;
1171 *wheretoput = rec_md;
1174 rec_md = record_metadata_init(se->nmem, (char *) value,
1176 wheretoput = &cluster->metadata[md_field_id];
1178 // and polulate with data:
1179 // assign cluster or record based on merge action
1180 if (ser_md->merge == Metadata_merge_unique)
1182 struct record_metadata *mnode;
1183 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1184 if (!strcmp((const char *) mnode->data.text.disp,
1185 rec_md->data.text.disp))
1189 rec_md->next = *wheretoput;
1190 *wheretoput = rec_md;
1193 else if (ser_md->merge == Metadata_merge_longest)
1196 || strlen(rec_md->data.text.disp)
1197 > strlen((*wheretoput)->data.text.disp))
1199 *wheretoput = rec_md;
1202 const char *sort_str = 0;
1204 ser_sk->type == Metadata_sortkey_skiparticle;
1206 if (!cluster->sortkeys[sk_field_id])
1207 cluster->sortkeys[sk_field_id] =
1208 nmem_malloc(se->nmem,
1209 sizeof(union data_types));
1211 prt = pp2_relevance_tokenize(
1212 global_parameters.server->sort_pct,
1213 rec_md->data.text.disp);
1215 pp2_relevance_token_next(prt);
1217 sort_str = pp2_get_sort(prt, skip_article);
1219 cluster->sortkeys[sk_field_id]->text.disp =
1220 rec_md->data.text.disp;
1223 sort_str = rec_md->data.text.disp;
1225 "Could not make sortkey. Bug #1858");
1227 cluster->sortkeys[sk_field_id]->text.sort =
1228 nmem_strdup(se->nmem, sort_str);
1230 yaz_log(YLOG_LOG, "text disp=%s",
1231 cluster->sortkeys[sk_field_id]->text.disp);
1232 yaz_log(YLOG_LOG, "text sort=%s",
1233 cluster->sortkeys[sk_field_id]->text.sort);
1235 pp2_relevance_token_destroy(prt);
1239 else if (ser_md->merge == Metadata_merge_all)
1241 rec_md->next = *wheretoput;
1242 *wheretoput = rec_md;
1244 else if (ser_md->merge == Metadata_merge_range)
1248 *wheretoput = rec_md;
1250 cluster->sortkeys[sk_field_id]
1255 int this_min = rec_md->data.number.min;
1256 int this_max = rec_md->data.number.max;
1257 if (this_min < (*wheretoput)->data.number.min)
1258 (*wheretoput)->data.number.min = this_min;
1259 if (this_max > (*wheretoput)->data.number.max)
1260 (*wheretoput)->data.number.max = this_max;
1265 // ranking of _all_ fields enabled ...
1267 relevance_countwords(se->relevance, cluster,
1268 (char *) value, ser_md->rank);
1270 // construct facets ...
1271 if (ser_md->termlist)
1273 if (ser_md->type == Metadata_type_year)
1276 sprintf(year, "%d", rec_md->data.number.max);
1277 add_facet(se, (char *) type, year);
1278 if (rec_md->data.number.max != rec_md->data.number.min)
1280 sprintf(year, "%d", rec_md->data.number.min);
1281 add_facet(se, (char *) type, year);
1285 add_facet(se, (char *) type, (char *) value);
1295 if (se->number_of_warnings_unknown_elements == 0)
1297 "Unexpected element in internal record: %s", n->name);
1298 se->number_of_warnings_unknown_elements++;
1308 relevance_donerecord(se->relevance, cluster);
1309 se->total_records++;
1319 * c-file-style: "Stroustrup"
1320 * indent-tabs-mode: nil
1322 * vim: shiftwidth=4 tabstop=8 expandtab