Rename yaz_use_attribute_create to zget_AttributeList_use_string
[yaz-moved-to-github.git] / src / solr.c
index ca35e56..c1c1bba 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2013 Index Data
+ * Copyright (C) Index Data
  * See the file LICENSE for details.
  */
 /**
@@ -18,6 +18,7 @@
 #include <yaz/log.h>
 #include <yaz/facet.h>
 #include <yaz/wrbuf.h>
+#include <yaz/proto.h>
 
 #include "sru-p.h"
 
@@ -87,9 +88,8 @@ static void yaz_solr_decode_result_docs(ODR o, xmlNodePtr ptr,
             record->recordSchema = 0;
             record->recordPacking = Z_SRW_recordPacking_XML;
             record->recordData_len = buf->use;
-            record->recordData_buf = odr_malloc(o, buf->use + 1);
-            memcpy(record->recordData_buf, buf->content, buf->use);
-            record->recordData_buf[buf->use] = '\0';
+            record->recordData_buf =
+                odr_strdupn(o, (const char *) buf->content, buf->use);
             record->recordPosition = odr_intdup(o, start + offset + 1);
 
             xmlBufferFree(buf);
@@ -154,7 +154,7 @@ Z_FacetField *yaz_solr_decode_facet_field(ODR o, xmlNodePtr ptr,
     xmlNodePtr node;
     // USE attribute
     const char* name = yaz_element_attribute_value_get(ptr, "lst", "name");
-    list = yaz_use_attribute_create(o, name);
+    list = zget_AttributeList_use_string(o, name);
     for (node = ptr->children; node; node = node->next)
         num_terms++;
     facet_field = facet_field_create(o, list, num_terms);
@@ -397,32 +397,62 @@ static int yaz_solr_encode_facet_field(
     struct yaz_facet_attr attr_values;
     yaz_facet_attr_init(&attr_values);
     yaz_facet_attr_get_z_attributes(attribute_list, &attr_values);
-    // TODO do we want to support server decided
 
     if (attr_values.errcode)
         return -1;
     if (attr_values.useattr)
     {
         WRBUF wrbuf = wrbuf_alloc();
-        wrbuf_puts(wrbuf, (char *) attr_values.useattr);
         yaz_add_name_value_str(encode, name, value, i,
                                "facet.field",
-                               odr_strdup(encode, wrbuf_cstr(wrbuf)));
+                               odr_strdup(encode, attr_values.useattr));
+
         if (attr_values.limit > 0)
         {
-            WRBUF wrbuf2 = wrbuf_alloc();
-            Odr_int olimit;
-            wrbuf_puts(wrbuf2, "f.");
-            wrbuf_puts(wrbuf2, wrbuf_cstr(wrbuf));
-            wrbuf_puts(wrbuf2, ".facet.limit");
-            olimit = attr_values.limit;
+            Odr_int v = attr_values.limit;
+            wrbuf_rewind(wrbuf);
+            wrbuf_printf(wrbuf, "f.%s.facet.limit", attr_values.useattr);
+            yaz_add_name_value_int(encode, name, value, i,
+                                   odr_strdup(encode, wrbuf_cstr(wrbuf)),
+                                   &v);
+        }
+        if (attr_values.start > 1)
+        {
+            Odr_int v = attr_values.start - 1;
+            wrbuf_rewind(wrbuf);
+            wrbuf_printf(wrbuf, "f.%s.facet.offset", attr_values.useattr);
             yaz_add_name_value_int(encode, name, value, i,
-                                   odr_strdup(encode, wrbuf_cstr(wrbuf2)),
-                                   &olimit);
-            wrbuf_destroy(wrbuf2);
+                                   odr_strdup(encode, wrbuf_cstr(wrbuf)),
+                                   &v);
+        }
+        if (attr_values.sortorder == 1)
+        {
+            wrbuf_rewind(wrbuf);
+            wrbuf_printf(wrbuf, "f.%s.facet.sort", attr_values.useattr);
+            yaz_add_name_value_str(encode, name, value, i,
+                                   odr_strdup(encode, wrbuf_cstr(wrbuf)),
+                                   "index");
         }
         wrbuf_destroy(wrbuf);
     }
+    else
+    {
+        if (attr_values.limit > 0)
+        {
+            Odr_int v = attr_values.limit;
+            yaz_add_name_value_int(encode, name, value, i, "facet.limit", &v);
+        }
+        if (attr_values.start > 1)
+        {
+            Odr_int v = attr_values.start - 1;
+            yaz_add_name_value_int(encode, name, value, i, "facet.offset", &v);
+        }
+        if (attr_values.sortorder == 1)
+        {
+            yaz_add_name_value_str(encode, name, value, i, "facet.sort",
+                                   "index");
+        }
+    }
     return 0;
 }
 
@@ -453,6 +483,7 @@ int yaz_solr_encode_request(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
     char *q;
     char *pos;
     char *cp;
+    const char *path_args = 0;
     int i = 0;
 
     z_HTTP_header_add_basic_auth(encode, &hreq->headers,
@@ -551,8 +582,17 @@ int yaz_solr_encode_request(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
 
     path = (char *)
         odr_malloc(encode, strlen(hreq->path) +
-                   strlen(uri_args) + strlen(solr_op) + 4);
+                   strlen(uri_args) + strlen(solr_op) + 5);
 
+    cp = strchr(hreq->path, '#');
+    if (cp)
+        *cp = '\0';
+    cp = strchr(hreq->path, '?');
+    if (cp)
+    {
+        *cp = '\0'; /* args in path */
+        path_args = cp + 1;
+    }
     strcpy(path, hreq->path);
     cp = strrchr(path, '/');
     if (cp)
@@ -563,6 +603,11 @@ int yaz_solr_encode_request(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
     strcat(path, "/");
     strcat(path, solr_op);
     strcat(path, "?");
+    if (path_args)
+    {
+        strcat(path, path_args);
+        strcat(path, "&");
+    }
     strcat(path, uri_args);
     hreq->path = path;