1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2010 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 =
83 static void log_xml_doc(xmlDoc *doc)
85 FILE *lf = yaz_log_file();
88 #if LIBXML_VERSION >= 20600
89 xmlDocDumpFormatMemory(doc, &result, &len, 1);
91 xmlDocDumpMemory(doc, &result, &len);
95 (void) fwrite(result, 1, len, lf);
101 // Recursively traverse query structure to extract terms.
102 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
114 pull_terms(nmem, n->u.p[0], termlist, num);
115 pull_terms(nmem, n->u.p[1], termlist, num);
118 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
119 for (i = 0; i < numwords; i++)
120 termlist[(*num)++] = words[i];
129 static void add_facet(struct session *s, const char *type, const char *value)
135 for (i = 0; i < s->num_termlists; i++)
136 if (!strcmp(s->termlists[i].name, type))
138 if (i == s->num_termlists)
140 if (i == SESSION_MAX_TERMLISTS)
142 yaz_log(YLOG_FATAL, "Too many termlists");
146 s->termlists[i].name = nmem_strdup(s->nmem, type);
147 s->termlists[i].termlist
148 = termlist_create(s->nmem, TERMLIST_HIGH_SCORE);
149 s->num_termlists = i + 1;
151 termlist_insert(s->termlists[i].termlist, value);
154 static xmlDoc *record_to_xml(struct session_database *sdb, const char *rec)
156 struct database *db = sdb->database;
159 rdoc = xmlParseMemory(rec, strlen(rec));
163 yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s",
168 if (global_parameters.dump_records)
170 yaz_log(YLOG_LOG, "Un-normalized record from %s", db->url);
177 #define MAX_XSLT_ARGS 16
179 // Add static values from session database settings if applicable
180 static void insert_settings_parameters(struct session_database *sdb,
181 struct session *se, char **parms)
183 struct conf_service *service = se->service;
188 for (i = 0; i < service->num_metadata; i++)
190 struct conf_metadata *md = &service->metadata[i];
193 if (md->setting == Metadata_setting_parameter &&
194 (setting = settings_lookup_offset(service, md->name)) >= 0)
196 const char *val = session_setting_oneval(sdb, setting);
197 if (val && nparms < MAX_XSLT_ARGS)
200 int len = strlen(val);
201 buf = nmem_malloc(se->nmem, len + 3);
203 strcpy(buf + 1, val);
206 parms[offset++] = md->name;
207 parms[offset++] = buf;
215 // Add static values from session database settings if applicable
216 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
217 struct conf_service *service)
221 for (i = 0; i < service->num_metadata; i++)
223 struct conf_metadata *md = &service->metadata[i];
226 if (md->setting == Metadata_setting_postproc &&
227 (offset = settings_lookup_offset(service, md->name)) >= 0)
229 const char *val = session_setting_oneval(sdb, offset);
232 xmlNode *r = xmlDocGetRootElement(doc);
233 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
235 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
241 static xmlDoc *normalize_record(struct session_database *sdb,
245 xmlDoc *rdoc = record_to_xml(sdb, rec);
249 char *parms[MAX_XSLT_ARGS*2+1];
251 insert_settings_parameters(sdb, se, parms);
253 if (normalize_record_transform(sdb->map, &rdoc, (const char **)parms))
255 yaz_log(YLOG_WARN, "Normalize failed from %s", sdb->database->url);
259 insert_settings_values(sdb, rdoc, se->service);
261 if (global_parameters.dump_records)
263 yaz_log(YLOG_LOG, "Normalized record from %s",
272 void session_settings_dump(struct session *se,
273 struct session_database *db,
278 int i, num = db->num_settings;
279 for (i = 0; i < num; i++)
281 struct setting *s = db->settings[i];
282 for (;s ; s = s->next)
284 wrbuf_puts(w, "<set name=\"");
285 wrbuf_xmlputs(w, s->name);
286 wrbuf_puts(w, "\" value=\"");
287 wrbuf_xmlputs(w, s->value);
288 wrbuf_puts(w, "\"/>");
296 // Retrieve first defined value for 'name' for given database.
297 // Will be extended to take into account user associated with session
298 const char *session_setting_oneval(struct session_database *db, int offset)
300 if (!db->settings[offset])
302 return db->settings[offset]->value;
305 // Prepare XSLT stylesheets for record normalization
306 // Structures are allocated on the session_wide nmem to avoid having
307 // to recompute this for every search. This would lead
308 // to leaking if a single session was to repeatedly change the PZ_XSLT
309 // setting. However, this is not a realistic use scenario.
310 static int prepare_map(struct session *se, struct session_database *sdb)
316 yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url);
319 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
321 char auto_stylesheet[256];
323 if (!strcmp(s, "auto"))
325 const char *request_syntax = session_setting_oneval(
326 sdb, PZ_REQUESTSYNTAX);
330 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
331 "%s.xsl", request_syntax);
332 for (cp = auto_stylesheet; *cp; cp++)
334 /* deliberately only consider ASCII */
335 if (*cp > 32 && *cp < 127)
342 yaz_log(YLOG_WARN, "No pz:requestsyntax for auto stylesheet");
345 sdb->map = normalize_cache_get(se->normalize_cache,
353 // This analyzes settings and recomputes any supporting data structures
355 static int prepare_session_database(struct session *se,
356 struct session_database *sdb)
361 "No settings associated with %s", sdb->database->url);
364 if (sdb->settings[PZ_XSLT] && !sdb->map)
366 if (prepare_map(se, sdb) < 0)
372 // called if watch should be removed because http_channel is to be destroyed
373 static void session_watch_cancel(void *data, struct http_channel *c,
376 struct session_watchentry *ent = data;
383 // set watch. Returns 0=OK, -1 if watch is already set
384 int session_set_watch(struct session *s, int what,
385 session_watchfun fun, void *data,
386 struct http_channel *chan)
388 if (s->watchlist[what].fun)
390 s->watchlist[what].fun = fun;
391 s->watchlist[what].data = data;
392 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
393 session_watch_cancel);
397 void session_alert_watch(struct session *s, int what)
399 if (s->watchlist[what].fun)
401 /* our watch is no longer associated with http_channel */
403 session_watchfun fun;
405 http_remove_observer(s->watchlist[what].obs);
406 fun = s->watchlist[what].fun;
407 data = s->watchlist[what].data;
409 /* reset watch before fun is invoked - in case fun wants to set
411 s->watchlist[what].fun = 0;
412 s->watchlist[what].data = 0;
413 s->watchlist[what].obs = 0;
419 //callback for grep_databases
420 static void select_targets_callback(void *context, struct session_database *db)
422 struct session *se = (struct session*) context;
423 struct client *cl = client_create();
424 client_set_database(cl, db);
425 client_set_session(cl, se);
428 // Associates a set of clients with a session;
429 // Note: Session-databases represent databases with per-session
431 static int select_targets(struct session *se, const char *filter)
434 client_destroy(se->clients);
436 return session_grep_databases(se, filter, select_targets_callback);
439 int session_active_clients(struct session *s)
444 for (c = s->clients; c; c = client_next_in_session(c))
445 if (client_is_active(c))
452 enum pazpar2_error_code search(struct session *se,
454 const char *startrecs, const char *maxrecs,
456 const char **addinfo)
458 int live_channels = 0;
463 yaz_log(YLOG_DEBUG, "Search");
466 nmem_reset(se->nmem);
468 se->total_records = se->total_hits = se->total_merged = 0;
470 se->num_termlists = 0;
471 live_channels = select_targets(se, filter);
474 se->reclist = reclist_create(se->nmem);
477 return PAZPAR2_NO_TARGETS;
479 for (cl = se->clients; cl; cl = client_next_in_session(cl))
482 client_set_maxrecs(cl, atoi(maxrecs));
484 client_set_startrecs(cl, atoi(startrecs));
485 if (prepare_session_database(se, client_get_database(cl)) < 0)
487 // Parse query for target
488 if (client_parse_query(cl, query) < 0)
493 if (client_prep_connection(cl, se->service->z3950_operation_timeout,
494 se->service->z3950_session_timeout))
495 client_start_search(cl);
503 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
506 return PAZPAR2_NO_TARGETS;
508 return PAZPAR2_NO_ERROR;
511 // Creates a new session_database object for a database
512 static void session_init_databases_fun(void *context, struct database *db)
514 struct session *se = (struct session *) context;
515 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
521 assert(db->settings);
522 new->settings = nmem_malloc(se->session_nmem,
523 sizeof(struct settings *) * db->num_settings);
524 new->num_settings = db->num_settings;
525 for (i = 0; i < db->num_settings; i++)
526 new->settings[i] = db->settings[i];
527 new->next = se->databases;
531 // Doesn't free memory associated with sdb -- nmem takes care of that
532 static void session_database_destroy(struct session_database *sdb)
537 // Initialize session_database list -- this represents this session's view
538 // of the database list -- subject to modification by the settings ws command
539 void session_init_databases(struct session *se)
542 predef_grep_databases(se, se->service, session_init_databases_fun);
545 // Probably session_init_databases_fun should be refactored instead of
547 static struct session_database *load_session_database(struct session *se,
550 struct database *db = find_database(id, se->service);
552 resolve_database(db);
554 session_init_databases_fun((void*) se, db);
555 // New sdb is head of se->databases list
556 return se->databases;
559 // Find an existing session database. If not found, load it
560 static struct session_database *find_session_database(struct session *se,
563 struct session_database *sdb;
565 for (sdb = se->databases; sdb; sdb = sdb->next)
566 if (!strcmp(sdb->database->url, id))
568 return load_session_database(se, id);
571 // Apply a session override to a database
572 void session_apply_setting(struct session *se, char *dbname, char *setting,
575 struct session_database *sdb = find_session_database(se, dbname);
576 struct conf_service *service = se->service;
577 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
578 int offset = settings_create_offset(service, setting);
580 expand_settings_array(&sdb->settings, &sdb->num_settings, offset,
583 new->target = dbname;
586 new->next = sdb->settings[offset];
587 sdb->settings[offset] = new;
589 // Force later recompute of settings-driven data structures
590 // (happens when a search starts and client connections are prepared)
602 void destroy_session(struct session *s)
604 struct session_database *sdb;
607 client_destroy(s->clients);
608 for (sdb = s->databases; sdb; sdb = sdb->next)
609 session_database_destroy(sdb);
610 normalize_cache_destroy(s->normalize_cache);
611 nmem_destroy(s->nmem);
612 service_destroy(s->service);
613 wrbuf_destroy(s->wrbuf);
616 struct session *new_session(NMEM nmem, struct conf_service *service)
619 struct session *session = nmem_malloc(nmem, sizeof(*session));
621 yaz_log(YLOG_DEBUG, "New Pazpar2 session");
623 session->service = service;
624 session->relevance = 0;
625 session->total_hits = 0;
626 session->total_records = 0;
627 session->number_of_warnings_unknown_elements = 0;
628 session->number_of_warnings_unknown_metadata = 0;
629 session->num_termlists = 0;
630 session->reclist = 0;
631 session->clients = 0;
632 session->session_nmem = nmem;
633 session->nmem = nmem_create();
634 session->wrbuf = wrbuf_alloc();
635 session->databases = 0;
636 for (i = 0; i <= SESSION_WATCH_MAX; i++)
638 session->watchlist[i].data = 0;
639 session->watchlist[i].fun = 0;
641 session->normalize_cache = normalize_cache_create();
646 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
648 struct hitsbytarget *res = 0;
652 for (cl = se->clients; cl; cl = client_next_in_session(cl))
655 res = nmem_malloc(nmem, sizeof(*res) * sz);
657 for (cl = se->clients; cl; cl = client_next_in_session(cl))
659 WRBUF w = wrbuf_alloc();
660 const char *name = session_setting_oneval(client_get_database(cl),
663 res[*count].id = client_get_database(cl)->database->url;
664 res[*count].name = *name ? name : "Unknown";
665 res[*count].hits = client_get_hits(cl);
666 res[*count].records = client_get_num_records(cl);
667 res[*count].diagnostic = client_get_diagnostic(cl);
668 res[*count].state = client_get_state_str(cl);
669 res[*count].connected = client_get_connection(cl) ? 1 : 0;
670 session_settings_dump(se, client_get_database(cl), w);
671 res[*count].settings_xml = w;
677 struct termlist_score **termlist(struct session *s, const char *name, int *num)
681 for (i = 0; i < s->num_termlists; i++)
682 if (!strcmp((const char *) s->termlists[i].name, name))
683 return termlist_highscore(s->termlists[i].termlist, num);
687 #ifdef MISSING_HEADERS
688 void report_nmem_stats(void)
690 size_t in_use, is_free;
692 nmem_get_memory_in_use(&in_use);
693 nmem_get_memory_free(&is_free);
695 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
696 (long) in_use, (long) is_free);
700 struct record_cluster *show_single(struct session *s, const char *id,
701 struct record_cluster **prev_r,
702 struct record_cluster **next_r)
704 struct record_cluster *r;
706 reclist_rewind(s->reclist);
709 while ((r = reclist_read_record(s->reclist)))
711 if (!strcmp(r->recid, id))
713 *next_r = reclist_read_record(s->reclist);
721 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp,
722 int start, int *num, int *total, Odr_int *sumhits,
725 struct record_cluster **recs = nmem_malloc(nmem_show, *num
726 * sizeof(struct record_cluster *));
727 struct reclist_sortparms *spp;
730 yaz_timing_t t = yaz_timing_create();
742 for (spp = sp; spp; spp = spp->next)
743 if (spp->type == Metadata_sortkey_relevance)
745 relevance_prepare_read(s->relevance, s->reclist);
748 reclist_sort(s->reclist, sp);
750 *total = reclist_get_num_records(s->reclist);
751 *sumhits = s->total_hits;
753 for (i = 0; i < start; i++)
754 if (!reclist_read_record(s->reclist))
761 for (i = 0; i < *num; i++)
763 struct record_cluster *r = reclist_read_record(s->reclist);
774 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
775 yaz_timing_get_real(t), yaz_timing_get_user(t),
776 yaz_timing_get_sys(t));
777 yaz_timing_destroy(&t);
782 void statistics(struct session *se, struct statistics *stat)
787 memset(stat, 0, sizeof(*stat));
788 for (cl = se->clients; cl; cl = client_next_in_session(cl))
790 if (!client_get_connection(cl))
791 stat->num_no_connection++;
792 switch (client_get_state(cl))
794 case Client_Connecting: stat->num_connecting++; break;
795 case Client_Working: stat->num_working++; break;
796 case Client_Idle: stat->num_idle++; break;
797 case Client_Failed: stat->num_failed++; break;
798 case Client_Error: stat->num_error++; break;
803 stat->num_hits = se->total_hits;
804 stat->num_records = se->total_records;
806 stat->num_clients = count;
810 // Master list of connections we're handling events to
811 static iochan_man_t pazpar2_chan_man = 0; /* thread pr */
813 void pazpar2_chan_man_start(void)
815 pazpar2_chan_man = iochan_man_create(0 /* use threads */);
818 void pazpar2_add_channel(IOCHAN chan)
820 assert(pazpar2_chan_man);
821 iochan_add(pazpar2_chan_man, chan);
824 void pazpar2_event_loop()
826 assert(pazpar2_chan_man);
827 iochan_man_events(pazpar2_chan_man);
830 static struct record_metadata *record_metadata_init(
831 NMEM nmem, const char *value, enum conf_metadata_type type,
832 struct _xmlAttr *attr)
834 struct record_metadata *rec_md = record_metadata_create(nmem);
835 struct record_metadata_attr **attrp = &rec_md->attributes;
837 for (; attr; attr = attr->next)
839 if (attr->children && attr->children->content)
841 if (strcmp((const char *) attr->name, "type"))
842 { /* skip the "type" attribute.. Its value is already part of
843 the element in output (md-%s) and so repeating it here
845 *attrp = nmem_malloc(nmem, sizeof(**attrp));
847 nmem_strdup(nmem, (const char *) attr->name);
849 nmem_strdup(nmem, (const char *) attr->children->content);
850 attrp = &(*attrp)->next;
856 if (type == Metadata_type_generic)
858 char *p = nmem_strdup(nmem, value);
860 p = normalize7bit_generic(p, " ,/.:([");
862 rec_md->data.text.disp = p;
863 rec_md->data.text.sort = 0;
865 else if (type == Metadata_type_year || type == Metadata_type_date)
870 if (type == Metadata_type_date)
872 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
875 rec_md->data.number.min = first;
876 rec_md->data.number.max = last;
883 static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
884 struct conf_service *service, WRBUF norm_wr)
888 for (n = root->children; n; n = n->next)
890 if (n->type != XML_ELEMENT_NODE)
892 if (!strcmp((const char *) n->name, "metadata"))
894 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
895 if (!strcmp(name, (const char *) type))
897 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
900 const char *norm_str;
901 pp2_relevance_token_t prt =
902 pp2_relevance_tokenize(
903 service->mergekey_pct,
904 (const char *) value, 0);
906 if (wrbuf_len(norm_wr) > 0)
907 wrbuf_puts(norm_wr, " ");
908 wrbuf_puts(norm_wr, name);
910 pp2_relevance_token_next(prt)))
914 wrbuf_puts(norm_wr, " ");
915 wrbuf_puts(norm_wr, norm_str);
919 pp2_relevance_token_destroy(prt);
929 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
930 struct conf_service *service, NMEM nmem)
932 char *mergekey_norm = 0;
933 xmlNode *root = xmlDocGetRootElement(doc);
934 WRBUF norm_wr = wrbuf_alloc();
936 /* consider mergekey from XSL first */
937 xmlChar *mergekey = xmlGetProp(root, (xmlChar *) "mergekey");
940 const char *norm_str;
941 pp2_relevance_token_t prt =
942 pp2_relevance_tokenize(
943 service->mergekey_pct,
944 (const char *) mergekey, 0);
946 while ((norm_str = pp2_relevance_token_next(prt)))
950 if (wrbuf_len(norm_wr))
951 wrbuf_puts(norm_wr, " ");
952 wrbuf_puts(norm_wr, norm_str);
955 pp2_relevance_token_destroy(prt);
960 /* no mergekey defined in XSL. Look for mergekey metadata instead */
962 for (field_id = 0; field_id < service->num_metadata; field_id++)
964 struct conf_metadata *ser_md = &service->metadata[field_id];
965 if (ser_md->mergekey != Metadata_mergekey_no)
967 int r = get_mergekey_from_doc(doc, root, ser_md->name,
969 if (r == 0 && ser_md->mergekey == Metadata_mergekey_required)
971 /* no mergekey on this one and it is required..
972 Generate unique key instead */
973 wrbuf_rewind(norm_wr);
980 /* generate unique key if none is not generated already or is empty */
981 if (wrbuf_len(norm_wr) == 0)
983 wrbuf_printf(norm_wr, "%s-%d",
984 client_get_database(cl)->database->url, record_no);
986 if (wrbuf_len(norm_wr) > 0)
987 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
988 wrbuf_destroy(norm_wr);
989 return mergekey_norm;
992 /** \brief see if metadata for pz:recordfilter exists
993 \param root xml root element of normalized record
994 \param sdb session database for client
995 \retval 0 if there is no metadata for pz:recordfilter
996 \retval 1 if there is metadata for pz:recordfilter
998 If there is no pz:recordfilter defined, this function returns 1
1002 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1007 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1012 for (n = root->children; n; n = n->next)
1014 if (n->type != XML_ELEMENT_NODE)
1016 if (!strcmp((const char *) n->name, "metadata"))
1018 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1022 const char *eq = strchr(s, '~');
1027 if (len == strlen((const char *)type) &&
1028 !memcmp((const char *) type, s, len))
1030 xmlChar *value = xmlNodeGetContent(n);
1031 if (value && *value)
1033 if (!eq || strstr((const char *) value, eq+1))
1046 /** \brief ingest XML record
1047 \param cl client holds the result set for record
1048 \param rec record buffer (0 terminated)
1049 \param record_no record position (1, 2, ..)
1050 \returns resulting record or NULL on failure
1052 struct record *ingest_record(struct client *cl, const char *rec,
1055 struct session_database *sdb = client_get_database(cl);
1056 struct session *se = client_get_session(cl);
1057 xmlDoc *xdoc = normalize_record(sdb, se, rec);
1059 struct record *record;
1060 struct record_cluster *cluster;
1061 const char *mergekey_norm;
1064 struct conf_service *service = se->service;
1069 root = xmlDocGetRootElement(xdoc);
1071 if (!check_record_filter(root, sdb))
1073 yaz_log(YLOG_WARN, "Filtered out record no %d from %s", record_no,
1074 sdb->database->url);
1079 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, se->nmem);
1082 yaz_log(YLOG_WARN, "Got no mergekey");
1086 record = record_create(se->nmem,
1087 service->num_metadata, service->num_sortkeys, cl,
1090 cluster = reclist_insert(se->reclist,
1092 record, (char *) mergekey_norm,
1096 /* no room for record */
1100 if (global_parameters.dump_records)
1101 yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1102 sdb->database->url, record_no);
1103 relevance_newrec(se->relevance, cluster);
1105 // now parsing XML record and adding data to cluster or record metadata
1106 for (n = root->children; n; n = n->next)
1108 pp2_relevance_token_t prt;
1115 if (n->type != XML_ELEMENT_NODE)
1117 if (!strcmp((const char *) n->name, "metadata"))
1119 struct conf_metadata *ser_md = 0;
1120 struct conf_sortkey *ser_sk = 0;
1121 struct record_metadata **wheretoput = 0;
1122 struct record_metadata *rec_md = 0;
1123 int md_field_id = -1;
1124 int sk_field_id = -1;
1126 type = xmlGetProp(n, (xmlChar *) "type");
1127 value = xmlNodeListGetString(xdoc, n->children, 1);
1129 if (!type || !value || !*value)
1133 = conf_service_metadata_field_id(service, (const char *) type);
1134 if (md_field_id < 0)
1136 if (se->number_of_warnings_unknown_metadata == 0)
1139 "Ignoring unknown metadata element: %s", type);
1141 se->number_of_warnings_unknown_metadata++;
1145 ser_md = &service->metadata[md_field_id];
1147 if (ser_md->sortkey_offset >= 0){
1148 sk_field_id = ser_md->sortkey_offset;
1149 ser_sk = &service->sortkeys[sk_field_id];
1152 // non-merged metadata
1153 rec_md = record_metadata_init(se->nmem, (const char *) value,
1154 ser_md->type, n->properties);
1157 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1161 wheretoput = &record->metadata[md_field_id];
1163 wheretoput = &(*wheretoput)->next;
1164 *wheretoput = rec_md;
1167 rec_md = record_metadata_init(se->nmem, (const char *) value,
1169 wheretoput = &cluster->metadata[md_field_id];
1171 // and polulate with data:
1172 // assign cluster or record based on merge action
1173 if (ser_md->merge == Metadata_merge_unique)
1175 struct record_metadata *mnode;
1176 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1177 if (!strcmp((const char *) mnode->data.text.disp,
1178 rec_md->data.text.disp))
1182 rec_md->next = *wheretoput;
1183 *wheretoput = rec_md;
1186 else if (ser_md->merge == Metadata_merge_longest)
1189 || strlen(rec_md->data.text.disp)
1190 > strlen((*wheretoput)->data.text.disp))
1192 *wheretoput = rec_md;
1195 const char *sort_str = 0;
1197 ser_sk->type == Metadata_sortkey_skiparticle;
1199 if (!cluster->sortkeys[sk_field_id])
1200 cluster->sortkeys[sk_field_id] =
1201 nmem_malloc(se->nmem,
1202 sizeof(union data_types));
1204 prt = pp2_relevance_tokenize(
1206 rec_md->data.text.disp, skip_article);
1208 pp2_relevance_token_next(prt);
1210 sort_str = pp2_get_sort(prt);
1212 cluster->sortkeys[sk_field_id]->text.disp =
1213 rec_md->data.text.disp;
1216 sort_str = rec_md->data.text.disp;
1218 "Could not make sortkey. Bug #1858");
1220 cluster->sortkeys[sk_field_id]->text.sort =
1221 nmem_strdup(se->nmem, sort_str);
1223 yaz_log(YLOG_LOG, "text disp=%s",
1224 cluster->sortkeys[sk_field_id]->text.disp);
1225 yaz_log(YLOG_LOG, "text sort=%s",
1226 cluster->sortkeys[sk_field_id]->text.sort);
1228 pp2_relevance_token_destroy(prt);
1232 else if (ser_md->merge == Metadata_merge_all)
1234 rec_md->next = *wheretoput;
1235 *wheretoput = rec_md;
1237 else if (ser_md->merge == Metadata_merge_range)
1241 *wheretoput = rec_md;
1243 cluster->sortkeys[sk_field_id]
1248 int this_min = rec_md->data.number.min;
1249 int this_max = rec_md->data.number.max;
1250 if (this_min < (*wheretoput)->data.number.min)
1251 (*wheretoput)->data.number.min = this_min;
1252 if (this_max > (*wheretoput)->data.number.max)
1253 (*wheretoput)->data.number.max = this_max;
1258 // ranking of _all_ fields enabled ...
1260 relevance_countwords(se->relevance, cluster,
1261 (char *) value, ser_md->rank,
1264 // construct facets ...
1265 if (ser_md->termlist)
1267 if (ser_md->type == Metadata_type_year)
1270 sprintf(year, "%d", rec_md->data.number.max);
1271 add_facet(se, (char *) type, year);
1272 if (rec_md->data.number.max != rec_md->data.number.min)
1274 sprintf(year, "%d", rec_md->data.number.min);
1275 add_facet(se, (char *) type, year);
1279 add_facet(se, (char *) type, (char *) value);
1289 if (se->number_of_warnings_unknown_elements == 0)
1291 "Unexpected element in internal record: %s", n->name);
1292 se->number_of_warnings_unknown_elements++;
1302 relevance_donerecord(se->relevance, cluster);
1303 se->total_records++;
1313 * c-file-style: "Stroustrup"
1314 * indent-tabs-mode: nil
1316 * vim: shiftwidth=4 tabstop=8 expandtab