Add fallback template
[pazpar2-moved-to-github.git] / src / client.c
index 6a7f6a4..510cf68 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
@@ -51,6 +51,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <yaz/diagbib1.h>
 #include <yaz/snprintf.h>
 #include <yaz/rpn2cql.h>
+#include <yaz/rpn2solr.h>
 
 #define USE_TIMING 0
 #if USE_TIMING
@@ -66,23 +67,39 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include "relevance.h"
 #include "incref.h"
 
-/* client counting (1) , disable client counting (0) */
-#if 1
 static YAZ_MUTEX g_mutex = 0;
 static int no_clients = 0;
+static int no_clients_total = 0;
 
-static void client_use(int delta)
+static int client_use(int delta)
 {
+    int clients;
     if (!g_mutex)
         yaz_mutex_create(&g_mutex);
     yaz_mutex_enter(g_mutex);
     no_clients += delta;
+    if (delta > 0)
+        no_clients_total += delta;
+    clients = no_clients;
     yaz_mutex_leave(g_mutex);
-    yaz_log(YLOG_DEBUG, "%s clients=%d", delta > 0 ? "INC" : "DEC", no_clients);
+    yaz_log(YLOG_DEBUG, "%s clients=%d", delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), clients);
+    return clients;
+}
+
+int  clients_count(void) {
+    return client_use(0);
+}
+
+int  clients_count_total(void) {
+    int total = 0;
+    if (!g_mutex)
+        return 0;
+    yaz_mutex_enter(g_mutex);
+    total = no_clients_total;
+    yaz_mutex_leave(g_mutex);
+    return total;
 }
-#else
-#define client_use(x)
-#endif
+
 
 /** \brief Represents client state for a connection to one search target */
 struct client {
@@ -96,11 +113,22 @@ struct client {
     int maxrecs;
     int startrecs;
     int diagnostic;
+    int preferred;
+    struct suggestions *suggestions;
     enum client_state state;
     struct show_raw *show_raw;
     ZOOM_resultset resultset;
     YAZ_MUTEX mutex;
     int ref_count;
+    char *id;
+};
+
+struct suggestions {
+    NMEM nmem;
+    int num;
+    char **misspelled;
+    char **suggest;
+    char *passthrough;
 };
 
 struct show_raw {
@@ -134,6 +162,11 @@ enum client_state client_get_state(struct client *cl)
     return cl->state;
 }
 
+void client_set_state_nb(struct client *cl, enum client_state st)
+{
+    cl->state = st;
+}
+
 void client_set_state(struct client *cl, enum client_state st)
 {
     int was_active = 0;
@@ -145,9 +178,16 @@ void client_set_state(struct client *cl, enum client_state st)
        that session is not mutex locked if client is already active */
     if (was_active && !client_is_active(cl) && cl->session)
     {
+
         int no_active = session_active_clients(cl->session);
-        if (no_active == 0)
+        yaz_log(YLOG_DEBUG, "%s: releasing watches on zero active: %d",
+                client_get_id(cl), no_active);
+        if (no_active == 0) {
             session_alert_watch(cl->session, SESSION_WATCH_SHOW);
+            session_alert_watch(cl->session, SESSION_WATCH_BYTARGET);
+            session_alert_watch(cl->session, SESSION_WATCH_TERMLIST);
+            session_alert_watch(cl->session, SESSION_WATCH_SHOW_PREF);
+        }
     }
 }
 
@@ -323,7 +363,7 @@ static void client_send_raw_present(struct client *cl)
     assert(set);
 
     yaz_log(YLOG_DEBUG, "%s: trying to present %d record(s) from %d",
-            client_get_url(cl), 1, offset);
+            client_get_id(cl), 1, offset);
 
     if (cl->show_raw->syntax)
         syntax = cl->show_raw->syntax;
@@ -379,11 +419,6 @@ static int nativesyntax_to_type(struct session_database *sdb, char *type,
                 strcpy(type, "xml");
                 return 0;
             }
-            else if (!strcmp(syntax, "TXML"))
-                {
-                    strcpy(type, "txml");
-                    return 0;
-                }
             else if (!strcmp(syntax, "USmarc") || !strcmp(syntax, "MARC21"))
             {
                 strcpy(type, "xml; charset=marc8-s");
@@ -399,27 +434,48 @@ static int nativesyntax_to_type(struct session_database *sdb, char *type,
  * TODO Consider thread safety!!!
  *
  */
-int client_report_facets(struct client *cl, ZOOM_resultset rs) {
-    int facet_idx;
+void client_report_facets(struct client *cl, ZOOM_resultset rs)
+{
+    struct session_database *sdb = client_get_database(cl);
     ZOOM_facet_field *facets = ZOOM_resultset_facets(rs);
-    int facet_num;
-    struct session *se = client_get_session(cl);
-    facet_num = ZOOM_resultset_facets_size(rs);
-    yaz_log(YLOG_DEBUG, "client_report_facets: %d", facet_num);
-
-    for (facet_idx = 0; facet_idx < facet_num; facet_idx++) {
-        const char *name = ZOOM_facet_field_name(facets[facet_idx]);
-        size_t term_idx;
-        size_t term_num = ZOOM_facet_field_term_count(facets[facet_idx]);
-        for (term_idx = 0; term_idx < term_num; term_idx++ ) {
-            int freq;
-            const char *term = ZOOM_facet_field_get_term(facets[facet_idx], term_idx, &freq);
-            if (term)
-                add_facet(se, name, term, freq);
+
+    if (sdb && facets)
+    {
+        struct session *se = client_get_session(cl);
+        int facet_num = ZOOM_resultset_facets_size(rs);
+        struct setting *s;
+
+        for (s = sdb->settings[PZ_FACETMAP]; s; s = s->next)
+        {
+            const char *p = strchr(s->name + 3, ':');
+            if (p && p[1] && s->value && s->value[0])
+            {
+                int facet_idx;
+                p++; /* p now holds logical facet name */
+                for (facet_idx = 0; facet_idx < facet_num; facet_idx++)
+                {
+                    const char *native_name =
+                        ZOOM_facet_field_name(facets[facet_idx]);
+                    if (native_name && !strcmp(s->value, native_name))
+                    {
+                        size_t term_idx;
+                        size_t term_num =
+                            ZOOM_facet_field_term_count(facets[facet_idx]);
+                        for (term_idx = 0; term_idx < term_num; term_idx++ )
+                        {
+                            int freq;
+                            const char *term =
+                                ZOOM_facet_field_get_term(facets[facet_idx],
+                                                          term_idx, &freq);
+                            if (term)
+                                add_facet(se, p, term, freq);
+                        }
+                        break;
+                    }
+                }
+            }
         }
     }
-
-    return 0;
 }
 
 static void ingest_raw_record(struct client *cl, ZOOM_record rec)
@@ -441,10 +497,33 @@ static void ingest_raw_record(struct client *cl, ZOOM_record rec)
     client_show_raw_dequeue(cl);
 }
 
+void client_check_preferred_watch(struct client *cl)
+{
+    struct session *se = cl->session;
+    yaz_log(YLOG_DEBUG, "client_check_preferred_watch: %s ", client_get_id(cl));
+    if (se)
+    {
+        client_unlock(cl);
+        /* TODO possible threading issue. Session can have been destroyed */
+        if (session_is_preferred_clients_ready(se)) {
+            session_alert_watch(se, SESSION_WATCH_SHOW_PREF);
+        }
+        else
+            yaz_log(YLOG_DEBUG, "client_check_preferred_watch: Still locked on preferred targets.");
+
+        client_lock(cl);
+    }
+    else
+        yaz_log(YLOG_WARN, "client_check_preferred_watch: %s. No session!", client_get_id(cl));
+
+}
+
+struct suggestions* client_suggestions_create(const char* suggestions_string);
+static void client_suggestions_destroy(struct client *cl);
+
 void client_search_response(struct client *cl)
 {
     struct connection *co = cl->connection;
-    struct session *se = cl->session;
     ZOOM_connection link = connection_get_link(co);
     ZOOM_resultset resultset = cl->resultset;
 
@@ -455,15 +534,18 @@ void client_search_response(struct client *cl)
         cl->hits = 0;
         client_set_state(cl, Client_Error);
         yaz_log(YLOG_WARN, "Search error %s (%s): %s",
-                error, addinfo, client_get_url(cl));
+                error, addinfo, client_get_id(cl));
     }
     else
     {
+        yaz_log(YLOG_DEBUG, "client_search_response: hits "
+                ODR_INT_PRINTF, cl->hits);
         client_report_facets(cl, resultset);
         cl->record_offset = cl->startrecs;
         cl->hits = ZOOM_resultset_size(resultset);
-        if (se)
-            se->total_hits += cl->hits;
+        if (cl->suggestions)
+            client_suggestions_destroy(cl);
+        cl->suggestions = client_suggestions_create(ZOOM_resultset_option_get(resultset, "suggestions"));
     }
 }
 
@@ -474,6 +556,8 @@ void client_got_records(struct client *cl)
     {
         client_unlock(cl);
         session_alert_watch(se, SESSION_WATCH_SHOW);
+        session_alert_watch(se, SESSION_WATCH_BYTARGET);
+        session_alert_watch(se, SESSION_WATCH_TERMLIST);
         session_alert_watch(se, SESSION_WATCH_RECORD);
         client_lock(cl);
     }
@@ -490,7 +574,7 @@ void client_record_response(struct client *cl)
     {
         client_set_state(cl, Client_Error);
         yaz_log(YLOG_WARN, "Search error %s (%s): %s",
-            error, addinfo, client_get_url(cl));
+            error, addinfo, client_get_id(cl));
     }
     else
     {
@@ -522,7 +606,7 @@ void client_record_response(struct client *cl)
                 else if (ZOOM_record_error(rec, &msg, &addinfo, 0))
                 {
                     yaz_log(YLOG_WARN, "Record error %s (%s): %s (rec #%d)",
-                            msg, addinfo, client_get_url(cl),
+                            msg, addinfo, client_get_id(cl),
                             cl->record_offset);
                 }
                 else
@@ -537,12 +621,12 @@ void client_record_response(struct client *cl)
                     xmlrec = ZOOM_record_get(rec, type, NULL);
                     if (!xmlrec)
                         yaz_log(YLOG_WARN, "ZOOM_record_get failed from %s",
-                                client_get_url(cl));
+                                client_get_id(cl));
                     else
                     {
-                        if (ingest_record(cl, xmlrec, cl->record_offset, nmem))
-                            yaz_log(YLOG_WARN, "Failed to ingest from %s",
-                                    client_get_url(cl));
+                        /* OK = 0, -1 = failure, -2 = Filtered */
+                        if (ingest_record(cl, xmlrec, cl->record_offset, nmem) == -1)
+                            yaz_log(YLOG_WARN, "Failed to ingest from %s", client_get_id(cl));
                     }
                     nmem_destroy(nmem);
                 }
@@ -556,82 +640,107 @@ void client_record_response(struct client *cl)
     }
 }
 
-static int client_set_facets_request(struct client *cl, ZOOM_connection link)
+static void client_set_facets_request(struct client *cl, ZOOM_connection link)
 {
     struct session_database *sdb = client_get_database(cl);
-    const char *opt_facet_term_sort  = session_setting_oneval(sdb, PZ_TERMLIST_TERM_SORT);
-    const char *opt_facet_term_count = session_setting_oneval(sdb, PZ_TERMLIST_TERM_COUNT);
-    /* Disable when no count is set */
-    if (opt_facet_term_count && *opt_facet_term_count)
-    {
-        int index = 0;
-        struct session *session = client_get_session(cl);
-        struct conf_service *service = session->service;
-        int num = service->num_metadata;
-        WRBUF wrbuf = wrbuf_alloc();
-        yaz_log(YLOG_DEBUG, "Facet settings, sort: %s count: %s",
-                opt_facet_term_sort, opt_facet_term_count);
-        for (index = 0; index < num; index++)
+
+    WRBUF w = wrbuf_alloc();
+    
+    struct setting *s;
+
+    for (s = sdb->settings[PZ_FACETMAP]; s; s = s->next)
+    {
+        const char *p = strchr(s->name + 3, ':');
+        if (!p)
         {
-            struct conf_metadata *conf_meta = &service->metadata[index];
-            if (conf_meta->termlist)
-            {
-                if (wrbuf_len(wrbuf))
-                    wrbuf_puts(wrbuf, ", ");
-                wrbuf_printf(wrbuf, "@attr 1=%s", conf_meta->name);
-                
-                if (opt_facet_term_sort && *opt_facet_term_sort)
-                    wrbuf_printf(wrbuf, " @attr 2=%s", opt_facet_term_sort);
-                wrbuf_printf(wrbuf, " @attr 3=%s", opt_facet_term_count);
-            }
+            yaz_log(YLOG_WARN, "Malformed facetmap name: %s", s->name);
         }
-        if (wrbuf_len(wrbuf))
+        else if (s->value && s->value[0])
         {
-            yaz_log(YLOG_LOG, "Setting ZOOM facets option: %s", wrbuf_cstr(wrbuf));
-            ZOOM_connection_option_set(link, "facets", wrbuf_cstr(wrbuf));
-            return 1;
+            wrbuf_puts(w, "@attr 1=");
+            yaz_encode_pqf_term(w, s->value, strlen(s->value));
+            if (s->next)
+                wrbuf_puts(w, ",");
         }
     }
-    return 0;
+    yaz_log(YLOG_LOG, "using facets str: %s", wrbuf_cstr(w));
+    ZOOM_connection_option_set(link, "facets",
+                               wrbuf_len(w) ? wrbuf_cstr(w) : 0);
+    wrbuf_destroy(w);
 }
 
-int client_has_facet(struct client *cl, const char *name) {
-    ZOOM_facet_field facet_field;
-    if (!cl || !cl->resultset || !name) {
-        yaz_log(YLOG_DEBUG, "client has facet: Missing %p %p %s", cl, (cl ? cl->resultset: 0), name);
-        return 0;
-    }
-    facet_field = ZOOM_resultset_get_facet_field(cl->resultset, name);
-    if (facet_field) {
-        yaz_log(YLOG_DEBUG, "client: has facets for %s", name);
-        return 1;
+int client_has_facet(struct client *cl, const char *name)
+{
+    struct session_database *sdb = client_get_database(cl);
+    struct setting *s;
+
+    for (s = sdb->settings[PZ_FACETMAP]; s; s = s->next)
+    {
+        const char *p = strchr(s->name + 3, ':');
+        if (p && !strcmp(name, p + 1))
+            return 1;
     }
-    yaz_log(YLOG_DEBUG, "client: No facets for %s", name);
     return 0;
 }
 
+static const char *get_strategy_plus_sort(struct client *l, const char *field)
+{
+    struct session_database *sdb = client_get_database(l);
+    struct setting *s;
+
+    const char *strategy_plus_sort = 0;
+    
+    for (s = sdb->settings[PZ_SORTMAP]; s; s = s->next)
+    {
+        char *p = strchr(s->name + 3, ':');
+        if (!p)
+        {
+            yaz_log(YLOG_WARN, "Malformed sortmap name: %s", s->name);
+            continue;
+        }
+        p++;
+        if (!strcmp(p, field))
+        {
+            strategy_plus_sort = s->value;
+            break;
+        }
+    }
+    return strategy_plus_sort;
+}
 
 void client_start_search(struct client *cl)
 {
     struct session_database *sdb = client_get_database(cl);
     struct connection *co = client_get_connection(cl);
     ZOOM_connection link = connection_get_link(co);
+    struct session *se = client_get_session(cl);
     ZOOM_resultset rs;
-    char *databaseName = sdb->database->databases[0];
-    const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
-    const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
-    const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
-    const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
-    const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
-    const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
-    const char *opt_sort = session_setting_oneval(sdb, PZ_SORT);
+    const char *opt_piggyback   = session_setting_oneval(sdb, PZ_PIGGYBACK);
+    const char *opt_queryenc    = session_setting_oneval(sdb, PZ_QUERYENCODING);
+    const char *opt_elements    = session_setting_oneval(sdb, PZ_ELEMENTS);
+    const char *opt_requestsyn  = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
+    const char *opt_maxrecs     = session_setting_oneval(sdb, PZ_MAXRECS);
+    const char *opt_sru         = session_setting_oneval(sdb, PZ_SRU);
+    const char *opt_sort        = session_setting_oneval(sdb, PZ_SORT);
+    const char *opt_preferred   = session_setting_oneval(sdb, PZ_PREFERRED);
+    const char *extra_args      = session_setting_oneval(sdb, PZ_EXTRA_ARGS);
     char maxrecs_str[24], startrecs_str[24];
+    ZOOM_query q;
 
     assert(link);
 
-    cl->hits = -1;
     cl->record_offset = 0;
     cl->diagnostic = 0;
+
+    if (extra_args && *extra_args)
+        ZOOM_connection_option_set(link, "extraArgs", extra_args);
+
+    if (opt_preferred) {
+        cl->preferred = atoi(opt_preferred);
+        if (cl->preferred)
+            yaz_log(YLOG_LOG, "Target %s has preferred status: %d",
+                    client_get_id(cl), cl->preferred);
+    }
     client_set_state(cl, Client_Working);
 
     if (*opt_piggyback)
@@ -647,69 +756,110 @@ void client_start_search(struct client *cl)
     if (*opt_requestsyn)
         ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
 
-    if (!*opt_maxrecs)
+    if (opt_maxrecs && *opt_maxrecs)
     {
-        sprintf(maxrecs_str, "%d", cl->maxrecs);
-        opt_maxrecs = maxrecs_str;
+        cl->maxrecs = atoi(opt_maxrecs);
     }
-    ZOOM_connection_option_set(link, "count", opt_maxrecs);
 
+    /* convert back to string representation used in ZOOM API */
+    sprintf(maxrecs_str, "%d", cl->maxrecs);
+    ZOOM_connection_option_set(link, "count", maxrecs_str);
 
-    if (atoi(opt_maxrecs) > 20)
+    if (cl->maxrecs > 20)
         ZOOM_connection_option_set(link, "presentChunk", "20");
     else
-        ZOOM_connection_option_set(link, "presentChunk", opt_maxrecs);
+        ZOOM_connection_option_set(link, "presentChunk", maxrecs_str);
 
     sprintf(startrecs_str, "%d", cl->startrecs);
     ZOOM_connection_option_set(link, "start", startrecs_str);
 
-    if (databaseName)
-        ZOOM_connection_option_set(link, "databaseName", databaseName);
+    /* TODO Verify does it break something for CQL targets(non-SOLR) ? */
+    /* facets definition is in PQF */
+    client_set_facets_request(cl, link);
 
+    q = ZOOM_query_create();
     if (cl->cqlquery)
     {
-        ZOOM_query q = ZOOM_query_create();
-        yaz_log(YLOG_LOG, "Search %s CQL: %s", sdb->database->url, cl->cqlquery);
+        yaz_log(YLOG_LOG, "Search %s CQL: %s", client_get_id(cl),
+                cl->cqlquery);
         ZOOM_query_cql(q, cl->cqlquery);
         if (*opt_sort)
             ZOOM_query_sortby(q, opt_sort);
-        rs = ZOOM_connection_search(link, q);
-        ZOOM_query_destroy(q);
     }
     else
     {
-        client_set_facets_request(cl, link);
-        yaz_log(YLOG_LOG, "Search %s PQF: %s", sdb->database->url, cl->pquery);
-        rs = ZOOM_connection_search_pqf(link, cl->pquery);
+        yaz_log(YLOG_LOG, "Search %s PQF: %s", client_get_id(cl), cl->pquery);
+        
+        ZOOM_query_prefix(q, cl->pquery);
+    }
+    if (se->sorted_results)
+    {   /* first entry is current sorting ! */
+        const char *sort_strategy_and_spec =
+            get_strategy_plus_sort(cl, se->sorted_results->field);
+        int increasing = se->sorted_results->increasing;
+        if (sort_strategy_and_spec && strlen(sort_strategy_and_spec) < 40)
+        {
+            char spec[50], *p;
+            strcpy(spec, sort_strategy_and_spec);
+            p = strchr(spec, ':');
+            if (p)
+            {
+                *p++ = '\0'; /* cut the string in two */
+                while (*p == ' ')
+                    p++;
+                if (increasing)
+                    strcat(p, " <");
+                else
+                    strcat(p, " >");
+                yaz_log(YLOG_LOG, "applying %s %s", spec, p);
+                ZOOM_query_sortby2(q, spec, p);
+            }
+        }
+        else
+        {
+            /* no native sorting.. If this is not the first search, then
+               skip it entirely */
+            if (se->sorted_results->next)
+            {
+                ZOOM_query_destroy(q);
+                client_set_state_nb(cl, Client_Idle);
+                return;
+            }
+        }
     }
+    rs = ZOOM_connection_search(link, q);
+    ZOOM_query_destroy(q);
     ZOOM_resultset_destroy(cl->resultset);
     cl->resultset = rs;
     connection_continue(co);
 }
 
-struct client *client_create(void)
-{
-    struct client *r = xmalloc(sizeof(*r));
-    r->maxrecs = 100;
-    r->startrecs = 0;
-    r->pquery = 0;
-    r->cqlquery = 0;
-    r->database = 0;
-    r->connection = 0;
-    r->session = 0;
-    r->hits = 0;
-    r->record_offset = 0;
-    r->diagnostic = 0;
-    r->state = Client_Disconnected;
-    r->show_raw = 0;
-    r->resultset = 0;
-    r->mutex = 0;
-    pazpar2_mutex_create(&r->mutex, "client");
-
-    r->ref_count = 1;
+struct client *client_create(const char *id)
+{
+    struct client *cl = xmalloc(sizeof(*cl));
+    cl->maxrecs = 100;
+    cl->startrecs = 0;
+    cl->pquery = 0;
+    cl->cqlquery = 0;
+    cl->database = 0;
+    cl->connection = 0;
+    cl->session = 0;
+    cl->hits = 0;
+    cl->record_offset = 0;
+    cl->diagnostic = 0;
+    cl->state = Client_Disconnected;
+    cl->show_raw = 0;
+    cl->resultset = 0;
+    cl->suggestions = 0;
+    cl->mutex = 0;
+    pazpar2_mutex_create(&cl->mutex, "client");
+    cl->preferred = 0;
+    cl->ref_count = 1;
+    assert(id);
+    cl->id = xstrdup(id);
     client_use(1);
     
-    return r;
+    return cl;
 }
 
 void client_lock(struct client *c)
@@ -726,7 +876,7 @@ void client_incref(struct client *c)
 {
     pazpar2_incref(&c->ref_count, c->mutex);
     yaz_log(YLOG_DEBUG, "client_incref c=%p %s cnt=%d",
-            c, client_get_url(c), c->ref_count);
+            c, client_get_id(c), c->ref_count);
 }
 
 int client_destroy(struct client *c)
@@ -734,13 +884,14 @@ int client_destroy(struct client *c)
     if (c)
     {
         yaz_log(YLOG_DEBUG, "client_destroy c=%p %s cnt=%d",
-                c, client_get_url(c), c->ref_count);
+                c, client_get_id(c), c->ref_count);
         if (!pazpar2_decref(&c->ref_count, c->mutex))
         {
             xfree(c->pquery);
             c->pquery = 0;
             xfree(c->cqlquery);
             c->cqlquery = 0;
+            xfree(c->id);
             assert(!c->connection);
 
             if (c->resultset)
@@ -780,14 +931,6 @@ void client_disconnect(struct client *cl)
     client_set_connection(cl, 0);
 }
 
-// Extract terms from query into null-terminated termlist
-static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
-{
-    int num = 0;
-
-    pull_terms(nmem, query, termlist, &num);
-    termlist[num] = 0;
-}
 
 // Initialize CCL map for a target
 static CCL_bibset prepare_cclmap(struct client *cl)
@@ -841,8 +984,109 @@ static char *make_cqlquery(struct client *cl)
     return r;
 }
 
+// returns a xmalloced SOLR query corresponding to the pquery in client
+// TODO Could prob. be merge with the similar make_cqlquery
+static char *make_solrquery(struct client *cl)
+{
+    solr_transform_t sqlt = solr_transform_create();
+    Z_RPNQuery *zquery;
+    char *r;
+    WRBUF wrb = wrbuf_alloc();
+    int status;
+    ODR odr_out = odr_createmem(ODR_ENCODE);
+
+    zquery = p_query_rpn(odr_out, cl->pquery);
+    if (zquery == 0) {
+        yaz_log(YLOG_WARN, "Failed to generate RPN from PQF: %s", cl->pquery);
+        return 0;
+    }
+    yaz_log(YLOG_LOG, "PQF: %s", cl->pquery);
+    if ((status = solr_transform_rpn2solr_wrbuf(sqlt, wrb, zquery)))
+    {
+        yaz_log(YLOG_WARN, "Failed to generate SOLR query from PQF %s, code=%d", cl->pquery, status);
+        r = 0;
+    }
+    else
+    {
+        r = xstrdup(wrbuf_cstr(wrb));
+    }
+    wrbuf_destroy(wrb);
+    odr_destroy(odr_out);
+    solr_transform_close(sqlt);
+    return r;
+}
+
+static void apply_limit(struct session_database *sdb,
+                        facet_limits_t facet_limits,
+                        WRBUF w_pqf, WRBUF w_ccl)
+{
+    int i = 0;
+    const char *name;
+    const char *value;
+    NMEM nmem_tmp = nmem_create();
+    for (i = 0; (name = facet_limits_get(facet_limits, i, &value)); i++)
+    {
+        struct setting *s = 0;
+        
+        for (s = sdb->settings[PZ_LIMITMAP]; s; s = s->next)
+        {
+            const char *p = strchr(s->name + 3, ':');
+            if (p && !strcmp(p + 1, name) && s->value)
+            {
+                char **values = 0;
+                int i, num = 0;
+                nmem_strsplit_escape2(nmem_tmp, "|", value, &values,
+                                      &num, 1, '\\', 1);
+
+                if (!strncmp(s->value, "rpn:", 4))
+                {
+                    const char *pqf = s->value + 4;
+
+                    wrbuf_puts(w_pqf, "@and ");
+                    wrbuf_puts(w_pqf, pqf);
+                    wrbuf_puts(w_pqf, " ");
+                    for (i = 0; i < num; i++)
+                    {
+                        if (i < num - 1)
+                            wrbuf_puts(w_pqf, "@or ");
+                        yaz_encode_pqf_term(w_pqf, values[i],
+                                            strlen(values[i]));
+                    }
+                }
+                else if (!strncmp(s->value, "ccl:", 4))
+                {
+                    const char *ccl = s->value + 4;
+
+                    wrbuf_puts(w_ccl, " and (");
+
+                    for (i = 0; i < num; i++)
+                    {
+                        if (i)
+                            wrbuf_puts(w_ccl, " or ");
+                        wrbuf_puts(w_ccl, ccl);
+                        wrbuf_puts(w_ccl, "=\"");
+                        wrbuf_puts(w_ccl, values[i]);
+                        wrbuf_puts(w_ccl, "\"");
+                    }
+                    wrbuf_puts(w_ccl, ")");
+
+                }
+                break;
+            }
+        }
+        nmem_reset(nmem_tmp);
+        if (!s)
+        {
+            yaz_log(YLOG_WARN, "Target %s: limit %s used, but no limitmap defined",
+                    (sdb->database ? sdb->database->id : "<no id>"), name);
+        }
+    }
+    nmem_destroy(nmem_tmp);
+}
+                        
 // Parse the query given the settings specific to this client
-int client_parse_query(struct client *cl, const char *query)
+int client_parse_query(struct client *cl, const char *query,
+                       facet_limits_t facet_limits)
 {
     struct session *se = client_get_session(cl);
     struct session_database *sdb = client_get_database(cl);
@@ -852,28 +1096,41 @@ int client_parse_query(struct client *cl, const char *query)
     const char *sru = session_setting_oneval(sdb, PZ_SRU);
     const char *pqf_prefix = session_setting_oneval(sdb, PZ_PQF_PREFIX);
     const char *pqf_strftime = session_setting_oneval(sdb, PZ_PQF_STRFTIME);
-
+    const char *query_syntax = session_setting_oneval(sdb, PZ_QUERY_SYNTAX);
+    WRBUF w_ccl, w_pqf;
     if (!ccl_map)
         return -1;
 
-    cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
+    cl->hits = -1;
+    w_ccl = wrbuf_alloc();
+    wrbuf_puts(w_ccl, query);
+
+    w_pqf = wrbuf_alloc();
+    if (*pqf_prefix)
+    {
+        wrbuf_puts(w_pqf, pqf_prefix);
+        wrbuf_puts(w_pqf, " ");
+    }
+
+    apply_limit(sdb, facet_limits, w_pqf, w_ccl);
+
+    yaz_log(YLOG_LOG, "CCL query: %s", wrbuf_cstr(w_ccl));
+    cn = ccl_find_str(ccl_map, wrbuf_cstr(w_ccl), &cerror, &cpos);
     ccl_qual_rm(&ccl_map);
     if (!cn)
     {
         client_set_state(cl, Client_Error);
-        yaz_log(YLOG_WARN, "Failed to parse CCL query %s for %s",
-                query,
-                client_get_database(cl)->database->url);
+        session_log(se, YLOG_WARN, "Failed to parse CCL query '%s' for %s",
+                    wrbuf_cstr(w_ccl),
+                    client_get_id(cl));
+        wrbuf_destroy(w_ccl);
+        wrbuf_destroy(w_pqf);
         return -1;
     }
-    wrbuf_rewind(se->wrbuf);
-    if (*pqf_prefix)
-    {
-        wrbuf_puts(se->wrbuf, pqf_prefix);
-        wrbuf_puts(se->wrbuf, " ");
-    }
+    wrbuf_destroy(w_ccl);
+
     if (!pqf_strftime || !*pqf_strftime)
-        ccl_pquery(se->wrbuf, cn);
+        ccl_pquery(w_pqf, cn);
     else
     {
         time_t cur_time = time(0);
@@ -887,19 +1144,32 @@ int client_parse_query(struct client *cl, const char *query)
         for (; *cp; cp++)
         {
             if (cp[0] == '%')
-                ccl_pquery(se->wrbuf, cn);
+                ccl_pquery(w_pqf, cn);
             else
-                wrbuf_putc(se->wrbuf, cp[0]);
+                wrbuf_putc(w_pqf, cp[0]);
         }
     }
     xfree(cl->pquery);
-    cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
+    cl->pquery = xstrdup(wrbuf_cstr(w_pqf));
+    wrbuf_destroy(w_pqf);
+
+    yaz_log(YLOG_LOG, "PQF query: %s", cl->pquery);
 
     xfree(cl->cqlquery);
-    if (*sru)
+
+    /* Support for PQF on SRU targets. */
+    /* TODO Refactor */
+    yaz_log(YLOG_DEBUG, "Query syntax: %s", query_syntax);
+    if (strcmp(query_syntax, "pqf") != 0 && *sru)
     {
-        if (!(cl->cqlquery = make_cqlquery(cl)))
-            return -1;
+        if (!strcmp(sru, "solr")) {
+            if (!(cl->cqlquery = make_solrquery(cl)))
+                return -1;
+        }
+        else {
+            if (!(cl->cqlquery = make_cqlquery(cl)))
+                return -1;
+        }
     }
     else
         cl->cqlquery = 0;
@@ -908,11 +1178,8 @@ int client_parse_query(struct client *cl, const char *query)
     if (!se->relevance)
     {
         // Initialize relevance structure with query terms
-        char *p[512];
-        extract_terms(se->nmem, cn, p);
-        se->relevance = relevance_create(
-            se->service->relevance_pct,
-            se->nmem, (const char **) p);
+        se->relevance = relevance_create_ccl(
+            se->service->charsets, se->nmem, cn);
     }
 
     ccl_rpn_delete(cn);
@@ -932,6 +1199,18 @@ int client_is_active(struct client *cl)
     return 0;
 }
 
+int client_is_active_preferred(struct client *cl)
+{
+    /* only count if this is a preferred target. */
+    if (!cl->preferred)
+        return 0;
+    /* TODO No sure this the condition that Seb wants */
+    if (cl->connection && (cl->state == Client_Connecting ||
+                           cl->state == Client_Working))
+        return 1;
+    return 0;
+}
+
 Odr_int client_get_hits(struct client *cl)
 {
     return cl->hits;
@@ -952,22 +1231,45 @@ int client_get_diagnostic(struct client *cl)
     return cl->diagnostic;
 }
 
-void client_set_database(struct client *cl, struct session_database *db)
+const char * client_get_suggestions_xml(struct client *cl, WRBUF wrbuf)
 {
-    cl->database = db;
+    /* int idx; */
+    struct suggestions *suggestions = cl->suggestions;
+
+    if (!suggestions) {
+        //yaz_log(YLOG_DEBUG, "No suggestions found");
+        return "";
+    }
+    if (suggestions->passthrough) {
+        yaz_log(YLOG_DEBUG, "Passthrough Suggestions: \n%s\n", suggestions->passthrough);
+        return suggestions->passthrough;
+    }
+    if (suggestions->num == 0) {
+        return "";
+    }
+    /*
+    for (idx = 0; idx < suggestions->num; idx++) {
+        wrbuf_printf(wrbuf, "<suggest term=\"%s\"", suggestions->suggest[idx]);
+        if (suggestions->misspelled[idx] && suggestions->misspelled[idx]) {
+            wrbuf_puts(wrbuf, suggestions->misspelled[idx]);
+            wrbuf_puts(wrbuf, "</suggest>\n");
+        }
+        else
+            wrbuf_puts(wrbuf, "/>\n");
+    }
+    */
+    return wrbuf_cstr(wrbuf);
 }
 
-struct host *client_get_host(struct client *cl)
+
+void client_set_database(struct client *cl, struct session_database *db)
 {
-    return client_get_database(cl)->database->host;
+    cl->database = db;
 }
 
-const char *client_get_url(struct client *cl)
+const char *client_get_id(struct client *cl)
 {
-    if (cl->database)
-        return client_get_database(cl)->database->url;
-    else
-        return "NOURL";
+    return cl->id;
 }
 
 void client_set_maxrecs(struct client *cl, int v)
@@ -975,11 +1277,63 @@ void client_set_maxrecs(struct client *cl, int v)
     cl->maxrecs = v;
 }
 
+int client_get_maxrecs(struct client *cl)
+{
+    return cl->maxrecs;
+}
+
 void client_set_startrecs(struct client *cl, int v)
 {
     cl->startrecs = v;
 }
 
+void client_set_preferred(struct client *cl, int v)
+{
+    cl->preferred = v;
+}
+
+
+struct suggestions* client_suggestions_create(const char* suggestions_string)
+{
+    int i;
+    NMEM nmem;
+    struct suggestions *suggestions;
+    if (suggestions_string == 0)
+        return 0;
+    nmem = nmem_create();
+    suggestions = nmem_malloc(nmem, sizeof(*suggestions));
+    yaz_log(YLOG_DEBUG, "client target suggestions: %s", suggestions_string);
+
+    suggestions->nmem = nmem;
+    suggestions->num = 0;
+    suggestions->misspelled = 0;
+    suggestions->suggest = 0;
+    suggestions->passthrough = nmem_strdup_null(nmem, suggestions_string);
+
+    if (suggestions_string)
+        nmem_strsplit_escape2(suggestions->nmem, "\n", suggestions_string, &suggestions->suggest,
+                              &suggestions->num, 1, '\\', 0);
+    /* Set up misspelled array */
+    suggestions->misspelled = (char **) nmem_malloc(nmem, suggestions->num * sizeof(**suggestions->misspelled));
+    /* replace = with \0 .. for each item */
+    for (i = 0; i < suggestions->num; i++)
+    {
+        char *cp = strchr(suggestions->suggest[i], '=');
+        if (cp) {
+            *cp = '\0';
+            suggestions->misspelled[i] = cp+1;
+        }
+    }
+    return suggestions;
+}
+
+static void client_suggestions_destroy(struct client *cl)
+{
+    NMEM nmem = cl->suggestions->nmem;
+    cl->suggestions = 0;
+    nmem_destroy(nmem);
+}
+
 /*
  * Local variables:
  * c-basic-offset: 4