Add position type to sorted_results
[pazpar2-moved-to-github.git] / src / session.c
index 105c064..008a617 100644 (file)
@@ -78,8 +78,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 #include <libxml/tree.h>
 
-#define TERMLIST_HIGH_SCORE 25
-
 #define MAX_CHUNK 15
 
 #define MAX(a,b) ((a)>(b)?(a):(b))
@@ -231,13 +229,12 @@ void add_facet(struct session *s, const char *type, const char *value, int count
             }
             
             s->termlists[i].name = nmem_strdup(s->nmem, type);
-            s->termlists[i].termlist 
-                = termlist_create(s->nmem, TERMLIST_HIGH_SCORE);
+            s->termlists[i].termlist = termlist_create(s->nmem);
             s->num_termlists = i + 1;
         }
         
 #if 0
-        session_log(s, YLOG_DEBUG, "Facets for %s: %s norm:%s (%d)", type, value, wrbuf_cstr(facet_wrbuf), count);
+        session_log(s, YLOG_LOG, "Facets for %s: %s norm:%s (%d)", type, value, wrbuf_cstr(facet_wrbuf), count);
 #endif
         termlist_insert(s->termlists[i].termlist, wrbuf_cstr(display_wrbuf),
                         wrbuf_cstr(facet_wrbuf), count);
@@ -623,7 +620,7 @@ int session_is_preferred_clients_ready(struct session *s)
 }
 
 static void session_clear_set(struct session *se,
-                              const char *sort_field, int increasing)
+                              const char *sort_field, int increasing, int position)
 {
     reclist_destroy(se->reclist);
     se->reclist = 0;
@@ -638,47 +635,47 @@ static void session_clear_set(struct session *se,
     se->sorted_results = nmem_malloc(se->nmem, sizeof(*se->sorted_results));
     se->sorted_results->field = nmem_strdup(se->nmem, sort_field);
     se->sorted_results->increasing = increasing;
+    se->sorted_results->position = position;
     se->sorted_results->next = 0;
     
     se->reclist = reclist_create(se->nmem);
 }
 
 void session_sort(struct session *se, const char *field, int increasing,
-                  int clear_set)
+                  int position)
 {
     struct session_sorted_results *sr;
     struct client_list *l;
 
     session_enter(se);
 
-    yaz_log(YLOG_LOG, "session_sort field=%s", field);
-    if (clear_set)
+    yaz_log(YLOG_LOG, "session_sort field=%s increasing=%d position=%d", field, increasing, position);
+    /* see if we already have sorted for this critieria */
+    for (sr = se->sorted_results; sr; sr = sr->next)
     {
-        session_clear_set(se, field, increasing);
+        if (!strcmp(field, sr->field) && increasing == sr->increasing && sr->position == position)
+            break;
     }
-    else
+    if (sr)
     {
-        /* see if we already have sorted for this critieria */
-        for (sr = se->sorted_results; sr; sr = sr->next)
-        {
-            if (!strcmp(field, sr->field) && increasing == sr->increasing)
-                break;
-        }
-        if (sr)
-        {
-            session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d already fetched",
-                        field, increasing);
-            session_leave(se);
-            return;
-        }
-        session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d must fetch",
-                    field, increasing);
-        sr = nmem_malloc(se->nmem, sizeof(*sr));
-        sr->field = nmem_strdup(se->nmem, field);
-        sr->increasing = increasing;
-        sr->next = se->sorted_results;
-        se->sorted_results = sr;
+        z(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d position=%d already fetched",
+                    field, increasing, position);
+        session_leave(se);
+        return;
+    }
+    if (position)
+    {
+        session_clear_set(se, field, increasing, position);
     }
+
+    session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d must fetch",
+                field, increasing);
+    sr = nmem_malloc(se->nmem, sizeof(*sr));
+    sr->field = nmem_strdup(se->nmem, field);
+    sr->increasing = increasing;
+    sr->position = position;
+    sr->next = se->sorted_results;
+    se->sorted_results = sr;
         
     for (l = se->clients_active; l; l = l->next)
     {
@@ -754,7 +751,7 @@ enum pazpar2_error_code session_search(struct session *se,
             continue;
 
         parse_ret = client_parse_query(cl, query, facet_limits, startrecs,
-            maxrecs);
+                                       maxrecs, se->service->ccl_bibset);
         if (parse_ret == -1)
             no_failed_query++;
         else if (parse_ret == -2)
@@ -1014,23 +1011,6 @@ struct hitsbytarget *get_hitsbytarget(struct session *se, int *count, NMEM nmem)
     session_leave(se);
     return p;
 }
-    
-struct termlist_score **get_termlist_score(struct session *se,
-                                           const char *name, int *num)
-{
-    int i;
-    struct termlist_score **tl = 0;
-
-    session_enter(se);
-    for (i = 0; i < se->num_termlists; i++)
-        if (!strcmp((const char *) se->termlists[i].name, name))
-        {
-            tl = termlist_highscore(se->termlists[i].termlist, num);
-            break;
-        }
-    session_leave(se);
-    return tl;
-}
 
 // Compares two hitsbytarget nodes by hitcount
 static int cmp_ht(const void *p1, const void *p2)
@@ -1131,7 +1111,8 @@ void perform_termlist(struct http_channel *c, struct session *se,
                 wrbuf_puts(c->wrbuf, "\">\n");
                 must_generate_empty = 0;
 
-                p = termlist_highscore(se->termlists[i].termlist, &len);
+                p = termlist_highscore(se->termlists[i].termlist, &len,
+                                       nmem_tmp);
                 if (p)
                 {
                     int i;
@@ -1812,8 +1793,8 @@ static int ingest_to_cluster(struct client *cl,
             struct record_metadata *rec_md = 0;
             int md_field_id = -1;
             int sk_field_id = -1;
-            int rank = 0;
-            xmlChar *rank_str = 0;
+            const char *rank;
+            xmlChar *xml_rank;
             
             type = xmlGetProp(n, (xmlChar *) "type");
             value = xmlNodeListGetString(xdoc, n->children, 1);
@@ -1828,15 +1809,6 @@ static int ingest_to_cluster(struct client *cl,
             
             ser_md = &service->metadata[md_field_id];
 
-            rank_str = xmlGetProp(n, (xmlChar *) "rank");
-            if (rank_str)
-            {
-                rank = atoi((const char *) rank_str);
-                xmlFree(rank_str);
-            }
-            else
-                rank = ser_md->rank;
-            
             if (ser_md->sortkey_offset >= 0)
             {
                 sk_field_id = ser_md->sortkey_offset;
@@ -1849,6 +1821,9 @@ static int ingest_to_cluster(struct client *cl,
             if (!rec_md)
                 continue;
 
+            xml_rank = xmlGetProp(n, (xmlChar *) "rank");
+            rank = xml_rank ? (const char *) xml_rank : ser_md->rank;
+
             wheretoput = &cluster->metadata[md_field_id];
 
             // and polulate with data:
@@ -1933,7 +1908,6 @@ static int ingest_to_cluster(struct client *cl,
                 }
             }
 
-
             // ranking of _all_ fields enabled ... 
             if (rank)
             {
@@ -1961,6 +1935,8 @@ static int ingest_to_cluster(struct client *cl,
             }
 
             // cleaning up
+            if (xml_rank)
+                xmlFree(xml_rank);
             xmlFree(type);
             xmlFree(value);
             type = value = 0;