source: trunk/gsdl/src/recpt/querytools.cpp@ 608

Last change on this file since 608 was 608, checked in by sjboddie, 25 years ago

added Maxdocs queryfilter option which may be -1 for 'all'

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1/**********************************************************************
2 *
3 * querytools.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 * $Id: querytools.cpp 608 1999-09-21 11:34:42Z sjboddie $
25 *
26 *********************************************************************/
27
28/*
29 $Log$
30 Revision 1.7 1999/09/21 11:32:42 sjboddie
31 added Maxdocs queryfilter option which may be -1 for 'all'
32
33 Revision 1.6 1999/09/07 04:56:58 sjboddie
34 added GPL notice
35
36 Revision 1.5 1999/08/25 04:47:55 sjboddie
37 added advanced search option - other minor changes
38
39 Revision 1.4 1999/07/19 00:16:58 sjboddie
40 no longer display documents that don't match all phrases in query string
41
42 Revision 1.3 1999/07/16 00:19:02 sjboddie
43 some changes to the way quoted queries are handled
44
45 Revision 1.2 1999/07/07 06:12:21 rjmcnab
46 Added ability to combine two independant queries.
47
48 Revision 1.1 1999/06/15 02:24:06 sjboddie
49 moved do_query from queryaction to new querytools module (as do_action
50 is also called from documentaction when highlighting text)
51
52 */
53
54
55#include "querytools.h"
56
57
58// do_query sets the filter options and makes the protocol call to
59// do a query. The results are returned in response.
60// request.filterResultOptions and request.fields (if required)
61// should be set by the calling function.
62
63bool do_query (FilterRequest_t &request, cgiargsclass &args,
64 recptproto *collectproto, FilterResponse_t &response,
65 ostream &logout) {
66
67 request.filterName = "QueryFilter";
68
69 comerror_t err;
70 OptionValue_t option;
71 text_t formattedstring = args["q"];
72 format_querystring (formattedstring, args.getintarg("b"));
73
74 option.name = "Term";
75 option.value = formattedstring;
76 request.filterOptions.push_back (option);
77
78 option.name = "QueryType";
79 option.value = (args.getintarg("t")) ? "ranked" : "boolean";
80 request.filterOptions.push_back (option);
81
82 option.name = "Casefold";
83 option.value = (args.getintarg("k")) ? "true" : "false";
84 request.filterOptions.push_back (option);
85
86 option.name = "Stem";
87 option.value = (args.getintarg("s")) ? "true" : "false";
88 request.filterOptions.push_back (option);
89
90 if (!args["h"].empty()) {
91 option.name = "Index";
92 option.value = args["h"];
93 request.filterOptions.push_back (option);
94 }
95
96 if (!args["j"].empty()) {
97 option.name = "Subcollection";
98 option.value = args["j"];
99 request.filterOptions.push_back (option);
100 }
101
102 if (!args["n"].empty()) {
103 option.name = "Language";
104 option.value = args["n"];
105 request.filterOptions.push_back (option);
106 }
107
108 // fill in the second query if needed
109 if (!args["cq2"].empty()) {
110 option.name = "CombineQuery";
111 option.value = args["cq2"];
112 request.filterOptions.push_back (option);
113
114 text_t formattedstring2 = args["q2"];
115 format_querystring (formattedstring2, args.getintarg("b"));
116
117 option.name = "Term";
118 option.value = formattedstring2;
119 request.filterOptions.push_back (option);
120
121 option.name = "QueryType";
122 option.value = (args.getintarg("t")) ? "ranked" : "boolean";
123 request.filterOptions.push_back (option);
124
125 option.name = "Casefold";
126 option.value = (args.getintarg("k")) ? "true" : "false";
127 request.filterOptions.push_back (option);
128
129 option.name = "Stem";
130 option.value = (args.getintarg("s")) ? "true" : "false";
131 request.filterOptions.push_back (option);
132
133 if (!args["h2"].empty()) {
134 option.name = "Index";
135 option.value = args["h2"];
136 request.filterOptions.push_back (option);
137 }
138
139 if (!args["j2"].empty()) {
140 option.name = "Subcollection";
141 option.value = args["j2"];
142 request.filterOptions.push_back (option);
143 }
144
145 if (!args["n2"].empty()) {
146 option.name = "Language";
147 option.value = args["n2"];
148 request.filterOptions.push_back (option);
149 }
150 }
151
152 int arg_m = args.getintarg("m");
153
154 option.name = "Maxdocs";
155 option.value = arg_m;
156 request.filterOptions.push_back (option);
157
158 option.name = "StartResults";
159 option.value = args["r"];
160 request.filterOptions.push_back (option);
161
162 option.name = "EndResults";
163 int endresults = args.getintarg("o") + (args.getintarg("r") - 1);
164 if ((endresults > arg_m) && (arg_m != -1)) endresults = arg_m;
165 option.value = endresults;
166 request.filterOptions.push_back (option);
167
168 collectproto->filter (args["c"], request, response, err, logout);
169
170 if (err != noError) {
171 outconvertclass text_t2ascii;
172 logout << text_t2ascii
173 << "Error: call to QueryFilter failed in queryaction ("
174 << get_comerror_string (err) << ")\n";
175 return false;
176 }
177 return true;
178}
179
180void format_querystring (text_t &querystring, int querymode) {
181 text_t formattedstring;
182
183 text_t::const_iterator here = querystring.begin();
184 text_t::const_iterator end = querystring.end();
185
186 // space is used to insert spaces between Chinese
187 // characters. No space is needed before the first
188 // Chinese character.
189 bool space = false;
190
191 // want to remove ()|!& from querystring so boolean queries are just
192 // "all the words" queries (unless querymode is advanced)
193 while (here != end) {
194 if ((querymode == 0) && (*here == '(' || *here == ')' || *here == '|' ||
195 *here == '!' || *here == '&')) {
196 formattedstring.push_back(' ');
197 } else {
198 if ((*here >= 0x4e00 && *here <= 0x9fa5) ||
199 (*here >= 0xf900 && *here <= 0xfa2d)) {
200 // Chinese character
201 if (space) formattedstring.push_back (0x200b);
202 formattedstring.push_back (*here);
203 formattedstring.push_back (0x200b);
204 space = true;
205 } else {
206 // non-Chinese character
207 formattedstring.push_back (*here);
208 space = false;
209 }
210 }
211 here ++;
212 }
213 querystring = formattedstring;
214}
215
216void get_phrases (const text_t &querystring, text_tarray &phrases) {
217
218 phrases.erase (phrases.begin(), phrases.end());
219 if (!querystring.empty()) {
220
221 text_t::const_iterator end = querystring.end();
222 text_t::const_iterator here = findchar (querystring.begin(), end, '"');
223 if (here != end) {
224 text_t tmptext;
225 bool foundquote = false;
226 while (here != end) {
227 if (*here == '"') {
228 if (foundquote) {
229 if (!tmptext.empty()) {
230 phrases.push_back(tmptext);
231 tmptext.clear();
232 }
233 foundquote = false;
234 } else foundquote = true;
235 } else {
236 if (foundquote) tmptext.push_back (*here);
237 }
238 here ++;
239 }
240 }
241 }
242}
243
Note: See TracBrowser for help on using the repository browser.