+
+ZEBRA_RES resultSetRank(ZebraHandle zh, ZebraSet zebraSet,
+ RSET rset, NMEM nmem)
+{
+ struct it_key key;
+ TERMID termid;
+ TERMID *terms;
+ zint kno = 0;
+ int numTerms = 0;
+ int n = 0;
+ int i;
+ ZebraRankClass rank_class;
+ struct zset_sort_info *sort_info;
+ const char *rank_handler_name = res_get_def(zh->res, "rank", "rank-1");
+
+ if (!log_level_set)
+ loglevels();
+ sort_info = zebraSet->sort_info;
+ sort_info->num_entries = 0;
+ zebraSet->hits = 0;
+ rset_getterms(rset, 0, 0, &n);
+ terms = (TERMID *) nmem_malloc(nmem, sizeof(*terms)*n);
+ rset_getterms(rset, terms, n, &numTerms);
+
+ rank_class = zebraRankLookup(zh, rank_handler_name);
+ if (!rank_class)
+ {
+ yaz_log(YLOG_WARN, "No such rank handler: %s", rank_handler_name);
+ zebra_setError(zh, YAZ_BIB1_UNSUPP_SEARCH, "Cannot find rank handler");
+ return ZEBRA_FAIL;
+ }
+ else
+ {
+ RSFD rfd = rset_open(rset, RSETF_READ);
+ struct rank_control *rc = rank_class->control;
+ double score;
+ zint count = 0;
+
+ void *handle =
+ (*rc->begin) (zh->reg, rank_class->class_handle, rset, nmem,
+ terms, numTerms);
+ zint psysno = 0;
+ while (rset_read(rfd, &key, &termid))
+ {
+ zint this_sys = key.mem[0];
+ zint seqno = key.mem[key.len-1];
+ kno++;
+ if (log_level_searchhits)
+ key_logdump_txt(log_level_searchhits, &key, termid->name);
+ if (this_sys != psysno)
+ {
+ if (rfd->counted_items >= rset->hits_limit)
+ break;
+ if (psysno)
+ {
+ score = (*rc->calc) (handle, psysno);
+ resultSetInsertRank (zh, sort_info, psysno, score, 'A');
+ count++;
+ }
+ psysno = this_sys;
+ }
+ (*rc->add) (handle, CAST_ZINT_TO_INT(seqno), termid);
+ }
+ if (psysno)
+ {
+ score = (*rc->calc)(handle, psysno);
+ resultSetInsertRank(zh, sort_info, psysno, score, 'A');
+ count++;
+ }
+ (*rc->end) (zh->reg, handle);
+ rset_close (rfd);
+ }
+ zebraSet->hits = rset->hits_count;
+
+ yaz_log(log_level_searchterms, ZINT_FORMAT " keys, "
+ ZINT_FORMAT " sysnos, rank", kno, zebraSet->hits);
+ for (i = 0; i < numTerms; i++)
+ {
+ yaz_log(log_level_searchterms, "term=\"%s\" type=%s count="
+ ZINT_FORMAT,
+ terms[i]->name, terms[i]->flags, terms[i]->rset->hits_count);
+ }
+ return ZEBRA_OK;
+}
+
+ZebraRankClass zebraRankLookup(ZebraHandle zh, const char *name)
+{
+ ZebraRankClass p = zh->reg->rank_classes;
+ while (p && strcmp (p->control->name, name))
+ p = p->next;
+ if (p && !p->init_flag)
+ {
+ if (p->control->create)
+ p->class_handle = (*p->control->create)(zh);
+ p->init_flag = 1;
+ }
+ return p;
+}
+
+void zebraRankInstall(struct zebra_register *reg, struct rank_control *ctrl)
+{
+ ZebraRankClass p = (ZebraRankClass) xmalloc (sizeof(*p));
+ p->control = (struct rank_control *) xmalloc (sizeof(*p->control));
+ memcpy (p->control, ctrl, sizeof(*p->control));
+ p->control->name = xstrdup (ctrl->name);
+ p->init_flag = 0;
+ p->next = reg->rank_classes;
+ reg->rank_classes = p;
+}
+
+void zebraRankDestroy(struct zebra_register *reg)
+{
+ ZebraRankClass p = reg->rank_classes;
+ while (p)
+ {
+ ZebraRankClass p_next = p->next;
+ if (p->init_flag && p->control->destroy)
+ (*p->control->destroy)(reg, p->class_handle);
+ xfree(p->control->name);
+ xfree(p->control);
+ xfree(p);
+ p = p_next;
+ }
+ reg->rank_classes = NULL;
+}
+
+static int trav_rset_for_termids(RSET rset, TERMID *termid_array,
+ zint *hits_array, int *approx_array)
+{
+ int no = 0;
+ int i;
+ for (i = 0; i<rset->no_children; i++)
+ no += trav_rset_for_termids(rset->children[i],
+ (termid_array ? termid_array + no : 0),
+ (hits_array ? hits_array + no : 0),
+ (approx_array ? approx_array + no : 0));
+ if (rset->term)
+ {
+ if (termid_array)
+ termid_array[no] = rset->term;
+ if (hits_array)
+ hits_array[no] = rset->hits_count;
+ if (approx_array)
+ approx_array[no] = rset->hits_approx;
+#if 0
+ yaz_log(YLOG_LOG, "rset=%p term=%s count=" ZINT_FORMAT,
+ rset, rset->term->name, rset->hits_count);
+#endif
+ no++;
+ }
+ return no;
+}
+
+ZEBRA_RES zebra_result_set_term_no(ZebraHandle zh, const char *setname,
+ int *num_terms)
+{
+ ZebraSet sset = resultSetGet(zh, setname);
+ *num_terms = 0;
+ if (sset)
+ {
+ *num_terms = trav_rset_for_termids(sset->rset, 0, 0, 0);
+ return ZEBRA_OK;
+ }
+ return ZEBRA_FAIL;
+}
+
+ZEBRA_RES zebra_result_set_term_info(ZebraHandle zh, const char *setname,
+ int no, zint *count, int *approx,
+ char *termbuf, size_t *termlen)
+{
+ ZebraSet sset = resultSetGet(zh, setname);
+ if (sset)
+ {
+ int num_terms = trav_rset_for_termids(sset->rset, 0, 0, 0);
+ if (no >= 0 && no < num_terms)
+ {
+ TERMID *term_array = xmalloc(num_terms * sizeof(*term_array));
+ zint *hits_array = xmalloc(num_terms * sizeof(*hits_array));
+ int *approx_array = xmalloc(num_terms * sizeof(*approx_array));
+
+ trav_rset_for_termids(sset->rset, term_array,
+ hits_array, approx_array);
+
+ if (count)
+ *count = hits_array[no];
+ if (approx)
+ *approx = approx_array[no];
+ if (termbuf)
+ {
+ char *inbuf = term_array[no]->name;
+ size_t inleft = strlen(inbuf);
+ size_t outleft = *termlen - 1;
+
+ if (zh->iconv_from_utf8 != 0)
+ {
+ char *outbuf = termbuf;
+ size_t ret;
+
+ ret = yaz_iconv(zh->iconv_from_utf8, &inbuf, &inleft,
+ &outbuf, &outleft);
+ if (ret == (size_t)(-1))
+ *termlen = 0;
+ else
+ *termlen = outbuf - termbuf;
+ }
+ else
+ {
+ if (inleft > outleft)
+ inleft = outleft;
+ *termlen = inleft;
+ memcpy(termbuf, inbuf, *termlen);
+ }
+ termbuf[*termlen] = '\0';
+ }
+
+ xfree(term_array);
+ xfree(hits_array);
+ xfree(approx_array);
+ return ZEBRA_OK;
+ }
+ }
+ return ZEBRA_FAIL;
+}
+
+ZEBRA_RES zebra_snippets_hit_vector(ZebraHandle zh, const char *setname,
+ zint sysno, zebra_snippets *snippets)
+{
+ ZebraSet sset = resultSetGet(zh, setname);
+ yaz_log(YLOG_LOG, "zebra_get_hit_vector setname=%s zysno=" ZINT_FORMAT,
+ setname, sysno);
+ if (!sset)
+ return ZEBRA_FAIL;
+ else
+ {
+ struct rset_key_control *kc = zebra_key_control_create(zh);
+ NMEM nmem = nmem_create();
+ struct it_key key;
+ RSET rsets[2], rset_comb;
+ RSET rset_temp = rstemp_create(nmem, kc, kc->scope,
+ res_get (zh->res, "setTmpDir"),0 );
+
+ TERMID termid;
+ RSFD rsfd = rset_open(rset_temp, RSETF_WRITE);
+
+ key.mem[0] = sysno;
+ key.mem[1] = 0;
+ key.mem[2] = 0;
+ key.mem[3] = 0;
+ key.len = 2;
+ rset_write (rsfd, &key);
+ rset_close (rsfd);
+
+ rsets[0] = rset_temp;
+ rsets[1] = rset_dup(sset->rset);
+
+ rset_comb = rsmulti_and_create(nmem, kc, kc->scope, 2, rsets);
+
+ rsfd = rset_open(rset_comb, RSETF_READ);
+
+ while (rset_read(rsfd, &key, &termid))
+ {
+ if (termid)
+ {
+ struct ord_list *ol;
+ for (ol = termid->ol; ol; ol = ol->next)
+ {
+ zebra_snippets_append(snippets, key.mem[key.len-1],
+ termid->reg_type,
+ ol->ord, termid->name);
+ }
+ }
+ }
+ rset_close(rsfd);
+
+ rset_delete(rset_comb);
+ nmem_destroy(nmem);
+ }
+ return ZEBRA_OK;
+}
+