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

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

merged from branch ant-install-branch: merge 1

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