1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2012 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 = odr_malloc(o, buf->use + 1);
91 memcpy(record->recordData_buf, buf->content, buf->use);
92 record->recordData_buf[buf->use] = '\0';
93 record->recordPosition = odr_intdup(o, start + offset + 1);
103 static int yaz_solr_decode_result(ODR o, xmlNodePtr ptr,
104 Z_SRW_searchRetrieveResponse *sr)
107 struct _xmlAttr *attr;
108 for (attr = ptr->properties; attr; attr = attr->next)
109 if (attr->children && attr->children->type == XML_TEXT_NODE)
111 if (!strcmp((const char *) attr->name, "numFound"))
113 sr->numberOfRecords = odr_intdup(o, odr_atoi(
114 (const char *) attr->children->content));
116 else if (!strcmp((const char *) attr->name, "start"))
118 start = odr_atoi((const char *) attr->children->content);
121 if (sr->numberOfRecords && *sr->numberOfRecords > 0)
122 yaz_solr_decode_result_docs(o, ptr, start, sr);
123 if (sr->numberOfRecords)
128 static const char *get_facet_term_count(xmlNodePtr node, Odr_int *freq)
130 const char *term = yaz_element_attribute_value_get(node, "int", "name");
132 WRBUF wrbuf = wrbuf_alloc();
136 for (child = node->children; child ; child = child->next)
138 if (child->type == XML_TEXT_NODE)
139 wrbuf_puts(wrbuf, (const char *) child->content);
141 *freq = odr_atoi(wrbuf_cstr(wrbuf));
142 wrbuf_destroy(wrbuf);
146 Z_FacetField *yaz_solr_decode_facet_field(ODR o, xmlNodePtr ptr,
147 Z_SRW_searchRetrieveResponse *sr)
150 Z_AttributeList *list;
151 Z_FacetField *facet_field;
156 const char* name = yaz_element_attribute_value_get(ptr, "lst", "name");
157 list = yaz_use_attribute_create(o, name);
158 for (node = ptr->children; node; node = node->next)
160 facet_field = facet_field_create(o, list, num_terms);
162 for (node = ptr->children; node; node = node->next)
165 const char *term = get_facet_term_count(node, &count);
166 facet_field_term_set(o, facet_field,
167 facet_term_create_cstr(o, term, count), index);
173 static int yaz_solr_decode_facet_counts(ODR o, xmlNodePtr root,
174 Z_SRW_searchRetrieveResponse *sr)
177 for (ptr = root->children; ptr; ptr = ptr->next)
179 if (match_xml_node_attribute(ptr, "lst", "name", "facet_fields"))
182 Z_FacetList *facet_list;
184 for (node = ptr->children; node; node= node->next)
188 facet_list = facet_list_create(o, num_facets);
190 for (node = ptr->children; node; node= node->next)
192 facet_list_field_set(o, facet_list,
193 yaz_solr_decode_facet_field(o, node, sr),
197 sr->facetList = facet_list;
204 static void yaz_solr_decode_suggestion_values(xmlNodePtr listPptr, WRBUF wrbuf)
207 for (node = listPptr; node; node= node->next) {
208 if (!strcmp((char*) node->name, "lst")) {
210 for (child = node->children; child; child= child->next) {
211 if (match_xml_node_attribute(child, "str", "name", "word")) {
212 wrbuf_puts(wrbuf, "<suggestion>");
213 extract_text_node(child, wrbuf);
214 wrbuf_puts(wrbuf, "</suggestion>\n");
221 static void yaz_solr_decode_suggestion_lst(xmlNodePtr lstPtr, WRBUF wrbuf)
224 for (node = lstPtr; node; node= node->next) {
225 if (match_xml_node_attribute(node, "arr", "name", "suggestion")) {
226 yaz_solr_decode_suggestion_values(node->children, wrbuf);
231 static void yaz_solr_decode_misspelled(xmlNodePtr lstPtr, WRBUF wrbuf)
234 for (node = lstPtr; node; node= node->next)
236 if (!strcmp((const char*) node->name, "lst")) {
237 const char *misspelled = yaz_element_attribute_value_get(node, "lst", "name");
239 wrbuf_printf(wrbuf, "<misspelled term=\"%s\">\n", misspelled);
240 yaz_solr_decode_suggestion_lst(node->children, wrbuf);
241 wrbuf_puts(wrbuf, "</misspelled>\n");
247 static int yaz_solr_decode_spellcheck(ODR o, xmlNodePtr spellcheckPtr, Z_SRW_searchRetrieveResponse *sr)
250 WRBUF wrbuf = wrbuf_alloc();
251 wrbuf_puts(wrbuf, "");
252 for (ptr = spellcheckPtr->children; ptr; ptr = ptr->next)
254 if (match_xml_node_attribute(ptr, "lst", "name", "suggestions"))
256 yaz_solr_decode_misspelled(ptr->children, wrbuf);
259 sr->suggestions = odr_strdup(o, wrbuf_cstr(wrbuf));
264 int yaz_solr_decode_response(ODR o, Z_HTTP_Response *hres, Z_SRW_PDU **pdup)
267 const char *content_buf = hres->content_buf;
268 int content_len = hres->content_len;
269 xmlDocPtr doc = xmlParseMemory(content_buf, content_len);
272 Z_SRW_PDU *pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
273 Z_SRW_searchRetrieveResponse *sr = pdu->u.response;
281 xmlNodePtr root = xmlDocGetRootElement(doc);
286 else if (strcmp((const char *) root->name, "response"))
292 /** look for result (required) and facets node (optional) */
295 for (ptr = root->children; ptr; ptr = ptr->next)
297 if (ptr->type == XML_ELEMENT_NODE &&
298 !strcmp((const char *) ptr->name, "result"))
299 rc_result = yaz_solr_decode_result(o, ptr, sr);
300 /* TODO The check on hits is a work-around to avoid garbled facets on zero results from the SOLR server.
301 * The work-around works because the results is before the facets in the xml. */
302 if (rc_result == 0 && *sr->numberOfRecords > 0 &&
303 match_xml_node_attribute(ptr, "lst", "name", "facet_counts"))
304 rc_facets = yaz_solr_decode_facet_counts(o, ptr, sr);
305 if (rc_result == 0 && *sr->numberOfRecords == 0 &&
306 match_xml_node_attribute(ptr, "lst", "name", "spellcheck"))
307 rc_facets = yaz_solr_decode_spellcheck(o, ptr, sr);
310 ret = rc_result + rc_facets;
323 static int yaz_solr_encode_facet_field(
324 ODR encode, char **name, char **value, int *i,
325 Z_FacetField *facet_field)
327 Z_AttributeList *attribute_list = facet_field->attributes;
328 struct yaz_facet_attr attr_values;
329 yaz_facet_attr_init(&attr_values);
330 yaz_facet_attr_get_z_attributes(attribute_list, &attr_values);
331 // TODO do we want to support server decided
333 if (attr_values.errcode)
335 if (attr_values.useattr)
337 WRBUF wrbuf = wrbuf_alloc();
338 wrbuf_puts(wrbuf, (char *) attr_values.useattr);
339 yaz_add_name_value_str(encode, name, value, i,
341 odr_strdup(encode, wrbuf_cstr(wrbuf)));
342 if (attr_values.limit > 0)
344 WRBUF wrbuf2 = wrbuf_alloc();
346 wrbuf_puts(wrbuf2, "f.");
347 wrbuf_puts(wrbuf2, wrbuf_cstr(wrbuf));
348 wrbuf_puts(wrbuf2, ".facet.limit");
349 olimit = attr_values.limit;
350 yaz_add_name_value_int(encode, name, value, i,
351 odr_strdup(encode, wrbuf_cstr(wrbuf2)),
353 wrbuf_destroy(wrbuf2);
355 wrbuf_destroy(wrbuf);
360 static int yaz_solr_encode_facet_list(
361 ODR encode, char **name, char **value,
362 int *i, Z_FacetList *facet_list)
365 for (index = 0; index < facet_list->num; index++)
367 int r = yaz_solr_encode_facet_field(encode, name, value, i,
368 facet_list->elements[index]);
376 int yaz_solr_encode_request(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
377 ODR encode, const char *charset)
379 const char *solr_op = 0;
380 //TODO Change. not a nice hard coded, unchecked limit.
381 char *name[SOLR_MAX_PARAMETERS], *value[SOLR_MAX_PARAMETERS];
386 z_HTTP_header_add_basic_auth(encode, &hreq->headers,
387 srw_pdu->username, srw_pdu->password);
388 if (srw_pdu->which == Z_SRW_searchRetrieve_request)
390 Z_SRW_searchRetrieveRequest *request = srw_pdu->u.request;
392 switch (srw_pdu->u.request->query_type)
394 case Z_SRW_query_type_pqf:
395 yaz_add_name_value_str(encode, name, value, &i,
396 "q", request->query.pqf);
398 case Z_SRW_query_type_cql:
399 yaz_add_name_value_str(encode, name, value, &i,
400 "q", request->query.cql);
405 if (srw_pdu->u.request->startRecord)
407 Odr_int start = *request->startRecord - 1;
408 yaz_add_name_value_int(encode, name, value, &i,
411 yaz_add_name_value_int(encode, name, value, &i,
412 "rows", request->maximumRecords);
413 yaz_add_name_value_str(encode, name, value, &i,
414 "fl", request->recordSchema);
416 if (request->facetList)
418 Z_FacetList *facet_list = request->facetList;
419 yaz_add_name_value_str(encode, name, value, &i, "facet", "true");
420 yaz_add_name_value_str(encode, name, value, &i, "facet.mincount", "1");
421 if (yaz_solr_encode_facet_list(encode, name, value, &i, facet_list))
428 if (srw_pdu->extra_args)
430 Z_SRW_extra_arg *ea = srw_pdu->extra_args;
431 for (; ea && i < SOLR_MAX_PARAMETERS; ea = ea->next)
434 value[i] = ea->value;
441 yaz_array_to_uri(&uri_args, encode, name, value);
443 hreq->method = "GET";
446 odr_malloc(encode, strlen(hreq->path) +
447 strlen(uri_args) + strlen(solr_op) + 4);
449 sprintf(path, "%s/%s?%s", hreq->path, solr_op, uri_args);
452 z_HTTP_header_add_content_type(encode, &hreq->headers,
453 "text/xml", charset);
461 * c-file-style: "Stroustrup"
462 * indent-tabs-mode: nil
464 * vim: shiftwidth=4 tabstop=8 expandtab