2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1995-10-02 16:24:40 adam
8 * Use attribute actually used in search requests.
10 * Revision 1.15 1995/10/02 15:18:52 adam
11 * New member in recRetrieveCtrl: diagnostic.
13 * Revision 1.14 1995/09/28 12:10:32 adam
14 * Bug fixes. Field prefix used in queries.
16 * Revision 1.13 1995/09/18 14:17:50 adam
19 * Revision 1.12 1995/09/15 14:45:21 adam
23 * Revision 1.11 1995/09/14 11:53:27 adam
24 * First work on regular expressions/truncations.
26 * Revision 1.10 1995/09/11 15:23:26 adam
27 * More work on relevance search.
29 * Revision 1.9 1995/09/11 13:09:35 adam
30 * More work on relevance feedback.
32 * Revision 1.8 1995/09/08 14:52:27 adam
33 * Minor changes. Dictionary is lower case now.
35 * Revision 1.7 1995/09/07 13:58:36 adam
36 * New parameter: result-set file descriptor (RSFD) to support multiple
37 * positions within the same result-set.
38 * Boolean operators: and, or, not implemented.
39 * Result-set references.
41 * Revision 1.6 1995/09/06 16:11:18 adam
42 * Option: only one word key per file.
44 * Revision 1.5 1995/09/06 10:33:04 adam
45 * More work on present. Some log messages removed.
47 * Revision 1.4 1995/09/05 15:28:40 adam
48 * More work on search engine.
50 * Revision 1.3 1995/09/04 15:20:22 adam
53 * Revision 1.2 1995/09/04 12:33:43 adam
54 * Various cleanup. YAZ util used instead.
56 * Revision 1.1 1995/09/04 09:10:40 adam
57 * More work on index add/del/update.
58 * Merge sort implemented.
59 * Initial work on z39 server.
75 * attr_print: log attributes
77 static void attr_print (Z_AttributesPlusTerm *t)
80 for (of = 0; of < t->num_attributes; of++)
82 Z_AttributeElement *element;
83 element = t->attributeList[of];
85 switch (element->which)
87 case Z_AttributeValue_numeric:
88 logf (LOG_DEBUG, "attributeType=%d value=%d",
89 *element->attributeType,
90 *element->value.numeric);
92 case Z_AttributeValue_complex:
93 logf (LOG_DEBUG, "attributeType=%d complex",
94 *element->attributeType);
95 for (i = 0; i<element->value.complex->num_list; i++)
97 if (element->value.complex->list[i]->which ==
98 Z_StringOrNumeric_string)
99 logf (LOG_DEBUG, " string: '%s'",
100 element->value.complex->list[i]->u.string);
101 else if (element->value.complex->list[i]->which ==
102 Z_StringOrNumeric_numeric)
103 logf (LOG_DEBUG, " numeric: '%d'",
104 *element->value.complex->list[i]->u.numeric);
117 Z_AttributesPlusTerm *zapt;
120 static int attr_find (AttrType *src)
122 while (src->major < src->zapt->num_attributes)
124 Z_AttributeElement *element;
125 element = src->zapt->attributeList[src->major];
127 if (src->type == *element->attributeType)
129 switch (element->which)
131 case Z_AttributeValue_numeric:
133 return *element->value.numeric;
135 case Z_AttributeValue_complex:
136 if (src->minor >= element->value.complex->num_list ||
137 element->value.complex->list[src->minor]->which !=
138 Z_StringOrNumeric_numeric)
141 return *element->value.complex->list[src->minor-1]->u.numeric;
151 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
164 int (*cmp)(const void *p1, const void *p2);
171 static void heap_swap (struct trunc_info *ti, int i1, int i2)
175 memcpy (ti->swapbuf, ti->heap[i1], ti->keysize);
176 memcpy (ti->heap[i1], ti->heap[i2], ti->keysize);
177 memcpy (ti->heap[i2], ti->swapbuf, ti->keysize);
180 ti->indx[i1] = ti->indx[i2];
184 static void heap_delete (struct trunc_info *ti)
186 int cur = 1, child = 2;
188 assert (ti->heapnum > 0);
189 memcpy (ti->heap[1], ti->heap[ti->heapnum], ti->keysize);
190 ti->indx[1] = ti->indx[ti->heapnum--];
191 while (child <= ti->heapnum) {
192 if (child < ti->heapnum &&
193 (*ti->cmp)(ti->heap[child], ti->heap[1+child]) > 0)
195 if ((*ti->cmp)(ti->heap[cur], ti->heap[child]) > 0)
197 heap_swap (ti, cur, child);
206 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
210 cur = ++(ti->heapnum);
211 memcpy (ti->heap[cur], buf, ti->keysize);
212 ti->indx[cur] = indx;
214 while (parent && (*ti->cmp)(ti->heap[parent], ti->heap[cur]) > 0)
216 heap_swap (ti, cur, parent);
223 struct trunc_info *heap_init (int size, int key_size,
224 int (*cmp)(const void *p1, const void *p2))
226 struct trunc_info *ti = xmalloc (sizeof(*ti));
231 ti->keysize = key_size;
233 ti->indx = xmalloc (size * sizeof(*ti->indx));
234 ti->heap = xmalloc (size * sizeof(*ti->heap));
235 ti->swapbuf = xmalloc (ti->keysize);
236 ti->tmpbuf = xmalloc (ti->keysize);
237 ti->buf = xmalloc (size * ti->keysize);
238 for (i = size; --i >= 0; )
239 ti->heap[i] = ti->buf + ti->keysize * i;
243 static void heap_close (struct trunc_info *ti)
252 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int from, int to,
255 logf (LOG_DEBUG, "rset_trunc, range=%d-%d", from, to-1);
256 if (to - from > merge_chunk)
264 struct trunc_info *ti;
267 rset_temp_parms parms;
269 ispt = xmalloc (sizeof(*ispt) * (to-from));
270 parms.key_size = sizeof (struct it_key);
271 result = rset_create (rset_kind_temp, &parms);
272 rsfd = rset_open (result, 1);
274 ti = heap_init (to-from, sizeof(struct it_key),
276 for (i = to-from; --i >= 0; )
278 ispt[i] = is_position (isam, isam_p[from+i]);
279 if (is_readkey (ispt[i], ti->tmpbuf))
280 heap_insert (ti, ti->tmpbuf, i);
286 rset_write (result, rsfd, ti->heap[1]);
288 if (is_readkey (ispt[n], ti->tmpbuf))
289 heap_insert (ti, ti->tmpbuf, n);
291 for (i = to-from; --i >= 0; )
292 is_pt_free (ispt[i]);
293 rset_close (result, rsfd);
300 static ISAM_P *isam_p_buf = NULL;
301 static int isam_p_size = 0;
302 static int isam_p_indx;
304 static void add_isam_p (const char *info)
306 if (isam_p_indx == isam_p_size)
308 ISAM_P *new_isam_p_buf;
310 isam_p_size = 2*isam_p_size + 100;
311 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
315 memcpy (new_isam_p_buf, isam_p_buf,
316 isam_p_indx * sizeof(*isam_p_buf));
319 isam_p_buf = new_isam_p_buf;
321 assert (*info == sizeof(*isam_p_buf));
322 memcpy (isam_p_buf + isam_p_indx, info+1, sizeof(*isam_p_buf));
326 static int grep_handle (Dict_char *name, const char *info)
328 logf (LOG_DEBUG, "dict name: %s", name);
333 static int trunc_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
334 const char *term_sub, ISAM_P **isam_ps)
336 char term_dict[2*IT_MAX_WORD+2];
340 int truncation_value;
342 attr_init (&truncation, zapt, 5);
343 truncation_value = attr_find (&truncation);
344 logf (LOG_DEBUG, "truncation value %d", truncation_value);
345 switch (truncation_value)
347 case -1: /* not specified */
348 case 100: /* do not truncate */
349 strcpy (term_dict, term_sub);
350 logf (LOG_DEBUG, "dict_lookup: %s", term_dict);
351 if ((info = dict_lookup (zi->wordDict, term_dict)))
354 case 1: /* right truncation */
355 strcpy (term_dict, term_sub);
356 strcat (term_dict, ".*");
357 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
359 case 2: /* left truncation */
360 case 3: /* left&right truncation */
363 case 101: /* process # in term */
364 for (j = 0, i = 0; term_sub[i] && i < 2; i++)
365 term_dict[j++] = term_sub[i];
366 for (; term_sub[i]; i++)
367 if (term_sub[i] == '#')
369 term_dict[j++] = '.';
370 term_dict[j++] = '*';
373 term_dict[j++] = term_sub[i];
375 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
377 case 102: /* regular expression */
378 strcpy (term_dict, term_sub);
379 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
382 *isam_ps = isam_p_buf;
383 logf (LOG_DEBUG, "%d positions", isam_p_indx);
387 static void field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
393 Z_Term *term = zapt->term;
395 attr_init (&use, zapt, 1);
396 use_value = attr_find (&use);
400 i = index_word_prefix (termz, 1, use_value);
401 sizez = i + term->u.general->len;
402 if (sizez > IT_MAX_WORD)
404 for (j = 0; i < sizez; i++, j++)
405 termz[i] = index_char_cvt (term->u.general->buf[j]);
410 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
411 Z_AttributesPlusTerm *zapt)
413 rset_relevance_parms parms;
414 char termz[IT_MAX_WORD+1];
415 char term_sub[IT_MAX_WORD+1];
416 char *p0 = termz, *p1 = NULL;
417 Z_Term *term = zapt->term;
419 parms.key_size = sizeof(struct it_key);
421 parms.cmp = key_compare;
422 parms.is = zi->wordIsam;
424 if (term->which != Z_Term_general)
429 field_term (zi, zapt, termz);
430 isam_p_indx = 0; /* global, set by trunc_term - see below */
433 if ((p1 = strchr (p0, ' ')))
435 memcpy (term_sub, p0, p1-p0);
436 term_sub[p1-p0] = '\0';
439 strcpy (term_sub, p0);
440 if (trunc_term (zi, zapt, term_sub, &parms.isam_positions))
446 parms.no_isam_positions = isam_p_indx;
448 return rset_create (rset_kind_relevance, &parms);
450 return rset_create (rset_kind_null, NULL);
453 static RSET rpn_search_APT_word (ZServerInfo *zi,
454 Z_AttributesPlusTerm *zapt)
456 ISAM_P *isam_positions;
457 rset_isam_parms parms;
459 char termz[IT_MAX_WORD+1];
460 Z_Term *term = zapt->term;
462 if (term->which != Z_Term_general)
467 field_term (zi, zapt, termz);
468 isam_p_indx = 0; /* global, set by trunc_term - see below */
469 if (trunc_term (zi, zapt, termz, &isam_positions))
472 return rset_create (rset_kind_null, NULL);
473 else if (isam_p_indx == 1)
475 parms.is = zi->wordIsam;
476 parms.pos = *isam_positions;
477 return rset_create (rset_kind_isam, &parms);
480 return rset_trunc (zi->wordIsam, isam_positions, 0, isam_p_indx, 400);
483 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
484 Z_AttributesPlusTerm *zapt)
486 ISAM_P *isam_positions;
487 rset_isam_parms parms;
489 char termz[IT_MAX_WORD+1];
490 Z_Term *term = zapt->term;
492 if (term->which != Z_Term_general)
497 field_term (zi, zapt, termz);
498 isam_p_indx = 0; /* global, set by trunc_term - see below */
499 if (trunc_term (zi, zapt, termz, &isam_positions))
501 if (isam_p_indx != 1)
502 return rset_create (rset_kind_null, NULL);
503 parms.is = zi->wordIsam;
504 parms.pos = *isam_positions;
505 return rset_create (rset_kind_isam, &parms);
508 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt)
512 int relation_value, structure_value;
514 attr_init (&relation, zapt, 2);
515 attr_init (&structure, zapt, 4);
517 relation_value = attr_find (&relation);
518 structure_value = attr_find (&structure);
519 switch (structure_value)
522 if (relation_value == 102) /* relevance relation */
523 return rpn_search_APT_relevance (zi, zapt);
524 return rpn_search_APT_word (zi, zapt);
526 if (relation_value == 102) /* relevance relation */
527 return rpn_search_APT_relevance (zi, zapt);
528 return rpn_search_APT_phrase (zi, zapt);
531 if (relation_value == 102) /* relevance relation */
532 return rpn_search_APT_relevance (zi, zapt);
533 return rpn_search_APT_word (zi, zapt);
538 case 5: /* date - normalized */
540 case 6: /* word list */
541 return rpn_search_APT_relevance (zi, zapt);
542 case 100: /* date - un-normalized */
544 case 101: /* name - normalized */
546 case 102: /* date - un-normalized */
548 case 103: /* structure */
552 case 105: /* free-form-text */
553 return rpn_search_APT_relevance (zi, zapt);
554 case 106: /* document-text */
555 return rpn_search_APT_relevance (zi, zapt);
556 case 107: /* local-number */
558 case 108: /* string */
559 return rpn_search_APT_word (zi, zapt);
560 case 109: /* numeric string */
567 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
571 if (!(s = resultSetGet (zi, resultSetId)))
572 return rset_create (rset_kind_null, NULL);
576 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs)
579 if (zs->which == Z_RPNStructure_complex)
581 rset_bool_parms bool_parms;
583 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1);
584 if (bool_parms.rset_l == NULL)
586 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2);
587 if (bool_parms.rset_r == NULL)
589 rset_delete (bool_parms.rset_l);
592 bool_parms.key_size = sizeof(struct it_key);
593 bool_parms.cmp = key_compare;
595 switch (zs->u.complex->operator->which)
598 r = rset_create (rset_kind_and, &bool_parms);
601 r = rset_create (rset_kind_or, &bool_parms);
603 case Z_Operator_and_not:
604 r = rset_create (rset_kind_not, &bool_parms);
610 else if (zs->which == Z_RPNStructure_simple)
612 if (zs->u.simple->which == Z_Operand_APT)
614 logf (LOG_DEBUG, "rpn_search_APT");
615 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm);
617 else if (zs->u.simple->which == Z_Operand_resultSetId)
619 logf (LOG_DEBUG, "rpn_search_ref");
620 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
634 static void count_set (RSET r, int *count)
640 logf (LOG_DEBUG, "rpn_save_set");
642 rfd = rset_open (r, 0);
643 while (rset_read (r, rfd, &key))
645 if (key.sysno != psysno)
652 logf (LOG_DEBUG, "%d distinct sysnos", *count);
655 int rpn_search (ZServerInfo *zi,
656 Z_RPNQuery *rpn, int num_bases, char **basenames,
657 const char *setname, int *hits)
662 zi->errString = NULL;
663 rset = rpn_search_structure (zi, rpn->RPNStructure);
666 count_set (rset, hits);
667 resultSetAdd (zi, setname, 1, rset);