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 wrbuf_destroy(s->wrbuf);
708 struct session *new_session(NMEM nmem, struct conf_service *service)
711 struct session *session = nmem_malloc(nmem, sizeof(*session));
713 yaz_log(YLOG_DEBUG, "New Pazpar2 session");
715 session->service = service;
716 session->relevance = 0;
717 session->total_hits = 0;
718 session->total_records = 0;
719 session->number_of_warnings_unknown_elements = 0;
720 session->number_of_warnings_unknown_metadata = 0;
721 session->num_termlists = 0;
722 session->reclist = 0;
723 session->clients = 0;
724 session->expected_maxrecs = 0;
725 session->session_nmem = nmem;
726 session->nmem = nmem_create();
727 session->wrbuf = wrbuf_alloc();
728 session->databases = 0;
729 for (i = 0; i <= SESSION_WATCH_MAX; i++)
731 session->watchlist[i].data = 0;
732 session->watchlist[i].fun = 0;
737 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
739 struct hitsbytarget *res = 0;
743 for (cl = se->clients; cl; cl = client_next_in_session(cl))
746 res = nmem_malloc(nmem, sizeof(*res) * sz);
748 for (cl = se->clients; cl; cl = client_next_in_session(cl))
750 const char *name = session_setting_oneval(client_get_database(cl),
753 res[*count].id = client_get_database(cl)->database->url;
754 res[*count].name = *name ? name : "Unknown";
755 res[*count].hits = client_get_hits(cl);
756 res[*count].records = client_get_num_records(cl);
757 res[*count].diagnostic = client_get_diagnostic(cl);
758 res[*count].state = client_get_state_str(cl);
759 res[*count].connected = client_get_connection(cl) ? 1 : 0;
765 struct termlist_score **termlist(struct session *s, const char *name, int *num)
769 for (i = 0; i < s->num_termlists; i++)
770 if (!strcmp((const char *) s->termlists[i].name, name))
771 return termlist_highscore(s->termlists[i].termlist, num);
775 #ifdef MISSING_HEADERS
776 void report_nmem_stats(void)
778 size_t in_use, is_free;
780 nmem_get_memory_in_use(&in_use);
781 nmem_get_memory_free(&is_free);
783 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
784 (long) in_use, (long) is_free);
788 struct record_cluster *show_single(struct session *s, const char *id,
789 struct record_cluster **prev_r,
790 struct record_cluster **next_r)
792 struct record_cluster *r;
794 reclist_rewind(s->reclist);
797 while ((r = reclist_read_record(s->reclist)))
799 if (!strcmp(r->recid, id))
801 *next_r = reclist_read_record(s->reclist);
809 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp,
810 int start, int *num, int *total, int *sumhits,
813 struct record_cluster **recs = nmem_malloc(nmem_show, *num
814 * sizeof(struct record_cluster *));
815 struct reclist_sortparms *spp;
818 yaz_timing_t t = yaz_timing_create();
830 for (spp = sp; spp; spp = spp->next)
831 if (spp->type == Metadata_sortkey_relevance)
833 relevance_prepare_read(s->relevance, s->reclist);
836 reclist_sort(s->reclist, sp);
838 *total = s->reclist->num_records;
839 *sumhits = s->total_hits;
841 for (i = 0; i < start; i++)
842 if (!reclist_read_record(s->reclist))
849 for (i = 0; i < *num; i++)
851 struct record_cluster *r = reclist_read_record(s->reclist);
862 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
863 yaz_timing_get_real(t), yaz_timing_get_user(t),
864 yaz_timing_get_sys(t));
865 yaz_timing_destroy(&t);
870 void statistics(struct session *se, struct statistics *stat)
875 memset(stat, 0, sizeof(*stat));
876 for (cl = se->clients; cl; cl = client_next_in_session(cl))
878 if (!client_get_connection(cl))
879 stat->num_no_connection++;
880 switch (client_get_state(cl))
882 case Client_Connecting: stat->num_connecting++; break;
883 case Client_Working: stat->num_working++; break;
884 case Client_Idle: stat->num_idle++; break;
885 case Client_Failed: stat->num_failed++; break;
886 case Client_Error: stat->num_error++; break;
891 stat->num_hits = se->total_hits;
892 stat->num_records = se->total_records;
894 stat->num_clients = count;
898 // Master list of connections we're handling events to
899 static IOCHAN channel_list = 0; /* thread pr */
901 void pazpar2_add_channel(IOCHAN chan)
903 chan->next = channel_list;
907 void pazpar2_event_loop()
909 event_loop(&channel_list);
912 static struct record_metadata *record_metadata_init(
913 NMEM nmem, char *value, enum conf_metadata_type type)
915 struct record_metadata *rec_md = record_metadata_create(nmem);
916 if (type == Metadata_type_generic)
919 p = normalize7bit_generic(p, " ,/.:([");
921 rec_md->data.text.disp = nmem_strdup(nmem, p);
922 rec_md->data.text.sort = 0;
924 else if (type == Metadata_type_year || type == Metadata_type_date)
929 if (type == Metadata_type_date)
931 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
934 rec_md->data.number.min = first;
935 rec_md->data.number.max = last;
942 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
943 struct conf_service *service, NMEM nmem)
945 char *mergekey_norm = 0;
946 xmlNode *root = xmlDocGetRootElement(doc);
947 WRBUF norm_wr = wrbuf_alloc();
950 /* create mergekey based on mergekey attribute from XSL (if any) */
951 xmlChar *mergekey = xmlGetProp(root, (xmlChar *) "mergekey");
954 const char *norm_str;
955 pp2_relevance_token_t prt =
956 pp2_relevance_tokenize(
957 service->mergekey_pct,
958 (const char *) mergekey);
960 while ((norm_str = pp2_relevance_token_next(prt)))
964 if (wrbuf_len(norm_wr))
965 wrbuf_puts(norm_wr, " ");
966 wrbuf_puts(norm_wr, norm_str);
969 pp2_relevance_token_destroy(prt);
972 /* append (if any) mergekey=yes metadata values */
973 for (n = root->children; n; n = n->next)
975 if (n->type != XML_ELEMENT_NODE)
977 if (!strcmp((const char *) n->name, "metadata"))
979 struct conf_metadata *ser_md = 0;
980 int md_field_id = -1;
982 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
988 = conf_service_metadata_field_id(service,
989 (const char *) type);
990 if (md_field_id >= 0)
992 ser_md = &service->metadata[md_field_id];
993 if (ser_md->mergekey == Metadata_mergekey_yes)
995 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
998 const char *norm_str;
999 pp2_relevance_token_t prt =
1000 pp2_relevance_tokenize(
1001 service->mergekey_pct,
1002 (const char *) value);
1004 while ((norm_str = pp2_relevance_token_next(prt)))
1008 if (wrbuf_len(norm_wr))
1009 wrbuf_puts(norm_wr, " ");
1010 wrbuf_puts(norm_wr, norm_str);
1014 pp2_relevance_token_destroy(prt);
1022 /* generate unique key if none is not generated already or is empty */
1023 if (wrbuf_len(norm_wr) == 0)
1025 wrbuf_printf(norm_wr, "%s-%d",
1026 client_get_database(cl)->database->url, record_no);
1028 if (wrbuf_len(norm_wr) > 0)
1029 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1030 wrbuf_destroy(norm_wr);
1031 return mergekey_norm;
1036 /** \brief ingest XML record
1037 \param cl client holds the result set for record
1038 \param rec record buffer (0 terminated)
1039 \param record_no record position (1, 2, ..)
1040 \returns resulting record or NULL on failure
1042 struct record *ingest_record(struct client *cl, const char *rec,
1045 xmlDoc *xdoc = normalize_record(client_get_database(cl),
1046 client_get_session(cl), rec);
1048 struct record *record;
1049 struct record_cluster *cluster;
1050 struct session *se = client_get_session(cl);
1051 const char *mergekey_norm;
1054 struct conf_service *service = se->service;
1059 root = xmlDocGetRootElement(xdoc);
1061 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, se->nmem);
1064 yaz_log(YLOG_WARN, "Got no mergekey");
1068 record = record_create(se->nmem,
1069 service->num_metadata, service->num_sortkeys, cl,
1072 cluster = reclist_insert(se->reclist,
1074 record, (char *) mergekey_norm,
1076 if (global_parameters.dump_records)
1077 yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1078 client_get_database(cl)->database->url, record_no);
1081 /* no room for record */
1085 relevance_newrec(se->relevance, cluster);
1087 // now parsing XML record and adding data to cluster or record metadata
1088 for (n = root->children; n; n = n->next)
1090 pp2_relevance_token_t prt;
1097 if (n->type != XML_ELEMENT_NODE)
1099 if (!strcmp((const char *) n->name, "metadata"))
1101 struct conf_metadata *ser_md = 0;
1102 struct conf_sortkey *ser_sk = 0;
1103 struct record_metadata **wheretoput = 0;
1104 struct record_metadata *rec_md = 0;
1105 int md_field_id = -1;
1106 int sk_field_id = -1;
1108 type = xmlGetProp(n, (xmlChar *) "type");
1109 value = xmlNodeListGetString(xdoc, n->children, 1);
1111 if (!type || !value || !*value)
1115 = conf_service_metadata_field_id(service, (const char *) type);
1116 if (md_field_id < 0)
1118 if (se->number_of_warnings_unknown_metadata == 0)
1121 "Ignoring unknown metadata element: %s", type);
1123 se->number_of_warnings_unknown_metadata++;
1127 ser_md = &service->metadata[md_field_id];
1129 if (ser_md->sortkey_offset >= 0){
1130 sk_field_id = ser_md->sortkey_offset;
1131 ser_sk = &service->sortkeys[sk_field_id];
1134 // non-merged metadata
1135 rec_md = record_metadata_init(se->nmem, (char *) value,
1139 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1143 wheretoput = &record->metadata[md_field_id];
1145 wheretoput = &(*wheretoput)->next;
1146 *wheretoput = rec_md;
1149 rec_md = record_metadata_init(se->nmem, (char *) value,
1151 wheretoput = &cluster->metadata[md_field_id];
1153 // and polulate with data:
1154 // assign cluster or record based on merge action
1155 if (ser_md->merge == Metadata_merge_unique)
1157 struct record_metadata *mnode;
1158 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1159 if (!strcmp((const char *) mnode->data.text.disp,
1160 rec_md->data.text.disp))
1164 rec_md->next = *wheretoput;
1165 *wheretoput = rec_md;
1168 else if (ser_md->merge == Metadata_merge_longest)
1171 || strlen(rec_md->data.text.disp)
1172 > strlen((*wheretoput)->data.text.disp))
1174 *wheretoput = rec_md;
1177 const char *sort_str = 0;
1179 ser_sk->type == Metadata_sortkey_skiparticle;
1181 if (!cluster->sortkeys[sk_field_id])
1182 cluster->sortkeys[sk_field_id] =
1183 nmem_malloc(se->nmem,
1184 sizeof(union data_types));
1186 prt = pp2_relevance_tokenize(
1188 rec_md->data.text.disp);
1190 pp2_relevance_token_next(prt);
1192 sort_str = pp2_get_sort(prt, skip_article);
1194 cluster->sortkeys[sk_field_id]->text.disp =
1195 rec_md->data.text.disp;
1198 sort_str = rec_md->data.text.disp;
1200 "Could not make sortkey. Bug #1858");
1202 cluster->sortkeys[sk_field_id]->text.sort =
1203 nmem_strdup(se->nmem, sort_str);
1205 yaz_log(YLOG_LOG, "text disp=%s",
1206 cluster->sortkeys[sk_field_id]->text.disp);
1207 yaz_log(YLOG_LOG, "text sort=%s",
1208 cluster->sortkeys[sk_field_id]->text.sort);
1210 pp2_relevance_token_destroy(prt);
1214 else if (ser_md->merge == Metadata_merge_all)
1216 rec_md->next = *wheretoput;
1217 *wheretoput = rec_md;
1219 else if (ser_md->merge == Metadata_merge_range)
1223 *wheretoput = rec_md;
1225 cluster->sortkeys[sk_field_id]
1230 int this_min = rec_md->data.number.min;
1231 int this_max = rec_md->data.number.max;
1232 if (this_min < (*wheretoput)->data.number.min)
1233 (*wheretoput)->data.number.min = this_min;
1234 if (this_max > (*wheretoput)->data.number.max)
1235 (*wheretoput)->data.number.max = this_max;
1240 // ranking of _all_ fields enabled ...
1242 relevance_countwords(se->relevance, cluster,
1243 (char *) value, ser_md->rank);
1245 // construct facets ...
1246 if (ser_md->termlist)
1248 if (ser_md->type == Metadata_type_year)
1251 sprintf(year, "%d", rec_md->data.number.max);
1252 add_facet(se, (char *) type, year);
1253 if (rec_md->data.number.max != rec_md->data.number.min)
1255 sprintf(year, "%d", rec_md->data.number.min);
1256 add_facet(se, (char *) type, year);
1260 add_facet(se, (char *) type, (char *) value);
1270 if (se->number_of_warnings_unknown_elements == 0)
1272 "Unexpected element in internal record: %s", n->name);
1273 se->number_of_warnings_unknown_elements++;
1283 relevance_donerecord(se->relevance, cluster);
1284 se->total_records++;
1294 * c-file-style: "Stroustrup"
1295 * indent-tabs-mode: nil
1297 * vim: shiftwidth=4 tabstop=8 expandtab