Changeset 3823
- Timestamp:
- 2003-03-10T10:59:12+13:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS2MGSearch.java
r3800 r3823 19 19 package org.greenstone.gsdl3.service; 20 20 21 // greenstone classes 21 22 // Greenstone classes 22 23 import org.greenstone.mg.*; 23 import org.greenstone.gdbm.*;24 24 import org.greenstone.gsdl3.util.*; 25 25 26 // xml classes 27 import org.w3c.dom.Document; 26 // XML classes 28 27 import org.w3c.dom.Element; 29 28 import org.w3c.dom.Node; 30 29 import org.w3c.dom.NodeList; 31 import org.w3c.dom.Text; 32 33 // general java classes 30 31 // General Java classes 34 32 import java.io.File; 35 33 import java.util.HashMap; … … 39 37 import java.util.Vector; 40 38 39 41 40 /** 42 41 * … … 46 45 47 46 public class GS2MGSearch 48 extends ServiceRack { 49 50 // the services on offer 51 // these strings must match what is found in the properties file 52 private static final String TEXT_QUERY_SERVICE = "TextQuery"; 53 54 // params used 47 extends GS2Search { 48 49 // Parameters used 55 50 private static final String INDEX_PARAM = "index"; 56 private static final String CASE_PARAM = "case"; 57 private static final String STEM_PARAM = "stem"; 58 private static final String MATCH_PARAM = "matchMode"; 59 private static final String MATCH_PARAM_ALL = "all"; 60 private static final String MATCH_PARAM_SOME = "some"; 61 private static final String RANK_PARAM = "sortBy"; 62 private static final String RANK_PARAM_RANK = "rank"; 63 private static final String RANK_PARAM_NONE = "natural"; 64 private static final String MAXDOCS_PARAM = "maxDocs"; 65 private static final String BOOLEAN_PARAM_ON = "1"; 66 private static final String BOOLEAN_PARAM_OFF = "0"; 67 private static final String QUERY_PARAM = "query"; 68 69 // elements used in the config file that are specific to this class 51 52 // Elements used in the config file that are specific to this class 70 53 private static final String DEFAULT_INDEX_ELEM = "defaultIndex"; 71 54 private static final String INDEX_ELEM = "index"; 72 55 73 private static final String EQUIV_TERM_ELEM = "equivTerm";74 75 private static final String STEM_ATT = "stem";76 private static final String NUM_DOCS_MATCH_ATT = "numDocsMatch";77 private static final String FREQ_ATT = "freq";78 79 56 private MGWrapper mg_src_ = null; 80 private GDBMWrapper gdbm_src_ = null;81 57 82 58 private String default_index_ = null; 83 59 84 private Element config_info_ = null;85 86 60 87 61 /** constructor */ 88 public GS2MGSearch() { 62 public GS2MGSearch() 63 { 89 64 mg_src_ = new MGWrapper(); 90 gdbm_src_ = new GDBMWrapper();91 65 } 92 66 … … 95 69 public boolean configure(Element info) 96 70 { 97 System.out.println("configuring GS2MGSearch"); 98 config_info_ = info; 99 100 // get the default index out of <defaultIndex> (buildConfig.xml) 71 // Do generic configuration 72 if (super.configure(info) == false) 73 return false; 74 75 // Do specific configuration 76 System.out.println("Configuring GS2MGSearch..."); 77 78 // Get the default index out of <defaultIndex> (buildConfig.xml) 101 79 Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_INDEX_ELEM); 102 80 if (def != null) { … … 107 85 return false; 108 86 } 109 110 Element e = null; 111 // these entries should reflect the build config file - some services may not be available depending on how the colleciton was built. 112 // set up short_service_info_ - for now just has name and type 113 e = doc_.createElement(GSXML.SERVICE_ELEM); 114 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY); 115 e.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE); 116 short_service_info_.appendChild(e); 117 118 // set up service_info_map_ - for now, just has the same elements as above 119 // should have full details about each service incl params lists etc. 120 e = doc_.createElement(GSXML.SERVICE_ELEM); 121 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY); 122 e.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE); 123 Element param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 124 createTextQueryParamList(param_list, false, null); 125 e.appendChild(param_list); 126 service_info_map_.put(TEXT_QUERY_SERVICE, e); 127 128 // Open GDBM database for querying 129 String gdbm_db_file = GSFile.GDBMDatabaseFile(site_home_, cluster_name_); 130 if (gdbm_src_.openDatabase(gdbm_db_file, GDBMWrapper.READER)) { 131 return true; 132 } 133 else { 134 System.err.println("Error: Could not open gdbm database!"); 135 return false; 136 } 87 // System.out.println("Default index: " + default_index_); 88 89 return true; 137 90 } 138 91 139 92 140 93 /** creates a new param element and adds it to the param list */ 141 protected void createParameter(String name, Element param_list, boolean display, String lang) 142 { 143 Element param=null; 94 protected void createParameter(String name, Element param_list, boolean display, 95 String lang) 96 { 97 Element param = null; 144 98 145 99 if (name.equals(INDEX_PARAM)) { … … 149 103 int len = indexes.getLength(); 150 104 if (len > 1) { // add index param to list only if more than one index specified 151 String [] inds= new String[len];105 String[] inds = new String[len]; 152 106 for (int i=0; i<len; i++) { 153 107 inds[i] = ((Element)indexes.item(i)).getAttribute(GSXML.NAME_ATT); … … 161 115 } 162 116 } 163 else if (name.equals(CASE_PARAM)) {164 if (display) {165 String[] bool_ops = {"0", "1"};166 String[] bool_texts = {getTextString("param.boolean.off", lang),getTextString("param.boolean.on", lang)};167 param = GSXML.createParameterDisplay(doc_, CASE_PARAM, getTextString("param."+CASE_PARAM, lang), bool_ops, bool_texts);168 } else {169 param = GSXML.createParameter(doc_, CASE_PARAM, GSXML.PARAM_TYPE_BOOLEAN, BOOLEAN_PARAM_ON, null);170 }171 }172 else if (name.equals(STEM_PARAM)) {173 if (display) {174 String[] bool_ops = {"0", "1"};175 String[] bool_texts = {getTextString("param.boolean.off", lang),getTextString("param.boolean.on", lang)};176 param = GSXML.createParameterDisplay(doc_, STEM_PARAM, getTextString("param."+STEM_PARAM, lang), bool_ops, bool_texts);177 } else {178 param = GSXML.createParameter(doc_, STEM_PARAM, GSXML.PARAM_TYPE_BOOLEAN, BOOLEAN_PARAM_ON, null);179 }180 }181 else if (name.equals(MATCH_PARAM)) {182 String[] vals = {MATCH_PARAM_ALL, MATCH_PARAM_SOME};183 if (display) {184 String[] val_texts = {getTextString("param."+MATCH_PARAM+"."+MATCH_PARAM_ALL, lang),getTextString("param."+MATCH_PARAM+"."+MATCH_PARAM_SOME, lang)};185 186 param = GSXML.createParameterDisplay(doc_, MATCH_PARAM, getTextString("param."+MATCH_PARAM, lang), vals, val_texts);187 } else {188 param = GSXML.createParameter(doc_, MATCH_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, MATCH_PARAM_ALL, vals);189 }190 }191 else if (name.equals(MAXDOCS_PARAM)) {192 if (display) {193 param = GSXML.createParameterDisplay(doc_, MAXDOCS_PARAM, getTextString("param."+MAXDOCS_PARAM, lang), null, null);194 } else {195 param = GSXML.createParameter(doc_, MAXDOCS_PARAM, GSXML.PARAM_TYPE_INTEGER, "10", null);196 }197 }198 else if (name.equals(QUERY_PARAM)) {199 if (display) {200 param = GSXML.createParameterDisplay(doc_, QUERY_PARAM, getTextString("param."+QUERY_PARAM, lang), null, null);201 } else {202 param = GSXML.createParameter(doc_, QUERY_PARAM, GSXML.PARAM_TYPE_STRING, null, null);203 }204 }205 117 206 118 // add the param to the list 207 119 if (param != null) { 208 120 param_list.appendChild(param); 121 } 122 else { 123 super.createParameter(name, param_list, display, lang); 209 124 } 210 125 } … … 215 130 * otherwise it creates the description version 216 131 */ 217 protected boolean createTextQueryParamList(Element param_list, boolean display, String lang) 132 protected boolean createTextQueryParamList(Element param_list, boolean display, 133 String lang) 218 134 { 219 135 // the order they are specified here is the order they appear on 220 136 // the query form 221 137 createParameter(INDEX_PARAM, param_list, display, lang); 222 createParameter(CASE_PARAM, param_list, display, lang); 223 createParameter(STEM_PARAM, param_list, display, lang); 224 createParameter(MATCH_PARAM, param_list, display, lang); 225 createParameter(MAXDOCS_PARAM, param_list, display, lang); 226 createParameter(QUERY_PARAM, param_list, display, lang); 227 return true; 228 } 229 230 231 /** creates a display element containing all the text strings needed to display the service page, in the language specified */ 232 protected Element createServiceDisplay(String service, String lang) { 233 Element display = doc_.createElement(GSXML.DISPLAY_ELEM); 234 display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_NAME_ELEM, 235 getTextString(service+".name", lang))); 236 display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_SUBMIT_ELEM, 237 getTextString(service+".submit", lang))); 238 239 // now need to add in the params 240 if (service.equals(TEXT_QUERY_SERVICE)) { 241 createTextQueryParamList(display, true, lang); 242 } 243 244 return display; 245 } 246 247 248 /** process a text query */ 138 return super.createTextQueryParamList(param_list, display, lang); 139 } 140 141 142 /** Creates a display element containing all the text strings needed to display 143 the service page, in the language specified */ 144 protected Element createServiceDisplay(String service, String lang) 145 { 146 // Create a service display for the basic text query service 147 return super.createServiceDisplay(service, lang); 148 } 149 150 151 /** Process a text query */ 249 152 protected Element processTextQuery(Element request) 250 153 { 154 // Create a new (empty) result message 251 155 Element result = doc_.createElement(GSXML.RESPONSE_ELEM); 252 156 String from = GSPath.appendLink(cluster_name_, TEXT_QUERY_SERVICE); 253 157 result.setAttribute(GSXML.FROM_ATT, from); 254 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY); 255 256 // get param list 257 Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 258 if (param_list==null) { 259 System.err.println("GS2MGSearch, TextQuery Error: no param list in request!"); 260 return result; // empty result 261 } 158 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS); 159 Element result_content = doc_.createElement(GSXML.CONTENT_ELEM); 160 result.appendChild(result_content); 161 162 // Get the parameters of the request 163 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 164 if (param_list == null) { 165 System.err.println("Error: TextQuery request had no paramList."); 166 return result; // Return the empty result 167 } 168 169 // Process the request parameters 262 170 HashMap params = GSXML.extractParams(param_list); 263 String query = (String)params.get(QUERY_PARAM); 264 if (query == null) { 265 // no query, no result 266 return result; 267 } 268 171 172 // Make sure a query has been specified 173 String query = (String) params.get(QUERY_PARAM); 174 if (query == null || query == "") { 175 return result; // Return the empty result 176 } 177 178 // If an index hasn't been specified, use the default 269 179 String index = (String) params.get(INDEX_PARAM); 270 if (index == null) { // if it is not present, use the default index180 if (index == null) { 271 181 index = default_index_; 272 182 } 273 183 274 // now set up the mg stuff184 // The location of the MG index and text files 275 185 String basedir = GSFile.collectionBaseDir(site_home_, cluster_name_) + 276 186 File.separatorChar; // Needed for MG 277 187 String textdir = GSFile.collectionTextPath(cluster_name_); 278 188 String indexpath = GSFile.collectionIndexPath(cluster_name_, index); 189 mg_src_.setIndex(indexpath); 279 190 280 191 // set the mg query parameters to the values the user has specified 281 192 setStandardQueryParams(params); 282 mg_src_.setIndex(indexpath); 283 284 System.out.println("GS2MGSearch, query string: " + query); 285 mg_src_.runQuery(basedir + File.separatorChar, textdir, query); 193 mg_src_.runQuery(basedir, textdir, query); 286 194 MGQueryResult mqr = mg_src_.getQueryResult(); 287 195 long totalDocs = mqr.getTotalDocs(); 288 // System.out.println("Matching documents: " + totalDocs); 289 290 // get the docnums out, and convert to HASH ids 196 197 // Get the docnums out, and convert to HASH ids 291 198 Vector docs = mqr.getDocs(); 292 199 if (docs.size() == 0) { 293 // no docs found 294 System.out.println("No results found...\n"); 200 System.out.println("Warning: No results found...\n"); 295 201 } 296 202 … … 304 210 metadata_list.appendChild(num_matches_elem); 305 211 306 // Response content: documents and terms 307 Element c = doc_.createElement(GSXML.CONTENT_ELEM); 308 result.appendChild(c); 309 212 // Create a document list to store the matching documents, and add them 310 213 Element document_list = doc_.createElement(GSXML.DOCUMENT_ELEM+GSXML.LIST_MODIFIER); 311 c.appendChild(document_list); 312 // add each document 214 result_content.appendChild(document_list); 313 215 for (int d = 0; d < docs.size(); d++) { 314 216 long docnum = ((MGDocInfo) docs.elementAt(d)).num_; 315 String id = gdbm_src_.docnum2Oid(docnum);316 System.out.println("Docnum: " + docnum + " ID: " +id);317 Node no = GSXML.createDocumentElement(doc_, id);318 document_list.appendChild(no);319 } 320 217 String doc_id = gdbm_src_.docnum2Oid(docnum); 218 Element doc_node = createDocumentNodeElement(doc_id); 219 document_list.appendChild(doc_node); 220 } 221 222 // Create a term list to store the term information, and add it 321 223 Element term_list = doc_.createElement(GSXML.TERM_ELEM+GSXML.LIST_MODIFIER); 322 c.appendChild(term_list); 323 // get the terms out, and add to query result metadata 224 result_content.appendChild(term_list); 324 225 Vector terms = mqr.getTerms(); 325 226 for (int t = 0; t < terms.size(); t++) { … … 379 280 } 380 281 else if (name.equals(MATCH_PARAM)) { 381 int mode; 382 if (value.equals(MATCH_PARAM_ALL)) mode = 1; 383 else mode = 0; 282 int mode = (value.equals(MATCH_PARAM_ALL) ? 1 : 0); 384 283 mg_src_.setMatchMode(mode); 385 284 }
Note:
See TracChangeset
for help on using the changeset viewer.