Implement SOLR xml int reader (for term + freq)
[yaz-moved-to-github.git] / src / solr.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file srwutil.c
7  * \brief Implements SRW/SRU utilities.
8  */
9
10 #include <stdlib.h>
11 #include <assert.h>
12 #include <yaz/srw.h>
13 #include <yaz/matchstr.h>
14 #include <yaz/yaz-iconv.h>
15 #include <yaz/log.h>
16 #include <yaz/facet.h>
17 #include <yaz/wrbuf.h>
18
19 #include "sru-p.h"
20
21 #if YAZ_HAVE_XML2
22 #include <libxml/parser.h>
23 #include <libxml/tree.h>
24
25 #define SOLR_MAX_PARAMETERS  100
26
27 const char *xml_node_attribute_value_get(xmlNodePtr ptr, const char *node_name, const char *attribute_name) {
28
29     struct _xmlAttr *attr;
30     // check if the node name matches
31     if (strcmp((const char*) ptr->name, node_name))
32         return 0;
33     // check if the attribute name and return the value
34     for (attr = ptr->properties; attr; attr = attr->next)
35         if (attr->children && attr->children->type == XML_TEXT_NODE) {
36             if (!strcmp((const char *) attr->name, attribute_name))
37                 return (const char *) attr->children->content;
38         }
39     return 0;
40 }
41
42
43 static int match_xml_node_attribute(xmlNodePtr ptr, const char *node_name, const char *attribute_name, const char *value)
44 {
45     const char *attribute_value;
46     // check if the node name matches
47     if (strcmp((const char*) ptr->name, node_name))
48         return 0;
49     attribute_value = xml_node_attribute_value_get(ptr, node_name, attribute_name);
50     if (attribute_value && !strcmp(attribute_value, value))
51         return 1;
52     return 0;
53 }
54
55 static void yaz_solr_decode_result_docs(ODR o, xmlNodePtr ptr, Odr_int start, Z_SRW_searchRetrieveResponse *sr) {
56     xmlNodePtr node;
57     int offset = 0;
58     int i = 0;
59
60     sr->num_records = 0;
61     for (node = ptr->children; node; node = node->next)
62         if (node->type == XML_ELEMENT_NODE)
63             sr->num_records++;
64
65     sr->records = odr_malloc(o, sizeof(*sr->records) * sr->num_records);
66
67     for (node = ptr->children; node; node = node->next)
68     {
69         if (node->type == XML_ELEMENT_NODE)
70         {
71             Z_SRW_record *record = sr->records + i;
72             xmlBufferPtr buf = xmlBufferCreate();
73             xmlNode *tmp = xmlCopyNode(node, 1);
74
75             xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
76
77             xmlFreeNode(tmp);
78
79             record->recordSchema = 0;
80             record->recordPacking = Z_SRW_recordPacking_XML;
81             record->recordData_len = buf->use;
82             record->recordData_buf = odr_malloc(o, buf->use + 1);
83             memcpy(record->recordData_buf, buf->content, buf->use);
84             record->recordData_buf[buf->use] = '\0';
85             // TODO Solve the real problem in zoom-sru, that doesnt work with 0-based indexes.
86             // Work-around: Making the recordPosition 1-based.
87             record->recordPosition = odr_intdup(o, start + offset + 1);
88
89             xmlBufferFree(buf);
90
91             offset++;
92             i++;
93         }
94     }
95 }
96
97 static int  yaz_solr_decode_result(ODR o, xmlNodePtr ptr, Z_SRW_searchRetrieveResponse *sr) {
98     Odr_int start = 0;
99     struct _xmlAttr *attr;
100     for (attr = ptr->properties; attr; attr = attr->next)
101         if (attr->children && attr->children->type == XML_TEXT_NODE) {
102             if (!strcmp((const char *) attr->name, "numFound")) {
103                 sr->numberOfRecords = odr_intdup(o, odr_atoi(
104                         (const char *) attr->children->content));
105             } 
106             else if (!strcmp((const char *) attr->name, "start")) {
107                 start = odr_atoi((const char *) attr->children->content);
108             }
109         }
110     if (sr->numberOfRecords && *sr->numberOfRecords > 0)
111         yaz_solr_decode_result_docs(o, ptr, start, sr);
112     if (sr->numberOfRecords)
113         return 0;
114     return -1;
115 }
116
117 static Z_AttributeList *yaz_solr_use_atttribute_create(ODR o, const char *name) {
118     // TODO IMPLEMENT
119     return 0;
120 }
121
122
123 static const char *get_facet_term_count(xmlNodePtr node, int *freq) {
124
125     const char *term = xml_node_attribute_value_get(node, "int", "name");
126     xmlNodePtr child;
127     WRBUF wrbuf = wrbuf_alloc();
128     if (!term)
129         return term;
130
131     for (child = node->children; child ; child = child->next) {
132         if (child->type == XML_TEXT_NODE)
133         wrbuf_puts(wrbuf, (const char *) child->content);
134     }
135     *freq = atoi(wrbuf_cstr(wrbuf));
136     wrbuf_destroy(wrbuf);
137     return term;
138 }
139
140 Z_FacetField *yaz_solr_decode_facet_field(ODR o, xmlNodePtr ptr, Z_SRW_searchRetrieveResponse *sr)
141 {
142     // USE attribute
143     const char* name = xml_node_attribute_value_get(ptr, "lst", "name");
144     Z_AttributeList *list = yaz_solr_use_atttribute_create(o, name);
145     Z_FacetField *facet_field;
146     int num_terms = 0;
147     int index = 0;
148     xmlNodePtr node;
149     for (node = ptr->children; node; node = node->next) {
150         num_terms++;
151     }
152     facet_field = facet_field_create(o, list, num_terms);
153     index = 0;
154     for (node = ptr->children; node; node = node->next) {
155         int count = 0;
156         const char *term = get_facet_term_count(node, &count);
157         facet_field_term_set(o, facet_field, facet_term_create(o, term_create(o, term), count), index);
158         index++;
159     }
160     return facet_field;
161 }
162
163 static int yaz_solr_decode_facet_counts(ODR o, xmlNodePtr root, Z_SRW_searchRetrieveResponse *sr) {
164     xmlNodePtr ptr;
165     for (ptr = root->children; ptr; ptr = ptr->next)
166     {
167         if (match_xml_node_attribute(ptr, "lst", "name", "facet_fields"))
168         {
169             xmlNodePtr node;
170             Z_FacetList *facet_list;
171             int num_facets = 0;
172             for (node = ptr->children; node; node= node->next)
173             {
174                 num_facets++;
175             }
176             facet_list = facet_list_create(o, num_facets);
177             num_facets = 0;
178             for (node = ptr->children; node; node= node->next)
179             {
180                 facet_list_field_set(o, facet_list, yaz_solr_decode_facet_field(o, node, sr), num_facets);
181                 num_facets++;
182             }
183             sr->facetList = facet_list;
184             break;
185         }
186     }
187     return 0;
188 }
189
190 static void yaz_solr_decode_facets(ODR o, xmlNodePtr ptr, Z_SRW_searchRetrieveResponse *sr) {
191     if (match_xml_node_attribute(ptr, "lst", "name", "facet_counts"))
192         yaz_solr_decode_facet_counts(o, ptr->children, sr);
193 }
194 #endif
195
196 int yaz_solr_decode_response(ODR o, Z_HTTP_Response *hres, Z_SRW_PDU **pdup)
197 {
198 #if YAZ_HAVE_XML2
199     const char *content_buf = hres->content_buf;
200     int content_len = hres->content_len;
201     xmlDocPtr doc = xmlParseMemory(content_buf, content_len);
202     int ret = 0;
203     xmlNodePtr ptr = 0;
204     Z_SRW_PDU *pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
205     Z_SRW_searchRetrieveResponse *sr = pdu->u.response;
206
207     if (!doc)
208     {
209         ret = -1;
210     }
211     if (doc)
212     {
213         xmlNodePtr root = xmlDocGetRootElement(doc);
214         if (!root)
215         {
216             ret = -1;
217         }
218         else if (strcmp((const char *) root->name, "response"))
219         {
220             ret = -1;
221         }
222         else
223         {
224             /** look for result (required) and facets node (optional) */
225             int rc_result = -1;
226             int rc_facets = 0;
227             for (ptr = root->children; ptr; ptr = ptr->next)
228             {
229                 if (ptr->type == XML_ELEMENT_NODE &&
230                     !strcmp((const char *) ptr->name, "result"))
231                         rc_result = yaz_solr_decode_result(o, ptr, sr);
232                 if (match_xml_node_attribute(ptr, "lst", "name", "facet_counts"))
233                     rc_facets =  yaz_solr_decode_facet_counts(o, ptr, sr);
234             }
235             ret = rc_result + rc_facets;
236         }
237     }
238     if (doc)
239         xmlFreeDoc(doc);
240     if (ret == 0)
241         *pdup = pdu;
242     return ret;
243 #else
244     return -1;
245 #endif
246 }
247
248 static void yaz_solr_encode_facet_field(ODR encode, char **name, char **value, int *i, Z_FacetField *facet_field, int *limit) {
249       Z_AttributeList *attribute_list = facet_field->attributes;
250       struct yaz_facet_attr attr_values;
251       yaz_facet_attr_init(&attr_values);
252       yaz_facet_attr_get_z_attributes(attribute_list, &attr_values);
253       // TODO do we want to support server decided
254       if (!attr_values.errcode && attr_values.useattr) {
255           yaz_add_name_value_str(encode, name, value, i, "facet.field", (char *) attr_values.useattr);
256           // TODO max(attr_values, *limit);
257           if (attr_values.limit > 0 && attr_values.limit > *limit) {
258               *limit = attr_values.limit;
259           }
260       }
261 }
262
263 static void yaz_solr_encode_facet_list(ODR encode, char **name, char **value, int *i, Z_FacetList *facet_list, int *limit) {
264
265     int index;
266     for (index = 0; index < facet_list->num; index++)  {
267         yaz_solr_encode_facet_field(encode, name, value, i, facet_list->elements[index], limit);
268
269     }
270 }
271
272
273 int yaz_solr_encode_request(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
274                             ODR encode, const char *charset)
275 {
276     const char *solr_op = 0;
277     //TODO Change. not a nice hard coded, unchecked limit.
278     char *name[SOLR_MAX_PARAMETERS], *value[SOLR_MAX_PARAMETERS];
279     char *uri_args;
280     char *path;
281     int i = 0;
282
283     z_HTTP_header_add_basic_auth(encode, &hreq->headers, 
284                                  srw_pdu->username, srw_pdu->password);
285
286     switch (srw_pdu->which)
287     {
288     case Z_SRW_searchRetrieve_request: {
289         Z_SRW_searchRetrieveRequest *request = srw_pdu->u.request;
290         solr_op = "select";
291         switch(srw_pdu->u.request->query_type)
292         {
293         case Z_SRW_query_type_pqf:
294             yaz_add_name_value_str(encode, name, value, &i,
295                                    "q", request->query.pqf);
296             break;
297         case Z_SRW_query_type_cql:
298             yaz_add_name_value_str(encode, name, value, &i,
299                                    "q", request->query.cql);
300             break;
301         default:
302             return -1;
303         }
304         if (srw_pdu->u.request->startRecord)
305         {
306             Odr_int start = *request->startRecord - 1;
307             yaz_add_name_value_int(encode, name, value, &i,
308                                    "start", &start);
309         }
310         yaz_add_name_value_int(encode, name, value, &i,
311                                "rows", request->maximumRecords);
312         yaz_add_name_value_str(encode, name, value, &i,
313                                "fl", request->recordSchema);
314
315         if (request->facetList) {
316             Z_FacetList *facet_list = request->facetList;
317             int limit;
318             Odr_int olimit;
319             yaz_add_name_value_str(encode, name, value, &i, "facet", "true");
320             yaz_solr_encode_facet_list(encode, name, value, &i, facet_list, &limit);
321             olimit = limit;
322             yaz_add_name_value_int(encode, name, value, &i, "facet.limit", &olimit);
323
324         }
325         break;
326     }
327     default:
328         return -1;
329     }
330     name[i] = 0;
331     yaz_array_to_uri(&uri_args, encode, name, value);
332     
333     hreq->method = "GET";
334     
335     path = (char *)
336         odr_malloc(encode, strlen(hreq->path) +
337                    strlen(uri_args) + strlen(solr_op) + 4);
338
339     sprintf(path, "%s/%s?%s", hreq->path, solr_op, uri_args);
340     hreq->path = path;
341
342     z_HTTP_header_add_content_type(encode, &hreq->headers,
343                                    "text/xml", charset);
344     return 0;
345 }
346
347
348 /*
349  * Local variables:
350  * c-basic-offset: 4
351  * c-file-style: "Stroustrup"
352  * indent-tabs-mode: nil
353  * End:
354  * vim: shiftwidth=4 tabstop=8 expandtab
355  */
356