1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 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>
21 #include <yaz/proto.h>
26 #include <libxml/parser.h>
27 #include <libxml/tree.h>
29 static void extract_text_node(xmlNodePtr node, WRBUF wrbuf) {
31 for (child = node->children; child ; child = child->next)
33 if (child->type == XML_TEXT_NODE)
34 wrbuf_puts(wrbuf, (const char *) child->content);
38 static int match_xml_node_attribute(
40 const char *node_name, const char *attribute_name, const char *value)
42 const char *attribute_value;
43 // check if the node name matches
44 if (strcmp((const char*) ptr->name, node_name))
48 attribute_value = yaz_element_attribute_value_get(ptr, node_name,
50 if (attribute_value && !strcmp(attribute_value, value))
53 else /* No attribute to check */
58 static void yaz_solr_decode_result_docs(ODR o, xmlNodePtr ptr,
60 Z_SRW_searchRetrieveResponse *sr)
67 for (node = ptr->children; node; node = node->next)
68 if (node->type == XML_ELEMENT_NODE)
72 sr->records = odr_malloc(o, sizeof(*sr->records) * sr->num_records);
74 for (node = ptr->children; node; node = node->next)
76 if (node->type == XML_ELEMENT_NODE)
78 Z_SRW_record *record = sr->records + i;
79 xmlBufferPtr buf = xmlBufferCreate();
80 xmlNode *tmp = xmlCopyNode(node, 1);
82 xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
86 record->recordSchema = 0;
87 record->recordPacking = Z_SRW_recordPacking_XML;
88 record->recordData_len = buf->use;
89 record->recordData_buf =
90 odr_strdupn(o, (const char *) buf->content, buf->use);
91 record->recordPosition = odr_intdup(o, start + offset + 1);
101 static int yaz_solr_decode_result(ODR o, xmlNodePtr ptr,
102 Z_SRW_searchRetrieveResponse *sr)
105 struct _xmlAttr *attr;
106 for (attr = ptr->properties; attr; attr = attr->next)
107 if (attr->children && attr->children->type == XML_TEXT_NODE)
109 if (!strcmp((const char *) attr->name, "numFound"))
111 sr->numberOfRecords = odr_intdup(o, odr_atoi(
112 (const char *) attr->children->content));
114 else if (!strcmp((const char *) attr->name, "start"))
116 start = odr_atoi((const char *) attr->children->content);
119 if (sr->numberOfRecords && *sr->numberOfRecords > 0)
120 yaz_solr_decode_result_docs(o, ptr, start, sr);
121 if (sr->numberOfRecords)
126 static const char *get_facet_term_count(xmlNodePtr node, Odr_int *freq)
128 const char *term = yaz_element_attribute_value_get(node, "int", "name");
130 WRBUF wrbuf = wrbuf_alloc();
134 for (child = node->children; child ; child = child->next)
136 if (child->type == XML_TEXT_NODE)
137 wrbuf_puts(wrbuf, (const char *) child->content);
139 *freq = odr_atoi(wrbuf_cstr(wrbuf));
140 wrbuf_destroy(wrbuf);
144 Z_FacetField *yaz_solr_decode_facet_field(ODR o, xmlNodePtr ptr,
145 Z_SRW_searchRetrieveResponse *sr)
148 Z_AttributeList *list;
149 Z_FacetField *facet_field;
154 const char* name = yaz_element_attribute_value_get(ptr, "lst", "name");
155 list = zget_AttributeList_use_string(o, name);
156 for (node = ptr->children; node; node = node->next)
158 facet_field = facet_field_create(o, list, num_terms);
160 for (node = ptr->children; node; node = node->next)
163 const char *term = get_facet_term_count(node, &count);
164 facet_field_term_set(o, facet_field,
165 facet_term_create_cstr(o, term, count), index);
171 static int yaz_solr_decode_facet_counts(ODR o, xmlNodePtr root,
172 Z_SRW_searchRetrieveResponse *sr)
175 for (ptr = root->children; ptr; ptr = ptr->next)
177 if (match_xml_node_attribute(ptr, "lst", "name", "facet_fields"))
180 Z_FacetList *facet_list;
182 for (node = ptr->children; node; node= node->next)
186 facet_list = facet_list_create(o, num_facets);
188 for (node = ptr->children; node; node= node->next)
190 facet_list_field_set(o, facet_list,
191 yaz_solr_decode_facet_field(o, node, sr),
195 sr->facetList = facet_list;
202 static void yaz_solr_decode_suggestion_values(xmlNodePtr listPptr, WRBUF wrbuf)
205 for (node = listPptr; node; node= node->next) {
206 if (!strcmp((char*) node->name, "lst")) {
208 for (child = node->children; child; child= child->next) {
209 if (match_xml_node_attribute(child, "str", "name", "word")) {
210 wrbuf_puts(wrbuf, "<suggestion>");
211 extract_text_node(child, wrbuf);
212 wrbuf_puts(wrbuf, "</suggestion>\n");
219 static void yaz_solr_decode_suggestion_lst(xmlNodePtr lstPtr, WRBUF wrbuf)
222 for (node = lstPtr; node; node= node->next) {
223 if (match_xml_node_attribute(node, "arr", "name", "suggestion")) {
224 yaz_solr_decode_suggestion_values(node->children, wrbuf);
229 static void yaz_solr_decode_misspelled(xmlNodePtr lstPtr, WRBUF wrbuf)
232 for (node = lstPtr; node; node= node->next)
234 if (!strcmp((const char*) node->name, "lst")) {
235 const char *misspelled = yaz_element_attribute_value_get(node, "lst", "name");
237 wrbuf_printf(wrbuf, "<misspelled term=\"%s\">\n", misspelled);
238 yaz_solr_decode_suggestion_lst(node->children, wrbuf);
239 wrbuf_puts(wrbuf, "</misspelled>\n");
245 static int yaz_solr_decode_spellcheck(ODR o, xmlNodePtr spellcheckPtr, Z_SRW_searchRetrieveResponse *sr)
248 WRBUF wrbuf = wrbuf_alloc();
249 wrbuf_puts(wrbuf, "");
250 for (ptr = spellcheckPtr->children; ptr; ptr = ptr->next)
252 if (match_xml_node_attribute(ptr, "lst", "name", "suggestions"))
254 yaz_solr_decode_misspelled(ptr->children, wrbuf);
257 sr->suggestions = odr_strdup(o, wrbuf_cstr(wrbuf));
261 static int yaz_solr_decode_scan_result(ODR o, xmlNodePtr ptr,
262 Z_SRW_scanResponse *scr)
268 /* find the actual list */
269 for (node = ptr->children; node; node = node->next)
270 if (node->type == XML_ELEMENT_NODE) {
276 for (node = ptr->children; node; node = node->next)
277 if (node->type == XML_ELEMENT_NODE && !strcmp((const char *) node->name, "int"))
281 scr->terms = odr_malloc(o, sizeof(*scr->terms) * scr->num_terms);
283 for (node = ptr->children; node; node = node->next)
285 if (node->type == XML_ELEMENT_NODE && !strcmp((const char *) node->name, "int"))
287 Z_SRW_scanTerm *term = scr->terms + i;
290 const char *val = get_facet_term_count(node, &count);
292 term->numberOfRecords = odr_intdup(o, count);
294 /* if val contains a ^ then it is probably term<^>display term so separate them. This is due to
295 * SOLR not being able to encode them into 2 separate attributes.
297 pos = strchr(val, '^');
299 term->displayTerm = odr_strdup(o, pos + 1);
301 term->value = odr_strdup(o, val);
304 term->value = odr_strdup(o, val);
305 term->displayTerm = NULL;
307 term->whereInList = NULL;
319 int yaz_solr_decode_response(ODR o, Z_HTTP_Response *hres, Z_SRW_PDU **pdup)
322 const char *content_buf = hres->content_buf;
323 int content_len = hres->content_len;
324 xmlDocPtr doc = xmlParseMemory(content_buf, content_len);
328 Z_SRW_searchRetrieveResponse *sr = NULL;
329 Z_SRW_scanResponse *scr = NULL;
337 xmlNodePtr root = xmlDocGetRootElement(doc);
342 else if (strcmp((const char *) root->name, "response"))
348 /** look for result (required) and facets node (optional) */
351 for (ptr = root->children; ptr; ptr = ptr->next)
353 if (ptr->type == XML_ELEMENT_NODE &&
354 !strcmp((const char *) ptr->name, "result")) {
355 pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
356 sr = pdu->u.response;
357 rc_result = yaz_solr_decode_result(o, ptr, sr);
359 if (ptr->type == XML_ELEMENT_NODE &&
360 match_xml_node_attribute(ptr, "lst", "name", "terms")) {
361 pdu = yaz_srw_get(o, Z_SRW_scan_response);
362 scr = pdu->u.scan_response;
363 rc_result = yaz_solr_decode_scan_result(o, ptr, scr);
365 /* TODO The check on hits is a work-around to avoid garbled facets on zero results from the SOLR server.
366 * The work-around works because the results is before the facets in the xml. */
368 if (rc_result == 0 && *sr->numberOfRecords > 0 &&
369 match_xml_node_attribute(ptr, "lst", "name", "facet_counts"))
370 rc_facets = yaz_solr_decode_facet_counts(o, ptr, sr);
371 if (rc_result == 0 && *sr->numberOfRecords == 0 &&
372 match_xml_node_attribute(ptr, "lst", "name", "spellcheck"))
373 rc_facets = yaz_solr_decode_spellcheck(o, ptr, sr);
377 ret = rc_result + rc_facets;
390 static int yaz_solr_encode_facet_field(
391 ODR encode, char **name, char **value, int *i,
392 Z_FacetField *facet_field)
394 Z_AttributeList *attribute_list = facet_field->attributes;
395 struct yaz_facet_attr attr_values;
396 yaz_facet_attr_init(&attr_values);
397 yaz_facet_attr_get_z_attributes(attribute_list, &attr_values);
399 if (attr_values.errcode)
401 if (attr_values.useattr)
403 WRBUF wrbuf = wrbuf_alloc();
404 yaz_add_name_value_str(encode, name, value, i,
406 odr_strdup(encode, attr_values.useattr));
408 if (attr_values.limit > 0)
410 Odr_int v = attr_values.limit;
412 wrbuf_printf(wrbuf, "f.%s.facet.limit", attr_values.useattr);
413 yaz_add_name_value_int(encode, name, value, i,
414 odr_strdup(encode, wrbuf_cstr(wrbuf)),
417 if (attr_values.start > 1)
419 Odr_int v = attr_values.start - 1;
421 wrbuf_printf(wrbuf, "f.%s.facet.offset", attr_values.useattr);
422 yaz_add_name_value_int(encode, name, value, i,
423 odr_strdup(encode, wrbuf_cstr(wrbuf)),
426 if (attr_values.sortorder == 1)
429 wrbuf_printf(wrbuf, "f.%s.facet.sort", attr_values.useattr);
430 yaz_add_name_value_str(encode, name, value, i,
431 odr_strdup(encode, wrbuf_cstr(wrbuf)),
434 wrbuf_destroy(wrbuf);
438 if (attr_values.limit > 0)
440 Odr_int v = attr_values.limit;
441 yaz_add_name_value_int(encode, name, value, i, "facet.limit", &v);
443 if (attr_values.start > 1)
445 Odr_int v = attr_values.start - 1;
446 yaz_add_name_value_int(encode, name, value, i, "facet.offset", &v);
448 if (attr_values.sortorder == 1)
450 yaz_add_name_value_str(encode, name, value, i, "facet.sort",
457 static int yaz_solr_encode_facet_list(
458 ODR encode, char **name, char **value,
459 int *i, Z_FacetList *facet_list)
462 for (index = 0; index < facet_list->num; index++)
464 int r = yaz_solr_encode_facet_field(encode, name, value, i,
465 facet_list->elements[index]);
473 int yaz_solr_encode_request(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
474 ODR encode, const char *charset)
476 const char *solr_op = 0;
477 char **name, **value;
483 const char *path_args = 0;
485 int no_parms = 20; /* safe upper limit of args without extra_args */
488 for (ea = srw_pdu->extra_args; ea; ea = ea->next)
490 name = (char **) odr_malloc(encode, sizeof(*name) * no_parms);
491 value = (char **) odr_malloc(encode, sizeof(*value) * no_parms);
493 for (ea = srw_pdu->extra_args; ea; ea = ea->next)
496 value[i] = ea->value;
500 z_HTTP_header_add_basic_auth(encode, &hreq->headers,
501 srw_pdu->username, srw_pdu->password);
502 if (srw_pdu->which == Z_SRW_searchRetrieve_request)
504 Z_SRW_searchRetrieveRequest *request = srw_pdu->u.request;
506 if (!srw_pdu->u.request->query)
508 yaz_add_name_value_str(encode, name, value, &i, "defType", "lucene");
509 yaz_add_name_value_str(encode, name, value, &i, "q", request->query);
510 if (srw_pdu->u.request->startRecord)
512 Odr_int start = *request->startRecord - 1;
513 yaz_add_name_value_int(encode, name, value, &i,
516 yaz_add_name_value_int(encode, name, value, &i,
517 "rows", request->maximumRecords);
518 yaz_add_name_value_str(encode, name, value, &i,
519 "fl", request->recordSchema);
521 switch(srw_pdu->u.request->sort_type)
523 case Z_SRW_sort_type_none:
525 case Z_SRW_sort_type_sort:
526 yaz_add_name_value_str(encode, name, value, &i, "sort",
527 srw_pdu->u.request->sort.sortKeys);
530 if (request->facetList)
532 Z_FacetList *facet_list = request->facetList;
533 yaz_add_name_value_str(encode, name, value, &i, "facet", "true");
534 yaz_add_name_value_str(encode, name, value, &i, "facet.mincount", "1");
535 if (yaz_solr_encode_facet_list(encode, name, value, &i, facet_list))
539 else if (srw_pdu->which == Z_SRW_scan_request) {
540 Z_SRW_scanRequest *request = srw_pdu->u.scan_request;
542 if (!srw_pdu->u.scan_request->scanClause)
544 if (!strcmp(srw_pdu->u.scan_request->queryType, "pqf"))
546 yaz_add_name_value_str(encode, name, value, &i,
547 "terms.fl", request->scanClause);
548 yaz_add_name_value_str(encode, name, value, &i,
549 "terms.lower", request->scanClause);
551 else if (!strcmp(srw_pdu->u.scan_request->queryType, "cql"))
553 q = request->scanClause;
554 pos = strchr(q, ':');
556 yaz_add_name_value_str(encode, name, value, &i,
557 "terms.lower", odr_strdup(encode, pos + 1));
559 yaz_add_name_value_str(encode, name, value, &i,
560 "terms.fl", odr_strdup(encode, q));
563 yaz_add_name_value_str(encode, name, value, &i,
564 "terms.lower", odr_strdup(encode, q));
569 yaz_add_name_value_str(encode, name, value, &i,
570 "terms.sort", "index");
571 yaz_add_name_value_int(encode, name, value, &i,
572 "terms.limit", request->maximumTerms);
579 yaz_array_to_uri(&uri_args, encode, name, value);
581 hreq->method = "GET";
584 odr_malloc(encode, strlen(hreq->path) +
585 strlen(uri_args) + strlen(solr_op) + 5);
587 cp = strchr(hreq->path, '#');
590 cp = strchr(hreq->path, '?');
593 *cp = '\0'; /* args in path */
596 strcpy(path, hreq->path);
597 cp = strrchr(path, '/');
600 if (!strcmp(cp, "/select") || !strcmp(cp, "/"))
604 strcat(path, solr_op);
608 strcat(path, path_args);
611 strcat(path, uri_args);
614 z_HTTP_header_add_content_type(encode, &hreq->headers,
615 "text/xml", charset);
623 * c-file-style: "Stroustrup"
624 * indent-tabs-mode: nil
626 * vim: shiftwidth=4 tabstop=8 expandtab