source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/AbstractMGPPSearch.java@ 10651

Last change on this file since 10651 was 10651, checked in by kjdon, 19 years ago

made all index/gdbm db paths use indexstem instead of cluster_name

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