1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements Solr decoding/encoding
16 #include <yaz/matchstr.h>
17 #include <yaz/yaz-iconv.h>
19 #include <yaz/facet.h>
20 #include <yaz/wrbuf.h>
24 #define SOLR_MAX_PARAMETERS 100
27 #include <libxml/parser.h>
28 #include <libxml/tree.h>
30 static void extract_text_node(xmlNodePtr node, WRBUF wrbuf) {
32 for (child = node->children; child ; child = child->next)
34 if (child->type == XML_TEXT_NODE)
35 wrbuf_puts(wrbuf, (const char *) child->content);
39 static int match_xml_node_attribute(
41 const char *node_name, const char *attribute_name, const char *value)
43 const char *attribute_value;
44 // check if the node name matches
45 if (strcmp((const char*) ptr->name, node_name))
49 attribute_value = yaz_element_attribute_value_get(ptr, node_name,
51 if (attribute_value && !strcmp(attribute_value, value))
54 else /* No attribute to check */
59 static void yaz_solr_decode_result_docs(ODR o, xmlNodePtr ptr,
61 Z_SRW_searchRetrieveResponse *sr)
68 for (node = ptr->children; node; node = node->next)
69 if (node->type == XML_ELEMENT_NODE)
73 sr->records = odr_malloc(o, sizeof(*sr->records) * sr->num_records);
75 for (node = ptr->children; node; node = node->next)
77 if (node->type == XML_ELEMENT_NODE)
79 Z_SRW_record *record = sr->records + i;
80 xmlBufferPtr buf = xmlBufferCreate();
81 xmlNode *tmp = xmlCopyNode(node, 1);
83 xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
87 record->recordSchema = 0;
88 record->recordPacking = Z_SRW_recordPacking_XML;
89 record->recordData_len = buf->use;
90 record->recordData_buf =
91 odr_strdupn(o, (const char *) buf->content, buf->use);
92 record->recordPosition = odr_intdup(o, start + offset + 1);
102 static int yaz_solr_decode_result(ODR o, xmlNodePtr ptr,
103 Z_SRW_searchRetrieveResponse *sr)
106 struct _xmlAttr *attr;
107 for (attr = ptr->properties; attr; attr = attr->next)
108 if (attr->children && attr->children->type == XML_TEXT_NODE)
110 if (!strcmp((const char *) attr->name, "numFound"))
112 sr->numberOfRecords = odr_intdup(o, odr_atoi(
113 (const char *) attr->children->content));
115 else if (!strcmp((const char *) attr->name, "start"))
117 start = odr_atoi((const char *) attr->children->content);
120 if (sr->numberOfRecords && *sr->numberOfRecords > 0)
121 yaz_solr_decode_result_docs(o, ptr, start, sr);
122 if (sr->numberOfRecords)
127 static const char *get_facet_term_count(xmlNodePtr node, Odr_int *freq)
129 const char *term = yaz_element_attribute_value_get(node, "int", "name");
131 WRBUF wrbuf = wrbuf_alloc();
135 for (child = node->children; child ; child = child->next)
137 if (child->type == XML_TEXT_NODE)
138 wrbuf_puts(wrbuf, (const char *) child->content);
140 *freq = odr_atoi(wrbuf_cstr(wrbuf));
141 wrbuf_destroy(wrbuf);
145 Z_FacetField *yaz_solr_decode_facet_field(ODR o, xmlNodePtr ptr,
146 Z_SRW_searchRetrieveResponse *sr)
149 Z_AttributeList *list;
150 Z_FacetField *facet_field;
155 const char* name = yaz_element_attribute_value_get(ptr, "lst", "name");
156 list = yaz_use_attribute_create(o, name);
157 for (node = ptr->children; node; node = node->next)
159 facet_field = facet_field_create(o, list, num_terms);
161 for (node = ptr->children; node; node = node->next)
164 const char *term = get_facet_term_count(node, &count);
165 facet_field_term_set(o, facet_field,
166 facet_term_create_cstr(o, term, count), index);
172 static int yaz_solr_decode_facet_counts(ODR o, xmlNodePtr root,
173 Z_SRW_searchRetrieveResponse *sr)
176 for (ptr = root->children; ptr; ptr = ptr->next)
178 if (match_xml_node_attribute(ptr, "lst", "name", "facet_fields"))
181 Z_FacetList *facet_list;
183 for (node = ptr->children; node; node= node->next)
187 facet_list = facet_list_create(o, num_facets);
189 for (node = ptr->children; node; node= node->next)
191 facet_list_field_set(o, facet_list,
192 yaz_solr_decode_facet_field(o, node, sr),
196 sr->facetList = facet_list;
203 static void yaz_solr_decode_suggestion_values(xmlNodePtr listPptr, WRBUF wrbuf)
206 for (node = listPptr; node; node= node->next) {
207 if (!strcmp((char*) node->name, "lst")) {
209 for (child = node->children; child; child= child->next) {
210 if (match_xml_node_attribute(child, "str", "name", "word")) {
211 wrbuf_puts(wrbuf, "<suggestion>");
212 extract_text_node(child, wrbuf);
213 wrbuf_puts(wrbuf, "</suggestion>\n");
220 static void yaz_solr_decode_suggestion_lst(xmlNodePtr lstPtr, WRBUF wrbuf)
223 for (node = lstPtr; node; node= node->next) {
224 if (match_xml_node_attribute(node, "arr", "name", "suggestion")) {
225 yaz_solr_decode_suggestion_values(node->children, wrbuf);
230 static void yaz_solr_decode_misspelled(xmlNodePtr lstPtr, WRBUF wrbuf)
233 for (node = lstPtr; node; node= node->next)
235 if (!strcmp((const char*) node->name, "lst")) {
236 const char *misspelled = yaz_element_attribute_value_get(node, "lst", "name");
238 wrbuf_printf(wrbuf, "<misspelled term=\"%s\">\n", misspelled);
239 yaz_solr_decode_suggestion_lst(node->children, wrbuf);
240 wrbuf_puts(wrbuf, "</misspelled>\n");
246 static int yaz_solr_decode_spellcheck(ODR o, xmlNodePtr spellcheckPtr, Z_SRW_searchRetrieveResponse *sr)
249 WRBUF wrbuf = wrbuf_alloc();
250 wrbuf_puts(wrbuf, "");
251 for (ptr = spellcheckPtr->children; ptr; ptr = ptr->next)
253 if (match_xml_node_attribute(ptr, "lst", "name", "suggestions"))
255 yaz_solr_decode_misspelled(ptr->children, wrbuf);
258 sr->suggestions = odr_strdup(o, wrbuf_cstr(wrbuf));
262 static int yaz_solr_decode_scan_result(ODR o, xmlNodePtr ptr,
263 Z_SRW_scanResponse *scr)
269 /* find the actual list */
270 for (node = ptr->children; node; node = node->next)
271 if (node->type == XML_ELEMENT_NODE) {
277 for (node = ptr->children; node; node = node->next)
278 if (node->type == XML_ELEMENT_NODE && !strcmp((const char *) node->name, "int"))
282 scr->terms = odr_malloc(o, sizeof(*scr->terms) * scr->num_terms);
284 for (node = ptr->children; node; node = node->next)
286 if (node->type == XML_ELEMENT_NODE && !strcmp((const char *) node->name, "int"))
288 Z_SRW_scanTerm *term = scr->terms + i;
291 const char *val = get_facet_term_count(node, &count);
293 term->numberOfRecords = odr_intdup(o, count);
295 /* if val contains a ^ then it is probably term<^>display term so separate them. This is due to
296 * SOLR not being able to encode them into 2 separate attributes.
298 pos = strchr(val, '^');
300 term->displayTerm = odr_strdup(o, pos + 1);
302 term->value = odr_strdup(o, val);
305 term->value = odr_strdup(o, val);
306 term->displayTerm = NULL;
308 term->whereInList = NULL;
320 int yaz_solr_decode_response(ODR o, Z_HTTP_Response *hres, Z_SRW_PDU **pdup)
323 const char *content_buf = hres->content_buf;
324 int content_len = hres->content_len;
325 xmlDocPtr doc = xmlParseMemory(content_buf, content_len);
329 Z_SRW_searchRetrieveResponse *sr = NULL;
330 Z_SRW_scanResponse *scr = NULL;
338 xmlNodePtr root = xmlDocGetRootElement(doc);
343 else if (strcmp((const char *) root->name, "response"))
349 /** look for result (required) and facets node (optional) */
352 for (ptr = root->children; ptr; ptr = ptr->next)
354 if (ptr->type == XML_ELEMENT_NODE &&
355 !strcmp((const char *) ptr->name, "result")) {
356 pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
357 sr = pdu->u.response;
358 rc_result = yaz_solr_decode_result(o, ptr, sr);
360 if (ptr->type == XML_ELEMENT_NODE &&
361 match_xml_node_attribute(ptr, "lst", "name", "terms")) {
362 pdu = yaz_srw_get(o, Z_SRW_scan_response);
363 scr = pdu->u.scan_response;
364 rc_result = yaz_solr_decode_scan_result(o, ptr, scr);
366 /* TODO The check on hits is a work-around to avoid garbled facets on zero results from the SOLR server.
367 * The work-around works because the results is before the facets in the xml. */
369 if (rc_result == 0 && *sr->numberOfRecords > 0 &&
370 match_xml_node_attribute(ptr, "lst", "name", "facet_counts"))
371 rc_facets = yaz_solr_decode_facet_counts(o, ptr, sr);
372 if (rc_result == 0 && *sr->numberOfRecords == 0 &&
373 match_xml_node_attribute(ptr, "lst", "name", "spellcheck"))
374 rc_facets = yaz_solr_decode_spellcheck(o, ptr, sr);
378 ret = rc_result + rc_facets;
391 static int yaz_solr_encode_facet_field(
392 ODR encode, char **name, char **value, int *i,
393 Z_FacetField *facet_field)
395 Z_AttributeList *attribute_list = facet_field->attributes;
396 struct yaz_facet_attr attr_values;
397 yaz_facet_attr_init(&attr_values);
398 yaz_facet_attr_get_z_attributes(attribute_list, &attr_values);
400 if (attr_values.errcode)
402 if (attr_values.useattr)
404 WRBUF wrbuf = wrbuf_alloc();
405 yaz_add_name_value_str(encode, name, value, i,
407 odr_strdup(encode, attr_values.useattr));
409 if (attr_values.limit > 0)
411 Odr_int v = attr_values.limit;
413 wrbuf_printf(wrbuf, "f.%s.facet.limit", attr_values.useattr);
414 yaz_add_name_value_int(encode, name, value, i,
415 odr_strdup(encode, wrbuf_cstr(wrbuf)),
418 if (attr_values.start > 1)
420 Odr_int v = attr_values.start - 1;
422 wrbuf_printf(wrbuf, "f.%s.facet.offset", attr_values.useattr);
423 yaz_add_name_value_int(encode, name, value, i,
424 odr_strdup(encode, wrbuf_cstr(wrbuf)),
427 if (attr_values.sortorder == 1)
430 wrbuf_printf(wrbuf, "f.%s.facet.sort", attr_values.useattr);
431 yaz_add_name_value_str(encode, name, value, i,
432 odr_strdup(encode, wrbuf_cstr(wrbuf)),
435 wrbuf_destroy(wrbuf);
439 if (attr_values.limit > 0)
441 Odr_int v = attr_values.limit;
442 yaz_add_name_value_int(encode, name, value, i, "facet.limit", &v);
444 if (attr_values.start > 1)
446 Odr_int v = attr_values.start - 1;
447 yaz_add_name_value_int(encode, name, value, i, "facet.offset", &v);
449 if (attr_values.sortorder == 1)
451 yaz_add_name_value_str(encode, name, value, i, "facet.sort",
458 static int yaz_solr_encode_facet_list(
459 ODR encode, char **name, char **value,
460 int *i, Z_FacetList *facet_list)
463 for (index = 0; index < facet_list->num; index++)
465 int r = yaz_solr_encode_facet_field(encode, name, value, i,
466 facet_list->elements[index]);
474 int yaz_solr_encode_request(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
475 ODR encode, const char *charset)
477 const char *solr_op = 0;
478 //TODO Change. not a nice hard coded, unchecked limit.
479 char *name[SOLR_MAX_PARAMETERS], *value[SOLR_MAX_PARAMETERS];
487 z_HTTP_header_add_basic_auth(encode, &hreq->headers,
488 srw_pdu->username, srw_pdu->password);
489 if (srw_pdu->which == Z_SRW_searchRetrieve_request)
491 Z_SRW_searchRetrieveRequest *request = srw_pdu->u.request;
493 if (!srw_pdu->u.request->query)
495 /* not considering query type here ! */
496 yaz_add_name_value_str(encode, name, value, &i, "q", request->query);
497 if (srw_pdu->u.request->startRecord)
499 Odr_int start = *request->startRecord - 1;
500 yaz_add_name_value_int(encode, name, value, &i,
503 yaz_add_name_value_int(encode, name, value, &i,
504 "rows", request->maximumRecords);
505 yaz_add_name_value_str(encode, name, value, &i,
506 "fl", request->recordSchema);
508 switch(srw_pdu->u.request->sort_type)
510 case Z_SRW_sort_type_none:
512 case Z_SRW_sort_type_sort:
513 yaz_add_name_value_str(encode, name, value, &i, "sort",
514 srw_pdu->u.request->sort.sortKeys);
517 if (request->facetList)
519 Z_FacetList *facet_list = request->facetList;
520 yaz_add_name_value_str(encode, name, value, &i, "facet", "true");
521 yaz_add_name_value_str(encode, name, value, &i, "facet.mincount", "1");
522 if (yaz_solr_encode_facet_list(encode, name, value, &i, facet_list))
526 else if (srw_pdu->which == Z_SRW_scan_request) {
527 Z_SRW_scanRequest *request = srw_pdu->u.scan_request;
529 if (!srw_pdu->u.scan_request->scanClause)
531 if (!strcmp(srw_pdu->u.scan_request->queryType, "pqf"))
533 yaz_add_name_value_str(encode, name, value, &i,
534 "terms.fl", request->scanClause);
535 yaz_add_name_value_str(encode, name, value, &i,
536 "terms.lower", request->scanClause);
538 else if (!strcmp(srw_pdu->u.scan_request->queryType, "cql"))
540 q = request->scanClause;
541 pos = strchr(q, ':');
543 yaz_add_name_value_str(encode, name, value, &i,
544 "terms.lower", odr_strdup(encode, pos + 1));
546 yaz_add_name_value_str(encode, name, value, &i,
547 "terms.fl", odr_strdup(encode, q));
550 yaz_add_name_value_str(encode, name, value, &i,
551 "terms.lower", odr_strdup(encode, q));
556 yaz_add_name_value_str(encode, name, value, &i,
557 "terms.sort", "index");
558 yaz_add_name_value_int(encode, name, value, &i,
559 "terms.limit", request->maximumTerms);
564 if (srw_pdu->extra_args)
566 Z_SRW_extra_arg *ea = srw_pdu->extra_args;
567 for (; ea && i < SOLR_MAX_PARAMETERS; ea = ea->next)
570 value[i] = ea->value;
577 yaz_array_to_uri(&uri_args, encode, name, value);
579 hreq->method = "GET";
582 odr_malloc(encode, strlen(hreq->path) +
583 strlen(uri_args) + strlen(solr_op) + 4);
585 strcpy(path, hreq->path);
586 cp = strrchr(path, '/');
589 if (!strcmp(cp, "/select") || !strcmp(cp, "/"))
593 strcat(path, solr_op);
595 strcat(path, uri_args);
598 z_HTTP_header_add_content_type(encode, &hreq->headers,
599 "text/xml", charset);
607 * c-file-style: "Stroustrup"
608 * indent-tabs-mode: nil
610 * vim: shiftwidth=4 tabstop=8 expandtab