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 =
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 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, 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 channel_list = 0; /* thread pr */
813 void pazpar2_add_channel(IOCHAN chan)
815 chan->next = channel_list;
819 void pazpar2_event_loop()
821 event_loop(&channel_list);
824 static struct record_metadata *record_metadata_init(
825 NMEM nmem, const char *value, enum conf_metadata_type type,
826 struct _xmlAttr *attr)
828 struct record_metadata *rec_md = record_metadata_create(nmem);
829 struct record_metadata_attr **attrp = &rec_md->attributes;
831 for (; attr; attr = attr->next)
833 if (attr->children && attr->children->content)
835 if (strcmp((const char *) attr->name, "type"))
836 { /* skip the "type" attribute.. Its value is already part of
837 the element in output (md-%s) and so repeating it here
839 *attrp = nmem_malloc(nmem, sizeof(**attrp));
841 nmem_strdup(nmem, (const char *) attr->name);
843 nmem_strdup(nmem, (const char *) attr->children->content);
844 attrp = &(*attrp)->next;
850 if (type == Metadata_type_generic)
852 char *p = nmem_strdup(nmem, value);
854 p = normalize7bit_generic(p, " ,/.:([");
856 rec_md->data.text.disp = p;
857 rec_md->data.text.sort = 0;
859 else if (type == Metadata_type_year || type == Metadata_type_date)
864 if (type == Metadata_type_date)
866 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
869 rec_md->data.number.min = first;
870 rec_md->data.number.max = last;
877 static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
878 struct conf_service *service, WRBUF norm_wr)
882 for (n = root->children; n; n = n->next)
884 if (n->type != XML_ELEMENT_NODE)
886 if (!strcmp((const char *) n->name, "metadata"))
888 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
889 if (!strcmp(name, (const char *) type))
891 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
894 const char *norm_str;
895 pp2_relevance_token_t prt =
896 pp2_relevance_tokenize(
897 service->mergekey_pct,
898 (const char *) value);
900 wrbuf_puts(norm_wr, name);
901 wrbuf_puts(norm_wr, "=");
903 pp2_relevance_token_next(prt)))
907 if (wrbuf_len(norm_wr))
908 wrbuf_puts(norm_wr, " ");
909 wrbuf_puts(norm_wr, norm_str);
913 pp2_relevance_token_destroy(prt);
923 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
924 struct conf_service *service, NMEM nmem)
926 char *mergekey_norm = 0;
927 xmlNode *root = xmlDocGetRootElement(doc);
928 WRBUF norm_wr = wrbuf_alloc();
930 /* consider mergekey from XSL first */
931 xmlChar *mergekey = xmlGetProp(root, (xmlChar *) "mergekey");
934 const char *norm_str;
935 pp2_relevance_token_t prt =
936 pp2_relevance_tokenize(
937 service->mergekey_pct,
938 (const char *) mergekey);
940 while ((norm_str = pp2_relevance_token_next(prt)))
944 if (wrbuf_len(norm_wr))
945 wrbuf_puts(norm_wr, " ");
946 wrbuf_puts(norm_wr, norm_str);
949 pp2_relevance_token_destroy(prt);
954 /* no mergekey defined in XSL. Look for mergekey metadata instead */
956 for (field_id = 0; field_id < service->num_metadata; field_id++)
958 struct conf_metadata *ser_md = &service->metadata[field_id];
959 if (ser_md->mergekey != Metadata_mergekey_no)
961 int r = get_mergekey_from_doc(doc, root, ser_md->name,
963 if (r == 0 && ser_md->mergekey == Metadata_mergekey_required)
965 /* no mergekey on this one and it is required..
966 Generate unique key instead */
967 wrbuf_rewind(norm_wr);
974 /* generate unique key if none is not generated already or is empty */
975 if (wrbuf_len(norm_wr) == 0)
977 wrbuf_printf(norm_wr, "%s-%d",
978 client_get_database(cl)->database->url, record_no);
980 if (wrbuf_len(norm_wr) > 0)
981 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
982 wrbuf_destroy(norm_wr);
983 return mergekey_norm;
986 /** \brief see if metadata for pz:recordfilter exists
987 \param root xml root element of normalized record
988 \param sdb session database for client
989 \retval 0 if there is no metadata for pz:recordfilter
990 \retval 1 if there is metadata for pz:recordfilter
992 If there is no pz:recordfilter defined, this function returns 1
996 static int check_record_filter(xmlNode *root, struct session_database *sdb)
1001 s = session_setting_oneval(sdb, PZ_RECORDFILTER);
1006 for (n = root->children; n; n = n->next)
1008 if (n->type != XML_ELEMENT_NODE)
1010 if (!strcmp((const char *) n->name, "metadata"))
1012 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
1016 const char *eq = strchr(s, '~');
1021 if (len == strlen((const char *)type) &&
1022 !memcmp((const char *) type, s, len))
1024 xmlChar *value = xmlNodeGetContent(n);
1025 if (value && *value)
1027 if (!eq || strstr((const char *) value, eq+1))
1040 /** \brief ingest XML record
1041 \param cl client holds the result set for record
1042 \param rec record buffer (0 terminated)
1043 \param record_no record position (1, 2, ..)
1044 \returns resulting record or NULL on failure
1046 struct record *ingest_record(struct client *cl, const char *rec,
1049 struct session_database *sdb = client_get_database(cl);
1050 struct session *se = client_get_session(cl);
1051 xmlDoc *xdoc = normalize_record(sdb, se, rec);
1053 struct record *record;
1054 struct record_cluster *cluster;
1055 const char *mergekey_norm;
1058 struct conf_service *service = se->service;
1063 root = xmlDocGetRootElement(xdoc);
1065 if (!check_record_filter(root, sdb))
1067 yaz_log(YLOG_WARN, "Filtered out record no %d from %s", record_no,
1068 sdb->database->url);
1073 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, se->nmem);
1076 yaz_log(YLOG_WARN, "Got no mergekey");
1080 record = record_create(se->nmem,
1081 service->num_metadata, service->num_sortkeys, cl,
1084 cluster = reclist_insert(se->reclist,
1086 record, (char *) mergekey_norm,
1090 /* no room for record */
1094 if (global_parameters.dump_records)
1095 yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1096 sdb->database->url, record_no);
1097 relevance_newrec(se->relevance, cluster);
1099 // now parsing XML record and adding data to cluster or record metadata
1100 for (n = root->children; n; n = n->next)
1102 pp2_relevance_token_t prt;
1109 if (n->type != XML_ELEMENT_NODE)
1111 if (!strcmp((const char *) n->name, "metadata"))
1113 struct conf_metadata *ser_md = 0;
1114 struct conf_sortkey *ser_sk = 0;
1115 struct record_metadata **wheretoput = 0;
1116 struct record_metadata *rec_md = 0;
1117 int md_field_id = -1;
1118 int sk_field_id = -1;
1120 type = xmlGetProp(n, (xmlChar *) "type");
1121 value = xmlNodeListGetString(xdoc, n->children, 1);
1123 if (!type || !value || !*value)
1127 = conf_service_metadata_field_id(service, (const char *) type);
1128 if (md_field_id < 0)
1130 if (se->number_of_warnings_unknown_metadata == 0)
1133 "Ignoring unknown metadata element: %s", type);
1135 se->number_of_warnings_unknown_metadata++;
1139 ser_md = &service->metadata[md_field_id];
1141 if (ser_md->sortkey_offset >= 0){
1142 sk_field_id = ser_md->sortkey_offset;
1143 ser_sk = &service->sortkeys[sk_field_id];
1146 // non-merged metadata
1147 rec_md = record_metadata_init(se->nmem, (const char *) value,
1148 ser_md->type, n->properties);
1151 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1155 wheretoput = &record->metadata[md_field_id];
1157 wheretoput = &(*wheretoput)->next;
1158 *wheretoput = rec_md;
1161 rec_md = record_metadata_init(se->nmem, (const char *) value,
1163 wheretoput = &cluster->metadata[md_field_id];
1165 // and polulate with data:
1166 // assign cluster or record based on merge action
1167 if (ser_md->merge == Metadata_merge_unique)
1169 struct record_metadata *mnode;
1170 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1171 if (!strcmp((const char *) mnode->data.text.disp,
1172 rec_md->data.text.disp))
1176 rec_md->next = *wheretoput;
1177 *wheretoput = rec_md;
1180 else if (ser_md->merge == Metadata_merge_longest)
1183 || strlen(rec_md->data.text.disp)
1184 > strlen((*wheretoput)->data.text.disp))
1186 *wheretoput = rec_md;
1189 const char *sort_str = 0;
1191 ser_sk->type == Metadata_sortkey_skiparticle;
1193 if (!cluster->sortkeys[sk_field_id])
1194 cluster->sortkeys[sk_field_id] =
1195 nmem_malloc(se->nmem,
1196 sizeof(union data_types));
1198 prt = pp2_relevance_tokenize(
1200 rec_md->data.text.disp);
1202 pp2_relevance_token_next(prt);
1204 sort_str = pp2_get_sort(prt, skip_article);
1206 cluster->sortkeys[sk_field_id]->text.disp =
1207 rec_md->data.text.disp;
1210 sort_str = rec_md->data.text.disp;
1212 "Could not make sortkey. Bug #1858");
1214 cluster->sortkeys[sk_field_id]->text.sort =
1215 nmem_strdup(se->nmem, sort_str);
1217 yaz_log(YLOG_LOG, "text disp=%s",
1218 cluster->sortkeys[sk_field_id]->text.disp);
1219 yaz_log(YLOG_LOG, "text sort=%s",
1220 cluster->sortkeys[sk_field_id]->text.sort);
1222 pp2_relevance_token_destroy(prt);
1226 else if (ser_md->merge == Metadata_merge_all)
1228 rec_md->next = *wheretoput;
1229 *wheretoput = rec_md;
1231 else if (ser_md->merge == Metadata_merge_range)
1235 *wheretoput = rec_md;
1237 cluster->sortkeys[sk_field_id]
1242 int this_min = rec_md->data.number.min;
1243 int this_max = rec_md->data.number.max;
1244 if (this_min < (*wheretoput)->data.number.min)
1245 (*wheretoput)->data.number.min = this_min;
1246 if (this_max > (*wheretoput)->data.number.max)
1247 (*wheretoput)->data.number.max = this_max;
1252 // ranking of _all_ fields enabled ...
1254 relevance_countwords(se->relevance, cluster,
1255 (char *) value, ser_md->rank);
1257 // construct facets ...
1258 if (ser_md->termlist)
1260 if (ser_md->type == Metadata_type_year)
1263 sprintf(year, "%d", rec_md->data.number.max);
1264 add_facet(se, (char *) type, year);
1265 if (rec_md->data.number.max != rec_md->data.number.min)
1267 sprintf(year, "%d", rec_md->data.number.min);
1268 add_facet(se, (char *) type, year);
1272 add_facet(se, (char *) type, (char *) value);
1282 if (se->number_of_warnings_unknown_elements == 0)
1284 "Unexpected element in internal record: %s", n->name);
1285 se->number_of_warnings_unknown_elements++;
1295 relevance_donerecord(se->relevance, cluster);
1296 se->total_records++;
1306 * c-file-style: "Stroustrup"
1307 * indent-tabs-mode: nil
1309 * vim: shiftwidth=4 tabstop=8 expandtab