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

Last change on this file since 2770 was 1909, checked in by kjm18, 23 years ago

in configure, the gdbm_level of the mgppsearchptr is set to Section if
"Section" appears in the "levels" line of collect.cfg

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