source: trunk/greenstone3-extensions/gs3build/src/org/greenstone/gsdl3/service/AbstractMGPPSearch.java@ 13242

Last change on this file since 13242 was 13242, checked in by kjdon, 17 years ago

these files were removed from main greenstone repository, cos I reordered the class inheritance. so added them in here in case we ever want to use this gs3 building. can't guarantee that they will work without modification

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