source: trunk/gsdl/src/colservr/mgppqueryfilter.cpp@ 10606

Last change on this file since 10606 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:keywords set to Author Date Id Revision
File size: 12.0 KB
Line 
1/**********************************************************************
2 *
3 * queryfilter.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
27
28#include "mgppqueryfilter.h"
29#include "fileutil.h"
30#include <assert.h>
31#include "mgppsearch.h"
32
33/////////////////////////////////
34// functions for queryfilterclass
35/////////////////////////////////
36
37
38mgppqueryfilterclass::mgppqueryfilterclass ()
39 : queryfilterclass() {
40
41
42 FilterOption_t filtopt;
43
44 // -- onePerTerm Level enumerated
45 // likely to be Doc, Sec, Para, but we dont assume anything now
46 filtopt.clear();
47 filtopt.name = "Level";
48 filtopt.type = FilterOption_t::enumeratedt;
49 filtopt.repeatable = FilterOption_t::onePerTerm;
50 filterOptions["Level"] = filtopt;
51
52 // -- IndexField, enumerated, used to list available fields
53 filtopt.clear();
54 filtopt.name = "IndexField";
55 filtopt.type = FilterOption_t::enumeratedt;
56 filtopt.repeatable = FilterOption_t::onePerTerm;
57 filtopt.defaultValue = "";
58 filterOptions["IndexField"] = filtopt;
59
60}
61
62mgppqueryfilterclass::~mgppqueryfilterclass () {
63}
64
65
66//whether a query is a full text browse
67bool mgppqueryfilterclass::full_text_browse (int filterRequestOptions) {
68 return (filterRequestOptions & FRfullTextBrowse);
69}
70
71void mgppqueryfilterclass::configure (const text_t &key, const text_tarray &cfgline) {
72 queryfilterclass::configure(key, cfgline);
73
74 if (key == "indexfieldmap") {
75 indexfieldmap.importmap (cfgline);
76
77 // update the list of indexes in the filter information
78 text_tarray options;
79 indexfieldmap.gettoarray (options);
80 filterOptions["IndexField"].validValues.erase(filterOptions["IndexField"].validValues.begin(), filterOptions["IndexField"].validValues.end());
81 text_tarray::const_iterator here = options.begin();
82 text_tarray::const_iterator end = options.end();
83 bool start = true;
84 while (here !=end) {
85 if (!(*here).empty()) {
86 filterOptions["IndexField"].validValues.push_back(*here);
87 if (start) {
88 filterOptions["IndexField"].defaultValue = *here;
89 start = false;
90 }
91 }
92 ++here;
93 }
94
95 } else if (key == "indexlevels") {
96 text_tarray::const_iterator here = cfgline.begin();
97 text_tarray::const_iterator end = cfgline.end();
98 bool first=true;
99 filterOptions["Level"].validValues.erase(filterOptions["Level"].validValues.begin(), filterOptions["Level"].validValues.end());
100 while (here != end) {
101 if (!(*here).empty()) {
102 if (first) {
103 first = false;
104 // the default is the first value
105 filterOptions["Level"].defaultValue = *here;
106 }
107 filterOptions["Level"].validValues.push_back(*here);
108 }
109 ++here;
110 }
111 } else if (key == "textlevel") {
112 ((mgppsearchclass *)textsearchptr)->set_gdbm_level( cfgline[0]);
113 } else if (key == "indexstem") {
114 ((mgppsearchclass *)textsearchptr)->set_indexstem (cfgline[0]);
115 }
116
117}
118
119
120void mgppqueryfilterclass::filter(const FilterRequest_t &request,
121 FilterResponse_t &response,
122 comerror_t &err, ostream &logout) {
123
124
125 outconvertclass text_t2ascii;
126
127 response.clear ();
128 err = noError;
129 if (gdbmptr == NULL) {
130 // most likely a configuration problem
131 logout << text_t2ascii
132 << "configuration error: queryfilter contains a null gdbmclass\n\n";
133 err = configurationError;
134 return;
135 }
136 if (textsearchptr == NULL) {
137 // most likely a configuration problem
138 logout << text_t2ascii
139 << "configuration error: queryfilter contains a null textsearchclass for mgpp\n\n";
140 err = configurationError;
141 return;
142 }
143 if (full_text_browse(request.filterResultOptions)) {
144 browsefilter(request, response, err, logout);
145 return;
146 }
147 // open the database
148 gdbmptr->setlogout(&logout);
149 if (!gdbmptr->opendatabase (gdbm_filename, GDBM_READER, 100, false)) {
150 // most likely a system problem (we have already checked that the
151 // gdbm database exists)
152 logout << text_t2ascii
153 << "system problem: open on gdbm database \""
154 << gdbm_filename << "\" failed\n\n";
155 err = systemProblem;
156 return;
157 }
158
159
160 // get the query parameters
161 int startresults, endresults;
162 text_t phrasematch; // not used here any more
163 vector<queryparamclass> queryfilterparams;
164 parse_query_params (request, queryfilterparams, startresults,
165 endresults, phrasematch, logout);
166
167
168 // do query
169 queryresultsclass queryresults;
170 do_multi_query (request, queryfilterparams, queryresults, err, logout);
171 if (err != noError) return;
172 // assemble document results
173 if (need_matching_docs (request.filterResultOptions)) {
174
175 int resultnum = 1;
176 ResultDocInfo_t resultdoc;
177 text_t trans_OID;
178 vector<int>::iterator docorder_here = queryresults.docs.docorder.begin();
179 vector<int>::iterator docorder_end = queryresults.docs.docorder.end();
180
181 if (endresults == -1) endresults = MAXNUMDOCS;
182 while (docorder_here != docorder_end) {
183 if (resultnum > endresults) break;
184
185 // translate the document number
186 if (!translate(gdbmptr, *docorder_here, trans_OID)) {
187 logout << text_t2ascii
188 << "warning: could not translate mgpp document number \""
189 << *docorder_here << "\"to OID.\n\n";
190
191 } else {
192 docresultmap::iterator docset_here = queryresults.docs.docset.find (*docorder_here);
193
194 // see if there is a result for this number,
195 // if it is in the request set (or the request set is empty)
196 if (docset_here != queryresults.docs.docset.end() &&
197 (request.docSet.empty() || in_set(request.docSet, trans_OID))) {
198 if (resultnum >= startresults) {
199 // add this document
200 resultdoc.OID = trans_OID;
201 resultdoc.result_num = resultnum;
202 resultdoc.ranking = (int)((*docset_here).second.docweight * 10000.0 + 0.5);
203
204 response.docInfo.push_back (resultdoc);
205 }
206
207 ++resultnum;
208 }
209 } // else
210
211 ++docorder_here;
212 }
213 } // if need matching docs
214
215 // assemble the term results
216 if (need_term_info(request.filterResultOptions)) {
217 // note: the terms have already been sorted and uniqued - ?? have they??
218
219 TermInfo_t terminfo;
220 bool terms_first = true;
221
222 termfreqclassarray::iterator terms_here = queryresults.terms.begin();
223 termfreqclassarray::iterator terms_end = queryresults.terms.end();
224
225 while (terms_here != terms_end) {
226 terminfo.clear();
227 terminfo.term = (*terms_here).termstr;
228 terminfo.freq = (*terms_here).termfreq;
229
230 // this bit gets the matchTerms ie the equivalent (stem/casefold) terms
231 if (terms_first) {
232 text_tset::iterator termvariants_here = queryresults.termvariants.begin();
233 text_tset::iterator termvariants_end = queryresults.termvariants.end();
234 while (termvariants_here != termvariants_end) {
235 terminfo.matchTerms.push_back (*termvariants_here);
236 ++termvariants_here;
237 }
238 }
239 terms_first = false;
240
241 response.termInfo.push_back (terminfo);
242
243 ++terms_here;
244 }
245 }
246
247 response.numDocs = queryresults.docs_matched;
248 response.isApprox = queryresults.is_approx;
249}
250
251void mgppqueryfilterclass::browsefilter(const FilterRequest_t &request,
252 FilterResponse_t &response,
253 comerror_t &err, ostream &logout) {
254
255 outconvertclass text_t2ascii;
256
257 // get the query parameters
258 int startresults, endresults;
259 text_t phrasematch; // not used here any more, just have it so can use
260 // parse_query_params function
261
262 vector<queryparamclass> queryfilterparams;
263 parse_query_params (request, queryfilterparams, startresults,
264 endresults, phrasematch, logout);
265
266 vector<queryparamclass>::const_iterator query_here = queryfilterparams.begin();
267
268 // do query
269 queryresultsclass queryresults;
270 queryresults.clear();
271
272 int numDocs = endresults-startresults;
273 textsearchptr->setcollectdir (collectdir);
274
275 if (!((mgppsearchclass*)textsearchptr)->browse_search((*query_here), startresults, numDocs, queryresults)) {
276 // most likely a system problem
277 logout << text_t2ascii
278 << "system problem: could not do full text browse with mgpp for index \""
279 << (*query_here).index << (*query_here).subcollection
280 << (*query_here).language << "\".\n\n";
281 err = systemProblem;
282 return;
283 }
284
285 // assemble the term results
286 TermInfo_t terminfo;
287
288 termfreqclassarray::iterator terms_here = queryresults.terms.begin();
289 termfreqclassarray::iterator terms_end = queryresults.terms.end();
290
291 while (terms_here != terms_end) {
292 terminfo.clear();
293 terminfo.term = (*terms_here).termstr;
294 terminfo.freq = (*terms_here).termfreq;
295
296 response.termInfo.push_back (terminfo);
297
298 ++terms_here;
299 }
300
301
302}
303
304// mgppsearchptr and gdbmptr are assumed to be valid
305void mgppqueryfilterclass::do_multi_query (const FilterRequest_t &request,
306 const vector<queryparamclass> &query_params,
307 queryresultsclass &multiresults,
308 comerror_t &err, ostream &logout) {
309 outconvertclass text_t2ascii;
310
311 err = noError;
312 textsearchptr->setcollectdir (collectdir);
313 multiresults.clear();
314
315 vector<queryparamclass>::const_iterator query_here = query_params.begin();
316 vector<queryparamclass>::const_iterator query_end = query_params.end();
317 while (query_here != query_end) {
318 queryresultsclass thisqueryresults;
319 text_t indx((*query_here).index);
320 if (!textsearchptr->search((*query_here), thisqueryresults)) {
321 // most likely a system problem
322 logout << text_t2ascii
323 << "system problem: could not do search with mgpp for index \""
324 << (*query_here).index << (*query_here).subcollection
325 << (*query_here).language << "\".\n\n";
326 err = systemProblem;
327 return;
328 }
329
330 // check for syntax error
331 if (thisqueryresults.syntax_error==true) {
332 logout << text_t2ascii
333 << "syntax problem: invalid query string \""
334 << (*query_here).querystring<<"\".\n";
335 err = syntaxError;
336 return;
337 }
338 // combine the results
339 if (need_matching_docs (request.filterResultOptions)) {
340
341 if (query_params.size() == 1) {
342 multiresults.docs = thisqueryresults.docs; // just one set of results
343 multiresults.docs_matched = thisqueryresults.docs_matched;
344 multiresults.is_approx = thisqueryresults.is_approx;
345
346 } else {
347 if ((*query_here).combinequery == "and") {
348 multiresults.docs.combine_and (thisqueryresults.docs);
349 } else if ((*query_here).combinequery == "or") {
350 multiresults.docs.combine_or (thisqueryresults.docs);
351 } else if ((*query_here).combinequery == "not") {
352 multiresults.docs.combine_not (thisqueryresults.docs);
353 }
354 multiresults.docs_matched = multiresults.docs.docset.size();
355 multiresults.is_approx = Exact;
356 }
357 }
358
359 // combine the term information
360 if (need_term_info (request.filterResultOptions)) {
361 // append the terms
362 multiresults.orgterms.insert(multiresults.orgterms.end(),
363 thisqueryresults.orgterms.begin(),
364 thisqueryresults.orgterms.end());
365
366
367 // add the term variants -
368 text_tset::iterator termvar_here = thisqueryresults.termvariants.begin();
369 text_tset::iterator termvar_end = thisqueryresults.termvariants.end();
370 while (termvar_here != termvar_end) {
371 multiresults.termvariants.insert(*termvar_here);
372 ++termvar_here;
373 }
374 }
375
376 ++query_here;
377 }
378
379 // sort and unique the query terms
380 multiresults.sortuniqqueryterms ();
381}
382
383
384
Note: See TracBrowser for help on using the repository browser.