X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=ztest%2Fztest.c;h=d5e5d2769fef96796f2858917b02b949ce2d65ca;hp=bbbb402c871f817a0e8cd83f5eae7c1625782abc;hb=be042bc51e825a53074dc1b586f1fff0a002ab0b;hpb=4e627aa761e0b91b240f703173eeb82462ee8409 diff --git a/ztest/ztest.c b/ztest/ztest.c index bbbb402..d5e5d27 100644 --- a/ztest/ztest.c +++ b/ztest/ztest.c @@ -29,6 +29,8 @@ #include #include #include +#include +#include #include "ztest.h" @@ -133,12 +135,16 @@ static Odr_int get_term_hit(Z_RPNStructure *s) */ static Odr_int get_hit_count(Z_Query *q) { - Odr_int h = -1; if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101) + { + Odr_int h = -1; h = get_term_hit(q->u.type_1->RPNStructure); - if (h == -1) - h = rand() % 24; - return h; + if (h == -1) + h = rand() % 24; + return h; + } + else + return 24; } /** \brief checks if it's a dummy Slow database @@ -222,6 +228,61 @@ static void do_delay(const struct delay *delayp) } } +static void addterms(ODR odr, Z_FacetField *facet_field, const char *facet_name) { + int index; + int freq = 100; + int length = strlen(facet_name) + 10; + char key[length]; + key[0] = '\0'; + for (index = 0; index < facet_field->num_terms; index++) { + Z_Term *term; + Z_FacetTerm *facet_term; + // sprintf(key, "%s%d", facet_name, index); + //yaz_log(YLOG_DEBUG, "facet add term %s %d %s", facet_name, index, key); + term = term_create(odr, key); + facet_term = facet_term_create(odr, term, freq); + freq = freq - 10 ; + facet_field_term_set(odr, facet_field, facet_term, index); + } +} + +Z_OtherInformation *build_facet_response(ODR odr, Z_FacetList *facet_list) { + int index, new_index = 0; + Z_FacetList *new_list = facet_list_create(odr, facet_list->num); + + for (index = 0; index < facet_list->num; index++) { + struct attrvalues attrvalues; + facet_struct_init(&attrvalues); + attrvalues.limit = 10; + yaz_log(YLOG_LOG, "Attributes: %s %s %d ", attrvalues.useattr, attrvalues.useattrbuff, attrvalues.limit); + facetattrs(facet_list->elements[index]->attributes, &attrvalues); + yaz_log(YLOG_LOG, "Attributes: %s %s %d ", attrvalues.useattr, attrvalues.useattrbuff, attrvalues.limit); + if (attrvalues.errstring) + yaz_log(YLOG_LOG, "Error parsing attributes: %s", attrvalues.errstring); + if (attrvalues.limit > 0) { + new_list->elements[new_index] = facet_field_create(odr, facet_list->elements[index]->attributes, attrvalues.limit); + addterms(odr, new_list->elements[new_index], attrvalues.useattr); + new_index++; + } + } + new_list->num = new_index; + if (new_index > 0) { + Z_OtherInformation *oi = odr_malloc(odr, sizeof(*oi)); + Z_OtherInformationUnit *oiu = odr_malloc(odr, sizeof(*oiu)); + oi->num_elements = 1; + oi->list = odr_malloc(odr, oi->num_elements * sizeof(*oi->list)); + oiu->category = 0; + oiu->which = Z_OtherInfo_externallyDefinedInfo; + oiu->information.externallyDefinedInfo = odr_malloc(odr, sizeof(*oiu->information.externallyDefinedInfo)); + oiu->information.externallyDefinedInfo->direct_reference = odr_oiddup(odr, yaz_oid_userinfo_facet_1); + oiu->information.externallyDefinedInfo->which = Z_External_userFacets; + oiu->information.externallyDefinedInfo->u.facetList = new_list; + oi->list[0] = oiu; + return oi; + } + return 0; +} + int ztest_search(void *handle, bend_search_rr *rr) { struct session_handle *sh = (struct session_handle*) handle; @@ -327,6 +388,19 @@ int ztest_search(void *handle, bend_search_rr *rr) } rr->hits = get_hit_count(rr->query); + if (1) + { + /* TODO Not general. Only handles one (Facet) OtherInformation. Overwrite */ + Z_FacetList *facet_list = extract_facet_request(rr->stream, rr->search_input); + + if (facet_list) { + yaz_log(YLOG_LOG, "%d Facets in search request.", facet_list->num); + rr->search_info = build_facet_response(rr->stream, facet_list); + } + else + yaz_log(YLOG_DEBUG, "No facets parsed search request."); + + } do_delay(&new_set->search_delay); new_set->hits = rr->hits;