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

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

no longer need the dictionary_name field

  • 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 boolean configure(Element info, Element extra_info)
117 {
118 // Do generic configuration
119 if (super.configure(info, extra_info) == false)
120 return false;
121
122 // Do specific configuration
123 System.out.println("Configuring AbstractMGPPSearch...");
124
125 // do we support any of the extended features?
126 does_chunking = true;
127 // Get the default index out of <defaultIndex> (buildConfig.xml)
128 Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_INDEX_ELEM);
129 if (def != null) {
130 this.default_index = def.getAttribute(GSXML.NAME_ATT);
131 }
132 if (this.default_index == null || this.default_index.equals("")) {
133 System.err.println("Error: default index not specified!");
134 return false;
135 }
136 // the index stem is either specified in the config file or is "index"
137 Element index_stem_elem = (Element) GSXML.getChildByTagName(info, INDEX_STEM_ELEM);
138 if (index_stem_elem != null) {
139 this.index_stem = index_stem_elem.getAttribute(GSXML.NAME_ATT);
140 }
141 if (this.index_stem == null || this.index_stem.equals("")) {
142 System.err.println("AbstractMGPPSearch.configure(): indexStem element not found, stem will default to collection name");
143 this.index_stem = this.cluster_name;
144 }
145
146 // the generic config has set up the text query service, but we may not want it
147 Element search_type_list = (Element) GSXML.getChildByTagName(info, SEARCH_TYPE_ELEM + GSXML.LIST_MODIFIER);
148 if (search_type_list == null) {
149 // assume form and plain
150 this.plain_search = true;
151 this.simple_form_search = true;
152 this.advanced_form_search = true;
153 } else {
154 NodeList types = search_type_list.getElementsByTagName(SEARCH_TYPE_ELEM);
155 for (int i=0; i<types.getLength(); i++) {
156 Element t = (Element)types.item(i);
157 String type_name = t.getAttribute(GSXML.NAME_ATT);
158 if (type_name.equals(SEARCH_TYPE_PLAIN)) {
159 this.plain_search = true;
160 } else if (type_name.equals(SEARCH_TYPE_FORM)) {
161 String type_type = t.getAttribute(GSXML.TYPE_ATT);
162 if (type_type.equals("")) {
163 this.simple_form_search = true;
164 this.advanced_form_search = true;
165 } else if (type_type.equals(SEARCH_TYPE_FORM_SIMPLE)) {
166 this.simple_form_search = true;
167 } else if (type_type.equals(SEARCH_TYPE_FORM_ADVANCED)) {
168 this.advanced_form_search = true;
169
170 }
171 }
172 }
173 }
174
175 if (!this.plain_search) {
176 // need to remove the TextQuery service
177 Element tq_service = GSXML.getNamedElement(short_service_info, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
178 short_service_info.removeChild(tq_service);
179
180 }
181
182
183 // Get the default level out of <defaultLevel> (buildConfig.xml)
184 def = (Element) GSXML.getChildByTagName(info, DEFAULT_LEVEL_ELEM);
185 if (def != null) {
186 this.default_level = def.getAttribute(GSXML.NAME_ATT);
187 }
188 if (this.default_level == null || this.default_level.equals("")) {
189 System.err.println("Error: default level not specified!");
190 return false;
191 }
192
193 // the default level is also the level which gdbm is expecting
194 // this must not be overwritten
195 this.mgpp_src.setReturnLevel(this.default_level);
196 // return term info
197 this.mgpp_src.setReturnTerms(true);
198 // set the default - this may be overwritten by query params
199 this.mgpp_src.setQueryLevel(this.default_level);
200
201 // set up the extra services which are available for this collection
202 // check the config info - if there is no field list, then there is no fielded searching
203
204 Element field_list = (Element) GSXML.getChildByTagName(info, GSXML.FIELD_ELEM+GSXML.LIST_MODIFIER);
205 if (field_list==null) {
206 // nothing more to do
207 return true;
208 }
209
210 // the format info is the same for all services
211 Element format_info = (Element)format_info_map.get(TEXT_QUERY_SERVICE);
212
213 // find the default field - use the first one
214 Element first_field = (Element)GSXML.getChildByTagName(field_list, GSXML.FIELD_ELEM);
215 default_field = first_field.getAttribute(GSXML.SHORTNAME_ATT);
216 // else set up the fielded query services
217
218 if (this.simple_form_search) {
219 // set up short_service_info_ - for now just has id and type - name will be added in on teh fly
220 Element fq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
221 fq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
222 fq_service.setAttribute(GSXML.NAME_ATT, FIELD_QUERY_SERVICE);
223 this.short_service_info.appendChild(fq_service);
224
225 if (format_info != null) {
226 this.format_info_map.put(FIELD_QUERY_SERVICE, format_info);
227 }
228 }
229
230 if (this.advanced_form_search) {
231 Element afq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
232 afq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
233 afq_service.setAttribute(GSXML.NAME_ATT, ADVANCED_FIELD_QUERY_SERVICE);
234 this.short_service_info.appendChild(afq_service);
235
236 if (format_info != null) {
237 this.format_info_map.put(ADVANCED_FIELD_QUERY_SERVICE, format_info);
238 }
239 }
240 return true;
241
242 }
243 protected Element getServiceDescription(String service_id, String lang, String subset) {
244 // should we check that the service is actually on offer? presumably we wont get asked for services that we haven't advertised previously.
245
246 if (!service_id.equals(FIELD_QUERY_SERVICE) && !service_id.equals(ADVANCED_FIELD_QUERY_SERVICE)) {
247 return super.getServiceDescription(service_id, lang, subset);
248 }
249
250
251 Element service = this.doc.createElement(GSXML.SERVICE_ELEM);
252 service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
253 service.setAttribute(GSXML.NAME_ATT, service_id);
254 if (subset == null || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
255 service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getTextString(service_id+".name", lang)));
256 service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_SUBMIT, getTextString(service_id+".submit", lang)));
257 service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getTextString(service_id+".description", lang)));
258
259 }
260 if (subset == null || subset.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
261 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
262 service.appendChild(param_list);
263 if (service_id.equals(FIELD_QUERY_SERVICE)) {
264
265 addCustomQueryParams(param_list, lang);
266 createParameter(MAXDOCS_PARAM, param_list, lang);
267 // create a multi param for the fields etc
268 // text box, field
269 Element multiparam = null;
270 Element param=null;
271 multiparam = GSXML.createParameterDescription(this.doc, SIMPLE_FIELD_PARAM, "", GSXML.PARAM_TYPE_MULTI, null, null, null);
272 multiparam.setAttribute("occurs", "4");
273 param_list.appendChild(multiparam);
274
275 // the components
276 createParameter(FIELD_QUERY_PARAM, multiparam, lang);
277 createParameter(FIELD_FIELD_PARAM, multiparam, lang);
278
279 } else {
280 createParameter(LEVEL_PARAM, param_list, lang);
281 createParameter(RANK_PARAM, param_list, lang);
282 createParameter(MAXDOCS_PARAM, param_list, lang);
283
284
285 // create a multi param for the fields etc
286 // text box, stem, case, field
287
288 Element multiparam = null;
289 Element param=null;
290
291 multiparam = GSXML.createParameterDescription(this.doc, ADVANCED_FIELD_PARAM, "", GSXML.PARAM_TYPE_MULTI, null, null, null);
292 multiparam.setAttribute("occurs", "4");
293 param_list.appendChild(multiparam);
294
295 createParameter(FIELD_COMBINE_PARAM, multiparam, lang);
296 createParameter(FIELD_QUERY_PARAM, multiparam, lang);
297 createParameter(FIELD_CASE_PARAM, multiparam, lang);
298 createParameter(FIELD_STEM_PARAM, multiparam, lang);
299 createParameter(FIELD_FIELD_PARAM, multiparam, lang);
300
301 }
302 }
303 return service;
304
305 }
306
307 /** add in the mgpp specific params to TextQuery */
308 protected void addCustomQueryParams(Element param_list, String lang)
309 {
310 createParameter(LEVEL_PARAM, param_list, lang);
311 createParameter(CASE_PARAM, param_list, lang);
312 createParameter(STEM_PARAM, param_list, lang);
313 createParameter(MATCH_PARAM, param_list, lang);
314 createParameter(RANK_PARAM, param_list, lang);
315
316 }
317
318 /** create a param and add to the list */
319 protected void createParameter(String name, Element param_list, String lang)
320 {
321 Element param = null;
322 if (name.equals(LEVEL_PARAM)) {
323 ArrayList level_ids = new ArrayList();
324 ArrayList level_names = new ArrayList();
325 getLevelData(level_ids, level_names, lang);
326 if (level_ids.size()>1) {
327 // the first one is the default
328 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);
329 }
330
331 } else if (name.equals(RANK_PARAM)) {
332 String [] vals1 = {RANK_PARAM_RANK, RANK_PARAM_NONE };
333 String [] vals1_texts = { getTextString("param."+RANK_PARAM+"."+RANK_PARAM_RANK, lang), getTextString("param."+RANK_PARAM+"."+RANK_PARAM_NONE, lang, "MGPPSearch")};
334
335 param = GSXML.createParameterDescription(this.doc, RANK_PARAM, getTextString("param."+RANK_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, RANK_PARAM_RANK, vals1, vals1_texts );
336
337 } else if (name.equals(FIELD_QUERY_PARAM)) {
338 param = GSXML.createParameterDescription(this.doc, FIELD_QUERY_PARAM, getTextString("param."+FIELD_QUERY_PARAM, lang), GSXML.PARAM_TYPE_STRING, null, null, null);
339
340
341 } else if (name.equals(FIELD_CASE_PARAM) || name.equals(FIELD_STEM_PARAM)) {
342 String[] bool_ops = {"0", "1"};
343 String[] bool_texts = {getTextString("param.boolean.off", lang, "AbstractSearch"),getTextString("param.boolean.on", lang, "AbstractSearch")};
344 param = GSXML.createParameterDescription(this.doc, name, getTextString("param."+name, lang), GSXML.PARAM_TYPE_BOOLEAN, BOOLEAN_PARAM_ON, bool_ops, bool_texts);
345
346 } else if (name.equals(FIELD_FIELD_PARAM)) {
347 ArrayList fields = new ArrayList();
348 ArrayList field_names = new ArrayList();
349 getIndexData(fields, field_names, lang);
350 // the field list - read from config file
351 param = GSXML.createParameterDescription2(this.doc, name, getTextString("param."+name, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, (String)fields.get(0), fields, field_names );
352
353 } else if (name.equals(FIELD_COMBINE_PARAM)) {
354
355 String []vals = {FIELD_COMBINE_PARAM_AND, FIELD_COMBINE_PARAM_OR, FIELD_COMBINE_PARAM_NOT};
356 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)};
357
358
359 param = GSXML.createParameterDescription(this.doc, FIELD_COMBINE_PARAM, "", GSXML.PARAM_TYPE_ENUM_SINGLE, FIELD_COMBINE_PARAM_AND, vals, val_texts);
360 param.setAttribute(GSXML.PARAM_IGNORE_POS_ATT, "0");
361 }
362
363 if (param != null) {
364 param_list.appendChild(param);
365 } else {
366 super.createParameter(name, param_list, lang);
367 }
368 }
369
370 // should cache some of this
371 protected void getLevelData(ArrayList level_ids, ArrayList level_names, String lang)
372 {
373 Element level_list = (Element)GSXML.getChildByTagName(this.config_info, LEVEL_ELEM+GSXML.LIST_MODIFIER);
374 NodeList levels = level_list.getElementsByTagName(LEVEL_ELEM);
375 for (int i=0; i<levels.getLength(); i++) {
376 level_ids.add(((Element)levels.item(i)).getAttribute(GSXML.NAME_ATT));
377 level_names.add(getTextString("level."+level_ids.get(i), lang));
378 }
379
380 }
381 protected void getIndexData(ArrayList index_ids, ArrayList index_names, String lang){
382 // we currently only have one index, and use the field data as the index
383
384 // the field list - read from config file
385 Element field_list = (Element)GSXML.getChildByTagName(this.config_info, GSXML.FIELD_ELEM+GSXML.LIST_MODIFIER);
386 NodeList fields = field_list.getElementsByTagName(GSXML.FIELD_ELEM);
387 for (int i=0; i< fields.getLength();i++) {
388 index_ids.add(((Element)fields.item(i)).getAttribute(GSXML.SHORTNAME_ATT));
389 // should these be changed to a text element based on lang?
390 // or is the name of a metadata element eg dc:Title its
391 // name in all langs
392 index_names.add(((Element)fields.item(i)).getAttribute(GSXML.NAME_ATT));
393
394 }
395 }
396
397 // the following three functions are needed so the base class can
398 // call the process+SERVICE_NAME methods
399 /** process a text query */
400 protected Element processTextQuery(Element request) {
401 return processAnyQuery(request, TEXT_QUERY);
402 }
403
404 /** process a field query */
405 protected Element processFieldQuery(Element request) {
406 return processAnyQuery(request, SIMPLE_QUERY);
407 }
408
409 /** process an advanced field query */
410 protected Element processAdvancedFieldQuery(Element request) {
411 return processAnyQuery(request, ADVANCED_QUERY);
412 }
413
414 /** process a query */
415 protected Element processAnyQuery(Element request, int query_type)
416 {
417
418 String service_name=null;
419 String empty_query_test_param=null;
420 // set up the type specific bits
421 switch (query_type) {
422 case TEXT_QUERY:
423 service_name = TEXT_QUERY_SERVICE;
424 empty_query_test_param = QUERY_PARAM;
425 break;
426 case SIMPLE_QUERY:
427 service_name = FIELD_QUERY_SERVICE;
428 empty_query_test_param = FIELD_QUERY_PARAM;
429 break;
430 case ADVANCED_QUERY:
431 service_name = ADVANCED_FIELD_QUERY_SERVICE;
432 empty_query_test_param = FIELD_QUERY_PARAM;
433 break;
434 default:
435 // should never get here
436 System.err.println("AbstractMGPPSearch: wrong query type!!");
437 return null;
438 }
439
440 // Create a new (empty) result message
441 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
442 result.setAttribute(GSXML.FROM_ATT, service_name);
443 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
444
445 // Get the parameters of the request
446 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
447 if (param_list == null) {
448 System.err.println("Error: TextQuery request had no paramList.");
449 return result; // Return the empty result
450 }
451
452 // Process the request parameters
453 HashMap params = GSXML.extractParams(param_list, false);
454
455 // Make sure a query has been specified
456 String query = (String) params.get(empty_query_test_param);
457 if (query == null || query.equals("")) {
458 return result; // Return the empty result
459 }
460
461 // If a field hasn't been specified, use the default - for textQuery
462 String field = (String) params.get(INDEX_PARAM);
463 if (field == null) {
464 field = default_field;
465 }
466
467 // set up mgpp_src
468 String indexdir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) +
469 File.separatorChar + GSFile.collectionIndexPath(this.index_stem, this.default_index);
470 this.mgpp_src.loadIndexData(indexdir);
471 setStandardQueryParams(params);
472
473 // if field search, create the query string
474 switch (query_type) {
475 case TEXT_QUERY:
476 query = addFieldInfo(query, field);
477 break;
478 case SIMPLE_QUERY:
479 query = parseFieldQueryParams(params);
480 break;
481 case ADVANCED_QUERY:
482 query = parseAdvancedFieldQueryParams(params);
483 break;
484 }
485 // run the query
486 this.mgpp_src.runQuery(query);
487 MGPPQueryResult mqr= this.mgpp_src.getQueryResult();
488
489 // build up the response
490
491 // Create a metadata list to store information about the query results
492 // should we be using metadataList? or something else?
493 Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
494 result.appendChild(metadata_list);
495
496 // Add a metadata element specifying the number of matching documents
497 long totalDocs = mqr.getTotalDocs();
498 GSXML.addMetadata(this.doc, metadata_list, "numDocsMatched", ""+totalDocs);
499
500 // Create a document list to store the matching documents, and add them
501 Vector docs = mqr.getDocs();
502
503 // add a metadata item to specify docs returned
504 GSXML.addMetadata(this.doc, metadata_list, "numDocsReturned", ""+docs.size());
505 // 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
506 GSXML.addMetadata(this.doc, metadata_list, "query", query);
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.