+
+ /* reset list of sorted results and clear to relevance search */
+ se->sorted_results = nmem_malloc(se->nmem, sizeof(*se->sorted_results));
+ se->sorted_results->name = nmem_strdup(se->nmem, sp->name);
+ se->sorted_results->increasing = sp->increasing;
+ se->sorted_results->type = sp->type;
+ se->sorted_results->next = 0;
+
+ session_log(se, YLOG_DEBUG, "clear_set session_sort: field=%s increasing=%d type=%d configured",
+ sp->name, sp->increasing, sp->type);
+
+ se->reclist = reclist_create(se->nmem);
+}
+
+static void session_sort_unlocked(struct session *se, struct reclist_sortparms *sp)
+{
+ struct reclist_sortparms *sr;
+ struct client_list *l;
+ const char *field = sp->name;
+ int increasing = sp->increasing;
+ int type = sp->type;
+ int clients_research = 0;
+
+ yaz_log(YLOG_LOG, "session_sort field=%s increasing=%d type=%d", field, increasing, type);
+ /* see if we already have sorted for this criteria */
+ for (sr = se->sorted_results; sr; sr = sr->next)
+ {
+ if (!reclist_sortparms_cmp(sr,sp))
+ break;
+ }
+ if (sr)
+ {
+ session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d type=%d already fetched",
+ field, increasing, type);
+ return;
+ }
+ session_log(se, YLOG_DEBUG, "search_sort: field=%s increasing=%d type=%d must fetch",
+ field, increasing, type);
+
+ // We need to reset reclist on every sort that changes the records, not just for position
+ // So if just one client requires new searching, we need to clear set.
+ // Ask each of the client if sorting requires re-search due to native sort
+ // If it does it will require us to
+ for (l = se->clients_active; l; l = l->next)
+ {
+ struct client *cl = l->client;
+ // Assume no re-search is required.
+ client_parse_init(cl, 1);
+ clients_research += client_parse_sort(cl, sp);
+ }
+ if (clients_research) {
+ yaz_log(YLOG_DEBUG, "Reset results due to %d clients researching", clients_research);
+ session_clear_set(se, sp);
+ }
+ else {
+ // A new sorting based on same record set
+ sr = nmem_malloc(se->nmem, sizeof(*sr));
+ sr->name = nmem_strdup(se->nmem, field);
+ sr->increasing = increasing;
+ sr->type = type;
+ sr->next = se->sorted_results;
+ se->sorted_results = sr;
+ session_log(se, YLOG_DEBUG, "No research/ingesting done");
+ return ;
+ }
+ session_log(se, YLOG_DEBUG, "Re- search/ingesting for clients due to change in sort order");
+
+ for (l = se->clients_active; l; l = l->next)
+ {
+ struct client *cl = l->client;
+ if (client_get_state(cl) == Client_Connecting ||
+ client_get_state(cl) == Client_Idle ||
+ client_get_state(cl) == Client_Working) {
+ client_start_search(cl);
+ }
+ else {
+ yaz_log(YLOG_DEBUG, "Client %s: No re-start/ingest in show. Wrong client state: %d",
+ client_get_id(cl), client_get_state(cl));
+ }
+
+ }
+}
+
+void session_sort(struct session *se, struct reclist_sortparms *sp) {
+ //session_enter(se, "session_sort");
+ session_sort_unlocked(se, sp);
+ //session_leave(se, "session_sort");
+}
+
+
+enum pazpar2_error_code session_search(struct session *se,
+ const char *query,
+ const char *startrecs,
+ const char *maxrecs,
+ const char *filter,
+ const char *limit,
+ const char **addinfo,
+ struct reclist_sortparms *sp)
+{
+ int live_channels = 0;
+ int no_working = 0;
+ int no_failed_query = 0;
+ int no_failed_limit = 0;
+ struct client_list *l, *l0;
+ int same_sort_order = 0;
+
+ session_log(se, YLOG_DEBUG, "Search");
+
+ *addinfo = 0;
+
+ if (se->settings_modified) {
+ session_remove_cached_clients(se);
+ }
+ else
+ session_reset_active_clients(se, 0);
+
+ session_enter(se, "session_search");
+ se->settings_modified = 0;
+
+ if (se->sorted_results) {
+ if (!reclist_sortparms_cmp(se->sorted_results, sp))
+ same_sort_order = 1;
+ }
+ session_clear_set(se, sp);
+ relevance_destroy(&se->relevance);
+