source: trunk/gsdl/src/colservr/mgsearch.cpp@ 9937

Last change on this file since 9937 was 9937, checked in by kjdon, 19 years ago

modified the filters/sources etc so that if an indexstem is specified in the build.cfg file, then this will be used as the root of the index/gdbm filenames instead of the collection name. colleciton name still used by default. this means that we can rename a coll directory without rebuilding.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 16.3 KB
Line 
1/**********************************************************************
2 *
3 * mgsearch.cpp --
4 * Copyright (C) 1999 The New Zealand Digital Library Project
5 *
6 * A component of the Greenstone digital library software
7 * from the New Zealand Digital Library Project at the
8 * University of Waikato, New Zealand.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *********************************************************************/
25
26#include "gsdlconf.h"
27#include "mgsearch.h"
28#include "fileutil.h"
29
30#include <string.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <ctype.h>
34
35#if defined(GSDL_USE_OBJECTSPACE)
36# include <ospace\std\iostream>
37#elif defined(GSDL_USE_IOS_H)
38# include <iostream.h>
39#else
40# include <iostream>
41#endif
42
43#if defined(__WIN32__)
44// gdbm stuff
45# include "autoconf.h"
46# include "systems.h"
47# include "gdbmconst.h"
48# include "gdbm.h"
49#else
50# include <gdbm.h>
51#endif
52
53
54#include <assert.h>
55
56#include "mgq.h"
57// #include "locateinfo.h"
58#include "gsdlunicode.h"
59#include "unitool.h"
60
61
62/////////////
63// globals //
64/////////////
65
66static char *tempdoc = NULL;
67static int templen = 0;
68
69
70//////////////////////
71// useful functions //
72//////////////////////
73
74
75// input and output are in utf8
76text_t mgsearch_stemword (const text_t &word) {
77 // allocate working stem space
78 int maxstemlen = mgq_getmaxstemlen ();
79 unsigned char *word_stem = new unsigned char [maxstemlen + 2];
80 if (word_stem == NULL) return "";
81
82 // copy word to word_stem
83 int len = 0;
84 text_t::const_iterator here = word.begin();
85 text_t::const_iterator end = word.end();
86 while (len < maxstemlen && here != end) {
87 word_stem[len+1] = (unsigned char)(*here);
88 ++len; ++here;
89 }
90 word_stem[len+1] = '\0';
91 word_stem[0] = len;
92
93 mgq_stemword (word_stem);
94
95 // copy word_stem back to tempstr
96 text_t tempstr;
97 tempstr.setcarr((char *)(&word_stem[1]), word_stem[0]);
98
99 delete [] word_stem;
100
101 return tempstr;
102}
103
104
105
106////////////////////////
107// callback functions //
108////////////////////////
109
110// This routine is called for each document found in a search
111// it assumes that cache_num is set up correctly to point to
112// a suitable result cache
113int ourquerycallback(char * /*UDoc*/, int /*ULen*/, int DocNum,
114 float Weight, void *info) {
115
116
117 queryresultsclass *queryresults = (queryresultsclass * )info;
118
119 // append this entry to the document results
120 docresultclass docresult;
121 docresult.docnum = DocNum;
122 docresult.num_query_terms_matched = (int)(Weight/100.0); // will always be 0 on some versions of mg...
123 docresult.docweight = Weight - docresult.num_query_terms_matched*100;
124
125 queryresults->docs.docset[DocNum] = docresult;
126 queryresults->docs.docorder.push_back(DocNum);
127
128 return 0;
129}
130
131int termequivcallback(char *Word, int ULen, int /*Freq*/,
132 float /*Weight*/, void *info) {
133 text_tset *equivterms = (text_tset *)info;
134 if (equivterms == NULL) return 0;
135
136 text_t thisterm;
137 thisterm.setcarr(Word, ULen);
138
139 equivterms->insert(thisterm);
140
141 return 0;
142}
143
144
145void mgsearch_equivterms (const text_t &word, text_tset &equivterms) {
146 // allocate working stem space
147 int maxstemlen = mgq_getmaxstemlen ();
148 unsigned char *word_stem = new unsigned char [maxstemlen + 2];
149 if (word_stem == NULL) return;
150
151 // copy word to word_stem
152 int len = 0;
153 text_t::const_iterator here = word.begin();
154 text_t::const_iterator end = word.end();
155 while (len < maxstemlen && here != end) {
156 word_stem[len+1] = (unsigned char)(*here);
157 ++len; ++here;
158 }
159 word_stem[len+1] = '\0';
160 word_stem[0] = len;
161
162 // get the equivalent terms
163 mgq_equivterms (word_stem, termequivcallback, (void *)(&equivterms));
164
165 delete [] word_stem;
166
167 return;
168}
169
170 text_tset utf8equivterms; // kept as utf8 string for fast matching
171
172
173// This callback is called once for each term in the query
174int termfreqcallback(char *Word, int ULen, int Freq,
175 float /*Weight*/, void *info) {
176 queryresultsclass *queryresults = (queryresultsclass *)info;
177 if (queryresults == NULL) return 0;
178
179 text_t term;
180 term.setcarr(Word, ULen);
181 termfreqclass termfreq;
182
183 termfreq.termstr = to_uni(term);
184 text_t utf8termstem = mgsearch_stemword (term);
185 termfreq.termstemstr = to_uni (utf8termstem);
186
187 mgsearch_equivterms (utf8termstem, termfreq.utf8equivterms);
188
189 termfreq.termfreq = Freq;
190 queryresults->orgterms.push_back(termfreq);
191
192 return 0;
193}
194
195// this callback is called once for each variation of each term
196int termvariantscallback(char *Word, int ULen, int /*Freq*/,
197 float /*Weight*/, void *info) {
198
199 text_t term;
200 term.setcarr(Word, ULen);
201 queryresultsclass *queryresults = (queryresultsclass *)info;
202 queryresults->termvariants.insert(to_uni(term));
203
204 return 0;
205}
206
207// This callback is for getting document text
208int doctextcallback(char *Doc, int ULen, int /*Freq*/,
209 float /*Weight*/, void * /*info*/) {
210 tempdoc = Doc;
211 templen = ULen;
212
213 return 0;
214}
215
216
217text_t mgsearchclass::getindexsuffix (const text_t &collection,
218 const text_t &index) {
219
220 text_t indexsuffix = "index";
221 indexsuffix = filename_cat (indexsuffix, index);
222 if (indexstem.empty()) {
223 // no index stem, use the coll name
224 indexsuffix = filename_cat (indexsuffix, collection);
225 } else {
226 indexsuffix = filename_cat (indexsuffix, indexstem);
227 }
228 return indexsuffix;
229}
230
231
232
233
234////////////////////
235// mgsearch class //
236////////////////////
237
238mgsearchclass::mgsearchclass ()
239 : searchclass() {
240
241}
242
243mgsearchclass::~mgsearchclass ()
244{
245 if (cache != NULL)
246 {
247 delete cache;
248 cache = NULL;
249 }
250}
251
252void mgsearchclass::set_indexstem(const text_t &stem) {
253 indexstem = stem;
254
255}
256
257// you only need to use this function before doing any stemming
258// casefolding and stemming will be set if values for them are
259// provided (0 or 1).
260// makeindexcurrent returns true if it was able to load the database
261bool mgsearchclass::makeindexcurrent (const text_t &index,
262 const text_t &subcollection,
263 const text_t &language,
264 const text_t &collection,
265 int casefolding,
266 int stemming) {
267 bool databaseloaded = true;
268
269 // get the names of the collection, index and text suffixes
270 char *ccollection = collection.getcstr();
271 assert (ccollection != NULL);
272 char *idxsuffix = (getindexsuffix (collection, (index+subcollection+language))).getcstr();
273 assert (idxsuffix != NULL);
274 char *txtsuffix = (getindexsuffix (collection, "text")).getcstr();
275 assert (txtsuffix != NULL);
276#ifdef __WIN32__
277 char *ccollectdir = (collectdir+"\\").getcstr(); assert (ccollectdir != NULL);
278#else
279 char *ccollectdir = collectdir.getcstr(); assert (ccollectdir != NULL);
280#endif
281
282 if (load_database(ccollection, ccollectdir, idxsuffix, txtsuffix)) {
283 if (casefolding == 0) mgq_ask(".set casefold off");
284 else if (casefolding > 0) mgq_ask(".set casefold on");
285 if (stemming == 0) mgq_ask(".set stem off");
286 else if (stemming > 0) mgq_ask(".set stem on");
287
288 } else databaseloaded = false;
289
290 // free up the c strings
291 delete []ccollection;
292 delete []idxsuffix;
293 delete []txtsuffix;
294 delete []ccollectdir;
295
296 return databaseloaded;
297}
298
299
300// stem word uses the values set in the last call to makeindexcurrent
301// to stem the word. It is assumed that word is in unicode
302text_t mgsearchclass::stemword (const text_t &word) {
303 return to_uni (mgsearch_stemword (to_utf8 (word)));
304}
305
306text_t mgsearchclass::stemword (text_t::const_iterator here, text_t::const_iterator end) {
307 return to_uni (mgsearch_stemword (to_utf8 (here, end)));
308}
309
310/**
311 * search directs the whole execution of the search; a number of other
312 * functions in this class are called as a result, and precondition
313 * checks are also made
314 */
315bool mgsearchclass::search(const queryparamclass &queryparams,
316 queryresultsclass &queryresults) {
317 // assert (cache != NULL);
318
319 // clear any previous results
320 queryresults.clear();
321 // first check the cache
322 if (cache != NULL) {
323 if (cache->find(queryparams, queryresults)) return true;
324 }
325 // make sure there is a query to be processed
326 if (!has_unicode_letdig(queryparams.querystring)) return true;
327
328 if (makeindexcurrent (queryparams.index, queryparams.subcollection,
329 queryparams.language, queryparams.collection)) {
330 // initialise the form of results
331 setsearchmode (queryparams);
332
333 // execute the query
334 submitquery (queryparams);
335
336 // retrieve the results
337 getresults (queryparams, queryresults);
338 return true;
339 }
340
341 return false;
342}
343
344/* accumulator_method has been changed to use array rather than list.
345list appears to be broken somewhat - for some ranked queries, it returned
346fewer results than it should have (eg 45 instead of 50). The three other
347methods (array, splay_tree, hash_table) all return the same number of
348documents, in the same order, with the same ranks. list returns what
349appears to be the same documents (but less of them), but with different ranks,
350and in a different order. Minimal time tests dont show any speed improvement
351of list over array (maybe because its broken??). [02/2001, kjm18]
352
353... [sjboddie, also 02/2001] turns out that changing the accumulator_method
354introduced a more serious bug than it fixed (i.e. occasionally when doing a
355ranked search for a very common word you get no results at all). I've
356changed it back to list for now, one day we should play with other
357accumulator_methods but for now I don't have time and don't want to risk
358introducing bugs (better the devil you know ;)
359*/
360void mgsearchclass::setsearchmode (const queryparamclass &queryparams)
361{
362 mgq_ask(".set expert true");
363 mgq_ask(".set sorted_terms true");
364 mgq_ask(".set accumulator_method list");
365 mgq_ask(".set max_accumulators 500000");
366 mgq_ask(".set maxparas 500000");
367 mgq_ask(".set verbatim true");
368 mgq_ask(".unset skip_dump");
369 mgq_ask(".set mode docnums");
370
371 switch (queryparams.search_type)
372 {
373 case 0: mgq_ask(".set query boolean"); break;
374 case 1: mgq_ask(".set query ranked"); break;
375 }
376 switch (queryparams.casefolding)
377 {
378 case 1: mgq_ask(".set casefold on"); break;
379 case 0: mgq_ask(".set casefold off"); break;
380 }
381 switch (queryparams.stemming)
382 {
383 case 1: mgq_ask(".set stem on"); break;
384 case 0: mgq_ask(".set stem off"); break;
385 }
386 mgq_ask(".set heads_length 150");
387
388 if (queryparams.maxdocs == -1) {
389 mgq_ask(".set maxdocs all");
390 } else {
391 char maxdocstr[32];
392 sprintf(maxdocstr, ".set maxdocs %i", queryparams.maxdocs);
393 mgq_ask(maxdocstr);
394 }
395
396 char maxnumericstr[32];
397 sprintf(maxnumericstr, ".set maxnumeric %i", queryparams.maxnumeric);
398 mgq_ask(maxnumericstr);
399
400}
401
402/**
403 * submitquery constructs the query string (into UTF8 encoding)
404 * and submits it using mgq_ask to the mg search engine. Most
405 * of the processing will be done inside Greenstone
406 */
407void mgsearchclass::submitquery (const queryparamclass &queryparams)
408{
409 // sort out the query string; copy it, remove all special characters
410 // and then convert it to a string in UTF8 format
411 text_t ttquerystring = queryparams.querystring;
412 filterquery (ttquerystring);
413 char *querystring = to_utf8(ttquerystring).getcstr();
414
415 // submit the query
416 mgq_ask(querystring);
417
418 // destroy the temporary character array
419 delete []querystring;
420}
421
422/**
423 * getrults is called to retrieve the required data on the docs
424 * which responded to the query submitted in submitquery above.
425 *
426 * It calls the local mgquery (mgq) interface to MG several times,
427 * to obtain the document numbers, term frequencies, term variants
428 * etc. All processing of the query will be done by Greenstone
429 * thereafter
430 */
431void mgsearchclass::getresults (const queryparamclass &queryparams,
432 queryresultsclass &queryresults) {
433 // get the configuration for the maximum number of documents to
434 // retrieve
435 int howmany = queryparams.maxdocs;
436 if (howmany == -1) howmany = MAXNUMDOCS;
437 mgq_results(result_docnums, 0, howmany,
438 ourquerycallback, (void *)(&queryresults));
439
440 // get the term frequencies
441 mgq_results(result_termfreqs, 0, MAXNUMTERMS,
442 termfreqcallback, (void *)(&queryresults));
443 queryresults.sortuniqqueryterms();
444
445 // get term variants
446 mgq_results(result_terms, 0, MAXNUMTERMS,
447 termvariantscallback, (void *)(&queryresults));
448
449 // get the number of documents retrieved
450 int total_retrieved = 0, is_approx = 0;
451 mgq_docsretrieved (&total_retrieved, &is_approx);
452
453 if (total_retrieved == 0) {
454 // not available (or really was zero)
455 queryresults.docs_matched = queryresults.docs.docset.size();
456 if ((queryparams.maxdocs == -1) ||
457 (queryresults.docs_matched < queryparams.maxdocs))
458 queryresults.is_approx = Exact;
459 else
460 queryresults.is_approx = MoreThan;
461 } else {
462 queryresults.docs_matched = total_retrieved;
463 if (is_approx) queryresults.is_approx = Approximate;
464 else queryresults.is_approx = Exact;
465 }
466}
467
468/**
469 * Tidies the given querystring, removing special characters
470 */
471void mgsearchclass::filterquery (text_t &ttquerystring) {
472 text_t::iterator ithere = ttquerystring.begin ();
473 text_t::iterator itend = ttquerystring.end ();
474
475 // remove all non alphanumeric characters (except
476 // boolean operators
477 while (ithere != itend) {
478 if ((!is_unicode_letdig(*ithere)) && (*ithere != '!') &&
479 (*ithere != '&') && (*ithere != '|') && (*ithere != '(') &&
480 (*ithere != ')')) (*ithere) = ' ';
481 ++ithere;
482 }
483}
484
485
486// the document text for 'docnum' is placed in 'output'
487// docTargetDocument returns 'true' if it was able to
488// try to get a document
489// collection is needed to see if an index from the
490// collection is loaded. If no index has been loaded
491// defaultindex is needed to load one
492bool mgsearchclass::docTargetDocument(const text_t &defaultindex,
493 const text_t &defaultsubcollection,
494 const text_t &defaultlanguage,
495 const text_t &collection,
496 int docnum,
497 text_t &output) {
498 output.clear();
499
500 // get the mg version of the document
501 char *mgdoc = NULL;
502 int doclen = 0;
503 if (!mgdocument (defaultindex, defaultsubcollection, defaultlanguage,
504 collection, docnum, mgdoc, doclen)) return false;
505 if (mgdoc == NULL) return false;
506
507 // replace all control-Cs with spaces
508 char *mgdoc_here = mgdoc;
509 char *mgdoc_end = mgdoc + doclen;
510 while (mgdoc_here < mgdoc_end) {
511 if (*mgdoc_here == '\x3') *mgdoc_here = ' ';
512 ++mgdoc_here;
513 }
514
515 // convert this document to unicode
516 utf8inconvertclass inconvert;
517 convertclass::status_t status;
518 inconvert.reset ();
519 inconvert.setinput (mgdoc, doclen);
520 inconvert.convert (output, status);
521
522 return true;
523}
524
525
526bool mgsearchclass::mgdocument (const text_t &defaultindex,
527 const text_t &defaultsubcollection,
528 const text_t &defaultlanguage,
529 const text_t &collection,
530 int docnum,
531 char *&UDoc, int &ULen) {
532 int databaseloaded = 0;
533
534 UDoc = NULL; ULen = 0;
535
536 // see if we can make an appropriate database current
537// char *ccollection = collection.getcstr();
538// assert (ccollection != NULL);
539// databaseloaded = load_text_database (ccollection);
540// delete []ccollection;
541
542 // try and load the database
543// if (!databaseloaded)
544 databaseloaded = makeindexcurrent (defaultindex, defaultsubcollection,
545 defaultlanguage, collection);
546
547 if (databaseloaded) {
548 // retrieve the document from mg
549 char docstr[32];
550 sprintf(docstr, "%i", docnum);
551
552 mgq_ask(".set mode text");
553 mgq_ask(".set query docnums");
554 mgq_ask(docstr);
555
556 tempdoc = NULL;
557 templen = 0;
558 mgq_results (result_docs, 0, 1, doctextcallback, (void *)NULL);
559 UDoc = tempdoc;
560 ULen = templen;
561 }
562
563 return (bool)databaseloaded;
564}
565
566// unload_database simply calls mgq's close_all_databases function to clear
567// any cached databases - this is useful when attempting to completely
568// remove all trace of a collectionserver at runtime (when using a
569// persistent version of Greenstone like the windows local library)
570void mgsearchclass::unload_database () {
571 close_all_databases();
572}
Note: See TracBrowser for help on using the repository browser.