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 | *********************************************************************/
|
---|
25 |
|
---|
26 | #include "querytools.h"
|
---|
27 | #include <ctype.h>
|
---|
28 | #include "unitool.h" // for is_unicode_letdig
|
---|
29 |
|
---|
30 | // request.filterResultOptions and request.fields (if required) should
|
---|
31 | // be set from the calling code
|
---|
32 | void set_queryfilter_options (FilterRequest_t &request, const text_t &querystring,
|
---|
33 | cgiargsclass &args) {
|
---|
34 |
|
---|
35 | request.filterName = "QueryFilter";
|
---|
36 |
|
---|
37 | OptionValue_t option;
|
---|
38 |
|
---|
39 | option.name = "Term";
|
---|
40 | option.value = querystring;
|
---|
41 | request.filterOptions.push_back (option);
|
---|
42 |
|
---|
43 | option.name = "QueryType";
|
---|
44 | option.value = (args.getintarg("t")) ? "ranked" : "boolean";
|
---|
45 | request.filterOptions.push_back (option);
|
---|
46 |
|
---|
47 | option.name = "MatchMode";
|
---|
48 | option.value = (args.getintarg("t")) ? "some" : "all";
|
---|
49 | request.filterOptions.push_back (option);
|
---|
50 |
|
---|
51 | option.name = "Casefold";
|
---|
52 | option.value = (args.getintarg("k")) ? "true" : "false";
|
---|
53 | request.filterOptions.push_back (option);
|
---|
54 |
|
---|
55 | option.name = "Stem";
|
---|
56 | option.value = (args.getintarg("s")) ? "true" : "false";
|
---|
57 | request.filterOptions.push_back (option);
|
---|
58 |
|
---|
59 | if (!args["h"].empty()) {
|
---|
60 | option.name = "Index";
|
---|
61 | option.value = args["h"];
|
---|
62 | request.filterOptions.push_back (option);
|
---|
63 | }
|
---|
64 |
|
---|
65 | if (!args["j"].empty()) {
|
---|
66 | option.name = "Subcollection";
|
---|
67 | option.value = args["j"];
|
---|
68 | request.filterOptions.push_back (option);
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (!args["n"].empty()) {
|
---|
72 | option.name = "Language";
|
---|
73 | option.value = args["n"];
|
---|
74 | request.filterOptions.push_back (option);
|
---|
75 | }
|
---|
76 |
|
---|
77 | if (!args["g"].empty()) { // granularity for mgpp
|
---|
78 | option.name = "Level";
|
---|
79 | option.value = args["g"];
|
---|
80 | request.filterOptions.push_back (option);
|
---|
81 | }
|
---|
82 |
|
---|
83 | set_more_queryfilter_options (request, args);
|
---|
84 | }
|
---|
85 |
|
---|
86 | void set_queryfilter_options (FilterRequest_t &request, const text_t &querystring1,
|
---|
87 | const text_t &querystring2, cgiargsclass &args) {
|
---|
88 |
|
---|
89 | set_queryfilter_options (request, querystring1, args);
|
---|
90 |
|
---|
91 | // fill in the second query if needed
|
---|
92 | if (!args["cq2"].empty()) {
|
---|
93 | OptionValue_t option;
|
---|
94 |
|
---|
95 | option.name = "CombineQuery";
|
---|
96 | option.value = args["cq2"];
|
---|
97 | request.filterOptions.push_back (option);
|
---|
98 |
|
---|
99 | option.name = "Term";
|
---|
100 | option.value = querystring2;
|
---|
101 | request.filterOptions.push_back (option);
|
---|
102 |
|
---|
103 | option.name = "QueryType";
|
---|
104 | option.value = (args.getintarg("t")) ? "ranked" : "boolean";
|
---|
105 | request.filterOptions.push_back (option);
|
---|
106 |
|
---|
107 | option.name = "Casefold";
|
---|
108 | option.value = (args.getintarg("k")) ? "true" : "false";
|
---|
109 | request.filterOptions.push_back (option);
|
---|
110 |
|
---|
111 | option.name = "Stem";
|
---|
112 | option.value = (args.getintarg("s")) ? "true" : "false";
|
---|
113 | request.filterOptions.push_back (option);
|
---|
114 |
|
---|
115 | if (!args["h2"].empty()) {
|
---|
116 | option.name = "Index";
|
---|
117 | option.value = args["h2"];
|
---|
118 | request.filterOptions.push_back (option);
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (!args["j2"].empty()) {
|
---|
122 | option.name = "Subcollection";
|
---|
123 | option.value = args["j2"];
|
---|
124 | request.filterOptions.push_back (option);
|
---|
125 | }
|
---|
126 |
|
---|
127 | if (!args["n2"].empty()) {
|
---|
128 | option.name = "Language";
|
---|
129 | option.value = args["n2"];
|
---|
130 | request.filterOptions.push_back (option);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | set_more_queryfilter_options (request, args);
|
---|
134 | }
|
---|
135 |
|
---|
136 | void set_more_queryfilter_options (FilterRequest_t &request, cgiargsclass &args) {
|
---|
137 |
|
---|
138 | OptionValue_t option;
|
---|
139 | int arg_m = args.getintarg("m");
|
---|
140 |
|
---|
141 | option.name = "Maxdocs";
|
---|
142 | option.value = arg_m;
|
---|
143 | request.filterOptions.push_back (option);
|
---|
144 |
|
---|
145 | // option.name = "StartResults";
|
---|
146 | // option.value = args["r"];
|
---|
147 | // request.filterOptions.push_back (option);
|
---|
148 |
|
---|
149 | // option.name = "EndResults";
|
---|
150 | // int endresults = args.getintarg("o") + (args.getintarg("r") - 1);
|
---|
151 | // if ((endresults > arg_m) && (arg_m != -1)) endresults = arg_m;
|
---|
152 | // option.value = endresults;
|
---|
153 | // request.filterOptions.push_back (option);
|
---|
154 | }
|
---|
155 |
|
---|
156 | void format_querystring (text_t &querystring, int querymode, bool segment) {
|
---|
157 | text_t formattedstring;
|
---|
158 |
|
---|
159 | if (querymode == 1 && !segment) return;
|
---|
160 |
|
---|
161 | text_t::const_iterator here = querystring.begin();
|
---|
162 | text_t::const_iterator end = querystring.end();
|
---|
163 |
|
---|
164 | // space is used to insert spaces between Chinese
|
---|
165 | // characters. No space is needed before the first
|
---|
166 | // Chinese character.
|
---|
167 | bool space = false;
|
---|
168 |
|
---|
169 | // want to remove ()|!& from querystring so boolean queries are just
|
---|
170 | // "all the words" queries (unless querymode is advanced)
|
---|
171 | while (here != end) {
|
---|
172 | if ((querymode == 0) && (*here == '(' || *here == ')' || *here == '|' ||
|
---|
173 | *here == '!' || *here == '&')) {
|
---|
174 | formattedstring.push_back(' ');
|
---|
175 | } else if (segment) {
|
---|
176 | if ((*here >= 0x4e00 && *here <= 0x9fa5) ||
|
---|
177 | (*here >= 0xf900 && *here <= 0xfa2d)) {
|
---|
178 | // Chinese character
|
---|
179 | if (!space) formattedstring.push_back (0x200b); // zero width space
|
---|
180 | formattedstring.push_back (*here);
|
---|
181 | formattedstring.push_back (0x200b);
|
---|
182 | space = true;
|
---|
183 | } else {
|
---|
184 |
|
---|
185 | // non-Chinese character
|
---|
186 | formattedstring.push_back (*here);
|
---|
187 | space = false;
|
---|
188 |
|
---|
189 | }
|
---|
190 |
|
---|
191 | } else {
|
---|
192 | formattedstring.push_back (*here);
|
---|
193 | }
|
---|
194 | ++here;
|
---|
195 | }
|
---|
196 | querystring = formattedstring;
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 |
|
---|
201 | void add_dates(text_t &querystring, int startdate, int enddate,
|
---|
202 | int startbc, int endbc, int ct)
|
---|
203 | {
|
---|
204 | if(startdate)
|
---|
205 | {
|
---|
206 | int querystringis = 0;
|
---|
207 | text_t::const_iterator here = querystring.begin();
|
---|
208 | text_t::const_iterator end = querystring.end();
|
---|
209 | while(here!=end)
|
---|
210 | {
|
---|
211 | if(!(isspace((*here)))){
|
---|
212 | here = end;
|
---|
213 | querystringis = 1;
|
---|
214 | }
|
---|
215 | else
|
---|
216 | ++here;
|
---|
217 | }
|
---|
218 | //converting BCE dates
|
---|
219 | if(startbc && startdate > 0)
|
---|
220 | {
|
---|
221 | startdate *= -1;
|
---|
222 | }
|
---|
223 | if(endbc && enddate > 0)
|
---|
224 | {
|
---|
225 | enddate *= -1;
|
---|
226 | }
|
---|
227 | if(enddate != 0 && enddate<startdate)
|
---|
228 | {
|
---|
229 | cout<<"enddate too small"<<endl;
|
---|
230 | return;
|
---|
231 | }
|
---|
232 | if(querystringis)
|
---|
233 | querystring.appendcstr(" AND");
|
---|
234 | if(!enddate)
|
---|
235 | {
|
---|
236 | if (ct==1) {
|
---|
237 | mgpp_adddateelem(querystring,startdate);
|
---|
238 | }
|
---|
239 | else { // lucene
|
---|
240 | lucene_adddateelem(querystring,startdate);
|
---|
241 | }
|
---|
242 | }
|
---|
243 | else{
|
---|
244 | int nextdate = startdate;
|
---|
245 | querystring.appendcstr(" (");
|
---|
246 | while(nextdate<=enddate)
|
---|
247 | {
|
---|
248 | if(nextdate!=0) {
|
---|
249 | if (ct==1) {
|
---|
250 | mgpp_adddateelem(querystring,nextdate);
|
---|
251 | }
|
---|
252 | else { // lucene
|
---|
253 | lucene_adddateelem(querystring,nextdate);
|
---|
254 | }
|
---|
255 | }
|
---|
256 | ++nextdate;
|
---|
257 | }
|
---|
258 | querystring.appendcstr(" )");
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | }
|
---|
263 |
|
---|
264 | // search history tool
|
---|
265 | // also used for form query macros
|
---|
266 | text_t escape_quotes(const text_t &querystring) {
|
---|
267 |
|
---|
268 | text_t::const_iterator here = querystring.begin();
|
---|
269 | text_t::const_iterator end = querystring.end();
|
---|
270 |
|
---|
271 | text_t escquery = "";
|
---|
272 | while (here != end) {
|
---|
273 | if (*here != '\'' && *here != '\"' && *here != '\n' && *here != '\r') escquery.push_back(*here);
|
---|
274 | else if (*here == '\n' || *here == '\r') {
|
---|
275 | escquery.push_back(' ');
|
---|
276 | } else {
|
---|
277 | escquery +="\\\\";
|
---|
278 | escquery.push_back(*here);
|
---|
279 | }
|
---|
280 |
|
---|
281 | ++here;
|
---|
282 | }
|
---|
283 | return escquery;
|
---|
284 |
|
---|
285 | }
|
---|
286 |
|
---|
287 | // some query form parsing functions for use with mgpp
|
---|
288 |
|
---|
289 | void parse_reg_query_form(text_t &querystring, cgiargsclass &args)
|
---|
290 | {
|
---|
291 | querystring.clear();
|
---|
292 |
|
---|
293 | const int ct = args.getintarg("ct");
|
---|
294 | int argt = args.getintarg("t");// t=0 -and, t=1 - or
|
---|
295 |
|
---|
296 | text_t combine;
|
---|
297 | if (ct==1) {
|
---|
298 | if (argt == 0) combine = "&";
|
---|
299 | else combine = "|";
|
---|
300 | }
|
---|
301 | else { // lucene
|
---|
302 | if (argt == 0) combine = "AND";
|
---|
303 | else combine = "OR";
|
---|
304 | }
|
---|
305 |
|
---|
306 | text_t field = args["fqf"];
|
---|
307 | if (field.empty()) return; // no query
|
---|
308 | text_tarray fields;
|
---|
309 | splitchar(field.begin(), field.end(), ',', fields);
|
---|
310 |
|
---|
311 | text_t value = args["fqv"];
|
---|
312 | if (value.empty()) return; // somethings wrong
|
---|
313 | text_tarray values;
|
---|
314 | splitchar(value.begin(), value.end(), ',', values);
|
---|
315 |
|
---|
316 |
|
---|
317 | for (int i=0; i< values.size(); ++i) {
|
---|
318 | if (!values[i].empty()) {
|
---|
319 | if (ct == 1) {
|
---|
320 | mgpp_addqueryelem(querystring, fields[i], values[i], combine);
|
---|
321 | }
|
---|
322 | else { // lucene
|
---|
323 | lucene_addqueryelem(querystring, fields[i], values[i], combine);
|
---|
324 | }
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | }
|
---|
329 |
|
---|
330 |
|
---|
331 | void parse_adv_query_form(text_t &querystring, cgiargsclass &args){
|
---|
332 |
|
---|
333 | querystring.clear();
|
---|
334 |
|
---|
335 | const int ct = args.getintarg("ct");
|
---|
336 | text_t combine;
|
---|
337 | if (ct==1) {
|
---|
338 | combine = "&";
|
---|
339 | }
|
---|
340 | else { // lucene
|
---|
341 | combine = "AND";
|
---|
342 | }
|
---|
343 |
|
---|
344 | text_t field = args["fqf"];
|
---|
345 | if (field.empty()) return; // no query
|
---|
346 | text_tarray fields;
|
---|
347 | splitchar(field.begin(), field.end(), ',', fields);
|
---|
348 |
|
---|
349 | text_t value = args["fqv"];
|
---|
350 | if (value.empty()) return; // somethings wrong
|
---|
351 | text_tarray values;
|
---|
352 | splitchar(value.begin(), value.end(), ',', values);
|
---|
353 |
|
---|
354 | text_t stem = args["fqs"];
|
---|
355 | if (stem.empty()) return; // somethings wrong
|
---|
356 | text_tarray stems;
|
---|
357 | splitchar(stem.begin(), stem.end(), ',', stems);
|
---|
358 |
|
---|
359 | text_t fold = args["fqk"];
|
---|
360 | if (fold.empty()) return; // somethings wrong
|
---|
361 | text_tarray folds;
|
---|
362 | splitchar(fold.begin(), fold.end(), ',', folds);
|
---|
363 |
|
---|
364 | text_t comb = args["fqc"];
|
---|
365 | if (comb.empty()) return; //somethings wrong
|
---|
366 | text_tarray combs;
|
---|
367 | splitchar(comb.begin(), comb.end(), ',', combs);
|
---|
368 |
|
---|
369 | for(int i=0; i< values.size(); ++i) {
|
---|
370 | if (!values[i].empty()) {
|
---|
371 | if (i!=0) {
|
---|
372 | if (ct==1) {
|
---|
373 | if (combs[i-1]=="and") combine = "&";
|
---|
374 | else if (combs[i-1]=="or")combine = "|";
|
---|
375 | else if (combs[i-1]=="not")combine = "!";
|
---|
376 | }
|
---|
377 | else { // lucene
|
---|
378 | if (combs[i-1]=="and") combine = "AND";
|
---|
379 | else if (combs[i-1]=="or")combine = "OR";
|
---|
380 | else if (combs[i-1]=="not")combine = "NOT";
|
---|
381 | }
|
---|
382 | }
|
---|
383 | text_t term = addstemcase(values[i], stems[i], folds[i]);
|
---|
384 | mgpp_addqueryelem(querystring, fields[i], term, combine);
|
---|
385 | }
|
---|
386 |
|
---|
387 | }
|
---|
388 | }
|
---|
389 |
|
---|
390 | text_t addstemcase(const text_t &terms, const text_t &stem, const text_t &fold) {
|
---|
391 |
|
---|
392 | text_t outtext;
|
---|
393 | text_t word;
|
---|
394 | //unsigned short c;
|
---|
395 | text_t::const_iterator here = terms.begin();
|
---|
396 | text_t::const_iterator end = terms.end();
|
---|
397 |
|
---|
398 | while (here !=end) {
|
---|
399 |
|
---|
400 | if (is_unicode_letdig(*here)) {
|
---|
401 | // not word boundary
|
---|
402 | word.push_back(*here);
|
---|
403 | ++here;
|
---|
404 | }
|
---|
405 | else {
|
---|
406 | // found word boundary
|
---|
407 | if (!word.empty() ) {
|
---|
408 | if (stem == "1" || fold =="1") {
|
---|
409 | word += "#";
|
---|
410 | if (stem == "1") word += "s";
|
---|
411 | //else word += "u";
|
---|
412 |
|
---|
413 | if (fold == "1") word += "i";
|
---|
414 | //else word += "c";
|
---|
415 | }
|
---|
416 |
|
---|
417 | word += " ";
|
---|
418 | outtext += word;
|
---|
419 | word.clear();
|
---|
420 | }
|
---|
421 | if (*here == '\"') {
|
---|
422 | outtext.push_back(*here);
|
---|
423 | }
|
---|
424 | ++here;
|
---|
425 | }
|
---|
426 | }
|
---|
427 |
|
---|
428 | // get last word
|
---|
429 | if (!word.empty()) {
|
---|
430 | if (stem == "1"|| fold == "1") {
|
---|
431 | word += "#";
|
---|
432 | if (stem == "1") word += "s";
|
---|
433 | //else word += "u";
|
---|
434 |
|
---|
435 | if (fold == "1") word += "i";
|
---|
436 | //else word += "c";
|
---|
437 | }
|
---|
438 | word += " ";
|
---|
439 | outtext += word;
|
---|
440 | }
|
---|
441 | return outtext;
|
---|
442 | }
|
---|
443 |
|
---|
444 |
|
---|
445 | void mgpp_adddateelem(text_t& querystring, const int date)
|
---|
446 | {
|
---|
447 | querystring.appendcstr(" [");
|
---|
448 | if(date<0) {
|
---|
449 | querystring.appendcstr("bc");
|
---|
450 | querystring.appendint((date*-1));
|
---|
451 | }
|
---|
452 | else {
|
---|
453 | querystring.appendint(date);
|
---|
454 | }
|
---|
455 | querystring.appendcstr("]:CV");
|
---|
456 | }
|
---|
457 |
|
---|
458 | void lucene_adddateelem(text_t& querystring, const int date)
|
---|
459 | {
|
---|
460 | querystring.appendcstr(" CV:(");
|
---|
461 | if(date<0) {
|
---|
462 | querystring.appendcstr("bc");
|
---|
463 | querystring.appendint((date*-1));
|
---|
464 | }
|
---|
465 | else {
|
---|
466 | querystring.appendint(date);
|
---|
467 | }
|
---|
468 | querystring.appendcstr(")");
|
---|
469 | }
|
---|
470 |
|
---|
471 |
|
---|
472 | void mgpp_addqueryelem(text_t &querystring, text_t &tag,
|
---|
473 | text_t &query, text_t &combine) {
|
---|
474 | if (!querystring.empty()) { // have to put and/or
|
---|
475 | querystring += " " + combine + " ";
|
---|
476 |
|
---|
477 | }
|
---|
478 | if (tag=="ZZ" || tag=="") { // just add onto querystring
|
---|
479 | querystring += query;
|
---|
480 | }
|
---|
481 | else {
|
---|
482 | querystring += "["+query+"]:"+tag;
|
---|
483 | }
|
---|
484 |
|
---|
485 | }
|
---|
486 |
|
---|
487 | void lucene_addqueryelem(text_t &querystring, text_t &tag,
|
---|
488 | text_t &query, text_t &combine) {
|
---|
489 | if (!querystring.empty()) { // have to put and/or
|
---|
490 | querystring += " " + combine + " ";
|
---|
491 |
|
---|
492 | }
|
---|
493 | if (tag=="ZZ" || tag=="") { // just add onto querystring
|
---|
494 | querystring += query;
|
---|
495 | }
|
---|
496 | else {
|
---|
497 | querystring += tag+":("+query+")";
|
---|
498 | }
|
---|
499 | }
|
---|
500 |
|
---|
501 |
|
---|
502 | void addqueryelem_ex(text_t &querystring, const text_t &tag,
|
---|
503 | const text_t &terms, const text_t &stem, const text_t &fold,
|
---|
504 | const text_t& combine, const text_t& word_combine) {
|
---|
505 | if (!querystring.empty()) { // have to put and/or
|
---|
506 | querystring += " " + combine + " ";
|
---|
507 | }
|
---|
508 | text_t outtext; outtext.reserve(512);
|
---|
509 | text_t word; word.reserve(100);
|
---|
510 | //unsigned short c;
|
---|
511 | text_t::const_iterator here = terms.begin();
|
---|
512 | text_t::const_iterator end = terms.end();
|
---|
513 | bool inquote = false, firstword = true;
|
---|
514 |
|
---|
515 | text_t word2; word2.reserve(256);
|
---|
516 |
|
---|
517 | while (here !=end) {
|
---|
518 | if (is_unicode_space(*here)) {
|
---|
519 | if (word2 == "AND") { word2.clear(); word2.push_back(7527); word2.appendcarr("AND", 3); word2.push_back(7527); }
|
---|
520 | else if (word2 == "OR") { word2.clear(); word2.push_back(7527); word2.appendcarr("OR", 2); word2.push_back(7527); }
|
---|
521 | else if (word2 == "NOT") { word2.clear(); word2.push_back(7527); word2.appendcarr("NOT", 3); word2.push_back(7527); }
|
---|
522 | else if (word2 == "NEAR") { word2.clear(); word2.push_back(7527); word2.appendcarr("NEAR", 4); word2.push_back(7527); }
|
---|
523 | else if (word2 == "WITHIN") { word2.clear(); word2.push_back(7527); word2.appendcarr("WITHIN", 6); word2.push_back(7527); }
|
---|
524 | if (inquote) {
|
---|
525 | word2.push_back(*here);
|
---|
526 | }
|
---|
527 | word.append(word2); word2.clear();
|
---|
528 |
|
---|
529 | if (!inquote && !word.empty() ) {
|
---|
530 | // found word boundary
|
---|
531 |
|
---|
532 | if (stem == "1" || fold =="1") {
|
---|
533 | word += "#";
|
---|
534 | if (stem == "1") word += "s";
|
---|
535 | //else word += "u";
|
---|
536 |
|
---|
537 | if (fold == "1") word += "i";
|
---|
538 | //else word += "c";
|
---|
539 | }
|
---|
540 | if (firstword) {
|
---|
541 | firstword = false;
|
---|
542 | } else {
|
---|
543 | outtext += " " + word_combine + " ";
|
---|
544 | }
|
---|
545 | outtext += "[" + word + "]:"+tag;
|
---|
546 | word.clear();
|
---|
547 | }
|
---|
548 | ++here;
|
---|
549 | } else if (*here == '\"') {
|
---|
550 | word2.push_back(*here);
|
---|
551 | inquote = !inquote;
|
---|
552 | ++here;
|
---|
553 | } else {
|
---|
554 | // not word boundary
|
---|
555 | word2.push_back(*here);
|
---|
556 | ++here;
|
---|
557 | }
|
---|
558 | }
|
---|
559 |
|
---|
560 | // get last word
|
---|
561 | if (!word2.empty()) {
|
---|
562 | if (word2 == "AND") { word2.clear(); word2.push_back(7527); word2.appendcarr("AND", 3); word2.push_back(7527); }
|
---|
563 | else if (word2 == "OR") { word2.clear(); word2.push_back(7527); word2.appendcarr("OR", 2); word2.push_back(7527); }
|
---|
564 | else if (word2 == "NOT") { word2.clear(); word2.push_back(7527); word2.appendcarr("NOT", 3); word2.push_back(7527); }
|
---|
565 | else if (word2 == "NEAR") { word2.clear(); word2.push_back(7527); word2.appendcarr("NEAR", 4); word2.push_back(7527); }
|
---|
566 | else if (word2 == "WITHIN") { word2.clear(); word2.push_back(7527); word2.appendcarr("WITHIN", 6); word2.push_back(7527); }
|
---|
567 | word.append(word2); word2.clear();
|
---|
568 |
|
---|
569 | if (stem == "1"|| fold == "1") {
|
---|
570 | word += "#";
|
---|
571 | if (stem == "1") word += "s";
|
---|
572 | //else word += "u";
|
---|
573 |
|
---|
574 | if (fold == "1") word += "i";
|
---|
575 | //else word += "c";
|
---|
576 | }
|
---|
577 | if (!outtext.empty()) outtext += " " + word_combine + " ";
|
---|
578 | outtext += "[" + word + "]:"+tag;
|
---|
579 | }
|
---|
580 | querystring += "(" + outtext + ")";
|
---|
581 | }
|
---|
582 |
|
---|
583 |
|
---|
584 | void add_field_info(text_t &querystring, const text_t &tag, int type) {
|
---|
585 |
|
---|
586 | if (tag == "") return; // do nothing
|
---|
587 | if (type == 1) { //mgpp
|
---|
588 | querystring = "["+querystring+"]:"+tag;
|
---|
589 | } else if (type == 2) { // lucene
|
---|
590 | querystring = tag+":("+querystring+")";
|
---|
591 | }
|
---|
592 |
|
---|
593 | }
|
---|
594 |
|
---|
595 | bool is_special_character(int indexer_type, unsigned short character) {
|
---|
596 | // mgpp
|
---|
597 | if (indexer_type == 1) {
|
---|
598 | return (character == '#' || character == '/' || character == '*');
|
---|
599 | }
|
---|
600 | // lucene
|
---|
601 | else if (indexer_type ==2) {
|
---|
602 | return (character == '?' || character == '*' || character == '~' ||
|
---|
603 | character == '^');
|
---|
604 | }
|
---|
605 | return false;
|
---|
606 | }
|
---|
607 |
|
---|
608 | void format_field_info(text_t &querystring, cgiargsclass &args) {
|
---|
609 |
|
---|
610 | text_t tag = args["fqf"];
|
---|
611 | if (tag == "ZZ") tag = ""; // ZZ is a special tag meaning no tag (all fields)
|
---|
612 |
|
---|
613 | int argct = args.getintarg("ct");
|
---|
614 | bool mgpp = (argct == 1);
|
---|
615 | bool lucene = (argct == 2);
|
---|
616 |
|
---|
617 | if (mgpp && tag == "") {
|
---|
618 | return; // no field specifier: do nothing
|
---|
619 | }
|
---|
620 |
|
---|
621 | int argt = args.getintarg("t");// t=0 -and, t=1 - or
|
---|
622 | int argb = args.getintarg("b"); // b=0 simple, b=1 advanced
|
---|
623 |
|
---|
624 | bool simple_AND_search = (argb==0 && argt==0);
|
---|
625 | bool simple_OR_search = (argb==0 && argt==1);
|
---|
626 |
|
---|
627 | if (mgpp && simple_AND_search) {
|
---|
628 | // mgpp, simple AND search, tag the whole query string
|
---|
629 | add_field_info(querystring, tag, argct);
|
---|
630 | return;
|
---|
631 | }
|
---|
632 | // resulting mgpp case - we need to tag each individual term or phrase
|
---|
633 | // TODO - allow AND. OR in query string and don't tag these words
|
---|
634 |
|
---|
635 | if (lucene && (simple_OR_search || argb == 1)) {
|
---|
636 | // OR search or advanced search (here we assume that the user has added their term mods - don't need to add term mods
|
---|
637 | if (tag != "") {
|
---|
638 | // tag the whole string
|
---|
639 | add_field_info(querystring, tag, argct);
|
---|
640 | }
|
---|
641 | return;
|
---|
642 | }
|
---|
643 |
|
---|
644 |
|
---|
645 | // if we have got here, we need to add in combiners (lucene) or
|
---|
646 | // we need to tag each individual word (mgpp OR search - mgpp can't do OR inside a field)
|
---|
647 |
|
---|
648 | text_t combine = ((lucene)? "+" : "");
|
---|
649 |
|
---|
650 | text_t processed_querystring = "";
|
---|
651 | text_t queryelement = "";
|
---|
652 |
|
---|
653 | bool in_phrase = false;
|
---|
654 | text_t::const_iterator here = querystring.begin();
|
---|
655 | text_t::const_iterator end = querystring.end();
|
---|
656 | while (here != end) {
|
---|
657 | if (is_unicode_letdig(*here) || is_special_character(argct, *here)) {
|
---|
658 | queryelement.push_back(*here);
|
---|
659 | }
|
---|
660 |
|
---|
661 | // Detect phrase starts/finishes
|
---|
662 | else if (*here == '"') {
|
---|
663 | queryelement.push_back(*here);
|
---|
664 | if (in_phrase == false) in_phrase = true;
|
---|
665 | else {
|
---|
666 | if (mgpp) {add_field_info(queryelement, tag, argct);}
|
---|
667 | processed_querystring += combine + queryelement;
|
---|
668 | queryelement.clear();
|
---|
669 | in_phrase = false;
|
---|
670 | }
|
---|
671 | }
|
---|
672 |
|
---|
673 | // Found word boundary, in a phrase
|
---|
674 | else if (in_phrase) {
|
---|
675 | queryelement.push_back(*here);
|
---|
676 | }
|
---|
677 | // Word boundary, but not in a phrase
|
---|
678 | else {
|
---|
679 | if (!queryelement.empty()) {
|
---|
680 | if (mgpp) {add_field_info(queryelement, tag, argct);}
|
---|
681 | processed_querystring += combine + queryelement;
|
---|
682 | queryelement.clear();
|
---|
683 | }
|
---|
684 | processed_querystring.push_back(*here);
|
---|
685 | }
|
---|
686 |
|
---|
687 | ++here;
|
---|
688 | }
|
---|
689 |
|
---|
690 | // Get last element
|
---|
691 | if (!queryelement.empty()) {
|
---|
692 | if (mgpp) {add_field_info(queryelement, tag, argct);}
|
---|
693 | processed_querystring += combine + queryelement;
|
---|
694 | }
|
---|
695 |
|
---|
696 | querystring = processed_querystring;
|
---|
697 |
|
---|
698 | if (lucene) {
|
---|
699 | // tag the whole query string
|
---|
700 | add_field_info(querystring, tag, argct);
|
---|
701 | }
|
---|
702 | }
|
---|
703 |
|
---|
704 |
|
---|