1 | /*
|
---|
2 | * GS2MGPPSearch.java
|
---|
3 | * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 | package org.greenstone.gsdl3.service;
|
---|
20 |
|
---|
21 |
|
---|
22 | // Greenstone classes
|
---|
23 | import org.greenstone.mgpp.*;
|
---|
24 | import org.greenstone.gsdl3.util.*;
|
---|
25 |
|
---|
26 | // XML classes
|
---|
27 | import org.w3c.dom.Element;
|
---|
28 | import org.w3c.dom.Node;
|
---|
29 | import org.w3c.dom.NodeList;
|
---|
30 |
|
---|
31 | // General Java classes
|
---|
32 | import java.io.File;
|
---|
33 | import java.util.HashMap;
|
---|
34 | import java.util.Vector;
|
---|
35 | import java.util.Set;
|
---|
36 | import java.util.Map;
|
---|
37 | import java.util.Iterator;
|
---|
38 |
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * A ServiceRack class for searching in greenstone 2 MGPP collections
|
---|
42 | *
|
---|
43 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
44 | * @version $Revision: 4012 $
|
---|
45 | * @see ServiceRack
|
---|
46 | */
|
---|
47 | public class GS2MGPPSearch
|
---|
48 | extends GS2Search {
|
---|
49 |
|
---|
50 | // extra services offered by mgpp collections
|
---|
51 | private static final String FIELD_QUERY_SERVICE = "FieldQuery";
|
---|
52 | private static final String ADVANCED_FIELD_QUERY_SERVICE = "AdvancedFieldQuery";
|
---|
53 |
|
---|
54 | // extra parameters used
|
---|
55 | private static final String LEVEL_PARAM = "level";
|
---|
56 | private static final String RANK_PARAM = "sortBy";
|
---|
57 | private static final String RANK_PARAM_RANK = "1";
|
---|
58 | private static final String RANK_PARAM_NONE = "0";
|
---|
59 | private static final String SIMPLE_FIELD_PARAM = "simpleField";
|
---|
60 | private static final String ADVANCED_FIELD_PARAM = "complexField";
|
---|
61 | // more params for field query
|
---|
62 | private static final String FIELD_QUERY_PARAM = "fqv";
|
---|
63 | private static final String FIELD_STEM_PARAM = "fqs";
|
---|
64 | private static final String FIELD_CASE_PARAM = "fqc";
|
---|
65 | private static final String FIELD_FIELD_PARAM = "fqf";
|
---|
66 | private static final String FIELD_COMBINE_PARAM = "fqk";
|
---|
67 | private static final String FIELD_COMBINE_PARAM_AND = "0";
|
---|
68 | private static final String FIELD_COMBINE_PARAM_OR = "1";
|
---|
69 | private static final String FIELD_COMBINE_PARAM_NOT = "2";
|
---|
70 |
|
---|
71 | private static final int TEXT_QUERY = 0;
|
---|
72 | private static final int SIMPLE_QUERY = 1;
|
---|
73 | private static final int ADVANCED_QUERY = 2;
|
---|
74 |
|
---|
75 | protected static final String FIELD_ATT = "field";
|
---|
76 | private MGPPWrapper mgpp_src_=null;
|
---|
77 |
|
---|
78 | private String default_level_=null;
|
---|
79 |
|
---|
80 | /** constructor */
|
---|
81 | public GS2MGPPSearch()
|
---|
82 | {
|
---|
83 | mgpp_src_ = new MGPPWrapper();
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | /** configure this service */
|
---|
88 | public boolean configure(Element info, Element extra_info)
|
---|
89 | {
|
---|
90 | // Do generic configuration
|
---|
91 | if (super.configure(info, extra_info) == false)
|
---|
92 | return false;
|
---|
93 |
|
---|
94 | // Do specific configuration
|
---|
95 | System.out.println("Configuring GS2MGPPSearch...");
|
---|
96 |
|
---|
97 | // Get the default level out of <defaultLevel> (buildConfig.xml)
|
---|
98 | Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_LEVEL_ELEM);
|
---|
99 | if (def != null) {
|
---|
100 | default_level_ = def.getAttribute(GSXML.NAME_ATT);
|
---|
101 | }
|
---|
102 | if (default_level_ == null || default_level_.equals("")) {
|
---|
103 | System.err.println("Error: default level not specified!");
|
---|
104 | return false;
|
---|
105 | }
|
---|
106 |
|
---|
107 | // the default level is also the level which gdbm is expecting
|
---|
108 | // this must not be overwritten
|
---|
109 | mgpp_src_.setReturnLevel(default_level_);
|
---|
110 | // return term info
|
---|
111 | mgpp_src_.setReturnTerms(true);
|
---|
112 | // set the default - this may be overwritten by query params
|
---|
113 | mgpp_src_.setQueryLevel(default_level_);
|
---|
114 |
|
---|
115 | // set up the extra services which are available for this collection
|
---|
116 | // check the config info - if there is no field list, then there is no fielded searching
|
---|
117 |
|
---|
118 | Element field_list = (Element) GSXML.getChildByTagName(info, GSXML.FIELD_ELEM+GSXML.LIST_MODIFIER);
|
---|
119 | if (field_list==null) {
|
---|
120 | // nothing more to do
|
---|
121 | return true;
|
---|
122 | }
|
---|
123 | // else set up the fielded query services
|
---|
124 |
|
---|
125 | // set up short_service_info_ - for now just has name and type
|
---|
126 | Element fq_service = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
127 | fq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
128 | fq_service.setAttribute(GSXML.NAME_ATT, FIELD_QUERY_SERVICE);
|
---|
129 | short_service_info_.appendChild(fq_service);
|
---|
130 |
|
---|
131 | Element afq_service = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
132 | afq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
133 | afq_service.setAttribute(GSXML.NAME_ATT, ADVANCED_FIELD_QUERY_SERVICE);
|
---|
134 | short_service_info_.appendChild(afq_service);
|
---|
135 |
|
---|
136 | // set up service_info_map_ has full details about each service
|
---|
137 | // incl params lists etc.
|
---|
138 | Element fq_service_full = (Element) fq_service.cloneNode(true);
|
---|
139 | Element fq_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
140 | createFieldQueryParamList(fq_param_list, false, null);
|
---|
141 | fq_service_full.appendChild(fq_param_list);
|
---|
142 | service_info_map_.put(FIELD_QUERY_SERVICE, fq_service_full);
|
---|
143 |
|
---|
144 | Element afq_service_full = (Element) afq_service.cloneNode(true);
|
---|
145 | Element afq_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
146 | createAdvancedFieldQueryParamList(afq_param_list, false, null);
|
---|
147 | afq_service_full.appendChild(afq_param_list);
|
---|
148 | service_info_map_.put(ADVANCED_FIELD_QUERY_SERVICE, afq_service_full);
|
---|
149 |
|
---|
150 |
|
---|
151 | //set up format info - the info is the same as that for text query
|
---|
152 | Element format_info = (Element)format_info_map_.get(TEXT_QUERY_SERVICE);
|
---|
153 | if (format_info != null) {
|
---|
154 | format_info_map_.put(FIELD_QUERY_SERVICE, format_info);
|
---|
155 | format_info_map_.put(ADVANCED_FIELD_QUERY_SERVICE, format_info);
|
---|
156 | }
|
---|
157 | return true;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | /** creates a new param element and adds it to the param list */
|
---|
162 | protected void createParameter(String name, Element param_list, boolean display, String lang) {
|
---|
163 | Element param=null;
|
---|
164 |
|
---|
165 | if (name.equals(LEVEL_PARAM)) {
|
---|
166 | // the level info - read from config file
|
---|
167 | Element level_list = (Element)GSXML.getChildByTagName(config_info_, LEVEL_ELEM+GSXML.LIST_MODIFIER);
|
---|
168 | NodeList levels = level_list.getElementsByTagName(LEVEL_ELEM);
|
---|
169 | int len = levels.getLength();
|
---|
170 | if (len > 1) { // add level param to list only if more than one index specified
|
---|
171 | String [] levs = new String[len];
|
---|
172 | String [] lev_names = new String[len];
|
---|
173 | for (int i=0; i<len; i++) {
|
---|
174 | levs[i] = ((Element)levels.item(i)).getAttribute(GSXML.NAME_ATT);
|
---|
175 | if (display) {
|
---|
176 | lev_names[i] = getTextString("level."+levs[i], lang);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | if (display) {
|
---|
180 | param = GSXML.createParameterDisplay(doc_, LEVEL_PARAM, getTextString("param."+LEVEL_PARAM, lang), levs, lev_names);
|
---|
181 | } else {
|
---|
182 | param = GSXML.createParameter(doc_, LEVEL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, default_level_, levs);
|
---|
183 | }
|
---|
184 | }
|
---|
185 | } else if (name.equals(RANK_PARAM)) {
|
---|
186 | String [] vals1 = {RANK_PARAM_RANK, RANK_PARAM_NONE };
|
---|
187 | if (display) {
|
---|
188 | String [] vals1_texts = { getTextString("param."+RANK_PARAM+"."+RANK_PARAM_RANK, lang), getTextString("param."+RANK_PARAM+"."+RANK_PARAM_NONE, lang)};
|
---|
189 |
|
---|
190 | param = GSXML.createParameterDisplay(doc_, RANK_PARAM, getTextString("param."+RANK_PARAM, lang), vals1, vals1_texts);
|
---|
191 | } else {
|
---|
192 | param = GSXML.createParameter(doc_, RANK_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, RANK_PARAM_RANK, vals1 );
|
---|
193 | }
|
---|
194 | } else if (name.equals(FIELD_QUERY_PARAM)) {
|
---|
195 | if (display) {
|
---|
196 | param = GSXML.createParameterDisplay(doc_, FIELD_QUERY_PARAM, getTextString("param."+FIELD_QUERY_PARAM, lang), null, null);
|
---|
197 | } else {
|
---|
198 | param = GSXML.createParameter(doc_, FIELD_QUERY_PARAM, GSXML.PARAM_TYPE_STRING, null, null);
|
---|
199 | }
|
---|
200 |
|
---|
201 | } else if (name.equals(FIELD_CASE_PARAM)) {
|
---|
202 | if (display) {
|
---|
203 | String[] bool_ops = {"0", "1"};
|
---|
204 | String[] bool_texts = {getTextString("param.boolean.off", lang),getTextString("param.boolean.on", lang)};
|
---|
205 | param = GSXML.createParameterDisplay(doc_, FIELD_CASE_PARAM, getTextString("param."+FIELD_CASE_PARAM, lang), bool_ops, bool_texts);
|
---|
206 |
|
---|
207 | } else {
|
---|
208 | param = GSXML.createParameter(doc_, FIELD_CASE_PARAM, GSXML.PARAM_TYPE_BOOLEAN, BOOLEAN_PARAM_ON, null);
|
---|
209 |
|
---|
210 | }
|
---|
211 | } else if (name.equals(FIELD_STEM_PARAM)) {
|
---|
212 | if (display) {
|
---|
213 | String[] bool_ops = {"0", "1"};
|
---|
214 | String[] bool_texts = {getTextString("param.boolean.off", lang),getTextString("param.boolean.on", lang)};
|
---|
215 | param = GSXML.createParameterDisplay(doc_, FIELD_STEM_PARAM, getTextString("param."+FIELD_STEM_PARAM, lang), bool_ops, bool_texts);
|
---|
216 | } else {
|
---|
217 | param = GSXML.createParameter(doc_, FIELD_STEM_PARAM, GSXML.PARAM_TYPE_BOOLEAN, BOOLEAN_PARAM_ON, null);
|
---|
218 |
|
---|
219 | }
|
---|
220 |
|
---|
221 | } else if (name.equals(FIELD_FIELD_PARAM)) {
|
---|
222 |
|
---|
223 | // the field list - read from config file
|
---|
224 | Element field_list = (Element)GSXML.getChildByTagName(config_info_, GSXML.FIELD_ELEM+GSXML.LIST_MODIFIER);
|
---|
225 | NodeList fields = field_list.getElementsByTagName(GSXML.FIELD_ELEM);
|
---|
226 | int len = fields.getLength();
|
---|
227 | String [] f_names = new String [len];
|
---|
228 | String [] f_texts = new String [len];
|
---|
229 |
|
---|
230 | for (int i=0; i< len;i++) {
|
---|
231 | f_names[i] = ((Element)fields.item(i)).getAttribute(GSXML.SHORTNAME_ATT);
|
---|
232 | if (display) {
|
---|
233 | // should these be changed to a text element based on lang?
|
---|
234 | // or is the name of a metadata element eg dc:Title its
|
---|
235 | // name in all langs
|
---|
236 | f_texts[i] = ((Element)fields.item(i)).getAttribute(GSXML.NAME_ATT);
|
---|
237 | }
|
---|
238 | }
|
---|
239 | if (display) {
|
---|
240 | param = GSXML.createParameterDisplay(doc_, FIELD_FIELD_PARAM, getTextString("param."+FIELD_FIELD_PARAM, lang), f_names, f_texts);
|
---|
241 |
|
---|
242 | } else {
|
---|
243 | param = GSXML.createParameter(doc_, FIELD_FIELD_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, "", f_names );
|
---|
244 |
|
---|
245 | }
|
---|
246 | } else if (name.equals(FIELD_COMBINE_PARAM)) {
|
---|
247 |
|
---|
248 | String []vals = {FIELD_COMBINE_PARAM_AND, FIELD_COMBINE_PARAM_OR, FIELD_COMBINE_PARAM_NOT};
|
---|
249 | if (display) {
|
---|
250 | String []val_texts = {getTextString("param."+FIELD_COMBINE_PARAM+"."+FIELD_COMBINE_PARAM_AND, lang), getTextString("param."+FIELD_COMBINE_PARAM+"."+FIELD_COMBINE_PARAM_OR, lang), getTextString("param."+FIELD_COMBINE_PARAM+"."+FIELD_COMBINE_PARAM_NOT, lang)};
|
---|
251 |
|
---|
252 | param = GSXML.createParameterDisplay(doc_, FIELD_COMBINE_PARAM, "", vals, val_texts);
|
---|
253 |
|
---|
254 | } else {
|
---|
255 | param = GSXML.createParameter(doc_, FIELD_COMBINE_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, FIELD_COMBINE_PARAM_AND, vals);
|
---|
256 | param.setAttribute(GSXML.PARAM_IGNORE_POS_ATT, "0");
|
---|
257 |
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | // add the param to the list
|
---|
262 | if (param !=null) {
|
---|
263 | param_list.appendChild(param);
|
---|
264 | }
|
---|
265 | else {
|
---|
266 | super.createParameter(name, param_list, display, lang);
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 |
|
---|
271 | /** this creates all teh params and appends them to param_list.
|
---|
272 | * if display=true it creates the text strings version
|
---|
273 | * otherwise it creates the description version
|
---|
274 | */
|
---|
275 | protected boolean createTextQueryParamList(Element param_list,
|
---|
276 | boolean display,
|
---|
277 | String lang)
|
---|
278 | {
|
---|
279 | // the order they are specified here is the order they appear on
|
---|
280 | // the query form
|
---|
281 | createParameter(INDEX_PARAM, param_list, display, lang);
|
---|
282 | createParameter(LEVEL_PARAM, param_list, display, lang);
|
---|
283 | createParameter(CASE_PARAM, param_list, display, lang);
|
---|
284 | createParameter(STEM_PARAM, param_list, display, lang);
|
---|
285 | createParameter(MATCH_PARAM, param_list, display, lang);
|
---|
286 | createParameter(RANK_PARAM, param_list, display, lang);
|
---|
287 | createParameter(MAXDOCS_PARAM, param_list, display, lang);
|
---|
288 | createParameter(QUERY_PARAM, param_list, display, lang);
|
---|
289 |
|
---|
290 | return true;
|
---|
291 | }
|
---|
292 |
|
---|
293 |
|
---|
294 | // for now, the field queries just use the default index.
|
---|
295 | // however, it is entirely possible to create two indexes with different
|
---|
296 | // metadata. then you run into problems with displaying the form - you
|
---|
297 | // need to use javascript to change teh field and maybe level lists
|
---|
298 | // depending on which index is selected, or have the index preselected
|
---|
299 | // before going to the query form
|
---|
300 |
|
---|
301 |
|
---|
302 | /** this creates all teh params and appends them to param_list.
|
---|
303 | * if display=true it creates the text strings version
|
---|
304 | * otherwise it creates the description version
|
---|
305 | */
|
---|
306 | protected boolean createFieldQueryParamList(Element param_list, boolean display,
|
---|
307 | String lang)
|
---|
308 | {
|
---|
309 | createParameter(LEVEL_PARAM, param_list, display, lang);
|
---|
310 | createParameter(MATCH_PARAM, param_list, display, lang);
|
---|
311 | createParameter(CASE_PARAM, param_list, display, lang);
|
---|
312 | createParameter(STEM_PARAM, param_list, display, lang);
|
---|
313 | createParameter(RANK_PARAM, param_list, display, lang);
|
---|
314 | createParameter(MAXDOCS_PARAM, param_list, display, lang);
|
---|
315 |
|
---|
316 | // create a multi param for the fields etc
|
---|
317 | // text box, field
|
---|
318 | Element multiparam = null;
|
---|
319 | Element param=null;
|
---|
320 | if (display) {
|
---|
321 | param = GSXML.createParameterDisplay(doc_, SIMPLE_FIELD_PARAM, "", null, null);
|
---|
322 | param_list.appendChild(param);
|
---|
323 | } else {
|
---|
324 | multiparam = GSXML.createParameter(doc_, SIMPLE_FIELD_PARAM, GSXML.PARAM_TYPE_MULTI, null, null);
|
---|
325 | multiparam.setAttribute("occurs", "4");
|
---|
326 | param_list.appendChild(multiparam);
|
---|
327 | }
|
---|
328 | // the components
|
---|
329 | if (display) {
|
---|
330 | createParameter(FIELD_QUERY_PARAM, param_list, display, lang);
|
---|
331 | createParameter(FIELD_FIELD_PARAM, param_list, display, lang);
|
---|
332 | } else {
|
---|
333 | createParameter(FIELD_QUERY_PARAM, multiparam, display, lang);
|
---|
334 | createParameter(FIELD_FIELD_PARAM, multiparam, display, lang);
|
---|
335 | }
|
---|
336 | return true;
|
---|
337 | }
|
---|
338 |
|
---|
339 | protected boolean createAdvancedFieldQueryParamList(Element param_list, boolean display,
|
---|
340 | String lang) {
|
---|
341 |
|
---|
342 | // first do index and level params
|
---|
343 | createParameter(LEVEL_PARAM, param_list, display, lang);
|
---|
344 | createParameter(RANK_PARAM, param_list, display, lang);
|
---|
345 | createParameter(MAXDOCS_PARAM, param_list, display, lang);
|
---|
346 |
|
---|
347 |
|
---|
348 | // create a multi param for the fields etc
|
---|
349 | // text box, stem, case, field
|
---|
350 |
|
---|
351 | Element multiparam = null;
|
---|
352 | Element param=null;
|
---|
353 |
|
---|
354 | if (display) {
|
---|
355 | param = GSXML.createParameterDisplay(doc_, ADVANCED_FIELD_PARAM, "", null, null);
|
---|
356 | param_list.appendChild(param);
|
---|
357 | } else {
|
---|
358 | multiparam = GSXML.createParameter(doc_, ADVANCED_FIELD_PARAM, GSXML.PARAM_TYPE_MULTI, null, null);
|
---|
359 | multiparam.setAttribute("occurs", "4");
|
---|
360 | param_list.appendChild(multiparam);
|
---|
361 | }
|
---|
362 |
|
---|
363 | if (display) {
|
---|
364 | createParameter(FIELD_COMBINE_PARAM, param_list, display, lang);
|
---|
365 | createParameter(FIELD_QUERY_PARAM, param_list, display, lang);
|
---|
366 | createParameter(FIELD_CASE_PARAM, param_list, display, lang);
|
---|
367 | createParameter(FIELD_STEM_PARAM, param_list, display, lang);
|
---|
368 | createParameter(FIELD_FIELD_PARAM, param_list, display, lang);
|
---|
369 | } else {
|
---|
370 | createParameter(FIELD_COMBINE_PARAM, multiparam, display, lang);
|
---|
371 | createParameter(FIELD_QUERY_PARAM, multiparam, display, lang);
|
---|
372 | createParameter(FIELD_CASE_PARAM, multiparam, display, lang);
|
---|
373 | createParameter(FIELD_STEM_PARAM, multiparam, display, lang);
|
---|
374 | createParameter(FIELD_FIELD_PARAM, multiparam, display, lang);
|
---|
375 | }
|
---|
376 |
|
---|
377 | return true;
|
---|
378 | }
|
---|
379 |
|
---|
380 |
|
---|
381 | /** creates a display element containing all the text strings needed to display
|
---|
382 | the service page, in the language specified */
|
---|
383 | protected Element createServiceDisplay(String service, String lang)
|
---|
384 | {
|
---|
385 | // Create a service display for the basic text query service
|
---|
386 | Element display = super.createServiceDisplay(service, lang);
|
---|
387 |
|
---|
388 | // Now add MGPP's special services
|
---|
389 | if (service.equals(FIELD_QUERY_SERVICE)) {
|
---|
390 | createFieldQueryParamList(display, true, lang);
|
---|
391 | }
|
---|
392 | if (service.equals(ADVANCED_FIELD_QUERY_SERVICE)) {
|
---|
393 | createAdvancedFieldQueryParamList(display, true, lang);
|
---|
394 | }
|
---|
395 |
|
---|
396 | return display;
|
---|
397 | }
|
---|
398 |
|
---|
399 | // the following three functions are needed so the base class can
|
---|
400 | // call the process+SERVICE_NAME methods
|
---|
401 | /** process a text query */
|
---|
402 | protected Element processTextQuery(Element request) {
|
---|
403 | return processAnyQuery(request, TEXT_QUERY);
|
---|
404 | }
|
---|
405 |
|
---|
406 | /** process a field query */
|
---|
407 | protected Element processFieldQuery(Element request) {
|
---|
408 | return processAnyQuery(request, SIMPLE_QUERY);
|
---|
409 | }
|
---|
410 |
|
---|
411 | /** process an advanced field query */
|
---|
412 | protected Element processAdvancedFieldQuery(Element request) {
|
---|
413 | return processAnyQuery(request, ADVANCED_QUERY);
|
---|
414 | }
|
---|
415 |
|
---|
416 | /** process a query */
|
---|
417 | protected Element processAnyQuery(Element request, int query_type)
|
---|
418 | {
|
---|
419 |
|
---|
420 | String service_name=null;
|
---|
421 | String empty_query_test_param=null;
|
---|
422 | // set up the type specific bits
|
---|
423 | switch (query_type) {
|
---|
424 | case TEXT_QUERY:
|
---|
425 | service_name = TEXT_QUERY_SERVICE;
|
---|
426 | empty_query_test_param = QUERY_PARAM;
|
---|
427 | break;
|
---|
428 | case SIMPLE_QUERY:
|
---|
429 | service_name = FIELD_QUERY_SERVICE;
|
---|
430 | empty_query_test_param = FIELD_QUERY_PARAM;
|
---|
431 | break;
|
---|
432 | case ADVANCED_QUERY:
|
---|
433 | service_name = ADVANCED_FIELD_QUERY_SERVICE;
|
---|
434 | empty_query_test_param = FIELD_QUERY_PARAM;
|
---|
435 | break;
|
---|
436 | default:
|
---|
437 | // should never get here
|
---|
438 | System.err.println("GS2MGPPSearch: wrong query type!!");
|
---|
439 | return null;
|
---|
440 | }
|
---|
441 |
|
---|
442 | // Create a new (empty) result message
|
---|
443 | Element result = doc_.createElement(GSXML.RESPONSE_ELEM);
|
---|
444 | result.setAttribute(GSXML.FROM_ATT, service_name);
|
---|
445 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
446 |
|
---|
447 | // Get the parameters of the request
|
---|
448 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
449 | if (param_list == null) {
|
---|
450 | System.err.println("Error: TextQuery request had no paramList.");
|
---|
451 | return result; // Return the empty result
|
---|
452 | }
|
---|
453 |
|
---|
454 | // Process the request parameters
|
---|
455 | HashMap params = GSXML.extractParams(param_list, false);
|
---|
456 |
|
---|
457 | // Make sure a query has been specified
|
---|
458 | String query = (String) params.get(empty_query_test_param);
|
---|
459 | if (query == null || query.equals("")) {
|
---|
460 | return result; // Return the empty result
|
---|
461 | }
|
---|
462 |
|
---|
463 | // If an index hasn't been specified, use the default
|
---|
464 | String index = (String) params.get(INDEX_PARAM);
|
---|
465 | if (index == null) {
|
---|
466 | index = default_index_;
|
---|
467 | }
|
---|
468 |
|
---|
469 | // set up mgpp_src
|
---|
470 | // String basedir = GSFile.collectionBaseDir(site_home_, cluster_name_);
|
---|
471 | // String indexdir = GSFile.collectionIndexPath(cluster_name_, index);
|
---|
472 | String basedir = "";
|
---|
473 | String indexdir = GSFile.collectionBaseDir(site_home_, cluster_name_) +
|
---|
474 | File.separatorChar + GSFile.collectionIndexPath(cluster_name_, index);
|
---|
475 |
|
---|
476 | mgpp_src_.loadIndexData(basedir, indexdir);
|
---|
477 | setStandardQueryParams(params);
|
---|
478 |
|
---|
479 | // if field search, create the query string
|
---|
480 | switch (query_type) {
|
---|
481 | case SIMPLE_QUERY:
|
---|
482 | query = parseFieldQueryParams(params);
|
---|
483 | break;
|
---|
484 | case ADVANCED_QUERY:
|
---|
485 | query = parseAdvancedFieldQueryParams(params);
|
---|
486 | break;
|
---|
487 | }
|
---|
488 | // run the query
|
---|
489 | mgpp_src_.runQuery(query);
|
---|
490 | MGPPQueryResult mqr= mgpp_src_.getQueryResult();
|
---|
491 |
|
---|
492 | // build up the response
|
---|
493 |
|
---|
494 | // Create a metadata list to store information about the query results
|
---|
495 | // should we be using metadataList? or something else?
|
---|
496 | Element metadata_list = doc_.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
497 | result.appendChild(metadata_list);
|
---|
498 |
|
---|
499 | // Add a metadata element specifying the number of matching documents
|
---|
500 | long totalDocs = mqr.getTotalDocs();
|
---|
501 | Element num_matches_elem = doc_.createElement(GSXML.METADATA_ELEM);
|
---|
502 | num_matches_elem.setAttribute(GSXML.NAME_ATT, "numDocsMatched");
|
---|
503 | num_matches_elem.setAttribute(GSXML.VALUE_ATT, "" + totalDocs);
|
---|
504 | metadata_list.appendChild(num_matches_elem);
|
---|
505 |
|
---|
506 | // Create a document list to store the matching documents, and add them
|
---|
507 | Vector docs = mqr.getDocs();
|
---|
508 | Element document_list = doc_.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
509 | result.appendChild(document_list);
|
---|
510 | for (int d = 0; d < docs.size(); d++) {
|
---|
511 | long docnum = ((MGPPDocInfo) docs.elementAt(d)).num_;
|
---|
512 | String doc_id = gdbm_src_.docnum2Oid(docnum);
|
---|
513 | Element doc_node = createDocumentNodeElement(doc_id);
|
---|
514 | document_list.appendChild(doc_node);
|
---|
515 | }
|
---|
516 |
|
---|
517 | // Create a term list to store the term information, and add it
|
---|
518 | String query_level = (String)params.get(LEVEL_PARAM); // the current query level
|
---|
519 | Element term_list = doc_.createElement(GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
|
---|
520 | result.appendChild(term_list);
|
---|
521 | Vector terms = mqr.getTerms();
|
---|
522 | for (int t = 0; t < terms.size(); t++) {
|
---|
523 | MGPPTermInfo term_info = (MGPPTermInfo) terms.get(t);
|
---|
524 |
|
---|
525 | Element term_elem = doc_.createElement(GSXML.TERM_ELEM);
|
---|
526 | term_elem.setAttribute(GSXML.NAME_ATT, term_info.term_);
|
---|
527 | term_elem.setAttribute(STEM_ATT, "" + term_info.stem_method_);
|
---|
528 | term_elem.setAttribute(FREQ_ATT, "" + term_info.term_freq_);
|
---|
529 | term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + term_info.match_docs_);
|
---|
530 | String field = term_info.tag_;
|
---|
531 | if (field.equals(query_level)) {
|
---|
532 | // ignore
|
---|
533 | field = "";
|
---|
534 | }
|
---|
535 | term_elem.setAttribute(FIELD_ATT, field);
|
---|
536 |
|
---|
537 | Vector equiv_terms = term_info.equiv_terms_;
|
---|
538 | Element equiv_term_list = doc_.createElement(EQUIV_TERM_ELEM+GSXML.LIST_MODIFIER);
|
---|
539 | term_elem.appendChild(equiv_term_list);
|
---|
540 |
|
---|
541 | for (int et = 0; et < equiv_terms.size(); et++) {
|
---|
542 | String equiv_term = (String) equiv_terms.get(et);
|
---|
543 |
|
---|
544 | Element equiv_term_elem = doc_.createElement(GSXML.TERM_ELEM);
|
---|
545 | equiv_term_elem.setAttribute(GSXML.NAME_ATT, equiv_term);
|
---|
546 | equiv_term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "");
|
---|
547 | equiv_term_elem.setAttribute(FREQ_ATT, "");
|
---|
548 | equiv_term_list.appendChild(equiv_term_elem);
|
---|
549 | }
|
---|
550 |
|
---|
551 | term_list.appendChild(term_elem);
|
---|
552 | }
|
---|
553 |
|
---|
554 | return result;
|
---|
555 | }
|
---|
556 |
|
---|
557 | // should probably use a list rather than map
|
---|
558 | protected boolean setStandardQueryParams(HashMap params) {
|
---|
559 |
|
---|
560 | Set entries = params.entrySet();
|
---|
561 | Iterator i = entries.iterator();
|
---|
562 | while (i.hasNext()) {
|
---|
563 | Map.Entry m = (Map.Entry)i.next();
|
---|
564 | String name = (String)m.getKey();
|
---|
565 | String value = (String)m.getValue();
|
---|
566 |
|
---|
567 | if (name.equals(CASE_PARAM)) {
|
---|
568 | boolean val = (value.equals(BOOLEAN_PARAM_ON)?true:false);
|
---|
569 | mgpp_src_.setCase(val);
|
---|
570 | } else if (name.equals(STEM_PARAM)) {
|
---|
571 | boolean val = (value.equals(BOOLEAN_PARAM_ON)?true:false);
|
---|
572 | mgpp_src_.setStem(val);
|
---|
573 | } else if (name.equals(MAXDOCS_PARAM)) {
|
---|
574 | int docs = Integer.parseInt(value);
|
---|
575 | mgpp_src_.setMaxDocs(docs);
|
---|
576 | } else if (name.equals(LEVEL_PARAM)) {
|
---|
577 | mgpp_src_.setQueryLevel(value);
|
---|
578 | } else if (name.equals(MATCH_PARAM)) {
|
---|
579 | int mode;
|
---|
580 | if (value.equals(MATCH_PARAM_ALL)) mode=1;
|
---|
581 | else mode=0;
|
---|
582 | mgpp_src_.setMatchMode(mode);
|
---|
583 | } else if (name.equals(RANK_PARAM)) {
|
---|
584 | if (value.equals(RANK_PARAM_RANK)) {
|
---|
585 | mgpp_src_.setSortByRank(true);
|
---|
586 | } else if (value.equals(RANK_PARAM_NONE)) {
|
---|
587 | mgpp_src_.setSortByRank(false);
|
---|
588 | }
|
---|
589 | } // ignore any others
|
---|
590 | }
|
---|
591 | return true;
|
---|
592 | }
|
---|
593 |
|
---|
594 | /** combines all the field params into a single query
|
---|
595 | * - for simple field query */
|
---|
596 | protected String parseFieldQueryParams(HashMap params) {
|
---|
597 |
|
---|
598 | StringBuffer final_query = new StringBuffer(256);
|
---|
599 | String text_line = (String)params.get(FIELD_QUERY_PARAM);
|
---|
600 | String[] texts = text_line.split(",", -1);
|
---|
601 | String field_line = (String)params.get(FIELD_FIELD_PARAM);
|
---|
602 | String[] fields = field_line.split(",", -1);
|
---|
603 | String combine="&";
|
---|
604 | String match = (String)params.get(MATCH_PARAM);
|
---|
605 | if (match.equals(MATCH_PARAM_SOME)) {
|
---|
606 | combine = "|";
|
---|
607 | }
|
---|
608 |
|
---|
609 | for (int i=0; i<texts.length; i++) {
|
---|
610 |
|
---|
611 | String q = texts[i].trim();
|
---|
612 | if (!q.equals("")) {
|
---|
613 | addQueryElem(final_query, q, fields[i], combine);
|
---|
614 | }
|
---|
615 | }
|
---|
616 |
|
---|
617 | return final_query.toString();
|
---|
618 | }
|
---|
619 |
|
---|
620 | /** combines all the field params into a single query
|
---|
621 | * - for advanced field query */
|
---|
622 | protected String parseAdvancedFieldQueryParams(HashMap params) {
|
---|
623 |
|
---|
624 | StringBuffer final_query = new StringBuffer(256);
|
---|
625 | String text_line = (String)params.get(FIELD_QUERY_PARAM);
|
---|
626 | String[] texts = text_line.split(",", -1);
|
---|
627 | String field_line = (String)params.get(FIELD_FIELD_PARAM);
|
---|
628 | String[] fields = field_line.split(",", -1);
|
---|
629 | String case_line = (String)params.get(FIELD_CASE_PARAM);
|
---|
630 | String[] cases = case_line.split(",", -1);
|
---|
631 | String stem_line = (String)params.get(FIELD_STEM_PARAM);
|
---|
632 | String[] stems = stem_line.split(",", -1);
|
---|
633 | String combine_line = (String)params.get(FIELD_COMBINE_PARAM);
|
---|
634 | String [] combines = combine_line.split(",", -1);
|
---|
635 | String combine = "&";
|
---|
636 | for (int i=0; i<texts.length; i++) {
|
---|
637 | if (i==0) {
|
---|
638 | combine = "";
|
---|
639 | } else {
|
---|
640 | String x = combines[i-1]; // assume first one is missing
|
---|
641 | if (x.equals(FIELD_COMBINE_PARAM_AND)) {
|
---|
642 | combine = "&";
|
---|
643 | } else if (x.equals(FIELD_COMBINE_PARAM_OR)) {
|
---|
644 | combine = "|";
|
---|
645 | } else if (x.equals(FIELD_COMBINE_PARAM_NOT)) {
|
---|
646 | combine = "!";
|
---|
647 | }
|
---|
648 |
|
---|
649 | }
|
---|
650 |
|
---|
651 | String q = texts[i].trim();
|
---|
652 | if (!q.equals("")) {
|
---|
653 | q = addStemAndCase(q, stems[i], cases[i]);
|
---|
654 | addQueryElem(final_query, q, fields[i], combine);
|
---|
655 | }
|
---|
656 | }
|
---|
657 |
|
---|
658 | return final_query.toString();
|
---|
659 | }
|
---|
660 |
|
---|
661 | protected void addQueryElem(StringBuffer s, String q, String f, String c) {
|
---|
662 |
|
---|
663 | String combine="";
|
---|
664 | if (s.length()>0) {
|
---|
665 | combine = " "+c+" ";
|
---|
666 | }
|
---|
667 | if (f!="") {
|
---|
668 | s.append(combine+"["+q+"]:"+f);
|
---|
669 | } else {
|
---|
670 | s.append(combine+q);
|
---|
671 | }
|
---|
672 | }
|
---|
673 |
|
---|
674 | protected String addStemAndCase(String q, String s, String c) {
|
---|
675 | String mods = "#";
|
---|
676 | if (c.equals("1")) {
|
---|
677 | mods += "i";
|
---|
678 | } else {
|
---|
679 | mods += "c";
|
---|
680 | }
|
---|
681 | if (s.equals("1")) {
|
---|
682 | mods += "s";
|
---|
683 | } else {
|
---|
684 | mods+= "u";
|
---|
685 | }
|
---|
686 | StringBuffer temp = new StringBuffer();
|
---|
687 | String [] terms = q.split(" ");
|
---|
688 | for (int i=0; i<terms.length; i++) {
|
---|
689 | String t = terms[i].trim();
|
---|
690 |
|
---|
691 | if (!t.equals("") && !t.equals("TX")) {
|
---|
692 | temp.append(" "+t+mods);
|
---|
693 | }
|
---|
694 | }
|
---|
695 | return temp.toString();
|
---|
696 | }
|
---|
697 |
|
---|
698 |
|
---|
699 | }
|
---|
700 |
|
---|
701 |
|
---|