Test for preferred target and block=preferred
[pazpar2-moved-to-github.git] / src / session.c
index bcd7353..7e7a489 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2010 Index Data
+   Copyright (C) 2006-2011 Index Data
 
 Pazpar2 is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -41,6 +41,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <signal.h>
 #include <ctype.h>
 #include <assert.h>
+#include <math.h>
 
 #include <yaz/marcdisp.h>
 #include <yaz/comstack.h>
@@ -79,6 +80,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 #define MAX_CHUNK 15
 
+#define MAX(a,b) ((a)>(b)?(a):(b))
+
 // Note: Some things in this structure will eventually move to configuration
 struct parameters global_parameters = 
 {
@@ -148,30 +151,51 @@ void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
 
 void add_facet(struct session *s, const char *type, const char *value, int count)
 {
-    int i;
-
-    if (!*value)
-        return;
-    for (i = 0; i < s->num_termlists; i++)
-        if (!strcmp(s->termlists[i].name, type))
-            break;
-    if (i == s->num_termlists)
+    struct conf_service *service = s->service;
+    pp2_relevance_token_t prt;
+    const char *facet_component;
+    WRBUF facet_wrbuf = wrbuf_alloc();
+    prt = pp2_relevance_tokenize(service->facet_pct);
+    
+    pp2_relevance_first(prt, value, 0);
+    while ((facet_component = pp2_relevance_token_next(prt)))
     {
-        if (i == SESSION_MAX_TERMLISTS)
+        if (*facet_component)
         {
-            session_log(s, YLOG_FATAL, "Too many termlists");
-            return;
+            if (wrbuf_len(facet_wrbuf))
+                wrbuf_puts(facet_wrbuf, " ");
+            wrbuf_puts(facet_wrbuf, facet_component);
         }
-
-        s->termlists[i].name = nmem_strdup(s->nmem, type);
-        s->termlists[i].termlist 
-            = termlist_create(s->nmem, TERMLIST_HIGH_SCORE);
-        s->num_termlists = i + 1;
     }
-    session_log(s, YLOG_DEBUG, "Session: facets for %s: %s (%d)",
-                type, value, count);
-
-    termlist_insert(s->termlists[i].termlist, value, count);
+    pp2_relevance_token_destroy(prt);
+    
+    if (wrbuf_len(facet_wrbuf))
+    {
+        int i;
+        for (i = 0; i < s->num_termlists; i++)
+            if (!strcmp(s->termlists[i].name, type))
+                break;
+        if (i == s->num_termlists)
+        {
+            if (i == SESSION_MAX_TERMLISTS)
+            {
+                session_log(s, YLOG_FATAL, "Too many termlists");
+                wrbuf_destroy(facet_wrbuf);
+                return;
+            }
+            
+            s->termlists[i].name = nmem_strdup(s->nmem, type);
+            s->termlists[i].termlist 
+                = termlist_create(s->nmem, TERMLIST_HIGH_SCORE);
+            s->num_termlists = i + 1;
+        }
+        
+        session_log(s, YLOG_DEBUG, "Session: facets for %s: %s norm:%s (%d)",
+                    type, value, wrbuf_cstr(facet_wrbuf), count);
+        termlist_insert(s->termlists[i].termlist, wrbuf_cstr(facet_wrbuf),
+                        count);
+    }
+    wrbuf_destroy(facet_wrbuf);
 }
 
 static xmlDoc *record_to_xml(struct session *se,
@@ -1242,6 +1266,21 @@ static int ingest_to_cluster(struct client *cl,
                                                     record,
                                                     mergekey_norm,
                                                     &se->total_merged);
+
+    const char *use_term_factor_str = session_setting_oneval(sdb, PZ_TERMLIST_TERM_FACTOR);
+    int use_term_factor = 1;
+    int term_factor = 1; 
+    if (use_term_factor_str)
+       use_term_factor =  atoi(use_term_factor_str);
+
+    if (use_term_factor) {
+        int maxrecs = client_get_maxrecs(cl);
+        int hits = (int) client_get_hits(cl);
+        term_factor = MAX(hits, maxrecs) /  MAX(1, maxrecs);
+        assert(term_factor >= 1);
+        yaz_log(YLOG_DEBUG, "Using term factor %d ", term_factor); 
+    }
+
     if (!cluster)
         return -1;
     if (global_parameters.dump_records)
@@ -1411,15 +1450,16 @@ static int ingest_to_cluster(struct client *cl,
                 {
                     char year[64];
                     sprintf(year, "%d", rec_md->data.number.max);
-                    add_facet(se, (char *) type, year, 1);
+
+                    add_facet(se, (char *) type, year, term_factor);
                     if (rec_md->data.number.max != rec_md->data.number.min)
                     {
                         sprintf(year, "%d", rec_md->data.number.min);
-                        add_facet(se, (char *) type, year, 1);
+                        add_facet(se, (char *) type, year, term_factor);
                     }
                 }
                 else
-                    add_facet(se, (char *) type, (char *) value, 1);
+                    add_facet(se, (char *) type, (char *) value, term_factor);
             }
 
             // cleaning up