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