1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * mgppqueryfilter.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 "mgppqueryfilter.h"
|
---|
27 | #include "fileutil.h"
|
---|
28 | #include "mgppsearch.h"
|
---|
29 |
|
---|
30 | mgppqueryfilterclass::mgppqueryfilterclass ()
|
---|
31 | : fieldedqueryfilterclass() {
|
---|
32 |
|
---|
33 | }
|
---|
34 |
|
---|
35 | mgppqueryfilterclass::~mgppqueryfilterclass () {
|
---|
36 | }
|
---|
37 |
|
---|
38 |
|
---|
39 |
|
---|
40 | void mgppqueryfilterclass::configure (const text_t &key, const text_tarray &cfgline) {
|
---|
41 | fieldedqueryfilterclass::configure(key, cfgline);
|
---|
42 |
|
---|
43 | if (key == "textlevel") {
|
---|
44 | ((mgppsearchclass *)textsearchptr)->set_text_level(cfgline[0]);
|
---|
45 | } else if (key == "indexstem") {
|
---|
46 | ((mgppsearchclass *)textsearchptr)->set_indexstem (cfgline[0]);
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | void mgppqueryfilterclass::filter(const FilterRequest_t &request,
|
---|
51 | FilterResponse_t &response,
|
---|
52 | comerror_t &err, ostream &logout) {
|
---|
53 |
|
---|
54 |
|
---|
55 | outconvertclass text_t2ascii;
|
---|
56 |
|
---|
57 | response.clear ();
|
---|
58 | err = noError;
|
---|
59 | if (db_ptr == NULL) {
|
---|
60 | // most likely a configuration problem
|
---|
61 | logout << text_t2ascii
|
---|
62 | << "configuration error: queryfilter contains a null dbclass\n\n";
|
---|
63 | err = configurationError;
|
---|
64 | return;
|
---|
65 | }
|
---|
66 | if (textsearchptr == NULL) {
|
---|
67 | // most likely a configuration problem
|
---|
68 | logout << text_t2ascii
|
---|
69 | << "configuration error: queryfilter contains a null textsearchclass for mgpp\n\n";
|
---|
70 | err = configurationError;
|
---|
71 | return;
|
---|
72 | }
|
---|
73 | if (full_text_browse(request.filterResultOptions)) {
|
---|
74 | browsefilter(request, response, err, logout);
|
---|
75 | return;
|
---|
76 | }
|
---|
77 | // open the database
|
---|
78 | db_ptr->setlogout(&logout);
|
---|
79 | if (!db_ptr->opendatabase (db_filename, DB_READER, 100, false)) {
|
---|
80 | // most likely a system problem (we have already checked that the database exists)
|
---|
81 | logout << text_t2ascii
|
---|
82 | << "system problem: open on database \"" << db_filename << "\" failed\n\n";
|
---|
83 | err = systemProblem;
|
---|
84 | return;
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | // get the query parameters
|
---|
89 | int startresults, endresults;
|
---|
90 | vector<queryparamclass> queryfilterparams;
|
---|
91 | parse_query_params (request, queryfilterparams, startresults,
|
---|
92 | endresults, logout);
|
---|
93 |
|
---|
94 |
|
---|
95 | // do query
|
---|
96 | queryresultsclass queryresults;
|
---|
97 | do_multi_query (request, queryfilterparams, queryresults, err, logout);
|
---|
98 | if (err != noError) return;
|
---|
99 | // assemble document results
|
---|
100 | if (need_matching_docs (request.filterResultOptions)) {
|
---|
101 |
|
---|
102 | int resultnum = 1;
|
---|
103 | ResultDocInfo_t resultdoc;
|
---|
104 | text_t trans_OID;
|
---|
105 | vector<text_t>::iterator docorder_here = queryresults.docs.docorder.begin();
|
---|
106 | vector<text_t>::iterator docorder_end = queryresults.docs.docorder.end();
|
---|
107 |
|
---|
108 | if (endresults == -1) endresults = MAXNUMDOCS;
|
---|
109 | while (docorder_here != docorder_end) {
|
---|
110 | if (resultnum > endresults) break;
|
---|
111 |
|
---|
112 | // translate the document number
|
---|
113 | if (!translate(db_ptr, *docorder_here, trans_OID)) {
|
---|
114 | logout << text_t2ascii
|
---|
115 | << "warning: could not translate mgpp document number \""
|
---|
116 | << *docorder_here << "\"to OID.\n\n";
|
---|
117 |
|
---|
118 | } else {
|
---|
119 | docresultmap::iterator docset_here = queryresults.docs.docset.find (*docorder_here);
|
---|
120 |
|
---|
121 | // see if there is a result for this number,
|
---|
122 | // if it is in the request set (or the request set is empty)
|
---|
123 | if (docset_here != queryresults.docs.docset.end() &&
|
---|
124 | (request.docSet.empty() || in_set(request.docSet, trans_OID))) {
|
---|
125 | if (resultnum >= startresults) {
|
---|
126 | // add this document
|
---|
127 | resultdoc.OID = trans_OID;
|
---|
128 | resultdoc.result_num = resultnum;
|
---|
129 | resultdoc.ranking = (int)((*docset_here).second.docweight * 10000.0 + 0.5);
|
---|
130 |
|
---|
131 | response.docInfo.push_back (resultdoc);
|
---|
132 | }
|
---|
133 |
|
---|
134 | ++resultnum;
|
---|
135 | }
|
---|
136 | } // else
|
---|
137 |
|
---|
138 | ++docorder_here;
|
---|
139 | }
|
---|
140 | } // if need matching docs
|
---|
141 |
|
---|
142 | // assemble the term results
|
---|
143 | if (need_term_info(request.filterResultOptions)) {
|
---|
144 | // note: the terms have already been sorted and uniqued - ?? have they??
|
---|
145 |
|
---|
146 | TermInfo_t terminfo;
|
---|
147 | bool terms_first = true;
|
---|
148 |
|
---|
149 | termfreqclassarray::iterator terms_here = queryresults.terms.begin();
|
---|
150 | termfreqclassarray::iterator terms_end = queryresults.terms.end();
|
---|
151 |
|
---|
152 | while (terms_here != terms_end) {
|
---|
153 | terminfo.clear();
|
---|
154 | terminfo.term = (*terms_here).termstr;
|
---|
155 | terminfo.freq = (*terms_here).termfreq;
|
---|
156 |
|
---|
157 | // this bit gets the matchTerms ie the equivalent (stem/casefold) terms
|
---|
158 | if (terms_first) {
|
---|
159 | text_tset::iterator termvariants_here = queryresults.termvariants.begin();
|
---|
160 | text_tset::iterator termvariants_end = queryresults.termvariants.end();
|
---|
161 | while (termvariants_here != termvariants_end) {
|
---|
162 | terminfo.matchTerms.push_back (*termvariants_here);
|
---|
163 | ++termvariants_here;
|
---|
164 | }
|
---|
165 | }
|
---|
166 | terms_first = false;
|
---|
167 |
|
---|
168 | response.termInfo.push_back (terminfo);
|
---|
169 |
|
---|
170 | ++terms_here;
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | db_ptr->closedatabase(); // Important that local library doesn't leave any files open
|
---|
175 | response.numDocs = queryresults.docs_matched;
|
---|
176 | response.isApprox = queryresults.is_approx;
|
---|
177 | }
|
---|
178 |
|
---|
179 | void mgppqueryfilterclass::browsefilter(const FilterRequest_t &request,
|
---|
180 | FilterResponse_t &response,
|
---|
181 | comerror_t &err, ostream &logout) {
|
---|
182 |
|
---|
183 | outconvertclass text_t2ascii;
|
---|
184 |
|
---|
185 | // get the query parameters
|
---|
186 | int startresults, endresults;
|
---|
187 | vector<queryparamclass> queryfilterparams;
|
---|
188 | parse_query_params (request, queryfilterparams, startresults,
|
---|
189 | endresults, logout);
|
---|
190 |
|
---|
191 | vector<queryparamclass>::const_iterator query_here = queryfilterparams.begin();
|
---|
192 |
|
---|
193 | // do query
|
---|
194 | queryresultsclass queryresults;
|
---|
195 | queryresults.clear();
|
---|
196 |
|
---|
197 | int numDocs = endresults-startresults;
|
---|
198 | textsearchptr->setcollectdir (collectdir);
|
---|
199 |
|
---|
200 | if (!((mgppsearchclass*)textsearchptr)->browse_search((*query_here), startresults, numDocs, queryresults)) {
|
---|
201 | // most likely a system problem
|
---|
202 | logout << text_t2ascii
|
---|
203 | << "system problem: could not do full text browse with mgpp for index \""
|
---|
204 | << (*query_here).index << (*query_here).subcollection
|
---|
205 | << (*query_here).language << "\".\n\n";
|
---|
206 | err = systemProblem;
|
---|
207 | return;
|
---|
208 | }
|
---|
209 |
|
---|
210 | // assemble the term results
|
---|
211 | TermInfo_t terminfo;
|
---|
212 |
|
---|
213 | termfreqclassarray::iterator terms_here = queryresults.terms.begin();
|
---|
214 | termfreqclassarray::iterator terms_end = queryresults.terms.end();
|
---|
215 |
|
---|
216 | while (terms_here != terms_end) {
|
---|
217 | terminfo.clear();
|
---|
218 | terminfo.term = (*terms_here).termstr;
|
---|
219 | terminfo.freq = (*terms_here).termfreq;
|
---|
220 |
|
---|
221 | response.termInfo.push_back (terminfo);
|
---|
222 |
|
---|
223 | ++terms_here;
|
---|
224 | }
|
---|
225 |
|
---|
226 |
|
---|
227 | }
|
---|
228 |
|
---|
229 | // textsearchptr and db_ptr are assumed to be valid
|
---|
230 | void mgppqueryfilterclass::do_multi_query (const FilterRequest_t &request,
|
---|
231 | const vector<queryparamclass> &query_params,
|
---|
232 | queryresultsclass &multiresults,
|
---|
233 | comerror_t &err, ostream &logout) {
|
---|
234 | outconvertclass text_t2ascii;
|
---|
235 |
|
---|
236 | err = noError;
|
---|
237 | textsearchptr->setcollectdir (collectdir);
|
---|
238 | multiresults.clear();
|
---|
239 |
|
---|
240 | vector<queryparamclass>::const_iterator query_here = query_params.begin();
|
---|
241 | vector<queryparamclass>::const_iterator query_end = query_params.end();
|
---|
242 | while (query_here != query_end) {
|
---|
243 | queryresultsclass thisqueryresults;
|
---|
244 | text_t indx((*query_here).index);
|
---|
245 | if (!textsearchptr->search((*query_here), thisqueryresults)) {
|
---|
246 | // most likely a system problem
|
---|
247 | logout << text_t2ascii
|
---|
248 | << "system problem: could not do search with mgpp for index \""
|
---|
249 | << (*query_here).index << (*query_here).subcollection
|
---|
250 | << (*query_here).language << "\".\n\n";
|
---|
251 | err = systemProblem;
|
---|
252 | return;
|
---|
253 | }
|
---|
254 |
|
---|
255 | // check for syntax error
|
---|
256 | if (thisqueryresults.syntax_error==true) {
|
---|
257 | logout << text_t2ascii
|
---|
258 | << "syntax problem: invalid query string \""
|
---|
259 | << (*query_here).querystring<<"\".\n";
|
---|
260 | err = syntaxError;
|
---|
261 | return;
|
---|
262 | }
|
---|
263 | // combine the results
|
---|
264 | if (need_matching_docs (request.filterResultOptions)) {
|
---|
265 |
|
---|
266 | if (query_params.size() == 1) {
|
---|
267 | multiresults.docs = thisqueryresults.docs; // just one set of results
|
---|
268 | multiresults.docs_matched = thisqueryresults.docs_matched;
|
---|
269 | multiresults.is_approx = thisqueryresults.is_approx;
|
---|
270 |
|
---|
271 | } else {
|
---|
272 | if ((*query_here).combinequery == "and") {
|
---|
273 | multiresults.docs.combine_and (thisqueryresults.docs);
|
---|
274 | } else if ((*query_here).combinequery == "or") {
|
---|
275 | multiresults.docs.combine_or (thisqueryresults.docs);
|
---|
276 | } else if ((*query_here).combinequery == "not") {
|
---|
277 | multiresults.docs.combine_not (thisqueryresults.docs);
|
---|
278 | }
|
---|
279 | multiresults.docs_matched = multiresults.docs.docset.size();
|
---|
280 | multiresults.is_approx = Exact;
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | // combine the term information
|
---|
285 | if (need_term_info (request.filterResultOptions)) {
|
---|
286 | // append the terms
|
---|
287 | multiresults.orgterms.insert(multiresults.orgterms.end(),
|
---|
288 | thisqueryresults.orgterms.begin(),
|
---|
289 | thisqueryresults.orgterms.end());
|
---|
290 |
|
---|
291 |
|
---|
292 | // add the term variants -
|
---|
293 | text_tset::iterator termvar_here = thisqueryresults.termvariants.begin();
|
---|
294 | text_tset::iterator termvar_end = thisqueryresults.termvariants.end();
|
---|
295 | while (termvar_here != termvar_end) {
|
---|
296 | multiresults.termvariants.insert(*termvar_here);
|
---|
297 | ++termvar_here;
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | ++query_here;
|
---|
302 | }
|
---|
303 |
|
---|
304 | // sort and unique the query terms
|
---|
305 | multiresults.sortuniqqueryterms ();
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 |
|
---|