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