-static void add_facet(struct session *s, const char *type, const char *value)
-{
- int i;
-
- for (i = 0; i < s->num_termlists; i++)
- if (!strcmp(s->termlists[i].name, type))
- break;
- if (i == s->num_termlists)
- {
- if (i == SESSION_MAX_TERMLISTS)
- {
- yaz_log(YLOG_FATAL, "Too many termlists");
- exit(1);
- }
- s->termlists[i].name = nmem_strdup(s->nmem, type);
- s->termlists[i].termlist = termlist_create(s->nmem, s->expected_maxrecs, 15);
- s->num_termlists = i + 1;
- }
- termlist_insert(s->termlists[i].termlist, value);
-}
-
-static xmlDoc *normalize_record(struct client *cl, Z_External *rec)
-{
- struct conf_retrievalprofile *rprofile = cl->database->rprofile;
- struct conf_retrievalmap *m;
- xmlNode *res;
- xmlDoc *rdoc;
-
- // First normalize to XML
- if (rprofile->native_syntax == Nativesyn_iso2709)
- {
- char *buf;
- int len;
- if (rec->which != Z_External_octet)
- {
- yaz_log(YLOG_WARN, "Unexpected external branch, probably BER");
- return 0;
- }
- buf = (char*) rec->u.octet_aligned->buf;
- len = rec->u.octet_aligned->len;
- if (yaz_marc_read_iso2709(rprofile->yaz_marc, buf, len) < 0)
- {
- yaz_log(YLOG_WARN, "Failed to decode MARC");
- return 0;
- }
- if (yaz_marc_write_xml(rprofile->yaz_marc, &res,
- "http://www.loc.gov/MARC21/slim", 0, 0) < 0)
- {
- yaz_log(YLOG_WARN, "Failed to encode as XML");
- return 0;
- }
- rdoc = xmlNewDoc("1.0");
- xmlDocSetRootElement(rdoc, res);
- }
- else
- {
- yaz_log(YLOG_FATAL, "Unknown native_syntax in normalize_record");
- exit(1);
- }
- for (m = rprofile->maplist; m; m = m->next)
- {
- xmlDoc *new;
- if (m->type != Map_xslt)
- {
- yaz_log(YLOG_WARN, "Unknown map type");
- return 0;
- }
- if (!(new = xsltApplyStylesheet(m->stylesheet, rdoc, 0)))
- {
- yaz_log(YLOG_WARN, "XSLT transformation failed");
- return 0;
- }
- xmlFreeDoc(rdoc);
- rdoc = new;
- }
- if (global_parameters.dump_records)
- {
- fprintf(stderr, "Record:\n----------------\n");
- xmlDocFormatDump(stderr, rdoc, 1);
- }
- return rdoc;
-}
-
-static struct record *ingest_record(struct client *cl, Z_External *rec)
-{
- xmlDoc *xdoc = normalize_record(cl, rec);
- xmlNode *root, *n;
- struct record *res, *head;
- struct session *se = cl->session;
- xmlChar *mergekey, *mergekey_norm;
-
- if (!xdoc)
- return 0;
-
- root = xmlDocGetRootElement(xdoc);
- if (!(mergekey = xmlGetProp(root, "mergekey")))
- {
- yaz_log(YLOG_WARN, "No mergekey found in record");
- return 0;
- }
-
- res = nmem_malloc(se->nmem, sizeof(struct record));
- res->next_cluster = 0;
- res->target_offset = -1;
- res->term_frequency_vec = 0;
- res->title = "Unknown";
- res->relevance = 0;
-
- mergekey_norm = nmem_strdup(se->nmem, (char*) mergekey);
- xmlFree(mergekey);
- res->merge_key = normalize_mergekey(mergekey_norm);
-
- head = reclist_insert(se->reclist, res);
- relevance_newrec(se->relevance, head);
-
- for (n = root->children; n; n = n->next)
- {
- if (n->type != XML_ELEMENT_NODE)
- continue;
- if (!strcmp(n->name, "facet"))
- {
- xmlChar *type = xmlGetProp(n, "type");
- xmlChar *value = xmlNodeListGetString(xdoc, n->children, 0);
- add_facet(se, type, value);
- relevance_countwords(se->relevance, head, value, 1);
- xmlFree(type);
- xmlFree(value);
- }
- else if (!strcmp(n->name, "metadata"))
- {
- xmlChar *type = xmlGetProp(n, "type"), *value;
- if (!strcmp(type, "title"))
- res->title = nmem_strdup(se->nmem,
- value = xmlNodeListGetString(xdoc, n->children, 0));
-
- relevance_countwords(se->relevance, head, value, 4);
- xmlFree(type);
- xmlFree(value);
- }
- else
- yaz_log(YLOG_WARN, "Unexpected element %s in internal record", n->name);
- }
-
- relevance_donerecord(se->relevance, head);
- se->total_records++;
-
- return res;
-}
-
-static void ingest_records(struct client *cl, Z_Records *r)
-{
- struct record *rec;
- struct session *s = cl->session;
- Z_NamePlusRecordList *rlist;
- int i;