2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.17 1995-10-04 12:55:17 adam
8 * Bug fix in ranked search. Use=Any keys inserted.
10 * Revision 1.16 1995/10/02 16:24:40 adam
11 * Use attribute actually used in search requests.
13 * Revision 1.15 1995/10/02 15:18:52 adam
14 * New member in recRetrieveCtrl: diagnostic.
16 * Revision 1.14 1995/09/28 12:10:32 adam
17 * Bug fixes. Field prefix used in queries.
19 * Revision 1.13 1995/09/18 14:17:50 adam
22 * Revision 1.12 1995/09/15 14:45:21 adam
26 * Revision 1.11 1995/09/14 11:53:27 adam
27 * First work on regular expressions/truncations.
29 * Revision 1.10 1995/09/11 15:23:26 adam
30 * More work on relevance search.
32 * Revision 1.9 1995/09/11 13:09:35 adam
33 * More work on relevance feedback.
35 * Revision 1.8 1995/09/08 14:52:27 adam
36 * Minor changes. Dictionary is lower case now.
38 * Revision 1.7 1995/09/07 13:58:36 adam
39 * New parameter: result-set file descriptor (RSFD) to support multiple
40 * positions within the same result-set.
41 * Boolean operators: and, or, not implemented.
42 * Result-set references.
44 * Revision 1.6 1995/09/06 16:11:18 adam
45 * Option: only one word key per file.
47 * Revision 1.5 1995/09/06 10:33:04 adam
48 * More work on present. Some log messages removed.
50 * Revision 1.4 1995/09/05 15:28:40 adam
51 * More work on search engine.
53 * Revision 1.3 1995/09/04 15:20:22 adam
56 * Revision 1.2 1995/09/04 12:33:43 adam
57 * Various cleanup. YAZ util used instead.
59 * Revision 1.1 1995/09/04 09:10:40 adam
60 * More work on index add/del/update.
61 * Merge sort implemented.
62 * Initial work on z39 server.
78 * attr_print: log attributes
80 static void attr_print (Z_AttributesPlusTerm *t)
83 for (of = 0; of < t->num_attributes; of++)
85 Z_AttributeElement *element;
86 element = t->attributeList[of];
88 switch (element->which)
90 case Z_AttributeValue_numeric:
91 logf (LOG_DEBUG, "attributeType=%d value=%d",
92 *element->attributeType,
93 *element->value.numeric);
95 case Z_AttributeValue_complex:
96 logf (LOG_DEBUG, "attributeType=%d complex",
97 *element->attributeType);
98 for (i = 0; i<element->value.complex->num_list; i++)
100 if (element->value.complex->list[i]->which ==
101 Z_StringOrNumeric_string)
102 logf (LOG_DEBUG, " string: '%s'",
103 element->value.complex->list[i]->u.string);
104 else if (element->value.complex->list[i]->which ==
105 Z_StringOrNumeric_numeric)
106 logf (LOG_DEBUG, " numeric: '%d'",
107 *element->value.complex->list[i]->u.numeric);
120 Z_AttributesPlusTerm *zapt;
123 static int attr_find (AttrType *src)
125 while (src->major < src->zapt->num_attributes)
127 Z_AttributeElement *element;
128 element = src->zapt->attributeList[src->major];
130 if (src->type == *element->attributeType)
132 switch (element->which)
134 case Z_AttributeValue_numeric:
136 return *element->value.numeric;
138 case Z_AttributeValue_complex:
139 if (src->minor >= element->value.complex->num_list ||
140 element->value.complex->list[src->minor]->which !=
141 Z_StringOrNumeric_numeric)
144 return *element->value.complex->list[src->minor-1]->u.numeric;
154 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
167 int (*cmp)(const void *p1, const void *p2);
174 static void heap_swap (struct trunc_info *ti, int i1, int i2)
178 memcpy (ti->swapbuf, ti->heap[i1], ti->keysize);
179 memcpy (ti->heap[i1], ti->heap[i2], ti->keysize);
180 memcpy (ti->heap[i2], ti->swapbuf, ti->keysize);
183 ti->indx[i1] = ti->indx[i2];
187 static void heap_delete (struct trunc_info *ti)
189 int cur = 1, child = 2;
191 assert (ti->heapnum > 0);
192 memcpy (ti->heap[1], ti->heap[ti->heapnum], ti->keysize);
193 ti->indx[1] = ti->indx[ti->heapnum--];
194 while (child <= ti->heapnum) {
195 if (child < ti->heapnum &&
196 (*ti->cmp)(ti->heap[child], ti->heap[1+child]) > 0)
198 if ((*ti->cmp)(ti->heap[cur], ti->heap[child]) > 0)
200 heap_swap (ti, cur, child);
209 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
213 cur = ++(ti->heapnum);
214 memcpy (ti->heap[cur], buf, ti->keysize);
215 ti->indx[cur] = indx;
217 while (parent && (*ti->cmp)(ti->heap[parent], ti->heap[cur]) > 0)
219 heap_swap (ti, cur, parent);
226 struct trunc_info *heap_init (int size, int key_size,
227 int (*cmp)(const void *p1, const void *p2))
229 struct trunc_info *ti = xmalloc (sizeof(*ti));
234 ti->keysize = key_size;
236 ti->indx = xmalloc (size * sizeof(*ti->indx));
237 ti->heap = xmalloc (size * sizeof(*ti->heap));
238 ti->swapbuf = xmalloc (ti->keysize);
239 ti->tmpbuf = xmalloc (ti->keysize);
240 ti->buf = xmalloc (size * ti->keysize);
241 for (i = size; --i >= 0; )
242 ti->heap[i] = ti->buf + ti->keysize * i;
246 static void heap_close (struct trunc_info *ti)
255 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int from, int to,
258 logf (LOG_DEBUG, "rset_trunc, range=%d-%d", from, to-1);
259 if (to - from > merge_chunk)
267 struct trunc_info *ti;
270 rset_temp_parms parms;
272 ispt = xmalloc (sizeof(*ispt) * (to-from));
273 parms.key_size = sizeof (struct it_key);
274 result = rset_create (rset_kind_temp, &parms);
275 rsfd = rset_open (result, 1);
277 ti = heap_init (to-from, sizeof(struct it_key),
279 for (i = to-from; --i >= 0; )
281 ispt[i] = is_position (isam, isam_p[from+i]);
282 if (is_readkey (ispt[i], ti->tmpbuf))
283 heap_insert (ti, ti->tmpbuf, i);
289 rset_write (result, rsfd, ti->heap[1]);
291 if (is_readkey (ispt[n], ti->tmpbuf))
292 heap_insert (ti, ti->tmpbuf, n);
294 for (i = to-from; --i >= 0; )
295 is_pt_free (ispt[i]);
296 rset_close (result, rsfd);
303 static ISAM_P *isam_p_buf = NULL;
304 static int isam_p_size = 0;
305 static int isam_p_indx;
307 static void add_isam_p (const char *info)
309 if (isam_p_indx == isam_p_size)
311 ISAM_P *new_isam_p_buf;
313 isam_p_size = 2*isam_p_size + 100;
314 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
318 memcpy (new_isam_p_buf, isam_p_buf,
319 isam_p_indx * sizeof(*isam_p_buf));
322 isam_p_buf = new_isam_p_buf;
324 assert (*info == sizeof(*isam_p_buf));
325 memcpy (isam_p_buf + isam_p_indx, info+1, sizeof(*isam_p_buf));
329 static int grep_handle (Dict_char *name, const char *info)
331 logf (LOG_DEBUG, "dict name: %s", name);
336 static int trunc_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
337 const char *term_sub, ISAM_P **isam_ps)
339 char term_dict[2*IT_MAX_WORD+2];
343 int truncation_value;
347 attr_init (&use, zapt, 1);
348 use_value = attr_find (&use);
349 logf (LOG_DEBUG, "use value %d", truncation_value);
350 attr_init (&truncation, zapt, 5);
351 truncation_value = attr_find (&truncation);
352 logf (LOG_DEBUG, "truncation value %d", truncation_value);
356 i = index_word_prefix (term_dict, 1, use_value);
358 switch (truncation_value)
360 case -1: /* not specified */
361 case 100: /* do not truncate */
362 strcat (term_dict, term_sub);
363 logf (LOG_DEBUG, "dict_lookup: %s", term_dict);
364 if ((info = dict_lookup (zi->wordDict, term_dict)))
367 case 1: /* right truncation */
368 strcat (term_dict, term_sub);
369 strcat (term_dict, ".*");
370 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
372 case 2: /* left truncation */
373 case 3: /* left&right truncation */
376 case 101: /* process # in term */
377 for (j = strlen(term_dict), i = 0; term_sub[i] && i < 2; i++)
378 term_dict[j++] = term_sub[i];
379 for (; term_sub[i]; i++)
380 if (term_sub[i] == '#')
382 term_dict[j++] = '.';
383 term_dict[j++] = '*';
386 term_dict[j++] = term_sub[i];
388 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
390 case 102: /* regular expression */
391 strcat (term_dict, term_sub);
392 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
395 *isam_ps = isam_p_buf;
396 logf (LOG_DEBUG, "%d positions", isam_p_indx);
401 static void field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
407 Z_Term *term = zapt->term;
409 attr_init (&use, zapt, 1);
410 use_value = attr_find (&use);
414 i = index_word_prefix (termz, 1, use_value);
415 sizez = i + term->u.general->len;
416 if (sizez > IT_MAX_WORD)
418 for (j = 0; i < sizez; i++, j++)
419 termz[i] = index_char_cvt (term->u.general->buf[j]);
423 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
427 Z_Term *term = zapt->term;
429 sizez = term->u.general->len;
430 if (sizez > IT_MAX_WORD)
432 for (i = 0; i < sizez; i++)
433 termz[i] = index_char_cvt (term->u.general->buf[i]);
438 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
439 Z_AttributesPlusTerm *zapt)
441 rset_relevance_parms parms;
442 char termz[IT_MAX_WORD+1];
443 char term_sub[IT_MAX_WORD+1];
444 char *p0 = termz, *p1 = NULL;
445 Z_Term *term = zapt->term;
447 parms.key_size = sizeof(struct it_key);
449 parms.cmp = key_compare;
450 parms.is = zi->wordIsam;
452 if (term->which != Z_Term_general)
457 trans_term (zi, zapt, termz);
458 isam_p_indx = 0; /* global, set by trunc_term - see below */
461 if ((p1 = strchr (p0, ' ')))
463 memcpy (term_sub, p0, p1-p0);
464 term_sub[p1-p0] = '\0';
467 strcpy (term_sub, p0);
468 if (trunc_term (zi, zapt, term_sub, &parms.isam_positions))
474 parms.no_isam_positions = isam_p_indx;
476 return rset_create (rset_kind_relevance, &parms);
478 return rset_create (rset_kind_null, NULL);
481 static RSET rpn_search_APT_word (ZServerInfo *zi,
482 Z_AttributesPlusTerm *zapt)
484 ISAM_P *isam_positions;
485 rset_isam_parms parms;
487 char termz[IT_MAX_WORD+1];
488 Z_Term *term = zapt->term;
490 if (term->which != Z_Term_general)
495 trans_term (zi, zapt, termz);
496 isam_p_indx = 0; /* global, set by trunc_term - see below */
497 if (trunc_term (zi, zapt, termz, &isam_positions))
500 return rset_create (rset_kind_null, NULL);
501 else if (isam_p_indx == 1)
503 parms.is = zi->wordIsam;
504 parms.pos = *isam_positions;
505 return rset_create (rset_kind_isam, &parms);
508 return rset_trunc (zi->wordIsam, isam_positions, 0, isam_p_indx, 400);
511 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
512 Z_AttributesPlusTerm *zapt)
514 ISAM_P *isam_positions;
515 rset_isam_parms parms;
517 char termz[IT_MAX_WORD+1];
518 Z_Term *term = zapt->term;
520 if (term->which != Z_Term_general)
525 trans_term (zi, zapt, termz);
526 isam_p_indx = 0; /* global, set by trunc_term - see below */
527 if (trunc_term (zi, zapt, termz, &isam_positions))
529 if (isam_p_indx != 1)
530 return rset_create (rset_kind_null, NULL);
531 parms.is = zi->wordIsam;
532 parms.pos = *isam_positions;
533 return rset_create (rset_kind_isam, &parms);
536 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt)
540 int relation_value, structure_value;
542 attr_init (&relation, zapt, 2);
543 attr_init (&structure, zapt, 4);
545 relation_value = attr_find (&relation);
546 structure_value = attr_find (&structure);
547 switch (structure_value)
550 if (relation_value == 102) /* relevance relation */
551 return rpn_search_APT_relevance (zi, zapt);
552 return rpn_search_APT_word (zi, zapt);
554 if (relation_value == 102) /* relevance relation */
555 return rpn_search_APT_relevance (zi, zapt);
556 return rpn_search_APT_phrase (zi, zapt);
559 if (relation_value == 102) /* relevance relation */
560 return rpn_search_APT_relevance (zi, zapt);
561 return rpn_search_APT_word (zi, zapt);
566 case 5: /* date - normalized */
568 case 6: /* word list */
569 return rpn_search_APT_relevance (zi, zapt);
570 case 100: /* date - un-normalized */
572 case 101: /* name - normalized */
574 case 102: /* date - un-normalized */
576 case 103: /* structure */
580 case 105: /* free-form-text */
581 return rpn_search_APT_relevance (zi, zapt);
582 case 106: /* document-text */
583 return rpn_search_APT_relevance (zi, zapt);
584 case 107: /* local-number */
586 case 108: /* string */
587 return rpn_search_APT_word (zi, zapt);
588 case 109: /* numeric string */
595 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
599 if (!(s = resultSetGet (zi, resultSetId)))
600 return rset_create (rset_kind_null, NULL);
604 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs)
607 if (zs->which == Z_RPNStructure_complex)
609 rset_bool_parms bool_parms;
611 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1);
612 if (bool_parms.rset_l == NULL)
614 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2);
615 if (bool_parms.rset_r == NULL)
617 rset_delete (bool_parms.rset_l);
620 bool_parms.key_size = sizeof(struct it_key);
621 bool_parms.cmp = key_compare;
623 switch (zs->u.complex->operator->which)
626 r = rset_create (rset_kind_and, &bool_parms);
629 r = rset_create (rset_kind_or, &bool_parms);
631 case Z_Operator_and_not:
632 r = rset_create (rset_kind_not, &bool_parms);
638 else if (zs->which == Z_RPNStructure_simple)
640 if (zs->u.simple->which == Z_Operand_APT)
642 logf (LOG_DEBUG, "rpn_search_APT");
643 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm);
645 else if (zs->u.simple->which == Z_Operand_resultSetId)
647 logf (LOG_DEBUG, "rpn_search_ref");
648 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
662 static void count_set (RSET r, int *count)
668 logf (LOG_DEBUG, "rpn_save_set");
670 rfd = rset_open (r, 0);
671 while (rset_read (r, rfd, &key))
673 if (key.sysno != psysno)
680 logf (LOG_DEBUG, "%d distinct sysnos", *count);
683 int rpn_search (ZServerInfo *zi,
684 Z_RPNQuery *rpn, int num_bases, char **basenames,
685 const char *setname, int *hits)
690 zi->errString = NULL;
691 rset = rpn_search_structure (zi, rpn->RPNStructure);
694 count_set (rset, hits);
695 resultSetAdd (zi, setname, 1, rset);