root/trunk/cstr/src/recpt/cstrquerytools.cpp @ 891

Revision 891, 6.5 KB (checked in by sjboddie, 20 years ago)

Initial revision

  • Property svn:keywords set to Author Date Id Revision
Line 
1/**********************************************************************
2 *
3 * cstrquerytools.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$
25 *
26 *********************************************************************/
27
28/*
29   $Log$
30   Revision 1.1  2000/02/01 22:32:37  sjboddie
31   Initial revision
32
33   Revision 1.1  1999/09/07 21:49:44  sjboddie
34   new cstr receptionist
35
36
37 */
38
39
40// this is overridden for cstr as we're always going to get
41// maxdocs matches. They can then be sorted in the
42// cstrqueryaction
43
44#include "cstrquerytools.h"
45
46
47// do_query sets the filter options and makes the protocol call to
48// do a query. The results are returned in response.
49// request.filterResultOptions and request.fields (if required)
50// should be set by the calling function.
51
52bool do_query (FilterRequest_t &request, cgiargsclass &args,
53           recptproto *collectproto, FilterResponse_t &response,
54           ostream &logout) {
55
56  request.filterName = "QueryFilter";
57
58  comerror_t err;
59  OptionValue_t option;
60  text_t formattedstring = args["q"];
61  format_querystring (formattedstring, args.getintarg("b")); 
62 
63  option.name = "Term";
64  option.value = formattedstring;
65  request.filterOptions.push_back (option);
66
67  option.name = "QueryType";
68  option.value = (args.getintarg("t")) ? "ranked" : "boolean";
69  request.filterOptions.push_back (option);
70
71  option.name = "Casefold";
72  option.value = (args.getintarg("k")) ? "true" : "false";
73  request.filterOptions.push_back (option);
74
75  option.name = "Stem";
76  option.value = (args.getintarg("s")) ? "true" : "false";
77  request.filterOptions.push_back (option);
78
79  if (!args["h"].empty()) {
80    option.name = "Index";
81    option.value = args["h"];
82    request.filterOptions.push_back (option);
83  }
84
85  if (!args["j"].empty()) {
86    option.name = "Subcollection";
87    option.value = args["j"];
88    request.filterOptions.push_back (option);
89  }
90
91  if (!args["n"].empty()) {
92    option.name = "Language";
93    option.value = args["n"];
94    request.filterOptions.push_back (option);
95  }
96
97  // fill in the second query if needed
98  if (!args["cq2"].empty()) {
99    option.name = "CombineQuery";
100    option.value = args["cq2"];
101    request.filterOptions.push_back (option);
102   
103    text_t formattedstring2 = args["q2"];
104    format_querystring (formattedstring2, args.getintarg("b"));
105
106    option.name = "Term";
107    option.value = formattedstring2;
108    request.filterOptions.push_back (option);
109
110    option.name = "QueryType";
111    option.value = (args.getintarg("t")) ? "ranked" : "boolean";
112    request.filterOptions.push_back (option);
113
114    option.name = "Casefold";
115    option.value = (args.getintarg("k")) ? "true" : "false";
116    request.filterOptions.push_back (option);
117
118    option.name = "Stem";
119    option.value = (args.getintarg("s")) ? "true" : "false";
120    request.filterOptions.push_back (option);
121
122    if (!args["h2"].empty()) {
123      option.name = "Index";
124      option.value = args["h2"];
125      request.filterOptions.push_back (option);
126    }
127
128    if (!args["j2"].empty()) {
129      option.name = "Subcollection";
130      option.value = args["j2"];
131      request.filterOptions.push_back (option);
132    }
133
134    if (!args["n2"].empty()) {
135      option.name = "Language";
136      option.value = args["n2"];
137      request.filterOptions.push_back (option);
138    }
139  }
140
141  // we always want to retrieve all documents (i.e. 1 to maxdocs)
142  // for this collection
143  option.name = "Maxdocs";
144  option.value = args["m"];
145  request.filterOptions.push_back (option);
146 
147  option.name = "StartResults";
148  option.value = 1;
149  request.filterOptions.push_back (option);
150
151  option.name = "EndResults";
152  option.value = args["m"];
153  request.filterOptions.push_back (option);
154
155  collectproto->filter (args["c"], request, response, err, logout);
156
157  if (err != noError) {
158    outconvertclass text_t2ascii;
159    logout << text_t2ascii
160       << "Error: call to QueryFilter failed in queryaction ("
161       << get_comerror_string (err) << ")\n";
162    return false;
163  }
164  return true;
165}
166
167void format_querystring (text_t &querystring, int querymode) {
168  text_t formattedstring;
169
170  text_t::const_iterator here = querystring.begin();
171  text_t::const_iterator end = querystring.end();
172
173  // space is used to insert spaces between Chinese
174  // characters. No space is needed before the first
175  // Chinese character.
176  bool space = false;
177
178  // want to remove ()|!& from querystring so boolean queries are just
179  // "all the words" queries (unless querymode is advanced)
180  while (here != end) {
181    if ((querymode == 0) && (*here == '(' || *here == ')' || *here == '|' ||
182                 *here == '!' || *here == '&')) {
183      formattedstring.push_back(' ');
184    } else {
185      if ((*here >= 0x4e00 && *here <= 0x9fa5) ||
186      (*here >= 0xf900 && *here <= 0xfa2d)) {
187    // Chinese character
188    if (space) formattedstring.push_back (0x200b);
189    formattedstring.push_back (*here);
190    formattedstring.push_back (0x200b);
191    space = true;
192      } else {
193    // non-Chinese character
194    formattedstring.push_back (*here);
195    space = false;
196      }
197    }
198    here ++;
199  }
200  querystring = formattedstring;
201}
202
203void get_phrases (const text_t &querystring, text_tarray &phrases) {
204
205  phrases.erase (phrases.begin(), phrases.end());
206  if (!querystring.empty()) {
207   
208    text_t::const_iterator end = querystring.end();
209    text_t::const_iterator here = findchar (querystring.begin(), end, '"');
210    if (here != end) {
211      text_t tmptext;
212      bool foundquote = false;
213      while (here != end) {
214    if (*here == '"') {
215      if (foundquote) {
216        if (!tmptext.empty()) {
217          phrases.push_back(tmptext);
218          tmptext.clear();
219        }
220        foundquote = false;
221      } else foundquote = true;
222    } else {
223      if (foundquote) tmptext.push_back (*here);
224    }
225    here ++;
226      }
227    }
228  }
229}
230
Note: See TracBrowser for help on using the browser.