1 /* $Id: logic.c,v 1.54 2007-07-16 17:01:46 adam Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 \brief high-level logic; mostly user sessions and settings
31 #include <sys/socket.h>
37 #include <yaz/marcdisp.h>
38 #include <yaz/comstack.h>
39 #include <yaz/tcpip.h>
40 #include <yaz/proto.h>
41 #include <yaz/readconf.h>
42 #include <yaz/pquery.h>
43 #include <yaz/otherinfo.h>
44 #include <yaz/yaz-util.h>
46 #include <yaz/query-charset.h>
47 #include <yaz/querytowrbuf.h>
48 #include <yaz/oid_db.h>
56 #include <yaz/timing.h>
59 #include <netinet/in.h>
64 #include "termlists.h"
66 #include "relevance.h"
71 #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 =
99 // Recursively traverse query structure to extract terms.
100 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
112 pull_terms(nmem, n->u.p[0], termlist, num);
113 pull_terms(nmem, n->u.p[1], termlist, num);
116 nmem_strsplit(nmem, " ", n->u.t.term, &words, &numwords);
117 for (i = 0; i < numwords; i++)
118 termlist[(*num)++] = words[i];
127 static void add_facet(struct session *s, const char *type, const char *value)
133 for (i = 0; i < s->num_termlists; i++)
134 if (!strcmp(s->termlists[i].name, type))
136 if (i == s->num_termlists)
138 if (i == SESSION_MAX_TERMLISTS)
140 yaz_log(YLOG_FATAL, "Too many termlists");
144 s->termlists[i].name = nmem_strdup(s->nmem, type);
145 s->termlists[i].termlist
146 = termlist_create(s->nmem, s->expected_maxrecs,
147 TERMLIST_HIGH_SCORE);
148 s->num_termlists = i + 1;
150 termlist_insert(s->termlists[i].termlist, value);
153 xmlDoc *record_to_xml(struct session_database *sdb, Z_External *rec)
155 struct database *db = sdb->database;
157 const Odr_oid *oid = rec->direct_reference;
159 /* convert response record to XML somehow */
160 if (rec->which == Z_External_octet && oid
161 && !oid_oidcmp(oid, yaz_oid_recsyn_xml))
164 rdoc = xmlParseMemory((char*) rec->u.octet_aligned->buf,
165 rec->u.octet_aligned->len);
168 yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s",
173 else if (rec->which == Z_External_OPAC)
177 yaz_log(YLOG_WARN, "MARC decoding not configured");
182 /* OPAC gets converted to XML too */
183 WRBUF wrbuf_opac = wrbuf_alloc();
184 /* MARCXML inside the OPAC XML. Charset is in effect because we
185 use the yaz_marc handle */
186 yaz_marc_xml(sdb->yaz_marc, YAZ_MARC_MARCXML);
187 yaz_opac_decode_wrbuf(sdb->yaz_marc, rec->u.opac, wrbuf_opac);
189 rdoc = xmlParseMemory((char*) wrbuf_buf(wrbuf_opac),
190 wrbuf_len(wrbuf_opac));
192 yaz_log(YLOG_WARN, "Unable to parse OPAC XML");
193 wrbuf_destroy(wrbuf_opac);
196 else if (oid && yaz_oid_is_iso2709(oid))
198 /* ISO2709 gets converted to MARCXML */
201 yaz_log(YLOG_WARN, "MARC decoding not configured");
210 if (rec->which != Z_External_octet)
212 yaz_log(YLOG_WARN, "Unexpected external branch, probably BER %s",
216 buf = (char*) rec->u.octet_aligned->buf;
217 len = rec->u.octet_aligned->len;
218 if (yaz_marc_read_iso2709(sdb->yaz_marc, buf, len) < 0)
220 yaz_log(YLOG_WARN, "Failed to decode MARC %s", db->url);
224 yaz_marc_write_using_libxml2(sdb->yaz_marc, 1);
225 if (yaz_marc_write_xml(sdb->yaz_marc, &res,
226 "http://www.loc.gov/MARC21/slim", 0, 0) < 0)
228 yaz_log(YLOG_WARN, "Failed to encode as XML %s",
232 rdoc = xmlNewDoc((xmlChar *) "1.0");
233 xmlDocSetRootElement(rdoc, res);
238 char oid_name_buf[OID_STR_MAX];
239 const char *oid_name = yaz_oid_to_string_buf(oid, 0, oid_name_buf);
241 "Unable to handle record of type %s from %s",
246 if (global_parameters.dump_records)
248 FILE *lf = yaz_log_file();
251 yaz_log(YLOG_LOG, "Un-normalized record from %s", db->url);
252 #if LIBXML_VERSION >= 20600
253 xmlDocFormatDump(lf, rdoc, 1);
255 xmlDocDump(lf, rdoc);
263 xmlDoc *normalize_record(struct session_database *sdb, Z_External *rec)
265 struct database_retrievalmap *m;
266 xmlDoc *rdoc = record_to_xml(sdb, rec);
269 for (m = sdb->map; m; m = m->next){
274 new = xsltApplyStylesheet(m->stylesheet, rdoc, 0);
275 root= xmlDocGetRootElement(new);
276 if (!new || !root || !(root->children))
278 yaz_log(YLOG_WARN, "XSLT transformation failed from %s",
289 if (global_parameters.dump_records)
291 FILE *lf = yaz_log_file();
295 yaz_log(YLOG_LOG, "Normalized record from %s",
297 #if LIBXML_VERSION >= 20600
298 xmlDocFormatDump(lf, rdoc, 1);
300 xmlDocDump(lf, rdoc);
309 // Retrieve first defined value for 'name' for given database.
310 // Will be extended to take into account user associated with session
311 char *session_setting_oneval(struct session_database *db, int offset)
313 if (!db->settings[offset])
315 return db->settings[offset]->value;
320 // Initialize YAZ Map structures for MARC-based targets
321 static int prepare_yazmarc(struct session_database *sdb)
327 yaz_log(YLOG_WARN, "No settings for %s", sdb->database->url);
330 if ((s = session_setting_oneval(sdb, PZ_NATIVESYNTAX))
331 && !strncmp(s, "iso2709", 7))
333 char *encoding = "marc-8s", *e;
336 // See if a native encoding is specified
337 if ((e = strchr(s, ';')))
340 sdb->yaz_marc = yaz_marc_create();
341 yaz_marc_subfield_str(sdb->yaz_marc, "\t");
343 cm = yaz_iconv_open("utf-8", encoding);
347 "Unable to map from %s to UTF-8 for target %s",
348 encoding, sdb->database->url);
351 yaz_marc_iconv(sdb->yaz_marc, cm);
356 // Prepare XSLT stylesheets for record normalization
357 // Structures are allocated on the session_wide nmem to avoid having
358 // to recompute this for every search. This would lead
359 // to leaking if a single session was to repeatedly change the PZ_XSLT
360 // setting. However, this is not a realistic use scenario.
361 static int prepare_map(struct session *se, struct session_database *sdb)
367 yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url);
370 if ((s = session_setting_oneval(sdb, PZ_XSLT)))
373 struct database_retrievalmap **m = &sdb->map;
376 nmem_strsplit(se->session_nmem, ",", s, &stylesheets, &num);
377 for (i = 0; i < num; i++)
379 (*m) = nmem_malloc(se->session_nmem, sizeof(**m));
381 if (!((*m)->stylesheet = conf_load_stylesheet(stylesheets[i])))
383 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
391 yaz_log(YLOG_WARN, "No Normalization stylesheet for target %s",
396 // This analyzes settings and recomputes any supporting data structures
398 static int prepare_session_database(struct session *se,
399 struct session_database *sdb)
404 "No settings associated with %s", sdb->database->url);
407 if (sdb->settings[PZ_NATIVESYNTAX] && !sdb->yaz_marc)
409 if (prepare_yazmarc(sdb) < 0)
412 if (sdb->settings[PZ_XSLT] && !sdb->map)
414 if (prepare_map(se, sdb) < 0)
420 // called if watch should be removed because http_channel is to be destroyed
421 static void session_watch_cancel(void *data, struct http_channel *c)
423 struct session_watchentry *ent = data;
430 // set watch. Returns 0=OK, -1 if watch is already set
431 int session_set_watch(struct session *s, int what,
432 session_watchfun fun, void *data,
433 struct http_channel *chan)
435 if (s->watchlist[what].fun)
437 s->watchlist[what].fun = fun;
438 s->watchlist[what].data = data;
439 s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
440 session_watch_cancel);
444 void session_alert_watch(struct session *s, int what)
446 if (!s->watchlist[what].fun)
448 http_remove_observer(s->watchlist[what].obs);
449 (*s->watchlist[what].fun)(s->watchlist[what].data);
450 s->watchlist[what].fun = 0;
451 s->watchlist[what].data = 0;
452 s->watchlist[what].obs = 0;
455 //callback for grep_databases
456 static void select_targets_callback(void *context, struct session_database *db)
458 struct session *se = (struct session*) context;
459 struct client *cl = client_create();
460 client_set_database(cl, db);
461 client_set_session(cl, se);
464 // Associates a set of clients with a session;
465 // Note: Session-databases represent databases with per-session
467 int select_targets(struct session *se, struct database_criterion *crit)
470 client_destroy(se->clients);
472 return session_grep_databases(se, crit, select_targets_callback);
475 int session_active_clients(struct session *s)
480 for (c = s->clients; c; c = client_next_in_session(c))
481 if (client_is_active(c))
487 // parses crit1=val1,crit2=val2|val3,...
488 static struct database_criterion *parse_filter(NMEM m, const char *buf)
490 struct database_criterion *res = 0;
497 nmem_strsplit(m, ",", buf, &values, &num);
498 for (i = 0; i < num; i++)
503 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
504 char *eq = strchr(values[i], '=');
507 yaz_log(YLOG_WARN, "Missing equal-sign in filter");
511 new->name = values[i];
512 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
514 for (subi = 0; subi < subnum; subi++)
516 struct database_criterion_value *newv
517 = nmem_malloc(m, sizeof(*newv));
518 newv->value = subvalues[subi];
519 newv->next = new->values;
528 enum pazpar2_error_code search(struct session *se,
529 char *query, char *filter,
530 const char **addinfo)
532 int live_channels = 0;
536 struct database_criterion *criteria;
538 yaz_log(YLOG_DEBUG, "Search");
541 nmem_reset(se->nmem);
542 criteria = parse_filter(se->nmem, filter);
544 live_channels = select_targets(se, criteria);
547 int maxrecs = live_channels * global_parameters.toget;
548 se->num_termlists = 0;
549 se->reclist = reclist_create(se->nmem, maxrecs);
550 // This will be initialized in send_search()
551 se->total_records = se->total_hits = se->total_merged = 0;
552 se->expected_maxrecs = maxrecs;
555 return PAZPAR2_NO_TARGETS;
559 for (cl = se->clients; cl; cl = client_next_in_session(cl))
561 if (prepare_session_database(se, client_get_database(cl)) < 0)
563 *addinfo = client_get_database(cl)->database->url;
564 return PAZPAR2_CONFIG_TARGET;
566 // Parse query for target
567 if (client_parse_query(cl, query) < 0)
572 client_prep_connection(cl);
576 // If no queries could be mapped, we signal an error
580 return PAZPAR2_MALFORMED_PARAMETER_VALUE;
582 return PAZPAR2_NO_ERROR;
585 // Creates a new session_database object for a database
586 static void session_init_databases_fun(void *context, struct database *db)
588 struct session *se = (struct session *) context;
589 struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
590 int num = settings_num();
597 if (global_parameters.server && global_parameters.server->icu_chn)
598 new->pct = pp2_charset_create(global_parameters.server->icu_chn);
600 new->pct = pp2_charset_create(0);
602 new->pct = pp2_charset_create(0);
607 = nmem_malloc(se->session_nmem, sizeof(struct settings *) * num);
608 memset(new->settings, 0, sizeof(struct settings*) * num);
612 for (i = 0; i < num; i++)
613 new->settings[i] = db->settings[i];
615 new->next = se->databases;
619 // Doesn't free memory associated with sdb -- nmem takes care of that
620 static void session_database_destroy(struct session_database *sdb)
622 struct database_retrievalmap *m;
624 for (m = sdb->map; m; m = m->next)
625 xsltFreeStylesheet(m->stylesheet);
627 yaz_marc_destroy(sdb->yaz_marc);
629 pp2_charset_destroy(sdb->pct);
632 // Initialize session_database list -- this represents this session's view
633 // of the database list -- subject to modification by the settings ws command
634 void session_init_databases(struct session *se)
637 predef_grep_databases(se, 0, session_init_databases_fun);
640 // Probably session_init_databases_fun should be refactored instead of
642 static struct session_database *load_session_database(struct session *se,
645 struct database *db = find_database(id, 0);
647 session_init_databases_fun((void*) se, db);
648 // New sdb is head of se->databases list
649 return se->databases;
652 // Find an existing session database. If not found, load it
653 static struct session_database *find_session_database(struct session *se,
656 struct session_database *sdb;
658 for (sdb = se->databases; sdb; sdb = sdb->next)
659 if (!strcmp(sdb->database->url, id))
661 return load_session_database(se, id);
664 // Apply a session override to a database
665 void session_apply_setting(struct session *se, char *dbname, char *setting,
668 struct session_database *sdb = find_session_database(se, dbname);
669 struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
670 int offset = settings_offset_cprefix(setting);
674 yaz_log(YLOG_WARN, "Unknown setting %s", setting);
677 // Jakub: This breaks the filter setting.
678 /*if (offset == PZ_ID)
680 yaz_log(YLOG_WARN, "No need to set pz:id setting. Ignoring");
684 new->target = dbname;
687 new->next = sdb->settings[offset];
688 sdb->settings[offset] = new;
690 // Force later recompute of settings-driven data structures
691 // (happens when a search starts and client connections are prepared)
694 case PZ_NATIVESYNTAX:
697 yaz_marc_destroy(sdb->yaz_marc);
704 struct database_retrievalmap *m;
705 // We don't worry about the map structure -- it's in nmem
706 for (m = sdb->map; m; m = m->next)
707 xsltFreeStylesheet(m->stylesheet);
714 void destroy_session(struct session *s)
716 struct session_database *sdb;
719 client_destroy(s->clients);
720 for (sdb = s->databases; sdb; sdb = sdb->next)
721 session_database_destroy(sdb);
722 nmem_destroy(s->nmem);
723 wrbuf_destroy(s->wrbuf);
726 struct session *new_session(NMEM nmem)
729 struct session *session = nmem_malloc(nmem, sizeof(*session));
731 yaz_log(YLOG_DEBUG, "New Pazpar2 session");
733 session->relevance = 0;
734 session->total_hits = 0;
735 session->total_records = 0;
736 session->num_termlists = 0;
737 session->reclist = 0;
738 session->requestid = -1;
739 session->clients = 0;
740 session->expected_maxrecs = 0;
741 session->session_nmem = nmem;
742 session->nmem = nmem_create();
743 session->wrbuf = wrbuf_alloc();
744 session->databases = 0;
745 for (i = 0; i <= SESSION_WATCH_MAX; i++)
747 session->watchlist[i].data = 0;
748 session->watchlist[i].fun = 0;
754 struct hitsbytarget *hitsbytarget(struct session *se, int *count)
756 static struct hitsbytarget res[1000]; // FIXME MM
760 for (cl = se->clients; cl; cl = client_next_in_session(cl))
762 char *name = session_setting_oneval(client_get_database(cl), PZ_NAME);
764 res[*count].id = client_get_database(cl)->database->url;
765 res[*count].name = *name ? name : "Unknown";
766 res[*count].hits = client_get_hits(cl);
767 res[*count].records = client_get_num_records(cl);
768 res[*count].diagnostic = client_get_diagnostic(cl);
769 res[*count].state = client_get_state_str(cl);
770 res[*count].connected = client_get_connection(cl) ? 1 : 0;
777 struct termlist_score **termlist(struct session *s, const char *name, int *num)
781 for (i = 0; i < s->num_termlists; i++)
782 if (!strcmp((const char *) s->termlists[i].name, name))
783 return termlist_highscore(s->termlists[i].termlist, num);
787 #ifdef MISSING_HEADERS
788 void report_nmem_stats(void)
790 size_t in_use, is_free;
792 nmem_get_memory_in_use(&in_use);
793 nmem_get_memory_free(&is_free);
795 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
796 (long) in_use, (long) is_free);
800 struct record_cluster *show_single(struct session *s, const char *id)
802 struct record_cluster *r;
804 reclist_rewind(s->reclist);
805 while ((r = reclist_read_record(s->reclist)))
806 if (!strcmp(r->recid, id))
811 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp,
812 int start, int *num, int *total, int *sumhits,
815 struct record_cluster **recs = nmem_malloc(nmem_show, *num
816 * sizeof(struct record_cluster *));
817 struct reclist_sortparms *spp;
820 yaz_timing_t t = yaz_timing_create();
832 for (spp = sp; spp; spp = spp->next)
833 if (spp->type == Metadata_sortkey_relevance)
835 relevance_prepare_read(s->relevance, s->reclist);
838 reclist_sort(s->reclist, sp);
840 *total = s->reclist->num_records;
841 *sumhits = s->total_hits;
843 for (i = 0; i < start; i++)
844 if (!reclist_read_record(s->reclist))
851 for (i = 0; i < *num; i++)
853 struct record_cluster *r = reclist_read_record(s->reclist);
864 yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f",
865 yaz_timing_get_real(t), yaz_timing_get_user(t),
866 yaz_timing_get_sys(t));
867 yaz_timing_destroy(&t);
872 void statistics(struct session *se, struct statistics *stat)
877 memset(stat, 0, sizeof(*stat));
878 for (cl = se->clients; cl; cl = client_next_in_session(cl))
880 if (!client_get_connection(cl))
881 stat->num_no_connection++;
882 switch (client_get_state(cl))
884 case Client_Connecting: stat->num_connecting++; break;
885 case Client_Initializing: stat->num_initializing++; break;
886 case Client_Searching: stat->num_searching++; break;
887 case Client_Presenting: stat->num_presenting++; break;
888 case Client_Idle: stat->num_idle++; break;
889 case Client_Failed: stat->num_failed++; break;
890 case Client_Error: stat->num_error++; break;
895 stat->num_hits = se->total_hits;
896 stat->num_records = se->total_records;
898 stat->num_clients = count;
901 void start_http_listener(void)
904 struct conf_server *ser = global_parameters.server;
906 if (*global_parameters.listener_override)
907 strcpy(hp, global_parameters.listener_override);
910 strcpy(hp, ser->host ? ser->host : "");
915 sprintf(hp + strlen(hp), "%d", ser->port);
921 void start_proxy(void)
924 struct conf_server *ser = global_parameters.server;
926 if (*global_parameters.proxy_override)
927 strcpy(hp, global_parameters.proxy_override);
928 else if (ser->proxy_host || ser->proxy_port)
930 strcpy(hp, ser->proxy_host ? ser->proxy_host : "");
935 sprintf(hp + strlen(hp), "%d", ser->proxy_port);
941 http_set_proxyaddr(hp, ser->myurl ? ser->myurl : "");
945 // Master list of connections we're handling events to
946 static IOCHAN channel_list = 0;
947 void pazpar2_add_channel(IOCHAN chan)
949 chan->next = channel_list;
953 void pazpar2_event_loop()
955 event_loop(&channel_list);
958 static struct record_metadata *record_metadata_init(
959 NMEM nmem, char *value, enum conf_metadata_type type)
961 struct record_metadata *rec_md = record_metadata_create(nmem);
962 if (type == Metadata_type_generic)
965 p = normalize7bit_generic(p, " ,/.:([");
967 rec_md->data.text = nmem_strdup(nmem, p);
969 else if (type == Metadata_type_year)
972 if (extract7bit_years((char *) value, &first, &last) < 0)
974 rec_md->data.number.min = first;
975 rec_md->data.number.max = last;
982 struct record *ingest_record(struct client *cl, Z_External *rec,
985 xmlDoc *xdoc = normalize_record(client_get_database(cl), rec);
987 struct record *record;
988 struct record_cluster *cluster;
989 struct session *se = client_get_session(cl);
990 xmlChar *mergekey, *mergekey_norm;
993 struct conf_service *service = global_parameters.server->service;
998 root = xmlDocGetRootElement(xdoc);
999 if (!(mergekey = xmlGetProp(root, (xmlChar *) "mergekey")))
1001 yaz_log(YLOG_WARN, "No mergekey found in record");
1006 record = record_create(se->nmem,
1007 service->num_metadata, service->num_sortkeys, cl,
1010 mergekey_norm = (xmlChar *) nmem_strdup(se->nmem, (char*) mergekey);
1012 normalize7bit_mergekey((char *) mergekey_norm, 0);
1014 cluster = reclist_insert(se->reclist,
1015 global_parameters.server->service,
1016 record, (char *) mergekey_norm,
1018 if (global_parameters.dump_records)
1019 yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
1020 client_get_database(cl)->database->url, record_no);
1023 /* no room for record */
1027 relevance_newrec(se->relevance, cluster);
1030 // now parsing XML record and adding data to cluster or record metadata
1031 for (n = root->children; n; n = n->next)
1039 if (n->type != XML_ELEMENT_NODE)
1041 if (!strcmp((const char *) n->name, "metadata"))
1043 struct conf_metadata *ser_md = 0;
1044 struct conf_sortkey *ser_sk = 0;
1045 struct record_metadata **wheretoput = 0;
1046 struct record_metadata *rec_md = 0;
1047 int md_field_id = -1;
1048 int sk_field_id = -1;
1050 type = xmlGetProp(n, (xmlChar *) "type");
1051 value = xmlNodeListGetString(xdoc, n->children, 1);
1053 if (!type || !value)
1057 = conf_service_metadata_field_id(service, (const char *) type);
1058 if (md_field_id < 0)
1061 "Ignoring unknown metadata element: %s", type);
1065 ser_md = &service->metadata[md_field_id];
1067 if (ser_md->sortkey_offset >= 0){
1068 sk_field_id = ser_md->sortkey_offset;
1069 ser_sk = &service->sortkeys[sk_field_id];
1072 // non-merged metadata
1073 rec_md = record_metadata_init(se->nmem, (char *) value,
1077 yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
1081 rec_md->next = record->metadata[md_field_id];
1082 record->metadata[md_field_id] = rec_md;
1085 rec_md = record_metadata_init(se->nmem, (char *) value,
1087 wheretoput = &cluster->metadata[md_field_id];
1089 // and polulate with data:
1090 // assign cluster or record based on merge action
1091 if (ser_md->merge == Metadata_merge_unique)
1093 struct record_metadata *mnode;
1094 for (mnode = *wheretoput; mnode; mnode = mnode->next)
1095 if (!strcmp((const char *) mnode->data.text,
1100 rec_md->next = *wheretoput;
1101 *wheretoput = rec_md;
1104 else if (ser_md->merge == Metadata_merge_longest)
1107 || strlen(rec_md->data.text)
1108 > strlen((*wheretoput)->data.text))
1110 *wheretoput = rec_md;
1113 char *s = nmem_strdup(se->nmem, rec_md->data.text);
1114 if (!cluster->sortkeys[sk_field_id])
1115 cluster->sortkeys[sk_field_id] =
1116 nmem_malloc(se->nmem,
1117 sizeof(union data_types));
1118 normalize7bit_mergekey(s,
1119 (ser_sk->type == Metadata_sortkey_skiparticle));
1120 cluster->sortkeys[sk_field_id]->text = s;
1124 else if (ser_md->merge == Metadata_merge_all)
1126 rec_md->next = *wheretoput;
1127 *wheretoput = rec_md;
1129 else if (ser_md->merge == Metadata_merge_range)
1133 *wheretoput = rec_md;
1135 cluster->sortkeys[sk_field_id]
1140 int this_min = rec_md->data.number.min;
1141 int this_max = rec_md->data.number.max;
1142 if (this_min < (*wheretoput)->data.number.min)
1143 (*wheretoput)->data.number.min = this_min;
1144 if (this_max > (*wheretoput)->data.number.max)
1145 (*wheretoput)->data.number.max = this_max;
1150 union data_types *sdata
1151 = cluster->sortkeys[sk_field_id];
1152 yaz_log(YLOG_LOG, "SK range: %d-%d",
1153 sdata->number.min, sdata->number.max);
1159 // ranking of _all_ fields enabled ...
1161 relevance_countwords(se->relevance, cluster,
1162 (char *) value, ser_md->rank);
1164 // construct facets ...
1165 if (ser_md->termlist)
1167 if (ser_md->type == Metadata_type_year)
1170 sprintf(year, "%d", rec_md->data.number.max);
1171 add_facet(se, (char *) type, year);
1172 if (rec_md->data.number.max != rec_md->data.number.min)
1174 sprintf(year, "%d", rec_md->data.number.min);
1175 add_facet(se, (char *) type, year);
1179 add_facet(se, (char *) type, (char *) value);
1189 "Unexpected element %s in internal record", n->name);
1198 relevance_donerecord(se->relevance, cluster);
1199 se->total_records++;
1209 * indent-tabs-mode: nil
1211 * vim: shiftwidth=4 tabstop=8 expandtab