source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS2MGPPSearch.java@ 6490

Last change on this file since 6490 was 5644, checked in by kjdon, 21 years ago

can now specify in teh config file what types of search should be provided - so just just have plain or form search

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