1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 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 int match_xml_node_attribute(
32 const char *node_name, const char *attribute_name, const char *value)
34 const char *attribute_value;
35 // check if the node name matches
36 if (strcmp((const char*) ptr->name, node_name))
40 attribute_value = yaz_element_attribute_value_get(ptr, node_name,
42 if (attribute_value && !strcmp(attribute_value, value))
45 else /* No attribute to check */
50 static void yaz_solr_decode_result_docs(ODR o, xmlNodePtr ptr,
52 Z_SRW_searchRetrieveResponse *sr)
59 for (node = ptr->children; node; node = node->next)
60 if (node->type == XML_ELEMENT_NODE)
64 sr->records = odr_malloc(o, sizeof(*sr->records) * sr->num_records);
66 for (node = ptr->children; node; node = node->next)
68 if (node->type == XML_ELEMENT_NODE)
70 Z_SRW_record *record = sr->records + i;
71 xmlBufferPtr buf = xmlBufferCreate();
72 xmlNode *tmp = xmlCopyNode(node, 1);
74 xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
78 record->recordSchema = 0;
79 record->recordPacking = Z_SRW_recordPacking_XML;
80 record->recordData_len = buf->use;
81 record->recordData_buf = odr_malloc(o, buf->use + 1);
82 memcpy(record->recordData_buf, buf->content, buf->use);
83 record->recordData_buf[buf->use] = '\0';
84 // TODO Solve the real problem in zoom-sru, that doesnt work with 0-based indexes.
85 // Work-around: Making the recordPosition 1-based.
86 record->recordPosition = odr_intdup(o, start + offset + 1);
96 static int yaz_solr_decode_result(ODR o, xmlNodePtr ptr,
97 Z_SRW_searchRetrieveResponse *sr)
100 struct _xmlAttr *attr;
101 for (attr = ptr->properties; attr; attr = attr->next)
102 if (attr->children && attr->children->type == XML_TEXT_NODE)
104 if (!strcmp((const char *) attr->name, "numFound"))
106 sr->numberOfRecords = odr_intdup(o, odr_atoi(
107 (const char *) attr->children->content));
109 else if (!strcmp((const char *) attr->name, "start"))
111 start = odr_atoi((const char *) attr->children->content);
114 if (sr->numberOfRecords && *sr->numberOfRecords > 0)
115 yaz_solr_decode_result_docs(o, ptr, start, sr);
116 if (sr->numberOfRecords)
121 static const char *get_facet_term_count(xmlNodePtr node, Odr_int *freq)
123 const char *term = yaz_element_attribute_value_get(node, "int", "name");
125 WRBUF wrbuf = wrbuf_alloc();
129 for (child = node->children; child ; child = child->next)
131 if (child->type == XML_TEXT_NODE)
132 wrbuf_puts(wrbuf, (const char *) child->content);
134 *freq = odr_atoi(wrbuf_cstr(wrbuf));
135 wrbuf_destroy(wrbuf);
139 Z_FacetField *yaz_solr_decode_facet_field(ODR o, xmlNodePtr ptr,
140 Z_SRW_searchRetrieveResponse *sr)
143 Z_AttributeList *list;
144 Z_FacetField *facet_field;
149 const char* name = yaz_element_attribute_value_get(ptr, "lst", "name");
150 list = yaz_use_attribute_create(o, name);
151 for (node = ptr->children; node; node = node->next)
153 facet_field = facet_field_create(o, list, num_terms);
155 for (node = ptr->children; node; node = node->next)
158 const char *term = get_facet_term_count(node, &count);
159 facet_field_term_set(o, facet_field,
160 facet_term_create_cstr(o, term, count), index);
166 static int yaz_solr_decode_facet_counts(ODR o, xmlNodePtr root,
167 Z_SRW_searchRetrieveResponse *sr)
170 for (ptr = root->children; ptr; ptr = ptr->next)
172 if (match_xml_node_attribute(ptr, "lst", "name", "facet_fields"))
175 Z_FacetList *facet_list;
177 for (node = ptr->children; node; node= node->next)
181 facet_list = facet_list_create(o, num_facets);
183 for (node = ptr->children; node; node= node->next)
185 facet_list_field_set(o, facet_list,
186 yaz_solr_decode_facet_field(o, node, sr),
190 sr->facetList = facet_list;
199 int yaz_solr_decode_response(ODR o, Z_HTTP_Response *hres, Z_SRW_PDU **pdup)
202 const char *content_buf = hres->content_buf;
203 int content_len = hres->content_len;
204 xmlDocPtr doc = xmlParseMemory(content_buf, content_len);
207 Z_SRW_PDU *pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
208 Z_SRW_searchRetrieveResponse *sr = pdu->u.response;
216 xmlNodePtr root = xmlDocGetRootElement(doc);
221 else if (strcmp((const char *) root->name, "response"))
227 /** look for result (required) and facets node (optional) */
230 for (ptr = root->children; ptr; ptr = ptr->next)
232 if (ptr->type == XML_ELEMENT_NODE &&
233 !strcmp((const char *) ptr->name, "result"))
234 rc_result = yaz_solr_decode_result(o, ptr, sr);
235 /* TODO The check on hits is a work-around to avoid garbled facets on zero results from the SOLR server.
236 * The work-around works because the results is before the facets in the xml. */
237 if (rc_result == 0 && *sr->numberOfRecords > 0 &&
238 match_xml_node_attribute(ptr, "lst", "name",
240 rc_facets = yaz_solr_decode_facet_counts(o, ptr, sr);
242 ret = rc_result + rc_facets;
255 static int yaz_solr_encode_facet_field(
256 ODR encode, char **name, char **value, int *i,
257 Z_FacetField *facet_field)
259 Z_AttributeList *attribute_list = facet_field->attributes;
260 struct yaz_facet_attr attr_values;
261 yaz_facet_attr_init(&attr_values);
262 yaz_facet_attr_get_z_attributes(attribute_list, &attr_values);
263 // TODO do we want to support server decided
265 if (attr_values.errcode)
267 if (attr_values.useattr)
269 WRBUF wrbuf = wrbuf_alloc();
270 wrbuf_puts(wrbuf, (char *) attr_values.useattr);
271 yaz_add_name_value_str(encode, name, value, i,
273 odr_strdup(encode, wrbuf_cstr(wrbuf)));
274 if (attr_values.limit > 0)
276 WRBUF wrbuf2 = wrbuf_alloc();
278 wrbuf_puts(wrbuf2, "f.");
279 wrbuf_puts(wrbuf2, wrbuf_cstr(wrbuf));
280 wrbuf_puts(wrbuf2, ".facet.limit");
281 olimit = attr_values.limit;
282 yaz_add_name_value_int(encode, name, value, i,
283 odr_strdup(encode, wrbuf_cstr(wrbuf2)),
285 wrbuf_destroy(wrbuf2);
287 wrbuf_destroy(wrbuf);
292 static int yaz_solr_encode_facet_list(
293 ODR encode, char **name, char **value,
294 int *i, Z_FacetList *facet_list)
297 for (index = 0; index < facet_list->num; index++)
299 int r = yaz_solr_encode_facet_field(encode, name, value, i,
300 facet_list->elements[index]);
308 int yaz_solr_encode_request(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
309 ODR encode, const char *charset)
311 const char *solr_op = 0;
312 //TODO Change. not a nice hard coded, unchecked limit.
313 char *name[SOLR_MAX_PARAMETERS], *value[SOLR_MAX_PARAMETERS];
318 z_HTTP_header_add_basic_auth(encode, &hreq->headers,
319 srw_pdu->username, srw_pdu->password);
320 if (srw_pdu->which == Z_SRW_searchRetrieve_request)
322 Z_SRW_searchRetrieveRequest *request = srw_pdu->u.request;
324 switch (srw_pdu->u.request->query_type)
326 case Z_SRW_query_type_pqf:
327 yaz_add_name_value_str(encode, name, value, &i,
328 "q", request->query.pqf);
330 case Z_SRW_query_type_cql:
331 yaz_add_name_value_str(encode, name, value, &i,
332 "q", request->query.cql);
337 if (srw_pdu->u.request->startRecord)
339 Odr_int start = *request->startRecord - 1;
340 yaz_add_name_value_int(encode, name, value, &i,
343 yaz_add_name_value_int(encode, name, value, &i,
344 "rows", request->maximumRecords);
345 yaz_add_name_value_str(encode, name, value, &i,
346 "fl", request->recordSchema);
348 if (request->facetList)
350 Z_FacetList *facet_list = request->facetList;
351 yaz_add_name_value_str(encode, name, value, &i, "facet", "true");
352 yaz_add_name_value_str(encode, name, value, &i, "facet.mincount", "1");
353 if (yaz_solr_encode_facet_list(encode, name, value, &i, facet_list))
360 if (srw_pdu->extra_args)
362 Z_SRW_extra_arg *ea = srw_pdu->extra_args;
363 for (; ea && i < SOLR_MAX_PARAMETERS; ea = ea->next)
366 value[i] = ea->value;
373 yaz_array_to_uri(&uri_args, encode, name, value);
375 hreq->method = "GET";
378 odr_malloc(encode, strlen(hreq->path) +
379 strlen(uri_args) + strlen(solr_op) + 4);
381 sprintf(path, "%s/%s?%s", hreq->path, solr_op, uri_args);
384 z_HTTP_header_add_content_type(encode, &hreq->headers,
385 "text/xml", charset);
393 * c-file-style: "Stroustrup"
394 * indent-tabs-mode: nil
396 * vim: shiftwidth=4 tabstop=8 expandtab