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"
73 #define TERMLIST_HIGH_SCORE 25
77 // Note: Some things in this structure will eventually move to configuration
78 struct parameters global_parameters =
85 static void log_xml_doc(xmlDoc *doc)
87 FILE *lf = yaz_log_file();
90 #if LIBXML_VERSION >= 20600
91 xmlDocDumpFormatMemory(doc, &result, &len, 1);
93 xmlDocDumpMemory(doc, &result, &len);
97 fwrite(result, 1, len, lf);
103 // Recursively traverse query structure to extract terms.
104 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
116 pull_terms(nmem, n->u.p[0], termlist, num);
117 pull_terms(nmem, n->u.p[1], termlist, num);
120 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
121 for (i = 0; i < numwords; i++)
122 termlist[(*num)++] = words[i];
131 static void add_facet(struct session *s, const char *type, const char *value)
137 for (i = 0; i < s->num_termlists; i++)
138 if (!strcmp(s->termlists[i].name, type))
140 if (i == s->num_termlists)
142 if (i == SESSION_MAX_TERMLISTS)
144 yaz_log(YLOG_FATAL, "Too many termlists");
148 s->termlists[i].name = nmem_strdup(s->nmem, type);
149 s->termlists[i].termlist
150 = termlist_create(s->nmem, s->expected_maxrecs,
151 TERMLIST_HIGH_SCORE);
152 s->num_termlists = i + 1;
154 termlist_insert(s->termlists[i].termlist, value);
157 xmlDoc *record_to_xml(struct session_database *sdb, const char *rec)
159 struct database *db = sdb->database;
162 rdoc = xmlParseMemory(rec, strlen(rec));
166 yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s",
171 if (global_parameters.dump_records)
173 yaz_log(YLOG_LOG, "Un-normalized record from %s", db->url);
180 #define MAX_XSLT_ARGS 16
182 // Add static values from session database settings if applicable
183 static void insert_settings_parameters(struct session_database *sdb,
184 struct session *se, char **parms)
186 struct conf_service *service = se->service;
191 for (i = 0; i < service->num_metadata; i++)
193 struct conf_metadata *md = &service->metadata[i];
196 if (md->setting == Metadata_setting_parameter &&
197 (setting = settings_offset(service, md->name)) > 0)
199 const char *val = session_setting_oneval(sdb, setting);
200 if (val && nparms < MAX_XSLT_ARGS)
203 int len = strlen(val);
204 buf = nmem_malloc(se->nmem, len + 3);
206 strcpy(buf + 1, val);
209 parms[offset++] = md->name;
210 parms[offset++] = buf;
218 // Add static values from session database settings if applicable
219 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
220 struct conf_service *service)
224 for (i = 0; i < service->num_metadata; i++)
226 struct conf_metadata *md = &service->metadata[i];
229 if (md->setting == Metadata_setting_postproc &&
230 (offset = settings_offset(service, md->name)) > 0)
232 const char *val = session_setting_oneval(sdb, offset);
235 xmlNode *r = xmlDocGetRootElement(doc);
236 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
238 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
244 xmlDoc *normalize_record(struct session_database *sdb, struct session *se,
247 struct database_retrievalmap *m;
248 xmlDoc *rdoc = record_to_xml(sdb, rec);
251 for (m = sdb->map; m; m = m->next)
257 char *parms[MAX_XSLT_ARGS*2+1];
259 insert_settings_parameters(sdb, se, parms);
263 new = xsltApplyStylesheet(m->stylesheet, rdoc, (const char **) parms);
267 new = marcmap_apply(m->marcmap, rdoc);
270 root = xmlDocGetRootElement(new);
272 if (!new || !root || !(root->children))
274 yaz_log(YLOG_WARN, "XSLT transformation failed from %s",
286 insert_settings_values(sdb, rdoc, se->service);
288 if (global_parameters.dump_records)
290 yaz_log(YLOG_LOG, "Normalized record from %s",
298 // Retrieve first defined value for 'name' for given database.
299 // Will be extended to take into account user associated with session
300 const char *session_setting_oneval(struct session_database *db, int offset)
302 if (!db->settings[offset])
304 return db->settings[offset]->value;
307 // Prepare XSLT stylesheets for record normalization
308 // Structures are allocated on the session_wide nmem to avoid having
309 // to recompute this for every search. This would lead
310 // to leaking if a single session was to repeatedly change the PZ_XSLT
311 // setting. However, this is not a realistic use scenario.
312 static int prepare_map(struct session *se, struct session_database *sdb)
318 yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url);
321 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
324 struct database_retrievalmap **m = &sdb->map;
326 char auto_stylesheet[256];
328 if (!strcmp(s, "auto"))
330 const char *request_syntax = session_setting_oneval(
331 sdb, PZ_REQUESTSYNTAX);
335 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
336 "%s.xsl", request_syntax);
337 for (cp = auto_stylesheet; *cp; cp++)
339 /* deliberately only consider ASCII */
340 if (*cp > 32 && *cp < 127)
347 yaz_log(YLOG_WARN, "No pz:requestsyntax for auto stylesheet");
350 nmem_strsplit(se->session_nmem, ",", s, &stylesheets, &num);
351 for (i = 0; i < num; i++)
353 (*m) = nmem_malloc(se->session_nmem, sizeof(**m));
357 if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-4], ".xsl"))
359 (*m)->marcmap = NULL;
360 if (!((*m)->stylesheet = conf_load_stylesheet(se->service, stylesheets[i])))
362 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
368 else if (!strcmp(&stylesheets[i][strlen(stylesheets[i])-5], ".mmap"))
370 (*m)->stylesheet = NULL;
371 if (!((*m)->marcmap = marcmap_load(stylesheets[i], se->session_nmem)))
373 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load marcmap: %s",
383 yaz_log(YLOG_WARN, "No Normalization stylesheet for target %s",
388 // This analyzes settings and recomputes any supporting data structures
390 static int prepare_session_database(struct session *se,
391 struct session_database *sdb)
396 "No settings associated with %s", sdb->database->url);
399 if (sdb->settings[PZ_XSLT] && !sdb->map)
401 if (prepare_map(se, sdb) < 0)
407 // called if watch should be removed because http_channel is to be destroyed
408 static void session_watch_cancel(void *data, struct http_channel *c,
411 struct session_watchentry *ent = data;
418 // set watch. Returns 0=OK, -1 if watch is already set
419 int session_set_watch(struct session *s, int what,
420 session_watchfun fun, void *data,
421 struct http_channel *chan)
423 if (s->watchlist[what].fun)
425 s->watchlist[what].fun = fun;
426 s->watchlist[what].data = data;
427 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
428 session_watch_cancel);
432 void session_alert_watch(struct session *s, int what)
434 if (s->watchlist[what].fun)
436 /* our watch is no longer associated with http_channel */
438 session_watchfun fun;
440 http_remove_observer(s->watchlist[what].obs);
441 fun = s->watchlist[what].fun;
442 data = s->watchlist[what].data;
444 /* reset watch before fun is invoked - in case fun wants to set
446 s->watchlist[what].fun = 0;
447 s->watchlist[what].data = 0;
448 s->watchlist[what].obs = 0;
454 //callback for grep_databases
455 static void select_targets_callback(void *context, struct session_database *db)
457 struct session *se = (struct session*) context;
458 struct client *cl = client_create();
459 client_set_database(cl, db);
460 client_set_session(cl, se);
463 // Associates a set of clients with a session;
464 // Note: Session-databases represent databases with per-session
466 int select_targets(struct session *se, struct database_criterion *crit)
469 client_destroy(se->clients);
471 return session_grep_databases(se, crit, select_targets_callback);
474 int session_active_clients(struct session *s)
479 for (c = s->clients; c; c = client_next_in_session(c))
480 if (client_is_active(c))
486 // parses crit1=val1,crit2=val2|val3,...
487 static struct database_criterion *parse_filter(NMEM m, const char *buf)
489 struct database_criterion *res = 0;
496 nmem_strsplit(m, ",", buf, &values, &num);
497 for (i = 0; i < num; i++)
502 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
503 char *eq = strchr(values[i], '=');
506 yaz_log(YLOG_WARN, "Missing equal-sign in filter");
510 new->name = values[i];
511 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
513 for (subi = 0; subi < subnum; subi++)
515 struct database_criterion_value *newv
516 = nmem_malloc(m, sizeof(*newv));
517 newv->value = subvalues[subi];
518 newv->next = new->values;
527 enum pazpar2_error_code search(struct session *se,
528 const char *query, const char *filter,
529 const char **addinfo)
531 int live_channels = 0;
535 struct database_criterion *criteria;
537 yaz_log(YLOG_DEBUG, "Search");
540 nmem_reset(se->nmem);
542 se->total_records = se->total_hits = se->total_merged = 0;
544 se->num_termlists = 0;
545 criteria = parse_filter(se->nmem, filter);
546 live_channels = select_targets(se, criteria);
549 int maxrecs = live_channels * global_parameters.toget; // This is buggy!!!
550 se->reclist = reclist_create(se->nmem, maxrecs);
551 se->expected_maxrecs = maxrecs;
554 return PAZPAR2_NO_TARGETS;
556 for (cl = se->clients; cl; cl = client_next_in_session(cl))
558 if (prepare_session_database(se, client_get_database(cl)) < 0)
560 *addinfo = client_get_database(cl)->database->url;
561 return PAZPAR2_CONFIG_TARGET;
563 // Parse query for target
564 if (client_parse_query(cl, query) < 0)
569 if (client_prep_connection(cl, se->service->z3950_connect_timeout,
570 se->service->z3950_session_timeout))
571 client_start_search(cl);
575 // If no queries could be mapped, we signal an error
579 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
581 return PAZPAR2_NO_ERROR;
584 // Creates a new session_database object for a database
585 static void session_init_databases_fun(void *context, struct database *db)
587 struct session *se = (struct session *) context;
588 struct conf_service *service = se->service;
589 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
590 int num = settings_num(service);
597 = nmem_malloc(se->session_nmem, sizeof(struct settings *) * num);
598 memset(new->settings, 0, sizeof(struct settings*) * num);
602 for (i = 0; i < num; i++)
603 new->settings[i] = db->settings[i];
605 new->next = se->databases;
609 // Doesn't free memory associated with sdb -- nmem takes care of that
610 static void session_database_destroy(struct session_database *sdb)
612 struct database_retrievalmap *m;
614 for (m = sdb->map; m; m = m->next)
615 xsltFreeStylesheet(m->stylesheet);
618 // Initialize session_database list -- this represents this session's view
619 // of the database list -- subject to modification by the settings ws command
620 void session_init_databases(struct session *se)
623 predef_grep_databases(se, se->service, 0, session_init_databases_fun);
626 // Probably session_init_databases_fun should be refactored instead of
628 static struct session_database *load_session_database(struct session *se,
631 struct database *db = find_database(id, 0, se->service);
633 resolve_database(db);
635 session_init_databases_fun((void*) se, db);
636 // New sdb is head of se->databases list
637 return se->databases;
640 // Find an existing session database. If not found, load it
641 static struct session_database *find_session_database(struct session *se,
644 struct session_database *sdb;
646 for (sdb = se->databases; sdb; sdb = sdb->next)
647 if (!strcmp(sdb->database->url, id))
649 return load_session_database(se, id);
652 // Apply a session override to a database
653 void session_apply_setting(struct session *se, char *dbname, char *setting,
656 struct session_database *sdb = find_session_database(se, dbname);
657 struct conf_service *service = se->service;
658 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
659 int offset = settings_offset_cprefix(service, setting);
663 yaz_log(YLOG_WARN, "Unknown setting %s", setting);
666 // Jakub: This breaks the filter setting.
667 /*if (offset == PZ_ID)
669 yaz_log(YLOG_WARN, "No need to set pz:id setting. Ignoring");
673 new->target = dbname;
676 new->next = sdb->settings[offset];
677 sdb->settings[offset] = new;
679 // Force later recompute of settings-driven data structures
680 // (happens when a search starts and client connections are prepared)
686 struct database_retrievalmap *m;
687 // We don't worry about the map structure -- it's in nmem
688 for (m = sdb->map; m; m = m->next)
689 xsltFreeStylesheet(m->stylesheet);
696 void destroy_session(struct session *s)
698 struct session_database *sdb;
701 client_destroy(s->clients);
702 for (sdb = s->databases; sdb; sdb = sdb->next)
703 session_database_destroy(sdb);
704 nmem_destroy(s->nmem);
705 service_destroy(s->service);
706 wrbuf_destroy(s->wrbuf);
709 struct session *new_session(NMEM nmem, struct conf_service *service)
712 struct session *session = nmem_malloc(nmem, sizeof(*session));
714 yaz_log(YLOG_DEBUG, "New Pazpar2 session");
716 session->service = service;
717 session->relevance = 0;
718 session->total_hits = 0;
719 session->total_records = 0;
720 session->number_of_warnings_unknown_elements = 0;
721 session->number_of_warnings_unknown_metadata = 0;
722 session->num_termlists = 0;
723 session->reclist = 0;
724 session->clients = 0;
725 session->expected_maxrecs = 0;
726 session->session_nmem = nmem;
727 session->nmem = nmem_create();
728 session->wrbuf = wrbuf_alloc();
729 session->databases = 0;
730 for (i = 0; i <= SESSION_WATCH_MAX; i++)
732 session->watchlist[i].data = 0;
733 session->watchlist[i].fun = 0;
738 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
740 struct hitsbytarget *res = 0;
744 for (cl = se->clients; cl; cl = client_next_in_session(cl))
747 res = nmem_malloc(nmem, sizeof(*res) * sz);
749 for (cl = se->clients; cl; cl = client_next_in_session(cl))
751 const char *name = session_setting_oneval(client_get_database(cl),
754 res[*count].id = client_get_database(cl)->database->url;
755 res[*count].name = *name ? name : "Unknown";
756 res[*count].hits = client_get_hits(cl);
757 res[*count].records = client_get_num_records(cl);
758 res[*count].diagnostic = client_get_diagnostic(cl);
759 res[*count].state = client_get_state_str(cl);
760 res[*count].connected = client_get_connection(cl) ? 1 : 0;
766 struct termlist_score **termlist(struct session *s, const char *name, int *num)
770 for (i = 0; i < s->num_termlists; i++)
771 if (!strcmp((const char *) s->termlists[i].name, name))
772 return termlist_highscore(s->termlists[i].termlist, num);
776 #ifdef MISSING_HEADERS
777 void report_nmem_stats(void)
779 size_t in_use, is_free;
781 nmem_get_memory_in_use(&in_use);
782 nmem_get_memory_free(&is_free);
784 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
785 (long) in_use, (long) is_free);
789 struct record_cluster *show_single(struct session *s, const char *id,
790 struct record_cluster **prev_r,
791 struct record_cluster **next_r)
793 struct record_cluster *r;
795 reclist_rewind(s->reclist);
798 while ((r = reclist_read_record(s->reclist)))
800 if (!strcmp(r->recid, id))
802 *next_r = reclist_read_record(s->reclist);
810 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp,
811 int start, int *num, int *total, int *sumhits,
814 struct record_cluster **recs = nmem_malloc(nmem_show, *num
815 * sizeof(struct record_cluster *));
816 struct reclist_sortparms *spp;
819 yaz_timing_t t = yaz_timing_create();
831 for (spp = sp; spp; spp = spp->next)
832 if (spp->type == Metadata_sortkey_relevance)
834 relevance_prepare_read(s->relevance, s->reclist);
837 reclist_sort(s->reclist, sp);
839 *total = s->reclist->num_records;
840 *sumhits = s->total_hits;
842 for (i = 0; i < start; i++)
843 if (!reclist_read_record(s->reclist))
850 for (i = 0; i < *num; i++)
852 struct record_cluster *r = reclist_read_record(s->reclist);
863 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
864 yaz_timing_get_real(t), yaz_timing_get_user(t),
865 yaz_timing_get_sys(t));
866 yaz_timing_destroy(&t);
871 void statistics(struct session *se, struct statistics *stat)
876 memset(stat, 0, sizeof(*stat));
877 for (cl = se->clients; cl; cl = client_next_in_session(cl))
879 if (!client_get_connection(cl))
880 stat->num_no_connection++;
881 switch (client_get_state(cl))
883 case Client_Connecting: stat->num_connecting++; break;
884 case Client_Working: stat->num_working++; break;
885 case Client_Idle: stat->num_idle++; break;
886 case Client_Failed: stat->num_failed++; break;
887 case Client_Error: stat->num_error++; break;
892 stat->num_hits = se->total_hits;
893 stat->num_records = se->total_records;
895 stat->num_clients = count;
899 // Master list of connections we're handling events to
900 static IOCHAN channel_list = 0; /* thread pr */
902 void pazpar2_add_channel(IOCHAN chan)
904 chan->next = channel_list;
908 void pazpar2_event_loop()
910 event_loop(&channel_list);
913 static struct record_metadata *record_metadata_init(
914 NMEM nmem, char *value, enum conf_metadata_type type)
916 struct record_metadata *rec_md = record_metadata_create(nmem);
917 if (type == Metadata_type_generic)
920 p = normalize7bit_generic(p, " ,/.:([");
922 rec_md->data.text.disp = nmem_strdup(nmem, p);
923 rec_md->data.text.sort = 0;
925 else if (type == Metadata_type_year || type == Metadata_type_date)
930 if (type == Metadata_type_date)
932 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
935 rec_md->data.number.min = first;
936 rec_md->data.number.max = last;
943 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
944 struct conf_service *service, NMEM nmem)
946 char *mergekey_norm = 0;
947 xmlNode *root = xmlDocGetRootElement(doc);
948 WRBUF norm_wr = wrbuf_alloc();
951 /* create mergekey based on mergekey attribute from XSL (if any) */
952 xmlChar *mergekey = xmlGetProp(root, (xmlChar *) "mergekey");
955 const char *norm_str;
956 pp2_relevance_token_t prt =
957 pp2_relevance_tokenize(
958 service->mergekey_pct,
959 (const char *) mergekey);
961 while ((norm_str = pp2_relevance_token_next(prt)))
965 if (wrbuf_len(norm_wr))
966 wrbuf_puts(norm_wr, " ");
967 wrbuf_puts(norm_wr, norm_str);
970 pp2_relevance_token_destroy(prt);
973 /* append (if any) mergekey=yes metadata values */
974 for (n = root->children; n; n = n->next)
976 if (n->type != XML_ELEMENT_NODE)
978 if (!strcmp((const char *) n->name, "metadata"))
980 struct conf_metadata *ser_md = 0;
981 int md_field_id = -1;
983 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
989 = conf_service_metadata_field_id(service,
990 (const char *) type);
991 if (md_field_id >= 0)
993 ser_md = &service->metadata[md_field_id];
994 if (ser_md->mergekey == Metadata_mergekey_yes)
996 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
999 const char *norm_str;
1000 pp2_relevance_token_t prt =
1001 pp2_relevance_tokenize(
1002 service->mergekey_pct,
1003 (const char *) value);
1005 while ((norm_str = pp2_relevance_token_next(prt)))
1009 if (wrbuf_len(norm_wr))
1010 wrbuf_puts(norm_wr, " ");
1011 wrbuf_puts(norm_wr, norm_str);
1015 pp2_relevance_token_destroy(prt);
1023 /* generate unique key if none is not generated already or is empty */
1024 if (wrbuf_len(norm_wr) == 0)
1026 wrbuf_printf(norm_wr, "%s-%d",
1027 client_get_database(cl)->database->url, record_no);
1029 if (wrbuf_len(norm_wr) > 0)
1030 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1031 wrbuf_destroy(norm_wr);
1032 return mergekey_norm;
1037 /** \brief ingest XML record
1038 \param cl client holds the result set for record
1039 \param rec record buffer (0 terminated)
1040 \param record_no record position (1, 2, ..)
1041 \returns resulting record or NULL on failure
1043 struct record *ingest_record(struct client *cl, const char *rec,
1046 xmlDoc *xdoc = normalize_record(client_get_database(cl),
1047 client_get_session(cl), rec);
1049 struct record *record;
1050 struct record_cluster *cluster;
1051 struct session *se = client_get_session(cl);
1052 const char *mergekey_norm;
1055 struct conf_service *service = se->service;
1060 root = xmlDocGetRootElement(xdoc);
1062 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, se->nmem);
1065 yaz_log(YLOG_WARN, "Got no mergekey");
1069 record = record_create(se->nmem,
1070 service->num_metadata, service->num_sortkeys, cl,
1073 cluster = reclist_insert(se->reclist,
1075 record, (char *) mergekey_norm,
1077 if (global_parameters.dump_records)
1078 yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1079 client_get_database(cl)->database->url, record_no);
1082 /* no room for record */
1086 relevance_newrec(se->relevance, cluster);
1088 // now parsing XML record and adding data to cluster or record metadata
1089 for (n = root->children; n; n = n->next)
1091 pp2_relevance_token_t prt;
1098 if (n->type != XML_ELEMENT_NODE)
1100 if (!strcmp((const char *) n->name, "metadata"))
1102 struct conf_metadata *ser_md = 0;
1103 struct conf_sortkey *ser_sk = 0;
1104 struct record_metadata **wheretoput = 0;
1105 struct record_metadata *rec_md = 0;
1106 int md_field_id = -1;
1107 int sk_field_id = -1;
1109 type = xmlGetProp(n, (xmlChar *) "type");
1110 value = xmlNodeListGetString(xdoc, n->children, 1);
1112 if (!type || !value || !*value)
1116 = conf_service_metadata_field_id(service, (const char *) type);
1117 if (md_field_id < 0)
1119 if (se->number_of_warnings_unknown_metadata == 0)
1122 "Ignoring unknown metadata element: %s", type);
1124 se->number_of_warnings_unknown_metadata++;
1128 ser_md = &service->metadata[md_field_id];
1130 if (ser_md->sortkey_offset >= 0){
1131 sk_field_id = ser_md->sortkey_offset;
1132 ser_sk = &service->sortkeys[sk_field_id];
1135 // non-merged metadata
1136 rec_md = record_metadata_init(se->nmem, (char *) value,
1140 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1144 wheretoput = &record->metadata[md_field_id];
1146 wheretoput = &(*wheretoput)->next;
1147 *wheretoput = rec_md;
1150 rec_md = record_metadata_init(se->nmem, (char *) value,
1152 wheretoput = &cluster->metadata[md_field_id];
1154 // and polulate with data:
1155 // assign cluster or record based on merge action
1156 if (ser_md->merge == Metadata_merge_unique)
1158 struct record_metadata *mnode;
1159 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1160 if (!strcmp((const char *) mnode->data.text.disp,
1161 rec_md->data.text.disp))
1165 rec_md->next = *wheretoput;
1166 *wheretoput = rec_md;
1169 else if (ser_md->merge == Metadata_merge_longest)
1172 || strlen(rec_md->data.text.disp)
1173 > strlen((*wheretoput)->data.text.disp))
1175 *wheretoput = rec_md;
1178 const char *sort_str = 0;
1180 ser_sk->type == Metadata_sortkey_skiparticle;
1182 if (!cluster->sortkeys[sk_field_id])
1183 cluster->sortkeys[sk_field_id] =
1184 nmem_malloc(se->nmem,
1185 sizeof(union data_types));
1187 prt = pp2_relevance_tokenize(
1189 rec_md->data.text.disp);
1191 pp2_relevance_token_next(prt);
1193 sort_str = pp2_get_sort(prt, skip_article);
1195 cluster->sortkeys[sk_field_id]->text.disp =
1196 rec_md->data.text.disp;
1199 sort_str = rec_md->data.text.disp;
1201 "Could not make sortkey. Bug #1858");
1203 cluster->sortkeys[sk_field_id]->text.sort =
1204 nmem_strdup(se->nmem, sort_str);
1206 yaz_log(YLOG_LOG, "text disp=%s",
1207 cluster->sortkeys[sk_field_id]->text.disp);
1208 yaz_log(YLOG_LOG, "text sort=%s",
1209 cluster->sortkeys[sk_field_id]->text.sort);
1211 pp2_relevance_token_destroy(prt);
1215 else if (ser_md->merge == Metadata_merge_all)
1217 rec_md->next = *wheretoput;
1218 *wheretoput = rec_md;
1220 else if (ser_md->merge == Metadata_merge_range)
1224 *wheretoput = rec_md;
1226 cluster->sortkeys[sk_field_id]
1231 int this_min = rec_md->data.number.min;
1232 int this_max = rec_md->data.number.max;
1233 if (this_min < (*wheretoput)->data.number.min)
1234 (*wheretoput)->data.number.min = this_min;
1235 if (this_max > (*wheretoput)->data.number.max)
1236 (*wheretoput)->data.number.max = this_max;
1241 // ranking of _all_ fields enabled ...
1243 relevance_countwords(se->relevance, cluster,
1244 (char *) value, ser_md->rank);
1246 // construct facets ...
1247 if (ser_md->termlist)
1249 if (ser_md->type == Metadata_type_year)
1252 sprintf(year, "%d", rec_md->data.number.max);
1253 add_facet(se, (char *) type, year);
1254 if (rec_md->data.number.max != rec_md->data.number.min)
1256 sprintf(year, "%d", rec_md->data.number.min);
1257 add_facet(se, (char *) type, year);
1261 add_facet(se, (char *) type, (char *) value);
1271 if (se->number_of_warnings_unknown_elements == 0)
1273 "Unexpected element in internal record: %s", n->name);
1274 se->number_of_warnings_unknown_elements++;
1284 relevance_donerecord(se->relevance, cluster);
1285 se->total_records++;
1295 * c-file-style: "Stroustrup"
1296 * indent-tabs-mode: nil
1298 * vim: shiftwidth=4 tabstop=8 expandtab