X-Git-Url: http://lists.indexdata.dk/cgi-bin?a=blobdiff_plain;f=src%2Fclient.c;h=5df785d1e9ab642a8d4c6c9d0dabb00dd4bede4f;hb=c61b8edfe5b60d9394f4db7e26d6c08fec9d1452;hp=bf3e04c8fa30bd26ab6919657615b25578224eff;hpb=646158a86d80e5ef0221ef7d7ce2cfc6ba31eacc;p=pazpar2-moved-to-github.git diff --git a/src/client.c b/src/client.c index bf3e04c..5df785d 100644 --- a/src/client.c +++ b/src/client.c @@ -114,6 +114,7 @@ struct client { int startrecs; int diagnostic; int preferred; + struct suggestions *suggestions; enum client_state state; struct show_raw *show_raw; ZOOM_resultset resultset; @@ -122,6 +123,14 @@ struct client { char *id; }; +struct suggestions { + NMEM nmem; + int num; + char **misspelled; + char **suggest; + char *passthrough; +}; + struct show_raw { int active; // whether this request has been sent to the server int position; @@ -170,6 +179,8 @@ void client_set_state(struct client *cl, enum client_state st) 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); } } @@ -502,10 +513,12 @@ void client_check_preferred_watch(struct client *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; @@ -525,11 +538,9 @@ void client_search_response(struct client *cl) client_report_facets(cl, resultset); cl->record_offset = cl->startrecs; cl->hits = ZOOM_resultset_size(resultset); - if (se) { - se->total_hits += cl->hits; - yaz_log(YLOG_DEBUG, "client_search_response: total hits " - ODR_INT_PRINTF, se->total_hits); - } + if (cl->suggestions) + client_suggestions_destroy(cl); + cl->suggestions = client_suggestions_create(ZOOM_resultset_option_get(resultset, "suggestions")); } } @@ -540,6 +551,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); } @@ -665,8 +678,8 @@ int client_has_facet(struct client *cl, const char *name) return 0; } -void client_start_search(struct client *cl, const char *sort_strategy, - const char *sort_spec) +void client_start_search(struct client *cl, const char *sort_strategy_and_spec, + int increasing) { struct session_database *sdb = client_get_database(cl); struct connection *co = client_get_connection(cl); @@ -682,10 +695,10 @@ void client_start_search(struct client *cl, const char *sort_strategy, 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; @@ -734,38 +747,42 @@ void client_start_search(struct client *cl, const char *sort_strategy, /* 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", client_get_id(cl), cl->cqlquery); ZOOM_query_cql(q, cl->cqlquery); if (*opt_sort) ZOOM_query_sortby(q, opt_sort); - if (sort_strategy && sort_spec) - { - yaz_log(YLOG_LOG, "applying %s %s", sort_strategy, sort_spec); - ZOOM_query_sortby2(q, sort_strategy, sort_spec); - } - rs = ZOOM_connection_search(link, q); - ZOOM_query_destroy(q); } else { - ZOOM_query q = ZOOM_query_create(); - yaz_log(YLOG_LOG, "Search %s PQF: %s", client_get_id(cl), cl->pquery); - + ZOOM_query_prefix(q, cl->pquery); - - if (sort_strategy && sort_spec) + } + if (sort_strategy_and_spec && + strlen(sort_strategy_and_spec) < 40 /* spec below */) + { + char spec[50], *p; + strcpy(spec, sort_strategy_and_spec); + p = strchr(spec, ':'); + if (p) { - yaz_log(YLOG_LOG, "applying %s %s", sort_strategy, sort_spec); - ZOOM_query_sortby2(q, sort_strategy, sort_spec); + *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); } - rs = ZOOM_connection_search(link, q); - ZOOM_query_destroy(q); } + rs = ZOOM_connection_search(link, q); + ZOOM_query_destroy(q); ZOOM_resultset_destroy(cl->resultset); cl->resultset = rs; connection_continue(co); @@ -787,6 +804,7 @@ struct client *client_create(const char *id) 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; @@ -1037,6 +1055,7 @@ int client_parse_query(struct client *cl, const char *query, if (!ccl_map) return -1; + cl->hits = -1; w_ccl = wrbuf_alloc(); wrbuf_puts(w_ccl, query); @@ -1166,6 +1185,37 @@ int client_get_diagnostic(struct client *cl) return cl->diagnostic; } +const char * client_get_suggestions_xml(struct client *cl, WRBUF wrbuf) +{ + /* 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[idx]); + if (suggestions->misspelled[idx] && suggestions->misspelled[idx]) { + wrbuf_puts(wrbuf, suggestions->misspelled[idx]); + wrbuf_puts(wrbuf, "\n"); + } + else + wrbuf_puts(wrbuf, "/>\n"); + } + */ + return wrbuf_cstr(wrbuf); +} + + void client_set_database(struct client *cl, struct session_database *db) { cl->database = db; @@ -1197,6 +1247,47 @@ void client_set_preferred(struct client *cl, int 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