1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 \brief high-level logic; mostly user sessions and settings
41 #include <yaz/marcdisp.h>
42 #include <yaz/comstack.h>
43 #include <yaz/tcpip.h>
44 #include <yaz/proto.h>
45 #include <yaz/readconf.h>
46 #include <yaz/pquery.h>
47 #include <yaz/otherinfo.h>
48 #include <yaz/yaz-util.h>
50 #include <yaz/query-charset.h>
51 #include <yaz/querytowrbuf.h>
52 #include <yaz/oid_db.h>
53 #include <yaz/snprintf.h>
57 #include <yaz/timing.h>
64 #include "termlists.h"
66 #include "relevance.h"
70 #include "normalize7bit.h"
72 #define TERMLIST_HIGH_SCORE 25
76 // Note: Some things in this structure will eventually move to configuration
77 struct parameters global_parameters =
81 30, // operations timeout
82 60, // session timeout
84 180, // Z39.50 session timeout
88 static void log_xml_doc(xmlDoc *doc)
90 FILE *lf = yaz_log_file();
93 #if LIBXML_VERSION >= 20600
94 xmlDocDumpFormatMemory(doc, &result, &len, 1);
96 xmlDocDumpMemory(doc, &result, &len);
100 fwrite(result, 1, len, lf);
106 // Recursively traverse query structure to extract terms.
107 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
119 pull_terms(nmem, n->u.p[0], termlist, num);
120 pull_terms(nmem, n->u.p[1], termlist, num);
123 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
124 for (i = 0; i < numwords; i++)
125 termlist[(*num)++] = words[i];
134 static void add_facet(struct session *s, const char *type, const char *value)
140 for (i = 0; i < s->num_termlists; i++)
141 if (!strcmp(s->termlists[i].name, type))
143 if (i == s->num_termlists)
145 if (i == SESSION_MAX_TERMLISTS)
147 yaz_log(YLOG_FATAL, "Too many termlists");
151 s->termlists[i].name = nmem_strdup(s->nmem, type);
152 s->termlists[i].termlist
153 = termlist_create(s->nmem, s->expected_maxrecs,
154 TERMLIST_HIGH_SCORE);
155 s->num_termlists = i + 1;
157 termlist_insert(s->termlists[i].termlist, value);
160 xmlDoc *record_to_xml(struct session_database *sdb, const char *rec)
162 struct database *db = sdb->database;
165 rdoc = xmlParseMemory(rec, strlen(rec));
169 yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s",
174 if (global_parameters.dump_records)
176 yaz_log(YLOG_LOG, "Un-normalized record from %s", db->url);
183 #define MAX_XSLT_ARGS 16
185 // Add static values from session database settings if applicable
186 static void insert_settings_parameters(struct session_database *sdb,
187 struct session *se, char **parms)
189 struct conf_service *service = se->service;
194 for (i = 0; i < service->num_metadata; i++)
196 struct conf_metadata *md = &service->metadata[i];
199 if (md->setting == Metadata_setting_parameter &&
200 (setting = settings_offset(service, md->name)) > 0)
202 const char *val = session_setting_oneval(sdb, setting);
203 if (val && nparms < MAX_XSLT_ARGS)
206 int len = strlen(val);
207 buf = nmem_malloc(se->nmem, len + 3);
209 strcpy(buf + 1, val);
212 parms[offset++] = md->name;
213 parms[offset++] = buf;
221 // Add static values from session database settings if applicable
222 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
223 struct conf_service *service)
227 for (i = 0; i < service->num_metadata; i++)
229 struct conf_metadata *md = &service->metadata[i];
232 if (md->setting == Metadata_setting_postproc &&
233 (offset = settings_offset(service, md->name)) > 0)
235 const char *val = session_setting_oneval(sdb, offset);
238 xmlNode *r = xmlDocGetRootElement(doc);
239 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
241 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
247 xmlDoc *normalize_record(struct session_database *sdb, struct session *se,
250 struct database_retrievalmap *m;
251 xmlDoc *rdoc = record_to_xml(sdb, rec);
254 for (m = sdb->map; m; m = m->next)
260 char *parms[MAX_XSLT_ARGS*2+1];
262 insert_settings_parameters(sdb, se, parms);
264 new = xsltApplyStylesheet(m->stylesheet, rdoc, (const char **) parms);
265 root= xmlDocGetRootElement(new);
266 if (!new || !root || !(root->children))
268 yaz_log(YLOG_WARN, "XSLT transformation failed from %s",
280 insert_settings_values(sdb, rdoc, se->service);
282 if (global_parameters.dump_records)
284 yaz_log(YLOG_LOG, "Normalized record from %s",
292 // Retrieve first defined value for 'name' for given database.
293 // Will be extended to take into account user associated with session
294 const char *session_setting_oneval(struct session_database *db, int offset)
296 if (!db->settings[offset])
298 return db->settings[offset]->value;
301 // Prepare XSLT stylesheets for record normalization
302 // Structures are allocated on the session_wide nmem to avoid having
303 // to recompute this for every search. This would lead
304 // to leaking if a single session was to repeatedly change the PZ_XSLT
305 // setting. However, this is not a realistic use scenario.
306 static int prepare_map(struct session *se, struct session_database *sdb)
312 yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url);
315 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
318 struct database_retrievalmap **m = &sdb->map;
320 char auto_stylesheet[256];
322 if (!strcmp(s, "auto"))
324 const char *request_syntax = session_setting_oneval(
325 sdb, PZ_REQUESTSYNTAX);
329 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
330 "%s.xsl", request_syntax);
331 for (cp = auto_stylesheet; *cp; cp++)
333 /* deliberately only consider ASCII */
334 if (*cp > 32 && *cp < 127)
341 yaz_log(YLOG_WARN, "No pz:requestsyntax for auto stylesheet");
344 nmem_strsplit(se->session_nmem, ",", s, &stylesheets, &num);
345 for (i = 0; i < num; i++)
347 (*m) = nmem_malloc(se->session_nmem, sizeof(**m));
349 if (!((*m)->stylesheet = conf_load_stylesheet(se->service->config,
352 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
360 yaz_log(YLOG_WARN, "No Normalization stylesheet for target %s",
365 // This analyzes settings and recomputes any supporting data structures
367 static int prepare_session_database(struct session *se,
368 struct session_database *sdb)
373 "No settings associated with %s", sdb->database->url);
376 if (sdb->settings[PZ_XSLT] && !sdb->map)
378 if (prepare_map(se, sdb) < 0)
384 // called if watch should be removed because http_channel is to be destroyed
385 static void session_watch_cancel(void *data, struct http_channel *c,
388 struct session_watchentry *ent = data;
395 // set watch. Returns 0=OK, -1 if watch is already set
396 int session_set_watch(struct session *s, int what,
397 session_watchfun fun, void *data,
398 struct http_channel *chan)
400 if (s->watchlist[what].fun)
402 s->watchlist[what].fun = fun;
403 s->watchlist[what].data = data;
404 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
405 session_watch_cancel);
409 void session_alert_watch(struct session *s, int what)
411 if (s->watchlist[what].fun)
413 /* our watch is no longer associated with http_channel */
415 session_watchfun fun;
417 http_remove_observer(s->watchlist[what].obs);
418 fun = s->watchlist[what].fun;
419 data = s->watchlist[what].data;
421 /* reset watch before fun is invoked - in case fun wants to set
423 s->watchlist[what].fun = 0;
424 s->watchlist[what].data = 0;
425 s->watchlist[what].obs = 0;
431 //callback for grep_databases
432 static void select_targets_callback(void *context, struct session_database *db)
434 struct session *se = (struct session*) context;
435 struct client *cl = client_create();
436 client_set_database(cl, db);
437 client_set_session(cl, se);
440 // Associates a set of clients with a session;
441 // Note: Session-databases represent databases with per-session
443 int select_targets(struct session *se, struct database_criterion *crit)
446 client_destroy(se->clients);
448 return session_grep_databases(se, crit, select_targets_callback);
451 int session_active_clients(struct session *s)
456 for (c = s->clients; c; c = client_next_in_session(c))
457 if (client_is_active(c))
463 // parses crit1=val1,crit2=val2|val3,...
464 static struct database_criterion *parse_filter(NMEM m, const char *buf)
466 struct database_criterion *res = 0;
473 nmem_strsplit(m, ",", buf, &values, &num);
474 for (i = 0; i < num; i++)
479 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
480 char *eq = strchr(values[i], '=');
483 yaz_log(YLOG_WARN, "Missing equal-sign in filter");
487 new->name = values[i];
488 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
490 for (subi = 0; subi < subnum; subi++)
492 struct database_criterion_value *newv
493 = nmem_malloc(m, sizeof(*newv));
494 newv->value = subvalues[subi];
495 newv->next = new->values;
504 enum pazpar2_error_code search(struct session *se,
505 const char *query, const char *filter,
506 const char **addinfo)
508 int live_channels = 0;
512 struct database_criterion *criteria;
514 yaz_log(YLOG_DEBUG, "Search");
517 nmem_reset(se->nmem);
519 se->total_records = se->total_hits = se->total_merged = 0;
521 se->num_termlists = 0;
522 criteria = parse_filter(se->nmem, filter);
523 live_channels = select_targets(se, criteria);
526 int maxrecs = live_channels * global_parameters.toget; // This is buggy!!!
527 se->reclist = reclist_create(se->nmem, maxrecs);
528 se->expected_maxrecs = maxrecs;
531 return PAZPAR2_NO_TARGETS;
533 for (cl = se->clients; cl; cl = client_next_in_session(cl))
535 if (prepare_session_database(se, client_get_database(cl)) < 0)
537 *addinfo = client_get_database(cl)->database->url;
538 return PAZPAR2_CONFIG_TARGET;
540 // Parse query for target
541 if (client_parse_query(cl, query) < 0)
546 if (client_prep_connection(cl))
547 client_start_search(cl);
551 // If no queries could be mapped, we signal an error
555 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
557 return PAZPAR2_NO_ERROR;
560 // Creates a new session_database object for a database
561 static void session_init_databases_fun(void *context, struct database *db)
563 struct session *se = (struct session *) context;
564 struct conf_service *service = se->service;
565 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
566 int num = settings_num(service);
573 = nmem_malloc(se->session_nmem, sizeof(struct settings *) * num);
574 memset(new->settings, 0, sizeof(struct settings*) * num);
578 for (i = 0; i < num; i++)
579 new->settings[i] = db->settings[i];
581 new->next = se->databases;
585 // Doesn't free memory associated with sdb -- nmem takes care of that
586 static void session_database_destroy(struct session_database *sdb)
588 struct database_retrievalmap *m;
590 for (m = sdb->map; m; m = m->next)
591 xsltFreeStylesheet(m->stylesheet);
594 // Initialize session_database list -- this represents this session's view
595 // of the database list -- subject to modification by the settings ws command
596 void session_init_databases(struct session *se)
599 predef_grep_databases(se, se->service, 0, session_init_databases_fun);
602 // Probably session_init_databases_fun should be refactored instead of
604 static struct session_database *load_session_database(struct session *se,
607 struct database *db = find_database(id, 0, se->service);
609 session_init_databases_fun((void*) se, db);
610 // New sdb is head of se->databases list
611 return se->databases;
614 // Find an existing session database. If not found, load it
615 static struct session_database *find_session_database(struct session *se,
618 struct session_database *sdb;
620 for (sdb = se->databases; sdb; sdb = sdb->next)
621 if (!strcmp(sdb->database->url, id))
623 return load_session_database(se, id);
626 // Apply a session override to a database
627 void session_apply_setting(struct session *se, char *dbname, char *setting,
630 struct session_database *sdb = find_session_database(se, dbname);
631 struct conf_service *service = se->service;
632 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
633 int offset = settings_offset_cprefix(service, setting);
637 yaz_log(YLOG_WARN, "Unknown setting %s", setting);
640 // Jakub: This breaks the filter setting.
641 /*if (offset == PZ_ID)
643 yaz_log(YLOG_WARN, "No need to set pz:id setting. Ignoring");
647 new->target = dbname;
650 new->next = sdb->settings[offset];
651 sdb->settings[offset] = new;
653 // Force later recompute of settings-driven data structures
654 // (happens when a search starts and client connections are prepared)
660 struct database_retrievalmap *m;
661 // We don't worry about the map structure -- it's in nmem
662 for (m = sdb->map; m; m = m->next)
663 xsltFreeStylesheet(m->stylesheet);
670 void destroy_session(struct session *s)
672 struct session_database *sdb;
675 client_destroy(s->clients);
676 for (sdb = s->databases; sdb; sdb = sdb->next)
677 session_database_destroy(sdb);
678 nmem_destroy(s->nmem);
679 wrbuf_destroy(s->wrbuf);
682 struct session *new_session(NMEM nmem, struct conf_service *service)
685 struct session *session = nmem_malloc(nmem, sizeof(*session));
687 yaz_log(YLOG_DEBUG, "New Pazpar2 session");
689 session->service = service;
690 session->relevance = 0;
691 session->total_hits = 0;
692 session->total_records = 0;
693 session->number_of_warnings_unknown_elements = 0;
694 session->number_of_warnings_unknown_metadata = 0;
695 session->num_termlists = 0;
696 session->reclist = 0;
697 session->clients = 0;
698 session->expected_maxrecs = 0;
699 session->session_nmem = nmem;
700 session->nmem = nmem_create();
701 session->wrbuf = wrbuf_alloc();
702 session->databases = 0;
703 for (i = 0; i <= SESSION_WATCH_MAX; i++)
705 session->watchlist[i].data = 0;
706 session->watchlist[i].fun = 0;
711 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
713 struct hitsbytarget *res = 0;
717 for (cl = se->clients; cl; cl = client_next_in_session(cl))
720 res = nmem_malloc(nmem, sizeof(*res) * sz);
722 for (cl = se->clients; cl; cl = client_next_in_session(cl))
724 const char *name = session_setting_oneval(client_get_database(cl),
727 res[*count].id = client_get_database(cl)->database->url;
728 res[*count].name = *name ? name : "Unknown";
729 res[*count].hits = client_get_hits(cl);
730 res[*count].records = client_get_num_records(cl);
731 res[*count].diagnostic = client_get_diagnostic(cl);
732 res[*count].state = client_get_state_str(cl);
733 res[*count].connected = client_get_connection(cl) ? 1 : 0;
739 struct termlist_score **termlist(struct session *s, const char *name, int *num)
743 for (i = 0; i < s->num_termlists; i++)
744 if (!strcmp((const char *) s->termlists[i].name, name))
745 return termlist_highscore(s->termlists[i].termlist, num);
749 #ifdef MISSING_HEADERS
750 void report_nmem_stats(void)
752 size_t in_use, is_free;
754 nmem_get_memory_in_use(&in_use);
755 nmem_get_memory_free(&is_free);
757 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
758 (long) in_use, (long) is_free);
762 struct record_cluster *show_single(struct session *s, const char *id,
763 struct record_cluster **prev_r,
764 struct record_cluster **next_r)
766 struct record_cluster *r;
768 reclist_rewind(s->reclist);
771 while ((r = reclist_read_record(s->reclist)))
773 if (!strcmp(r->recid, id))
775 *next_r = reclist_read_record(s->reclist);
783 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp,
784 int start, int *num, int *total, int *sumhits,
787 struct record_cluster **recs = nmem_malloc(nmem_show, *num
788 * sizeof(struct record_cluster *));
789 struct reclist_sortparms *spp;
792 yaz_timing_t t = yaz_timing_create();
804 for (spp = sp; spp; spp = spp->next)
805 if (spp->type == Metadata_sortkey_relevance)
807 relevance_prepare_read(s->relevance, s->reclist);
810 reclist_sort(s->reclist, sp);
812 *total = s->reclist->num_records;
813 *sumhits = s->total_hits;
815 for (i = 0; i < start; i++)
816 if (!reclist_read_record(s->reclist))
823 for (i = 0; i < *num; i++)
825 struct record_cluster *r = reclist_read_record(s->reclist);
836 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
837 yaz_timing_get_real(t), yaz_timing_get_user(t),
838 yaz_timing_get_sys(t));
839 yaz_timing_destroy(&t);
844 void statistics(struct session *se, struct statistics *stat)
849 memset(stat, 0, sizeof(*stat));
850 for (cl = se->clients; cl; cl = client_next_in_session(cl))
852 if (!client_get_connection(cl))
853 stat->num_no_connection++;
854 switch (client_get_state(cl))
856 case Client_Connecting: stat->num_connecting++; break;
857 case Client_Working: stat->num_working++; break;
858 case Client_Idle: stat->num_idle++; break;
859 case Client_Failed: stat->num_failed++; break;
860 case Client_Error: stat->num_error++; break;
865 stat->num_hits = se->total_hits;
866 stat->num_records = se->total_records;
868 stat->num_clients = count;
872 // Master list of connections we're handling events to
873 static IOCHAN channel_list = 0; /* thread pr */
875 void pazpar2_add_channel(IOCHAN chan)
877 chan->next = channel_list;
881 void pazpar2_event_loop()
883 event_loop(&channel_list);
886 static struct record_metadata *record_metadata_init(
887 NMEM nmem, char *value, enum conf_metadata_type type)
889 struct record_metadata *rec_md = record_metadata_create(nmem);
890 if (type == Metadata_type_generic)
893 p = normalize7bit_generic(p, " ,/.:([");
895 rec_md->data.text.disp = nmem_strdup(nmem, p);
896 rec_md->data.text.sort = 0;
898 else if (type == Metadata_type_year || type == Metadata_type_date)
903 if (type == Metadata_type_date)
905 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
908 rec_md->data.number.min = first;
909 rec_md->data.number.max = last;
916 static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
917 struct conf_service *service, NMEM nmem)
919 char *mergekey_norm = 0;
920 xmlNode *root = xmlDocGetRootElement(doc);
921 WRBUF norm_wr = wrbuf_alloc();
924 /* create mergekey based on mergekey attribute from XSL (if any) */
925 xmlChar *mergekey = xmlGetProp(root, (xmlChar *) "mergekey");
928 const char *norm_str;
929 pp2_relevance_token_t prt =
930 pp2_relevance_tokenize(
931 service->mergekey_pct,
932 (const char *) mergekey);
934 while ((norm_str = pp2_relevance_token_next(prt)))
938 if (wrbuf_len(norm_wr))
939 wrbuf_puts(norm_wr, " ");
940 wrbuf_puts(norm_wr, norm_str);
943 pp2_relevance_token_destroy(prt);
946 /* append (if any) mergekey=yes metadata values */
947 for (n = root->children; n; n = n->next)
949 if (n->type != XML_ELEMENT_NODE)
951 if (!strcmp((const char *) n->name, "metadata"))
953 struct conf_metadata *ser_md = 0;
954 int md_field_id = -1;
956 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
962 = conf_service_metadata_field_id(service,
963 (const char *) type);
964 if (md_field_id >= 0)
966 ser_md = &service->metadata[md_field_id];
967 if (ser_md->mergekey == Metadata_mergekey_yes)
969 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
972 const char *norm_str;
973 pp2_relevance_token_t prt =
974 pp2_relevance_tokenize(
975 service->mergekey_pct,
976 (const char *) value);
978 while ((norm_str = pp2_relevance_token_next(prt)))
982 if (wrbuf_len(norm_wr))
983 wrbuf_puts(norm_wr, " ");
984 wrbuf_puts(norm_wr, norm_str);
988 pp2_relevance_token_destroy(prt);
996 /* generate unique key if none is not generated already or is empty */
997 if (wrbuf_len(norm_wr) == 0)
999 wrbuf_printf(norm_wr, "%s-%d",
1000 client_get_database(cl)->database->url, record_no);
1002 if (wrbuf_len(norm_wr) > 0)
1003 mergekey_norm = nmem_strdup(nmem, wrbuf_cstr(norm_wr));
1004 wrbuf_destroy(norm_wr);
1005 return mergekey_norm;
1010 /** \brief ingest XML record
1011 \param cl client holds the result set for record
1012 \param rec record buffer (0 terminated)
1013 \param record_no record position (1, 2, ..)
1014 \returns resulting record or NULL on failure
1016 struct record *ingest_record(struct client *cl, const char *rec,
1019 xmlDoc *xdoc = normalize_record(client_get_database(cl),
1020 client_get_session(cl), rec);
1022 struct record *record;
1023 struct record_cluster *cluster;
1024 struct session *se = client_get_session(cl);
1025 const char *mergekey_norm;
1028 struct conf_service *service = se->service;
1033 root = xmlDocGetRootElement(xdoc);
1035 mergekey_norm = get_mergekey(xdoc, cl, record_no, service, se->nmem);
1038 yaz_log(YLOG_WARN, "Got no mergekey");
1042 record = record_create(se->nmem,
1043 service->num_metadata, service->num_sortkeys, cl,
1046 cluster = reclist_insert(se->reclist,
1048 record, (char *) mergekey_norm,
1050 if (global_parameters.dump_records)
1051 yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1052 client_get_database(cl)->database->url, record_no);
1055 /* no room for record */
1059 relevance_newrec(se->relevance, cluster);
1061 // now parsing XML record and adding data to cluster or record metadata
1062 for (n = root->children; n; n = n->next)
1064 pp2_relevance_token_t prt;
1071 if (n->type != XML_ELEMENT_NODE)
1073 if (!strcmp((const char *) n->name, "metadata"))
1075 struct conf_metadata *ser_md = 0;
1076 struct conf_sortkey *ser_sk = 0;
1077 struct record_metadata **wheretoput = 0;
1078 struct record_metadata *rec_md = 0;
1079 int md_field_id = -1;
1080 int sk_field_id = -1;
1082 type = xmlGetProp(n, (xmlChar *) "type");
1083 value = xmlNodeListGetString(xdoc, n->children, 1);
1085 if (!type || !value || !*value)
1089 = conf_service_metadata_field_id(service, (const char *) type);
1090 if (md_field_id < 0)
1092 if (se->number_of_warnings_unknown_metadata == 0)
1095 "Ignoring unknown metadata element: %s", type);
1097 se->number_of_warnings_unknown_metadata++;
1101 ser_md = &service->metadata[md_field_id];
1103 if (ser_md->sortkey_offset >= 0){
1104 sk_field_id = ser_md->sortkey_offset;
1105 ser_sk = &service->sortkeys[sk_field_id];
1108 // non-merged metadata
1109 rec_md = record_metadata_init(se->nmem, (char *) value,
1113 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1117 wheretoput = &record->metadata[md_field_id];
1119 wheretoput = &(*wheretoput)->next;
1120 *wheretoput = rec_md;
1123 rec_md = record_metadata_init(se->nmem, (char *) value,
1125 wheretoput = &cluster->metadata[md_field_id];
1127 // and polulate with data:
1128 // assign cluster or record based on merge action
1129 if (ser_md->merge == Metadata_merge_unique)
1131 struct record_metadata *mnode;
1132 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1133 if (!strcmp((const char *) mnode->data.text.disp,
1134 rec_md->data.text.disp))
1138 rec_md->next = *wheretoput;
1139 *wheretoput = rec_md;
1142 else if (ser_md->merge == Metadata_merge_longest)
1145 || strlen(rec_md->data.text.disp)
1146 > strlen((*wheretoput)->data.text.disp))
1148 *wheretoput = rec_md;
1151 const char *sort_str = 0;
1153 ser_sk->type == Metadata_sortkey_skiparticle;
1155 if (!cluster->sortkeys[sk_field_id])
1156 cluster->sortkeys[sk_field_id] =
1157 nmem_malloc(se->nmem,
1158 sizeof(union data_types));
1160 prt = pp2_relevance_tokenize(
1162 rec_md->data.text.disp);
1164 pp2_relevance_token_next(prt);
1166 sort_str = pp2_get_sort(prt, skip_article);
1168 cluster->sortkeys[sk_field_id]->text.disp =
1169 rec_md->data.text.disp;
1172 sort_str = rec_md->data.text.disp;
1174 "Could not make sortkey. Bug #1858");
1176 cluster->sortkeys[sk_field_id]->text.sort =
1177 nmem_strdup(se->nmem, sort_str);
1179 yaz_log(YLOG_LOG, "text disp=%s",
1180 cluster->sortkeys[sk_field_id]->text.disp);
1181 yaz_log(YLOG_LOG, "text sort=%s",
1182 cluster->sortkeys[sk_field_id]->text.sort);
1184 pp2_relevance_token_destroy(prt);
1188 else if (ser_md->merge == Metadata_merge_all)
1190 rec_md->next = *wheretoput;
1191 *wheretoput = rec_md;
1193 else if (ser_md->merge == Metadata_merge_range)
1197 *wheretoput = rec_md;
1199 cluster->sortkeys[sk_field_id]
1204 int this_min = rec_md->data.number.min;
1205 int this_max = rec_md->data.number.max;
1206 if (this_min < (*wheretoput)->data.number.min)
1207 (*wheretoput)->data.number.min = this_min;
1208 if (this_max > (*wheretoput)->data.number.max)
1209 (*wheretoput)->data.number.max = this_max;
1214 // ranking of _all_ fields enabled ...
1216 relevance_countwords(se->relevance, cluster,
1217 (char *) value, ser_md->rank);
1219 // construct facets ...
1220 if (ser_md->termlist)
1222 if (ser_md->type == Metadata_type_year)
1225 sprintf(year, "%d", rec_md->data.number.max);
1226 add_facet(se, (char *) type, year);
1227 if (rec_md->data.number.max != rec_md->data.number.min)
1229 sprintf(year, "%d", rec_md->data.number.min);
1230 add_facet(se, (char *) type, year);
1234 add_facet(se, (char *) type, (char *) value);
1244 if (se->number_of_warnings_unknown_elements == 0)
1246 "Unexpected element in internal record: %s", n->name);
1247 se->number_of_warnings_unknown_elements++;
1257 relevance_donerecord(se->relevance, cluster);
1258 se->total_records++;
1268 * c-file-style: "Stroustrup"
1269 * indent-tabs-mode: nil
1271 * vim: shiftwidth=4 tabstop=8 expandtab