1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2008 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
38 #include <sys/socket.h>
47 #include <yaz/marcdisp.h>
48 #include <yaz/comstack.h>
49 #include <yaz/tcpip.h>
50 #include <yaz/proto.h>
51 #include <yaz/readconf.h>
52 #include <yaz/pquery.h>
53 #include <yaz/otherinfo.h>
54 #include <yaz/yaz-util.h>
56 #include <yaz/query-charset.h>
57 #include <yaz/querytowrbuf.h>
58 #include <yaz/oid_db.h>
59 #include <yaz/snprintf.h>
63 #include <yaz/timing.h>
67 #include <netinet/in.h>
73 #include "termlists.h"
75 #include "relevance.h"
79 #include "normalize7bit.h"
81 #define TERMLIST_HIGH_SCORE 25
85 // Note: Some things in this structure will eventually move to configuration
86 struct parameters global_parameters =
94 30, // operations timeout
98 60, // session timeout
103 180, // Z39.50 session timeout
104 15 // Connect timeout
107 // Recursively traverse query structure to extract terms.
108 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
120 pull_terms(nmem, n->u.p[0], termlist, num);
121 pull_terms(nmem, n->u.p[1], termlist, num);
124 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
125 for (i = 0; i < numwords; i++)
126 termlist[(*num)++] = words[i];
135 static void add_facet(struct session *s, const char *type, const char *value)
141 for (i = 0; i < s->num_termlists; i++)
142 if (!strcmp(s->termlists[i].name, type))
144 if (i == s->num_termlists)
146 if (i == SESSION_MAX_TERMLISTS)
148 yaz_log(YLOG_FATAL, "Too many termlists");
152 s->termlists[i].name = nmem_strdup(s->nmem, type);
153 s->termlists[i].termlist
154 = termlist_create(s->nmem, s->expected_maxrecs,
155 TERMLIST_HIGH_SCORE);
156 s->num_termlists = i + 1;
158 termlist_insert(s->termlists[i].termlist, value);
161 xmlDoc *record_to_xml(struct session_database *sdb, const char *rec)
163 struct database *db = sdb->database;
166 rdoc = xmlParseMemory(rec, strlen(rec));
170 yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s",
175 if (global_parameters.dump_records)
177 FILE *lf = yaz_log_file();
180 yaz_log(YLOG_LOG, "Un-normalized record from %s", db->url);
181 #if LIBXML_VERSION >= 20600
182 xmlDocFormatDump(lf, rdoc, 1);
184 xmlDocDump(lf, rdoc);
193 #define MAX_XSLT_ARGS 16
195 // Add static values from session database settings if applicable
196 static void insert_settings_parameters(struct session_database *sdb,
197 struct session *se, char **parms)
199 struct conf_service *service = global_parameters.server->service;
204 for (i = 0; i < service->num_metadata; i++)
206 struct conf_metadata *md = &service->metadata[i];
209 if (md->setting == Metadata_setting_parameter &&
210 (setting = settings_offset(md->name)) > 0)
212 const char *val = session_setting_oneval(sdb, setting);
213 if (val && nparms < MAX_XSLT_ARGS)
216 int len = strlen(val);
217 buf = nmem_malloc(se->nmem, len + 3);
219 strcpy(buf + 1, val);
222 parms[offset++] = md->name;
223 parms[offset++] = buf;
231 // Add static values from session database settings if applicable
232 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc)
234 struct conf_service *service = global_parameters.server->service;
237 for (i = 0; i < service->num_metadata; i++)
239 struct conf_metadata *md = &service->metadata[i];
242 if (md->setting == Metadata_setting_postproc &&
243 (offset = settings_offset(md->name)) > 0)
245 const char *val = session_setting_oneval(sdb, offset);
248 xmlNode *r = xmlDocGetRootElement(doc);
249 xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
251 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
257 xmlDoc *normalize_record(struct session_database *sdb, struct session *se,
260 struct database_retrievalmap *m;
261 xmlDoc *rdoc = record_to_xml(sdb, rec);
264 for (m = sdb->map; m; m = m->next)
270 char *parms[MAX_XSLT_ARGS*2+1];
272 insert_settings_parameters(sdb, se, parms);
274 new = xsltApplyStylesheet(m->stylesheet, rdoc, (const char **) parms);
275 root= xmlDocGetRootElement(new);
276 if (!new || !root || !(root->children))
278 yaz_log(YLOG_WARN, "XSLT transformation failed from %s",
290 insert_settings_values(sdb, rdoc);
292 if (global_parameters.dump_records)
294 FILE *lf = yaz_log_file();
298 yaz_log(YLOG_LOG, "Normalized record from %s",
300 #if LIBXML_VERSION >= 20600
301 xmlDocFormatDump(lf, rdoc, 1);
303 xmlDocDump(lf, rdoc);
312 // Retrieve first defined value for 'name' for given database.
313 // Will be extended to take into account user associated with session
314 const char *session_setting_oneval(struct session_database *db, int offset)
316 if (!db->settings[offset])
318 return db->settings[offset]->value;
323 // Initialize YAZ Map structures for MARC-based targets
324 static int prepare_yazmarc(struct session_database *sdb)
330 yaz_log(YLOG_WARN, "No settings for %s", sdb->database->url);
333 if ((s = session_setting_oneval(sdb, PZ_NATIVESYNTAX))
334 && !strncmp(s, "iso2709", 7))
336 char *encoding = "marc-8s", *e;
339 // See if a native encoding is specified
340 if ((e = strchr(s, ';')))
343 sdb->yaz_marc = yaz_marc_create();
344 yaz_marc_subfield_str(sdb->yaz_marc, "\t");
346 cm = yaz_iconv_open("utf-8", encoding);
350 "Unable to map from %s to UTF-8 for target %s",
351 encoding, sdb->database->url);
354 yaz_marc_iconv(sdb->yaz_marc, cm);
359 // Prepare XSLT stylesheets for record normalization
360 // Structures are allocated on the session_wide nmem to avoid having
361 // to recompute this for every search. This would lead
362 // to leaking if a single session was to repeatedly change the PZ_XSLT
363 // setting. However, this is not a realistic use scenario.
364 static int prepare_map(struct session *se, struct session_database *sdb)
370 yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url);
373 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
376 struct database_retrievalmap **m = &sdb->map;
378 char auto_stylesheet[256];
380 if (!strcmp(s, "auto"))
382 const char *request_syntax = session_setting_oneval(
383 sdb, PZ_REQUESTSYNTAX);
387 yaz_snprintf(auto_stylesheet, sizeof(auto_stylesheet),
388 "%s.xsl", request_syntax);
389 for (cp = auto_stylesheet; *cp; cp++)
391 /* deliberately only consider ASCII */
392 if (*cp > 32 && *cp < 127)
399 yaz_log(YLOG_WARN, "No pz:requestsyntax for auto stylesheet");
402 nmem_strsplit(se->session_nmem, ",", s, &stylesheets, &num);
403 for (i = 0; i < num; i++)
405 (*m) = nmem_malloc(se->session_nmem, sizeof(**m));
407 if (!((*m)->stylesheet = conf_load_stylesheet(stylesheets[i])))
409 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
417 yaz_log(YLOG_WARN, "No Normalization stylesheet for target %s",
422 // This analyzes settings and recomputes any supporting data structures
424 static int prepare_session_database(struct session *se,
425 struct session_database *sdb)
430 "No settings associated with %s", sdb->database->url);
433 if (sdb->settings[PZ_NATIVESYNTAX] && !sdb->yaz_marc)
435 if (prepare_yazmarc(sdb) < 0)
438 if (sdb->settings[PZ_XSLT] && !sdb->map)
440 if (prepare_map(se, sdb) < 0)
446 // called if watch should be removed because http_channel is to be destroyed
447 static void session_watch_cancel(void *data, struct http_channel *c,
450 struct session_watchentry *ent = data;
457 // set watch. Returns 0=OK, -1 if watch is already set
458 int session_set_watch(struct session *s, int what,
459 session_watchfun fun, void *data,
460 struct http_channel *chan)
462 if (s->watchlist[what].fun)
464 s->watchlist[what].fun = fun;
465 s->watchlist[what].data = data;
466 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
467 session_watch_cancel);
471 void session_alert_watch(struct session *s, int what)
473 if (s->watchlist[what].fun)
475 /* our watch is no longer associated with http_channel */
477 session_watchfun fun;
479 http_remove_observer(s->watchlist[what].obs);
480 fun = s->watchlist[what].fun;
481 data = s->watchlist[what].data;
483 /* reset watch before fun is invoked - in case fun wants to set
485 s->watchlist[what].fun = 0;
486 s->watchlist[what].data = 0;
487 s->watchlist[what].obs = 0;
493 //callback for grep_databases
494 static void select_targets_callback(void *context, struct session_database *db)
496 struct session *se = (struct session*) context;
497 struct client *cl = client_create();
498 client_set_database(cl, db);
499 client_set_session(cl, se);
502 // Associates a set of clients with a session;
503 // Note: Session-databases represent databases with per-session
505 int select_targets(struct session *se, struct database_criterion *crit)
508 client_destroy(se->clients);
510 return session_grep_databases(se, crit, select_targets_callback);
513 int session_active_clients(struct session *s)
518 for (c = s->clients; c; c = client_next_in_session(c))
519 if (client_is_active(c))
525 // parses crit1=val1,crit2=val2|val3,...
526 static struct database_criterion *parse_filter(NMEM m, const char *buf)
528 struct database_criterion *res = 0;
535 nmem_strsplit(m, ",", buf, &values, &num);
536 for (i = 0; i < num; i++)
541 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
542 char *eq = strchr(values[i], '=');
545 yaz_log(YLOG_WARN, "Missing equal-sign in filter");
549 new->name = values[i];
550 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
552 for (subi = 0; subi < subnum; subi++)
554 struct database_criterion_value *newv
555 = nmem_malloc(m, sizeof(*newv));
556 newv->value = subvalues[subi];
557 newv->next = new->values;
566 enum pazpar2_error_code search(struct session *se,
567 char *query, char *filter,
568 const char **addinfo)
570 int live_channels = 0;
574 struct database_criterion *criteria;
576 yaz_log(YLOG_DEBUG, "Search");
579 nmem_reset(se->nmem);
581 se->total_records = se->total_hits = se->total_merged = 0;
583 se->num_termlists = 0;
584 criteria = parse_filter(se->nmem, filter);
586 live_channels = select_targets(se, criteria);
589 int maxrecs = live_channels * global_parameters.toget;
590 se->reclist = reclist_create(se->nmem, maxrecs);
591 se->expected_maxrecs = maxrecs;
594 return PAZPAR2_NO_TARGETS;
596 for (cl = se->clients; cl; cl = client_next_in_session(cl))
598 if (prepare_session_database(se, client_get_database(cl)) < 0)
600 *addinfo = client_get_database(cl)->database->url;
601 return PAZPAR2_CONFIG_TARGET;
603 // Parse query for target
604 if (client_parse_query(cl, query) < 0)
609 if (client_prep_connection(cl))
610 client_start_search(cl);
614 // If no queries could be mapped, we signal an error
618 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
620 return PAZPAR2_NO_ERROR;
623 // Creates a new session_database object for a database
624 static void session_init_databases_fun(void *context, struct database *db)
626 struct session *se = (struct session *) context;
627 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
628 int num = settings_num();
636 = nmem_malloc(se->session_nmem, sizeof(struct settings *) * num);
637 memset(new->settings, 0, sizeof(struct settings*) * num);
641 for (i = 0; i < num; i++)
642 new->settings[i] = db->settings[i];
644 new->next = se->databases;
648 // Doesn't free memory associated with sdb -- nmem takes care of that
649 static void session_database_destroy(struct session_database *sdb)
651 struct database_retrievalmap *m;
653 for (m = sdb->map; m; m = m->next)
654 xsltFreeStylesheet(m->stylesheet);
656 yaz_marc_destroy(sdb->yaz_marc);
659 // Initialize session_database list -- this represents this session's view
660 // of the database list -- subject to modification by the settings ws command
661 void session_init_databases(struct session *se)
664 predef_grep_databases(se, 0, session_init_databases_fun);
667 // Probably session_init_databases_fun should be refactored instead of
669 static struct session_database *load_session_database(struct session *se,
672 struct database *db = find_database(id, 0);
674 session_init_databases_fun((void*) se, db);
675 // New sdb is head of se->databases list
676 return se->databases;
679 // Find an existing session database. If not found, load it
680 static struct session_database *find_session_database(struct session *se,
683 struct session_database *sdb;
685 for (sdb = se->databases; sdb; sdb = sdb->next)
686 if (!strcmp(sdb->database->url, id))
688 return load_session_database(se, id);
691 // Apply a session override to a database
692 void session_apply_setting(struct session *se, char *dbname, char *setting,
695 struct session_database *sdb = find_session_database(se, dbname);
696 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
697 int offset = settings_offset_cprefix(setting);
701 yaz_log(YLOG_WARN, "Unknown setting %s", setting);
704 // Jakub: This breaks the filter setting.
705 /*if (offset == PZ_ID)
707 yaz_log(YLOG_WARN, "No need to set pz:id setting. Ignoring");
711 new->target = dbname;
714 new->next = sdb->settings[offset];
715 sdb->settings[offset] = new;
717 // Force later recompute of settings-driven data structures
718 // (happens when a search starts and client connections are prepared)
721 case PZ_NATIVESYNTAX:
724 yaz_marc_destroy(sdb->yaz_marc);
731 struct database_retrievalmap *m;
732 // We don't worry about the map structure -- it's in nmem
733 for (m = sdb->map; m; m = m->next)
734 xsltFreeStylesheet(m->stylesheet);
741 void destroy_session(struct session *s)
743 struct session_database *sdb;
746 client_destroy(s->clients);
747 for (sdb = s->databases; sdb; sdb = sdb->next)
748 session_database_destroy(sdb);
749 nmem_destroy(s->nmem);
750 wrbuf_destroy(s->wrbuf);
753 struct session *new_session(NMEM nmem)
756 struct session *session = nmem_malloc(nmem, sizeof(*session));
758 yaz_log(YLOG_DEBUG, "New Pazpar2 session");
760 session->relevance = 0;
761 session->total_hits = 0;
762 session->total_records = 0;
763 session->num_termlists = 0;
764 session->reclist = 0;
765 session->requestid = -1;
766 session->clients = 0;
767 session->expected_maxrecs = 0;
768 session->session_nmem = nmem;
769 session->nmem = nmem_create();
770 session->wrbuf = wrbuf_alloc();
771 session->databases = 0;
772 for (i = 0; i <= SESSION_WATCH_MAX; i++)
774 session->watchlist[i].data = 0;
775 session->watchlist[i].fun = 0;
780 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
782 struct hitsbytarget *res = 0;
786 for (cl = se->clients; cl; cl = client_next_in_session(cl))
789 res = nmem_malloc(nmem, sizeof(*res) * sz);
791 for (cl = se->clients; cl; cl = client_next_in_session(cl))
793 const char *name = session_setting_oneval(client_get_database(cl),
796 res[*count].id = client_get_database(cl)->database->url;
797 res[*count].name = *name ? name : "Unknown";
798 res[*count].hits = client_get_hits(cl);
799 res[*count].records = client_get_num_records(cl);
800 res[*count].diagnostic = client_get_diagnostic(cl);
801 res[*count].state = client_get_state_str(cl);
802 res[*count].connected = client_get_connection(cl) ? 1 : 0;
808 struct termlist_score **termlist(struct session *s, const char *name, int *num)
812 for (i = 0; i < s->num_termlists; i++)
813 if (!strcmp((const char *) s->termlists[i].name, name))
814 return termlist_highscore(s->termlists[i].termlist, num);
818 #ifdef MISSING_HEADERS
819 void report_nmem_stats(void)
821 size_t in_use, is_free;
823 nmem_get_memory_in_use(&in_use);
824 nmem_get_memory_free(&is_free);
826 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
827 (long) in_use, (long) is_free);
831 struct record_cluster *show_single(struct session *s, const char *id)
833 struct record_cluster *r;
835 reclist_rewind(s->reclist);
836 while ((r = reclist_read_record(s->reclist)))
837 if (!strcmp(r->recid, id))
842 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp,
843 int start, int *num, int *total, int *sumhits,
846 struct record_cluster **recs = nmem_malloc(nmem_show, *num
847 * sizeof(struct record_cluster *));
848 struct reclist_sortparms *spp;
851 yaz_timing_t t = yaz_timing_create();
863 for (spp = sp; spp; spp = spp->next)
864 if (spp->type == Metadata_sortkey_relevance)
866 relevance_prepare_read(s->relevance, s->reclist);
869 reclist_sort(s->reclist, sp);
871 *total = s->reclist->num_records;
872 *sumhits = s->total_hits;
874 for (i = 0; i < start; i++)
875 if (!reclist_read_record(s->reclist))
882 for (i = 0; i < *num; i++)
884 struct record_cluster *r = reclist_read_record(s->reclist);
895 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
896 yaz_timing_get_real(t), yaz_timing_get_user(t),
897 yaz_timing_get_sys(t));
898 yaz_timing_destroy(&t);
903 void statistics(struct session *se, struct statistics *stat)
908 memset(stat, 0, sizeof(*stat));
909 for (cl = se->clients; cl; cl = client_next_in_session(cl))
911 if (!client_get_connection(cl))
912 stat->num_no_connection++;
913 switch (client_get_state(cl))
915 case Client_Connecting: stat->num_connecting++; break;
916 case Client_Working: stat->num_working++; break;
917 case Client_Idle: stat->num_idle++; break;
918 case Client_Failed: stat->num_failed++; break;
919 case Client_Error: stat->num_error++; break;
924 stat->num_hits = se->total_hits;
925 stat->num_records = se->total_records;
927 stat->num_clients = count;
930 int start_http_listener(void)
933 struct conf_server *ser = global_parameters.server;
935 if (*global_parameters.listener_override)
936 strcpy(hp, global_parameters.listener_override);
939 strcpy(hp, ser->host ? ser->host : "");
944 sprintf(hp + strlen(hp), "%d", ser->port);
947 return http_init(hp);
950 void start_proxy(void)
953 struct conf_server *ser = global_parameters.server;
955 if (*global_parameters.proxy_override)
956 strcpy(hp, global_parameters.proxy_override);
957 else if (ser->proxy_host || ser->proxy_port)
959 strcpy(hp, ser->proxy_host ? ser->proxy_host : "");
964 sprintf(hp + strlen(hp), "%d", ser->proxy_port);
970 http_set_proxyaddr(hp, ser->myurl ? ser->myurl : "");
974 // Master list of connections we're handling events to
975 static IOCHAN channel_list = 0;
976 void pazpar2_add_channel(IOCHAN chan)
978 chan->next = channel_list;
982 void pazpar2_event_loop()
984 event_loop(&channel_list);
987 static struct record_metadata *record_metadata_init(
988 NMEM nmem, char *value, enum conf_metadata_type type)
990 struct record_metadata *rec_md = record_metadata_create(nmem);
991 if (type == Metadata_type_generic)
994 p = normalize7bit_generic(p, " ,/.:([");
996 rec_md->data.text.disp = nmem_strdup(nmem, p);
997 rec_md->data.text.sort = 0;
999 else if (type == Metadata_type_year || type == Metadata_type_date)
1004 if (type == Metadata_type_date)
1006 if (extract7bit_dates((char *) value, &first, &last, longdate) < 0)
1009 rec_md->data.number.min = first;
1010 rec_md->data.number.max = last;
1017 struct record *ingest_record(struct client *cl, const char *rec,
1020 xmlDoc *xdoc = normalize_record(client_get_database(cl),
1021 client_get_session(cl), rec);
1023 struct record *record;
1024 struct record_cluster *cluster;
1025 struct session *se = client_get_session(cl);
1026 xmlChar *mergekey, *mergekey_norm;
1029 struct conf_service *service = global_parameters.server->service;
1030 const char *norm_str = 0;
1031 pp2_relevance_token_t prt = 0;
1037 root = xmlDocGetRootElement(xdoc);
1038 if (!(mergekey = xmlGetProp(root, (xmlChar *) "mergekey")))
1040 yaz_log(YLOG_WARN, "No mergekey found in record");
1045 record = record_create(se->nmem,
1046 service->num_metadata, service->num_sortkeys, cl,
1049 prt = pp2_relevance_tokenize(
1050 global_parameters.server->mergekey_pct, (const char *) mergekey);
1053 norm_wr = wrbuf_alloc();
1055 while ((norm_str = pp2_relevance_token_next(prt)))
1059 if (wrbuf_len(norm_wr))
1060 wrbuf_puts(norm_wr, " ");
1061 wrbuf_puts(norm_wr, norm_str);
1065 mergekey_norm = (xmlChar *)nmem_strdup(se->nmem, wrbuf_cstr(norm_wr));
1066 wrbuf_destroy(norm_wr);
1068 pp2_relevance_token_destroy(prt);
1072 cluster = reclist_insert(se->reclist,
1073 global_parameters.server->service,
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);
1088 // now parsing XML record and adding data to cluster or record metadata
1089 for (n = root->children; n; n = n->next)
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)
1119 "Ignoring unknown metadata element: %s", type);
1123 ser_md = &service->metadata[md_field_id];
1125 if (ser_md->sortkey_offset >= 0){
1126 sk_field_id = ser_md->sortkey_offset;
1127 ser_sk = &service->sortkeys[sk_field_id];
1130 // non-merged metadata
1131 rec_md = record_metadata_init(se->nmem, (char *) value,
1135 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1139 wheretoput = &record->metadata[md_field_id];
1141 wheretoput = &(*wheretoput)->next;
1143 *wheretoput = rec_md;
1146 rec_md = record_metadata_init(se->nmem, (char *) value,
1148 wheretoput = &cluster->metadata[md_field_id];
1150 // and polulate with data:
1151 // assign cluster or record based on merge action
1152 if (ser_md->merge == Metadata_merge_unique)
1154 struct record_metadata *mnode;
1155 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1156 if (!strcmp((const char *) mnode->data.text.disp,
1157 rec_md->data.text.disp))
1161 rec_md->next = *wheretoput;
1162 *wheretoput = rec_md;
1165 else if (ser_md->merge == Metadata_merge_longest)
1168 || strlen(rec_md->data.text.disp)
1169 > strlen((*wheretoput)->data.text.disp))
1171 *wheretoput = rec_md;
1174 const char *sort_str = 0;
1176 ser_sk->type == Metadata_sortkey_skiparticle;
1178 if (!cluster->sortkeys[sk_field_id])
1179 cluster->sortkeys[sk_field_id] =
1180 nmem_malloc(se->nmem,
1181 sizeof(union data_types));
1183 prt = pp2_relevance_tokenize(
1184 global_parameters.server->sort_pct,
1185 rec_md->data.text.disp);
1187 pp2_relevance_token_next(prt);
1189 sort_str = pp2_get_sort(prt, skip_article);
1191 cluster->sortkeys[sk_field_id]->text.disp =
1192 rec_md->data.text.disp;
1195 sort_str = rec_md->data.text.disp;
1197 "Could not make sortkey. Bug #1858");
1199 cluster->sortkeys[sk_field_id]->text.sort =
1200 nmem_strdup(se->nmem, sort_str);
1202 yaz_log(YLOG_LOG, "text disp=%s",
1203 cluster->sortkeys[sk_field_id]->text.disp);
1204 yaz_log(YLOG_LOG, "text sort=%s",
1205 cluster->sortkeys[sk_field_id]->text.sort);
1207 pp2_relevance_token_destroy(prt);
1211 else if (ser_md->merge == Metadata_merge_all)
1213 rec_md->next = *wheretoput;
1214 *wheretoput = rec_md;
1216 else if (ser_md->merge == Metadata_merge_range)
1220 *wheretoput = rec_md;
1222 cluster->sortkeys[sk_field_id]
1227 int this_min = rec_md->data.number.min;
1228 int this_max = rec_md->data.number.max;
1229 if (this_min < (*wheretoput)->data.number.min)
1230 (*wheretoput)->data.number.min = this_min;
1231 if (this_max > (*wheretoput)->data.number.max)
1232 (*wheretoput)->data.number.max = this_max;
1237 // ranking of _all_ fields enabled ...
1239 relevance_countwords(se->relevance, cluster,
1240 (char *) value, ser_md->rank);
1242 // construct facets ...
1243 if (ser_md->termlist)
1245 if (ser_md->type == Metadata_type_year)
1248 sprintf(year, "%d", rec_md->data.number.max);
1249 add_facet(se, (char *) type, year);
1250 if (rec_md->data.number.max != rec_md->data.number.min)
1252 sprintf(year, "%d", rec_md->data.number.min);
1253 add_facet(se, (char *) type, year);
1257 add_facet(se, (char *) type, (char *) value);
1267 "Unexpected element %s in internal record", n->name);
1276 relevance_donerecord(se->relevance, cluster);
1277 se->total_records++;
1287 * indent-tabs-mode: nil
1289 * vim: shiftwidth=4 tabstop=8 expandtab