source: trunk/gsdl/src/colservr/lucenequeryfilter.cpp@ 9620

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

added some x++ -> ++x changes submitted by Emanuel Dejanu

  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 KB
Line 
1/**********************************************************************
2 *
3 * lucenequeryfilter.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 "lucenequeryfilter.h"
29#include "fileutil.h"
30#include <assert.h>
31#include "lucenesearch.h"
32
33/////////////////////////////////
34// functions for queryfilterclass
35/////////////////////////////////
36
37
38lucenequeryfilterclass::lucenequeryfilterclass ()
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
62lucenequeryfilterclass::~lucenequeryfilterclass () {
63}
64
65
66//whether a query is a full text browse
67bool lucenequeryfilterclass::full_text_browse (int filterRequestOptions) {
68 return (filterRequestOptions & FRfullTextBrowse);
69}
70
71void lucenequeryfilterclass::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
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 } else if (key == "indexlevels") {
95 text_tarray::const_iterator here = cfgline.begin();
96 text_tarray::const_iterator end = cfgline.end();
97 bool first=true;
98 while (here != end) {
99 if (!(*here).empty()) {
100 if (first) {
101 first = false;
102 // the default is the first value
103 filterOptions["Level"].defaultValue = *here;
104 }
105 filterOptions["Level"].validValues.push_back(*here);
106 }
107 ++here;
108 }
109 } else if (key == "textlevel") {
110 ((lucenesearchclass *)textsearchptr)->set_gdbm_level( cfgline[0]);
111 }
112
113}
114
115
116void lucenequeryfilterclass::filter(const FilterRequest_t &request,
117 FilterResponse_t &response,
118 comerror_t &err, ostream &logout) {
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 (textsearchptr == NULL) {
132 // most likely a configuration problem
133 logout << text_t2ascii
134 << "configuration error: queryfilter contains a null textsearchclass (lucene)\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 // assemble document results
168 if (need_matching_docs (request.filterResultOptions)) {
169
170 int resultnum = 1;
171 ResultDocInfo_t resultdoc;
172 text_t trans_OID;
173 vector<int>::iterator docorder_here = queryresults.docs.docorder.begin();
174 vector<int>::iterator docorder_end = queryresults.docs.docorder.end();
175
176 if (endresults == -1) endresults = MAXNUMDOCS;
177 while (docorder_here != docorder_end) {
178 if (resultnum > endresults) break;
179
180 // translate the document number
181 if (!translate(gdbmptr, *docorder_here, trans_OID)) {
182 logout << text_t2ascii
183 << "warning: could not translate lucene document number \""
184 << *docorder_here << "\" to OID.\n\n";
185
186 } else {
187 docresultmap::iterator docset_here = queryresults.docs.docset.find (*docorder_here);
188
189 // see if there is a result for this number,
190 // if it is in the request set (or the request set is empty)
191 if (docset_here != queryresults.docs.docset.end() &&
192 (request.docSet.empty() || in_set(request.docSet, trans_OID))) {
193 if (resultnum >= startresults) {
194 // add this document
195 resultdoc.OID = trans_OID;
196 resultdoc.result_num = resultnum;
197 resultdoc.ranking = (int)((*docset_here).second.docweight * 10000.0 + 0.5);
198
199 response.docInfo.push_back (resultdoc);
200 }
201
202 ++resultnum;
203 }
204 } // else
205
206 ++docorder_here;
207 }
208 } // if need matching docs
209
210 // assemble the term results
211 if (need_term_info(request.filterResultOptions)) {
212 // note: the terms have already been sorted and uniqued - ?? have they??
213
214 TermInfo_t terminfo;
215 bool terms_first = true;
216
217 termfreqclassarray::iterator terms_here = queryresults.terms.begin();
218 termfreqclassarray::iterator terms_end = queryresults.terms.end();
219
220 while (terms_here != terms_end) {
221 terminfo.clear();
222 terminfo.term = (*terms_here).termstr;
223 terminfo.freq = (*terms_here).termfreq;
224
225 // this bit gets the matchTerms ie the equivalent (stem/casefold) terms
226 if (terms_first) {
227 text_tset::iterator termvariants_here = queryresults.termvariants.begin();
228 text_tset::iterator termvariants_end = queryresults.termvariants.end();
229 while (termvariants_here != termvariants_end) {
230 terminfo.matchTerms.push_back (*termvariants_here);
231 ++termvariants_here;
232 }
233 }
234 terms_first = false;
235
236 response.termInfo.push_back (terminfo);
237
238 ++terms_here;
239 }
240 }
241
242 response.numDocs = queryresults.docs_matched;
243 response.isApprox = queryresults.is_approx;
244}
245
246void lucenequeryfilterclass::browsefilter(const FilterRequest_t &request,
247 FilterResponse_t &response,
248 comerror_t &err, ostream &logout) {
249
250 outconvertclass text_t2ascii;
251
252 // get the query parameters
253 int startresults, endresults;
254 text_t phrasematch; // not used here any more, just have it so can use
255 // parse_query_params function
256
257 vector<queryparamclass> queryfilterparams;
258 parse_query_params (request, queryfilterparams, startresults,
259 endresults, phrasematch, logout);
260
261 vector<queryparamclass>::const_iterator query_here = queryfilterparams.begin();
262
263 // do query
264 queryresultsclass queryresults;
265 queryresults.clear();
266
267 int numDocs = endresults-startresults;
268 textsearchptr->setcollectdir (collectdir);
269
270 if (!((lucenesearchclass*)textsearchptr)->browse_search((*query_here), startresults, numDocs, queryresults)) {
271 // most likely a system problem
272 logout << text_t2ascii
273 << "system problem: could not do full text browse with lucene for index \""
274 << (*query_here).index << (*query_here).subcollection
275 << (*query_here).language << "\".\n\n";
276 err = systemProblem;
277 return;
278 }
279
280 // assemble the term results
281 TermInfo_t terminfo;
282
283 termfreqclassarray::iterator terms_here = queryresults.terms.begin();
284 termfreqclassarray::iterator terms_end = queryresults.terms.end();
285
286 while (terms_here != terms_end) {
287 terminfo.clear();
288 terminfo.term = (*terms_here).termstr;
289 terminfo.freq = (*terms_here).termfreq;
290
291 response.termInfo.push_back (terminfo);
292
293 ++terms_here;
294 }
295
296
297}
298
299// lucenesearchptr and gdbmptr are assumed to be valid
300void lucenequeryfilterclass::do_multi_query (const FilterRequest_t &request,
301 const vector<queryparamclass> &query_params,
302 queryresultsclass &multiresults,
303 comerror_t &err, ostream &logout) {
304 outconvertclass text_t2ascii;
305
306 err = noError;
307 textsearchptr->setcollectdir (collectdir);
308 multiresults.clear();
309
310 vector<queryparamclass>::const_iterator query_here = query_params.begin();
311 vector<queryparamclass>::const_iterator query_end = query_params.end();
312 while (query_here != query_end) {
313 queryresultsclass thisqueryresults;
314 if (!textsearchptr->search((*query_here), thisqueryresults)) {
315 // most likely a system problem
316 logout << text_t2ascii
317 << "system problem: could not do search with lucene for index \""
318 << (*query_here).index << (*query_here).level
319 << (*query_here).subcollection
320 << (*query_here).language << "\".\n\n";
321 err = systemProblem;
322 return;
323 }
324
325 // check for syntax error
326 if (thisqueryresults.syntax_error==true) {
327 logout << text_t2ascii
328 << "syntax problem: invalid query string \""
329 << (*query_here).querystring<<"\".\n";
330 err = syntaxError;
331 return;
332 }
333 // combine the results
334 if (need_matching_docs (request.filterResultOptions)) {
335
336 if (query_params.size() == 1) {
337 multiresults.docs = thisqueryresults.docs; // just one set of results
338 multiresults.docs_matched = thisqueryresults.docs_matched;
339 multiresults.is_approx = thisqueryresults.is_approx;
340
341 } else {
342 if ((*query_here).combinequery == "and") {
343 multiresults.docs.combine_and (thisqueryresults.docs);
344 } else if ((*query_here).combinequery == "or") {
345 multiresults.docs.combine_or (thisqueryresults.docs);
346 } else if ((*query_here).combinequery == "not") {
347 multiresults.docs.combine_not (thisqueryresults.docs);
348 }
349 multiresults.docs_matched = multiresults.docs.docset.size();
350 multiresults.is_approx = Exact;
351 }
352 }
353
354 // combine the term information
355 if (need_term_info (request.filterResultOptions)) {
356 // append the terms
357 multiresults.orgterms.insert(multiresults.orgterms.end(),
358 thisqueryresults.orgterms.begin(),
359 thisqueryresults.orgterms.end());
360
361
362 // add the term variants -
363 text_tset::iterator termvar_here = thisqueryresults.termvariants.begin();
364 text_tset::iterator termvar_end = thisqueryresults.termvariants.end();
365 while (termvar_here != termvar_end) {
366 multiresults.termvariants.insert(*termvar_here);
367 ++termvar_here;
368 }
369 }
370
371 ++query_here;
372 }
373
374 // sort and unique the query terms
375 multiresults.sortuniqqueryterms ();
376}
377
378
379
Note: See TracBrowser for help on using the repository browser.