source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/GS2MGSearch.java@ 32453

Last change on this file since 32453 was 32452, checked in by kjdon, 6 years ago

removed author email

  • Property svn:keywords set to Author Date Id Revision
File size: 9.7 KB
RevLine 
[3649]1/*
2 * GS2MGSearch.java
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
[3851]6 * the Free Software Foundation; either version 2 of the License, or
[3649]7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18package org.greenstone.gsdl3.service;
19
[3823]20
21// Greenstone classes
[13238]22import org.greenstone.mg.*;
[3649]23import org.greenstone.gsdl3.util.*;
24
[3823]25// XML classes
[9002]26import org.w3c.dom.Document;
[14428]27import org.w3c.dom.Element;
[9002]28import org.w3c.dom.NodeList;
[3755]29
[13238]30// java
31import java.util.Vector;
32import java.util.ArrayList;
33import java.util.HashMap;
34import java.util.Map;
35import java.util.Set;
36import java.util.Iterator;
37import java.io.File;
[25635]38import java.io.Serializable;
[13238]39
[13124]40import org.apache.log4j.*;
41
[3649]42
[14428]43public class GS2MGSearch
[24394]44extends AbstractGS2TextSearch {
[14428]45
[14633]46 protected static MGSearchWrapper mg_src = null;
[14428]47
48 static Logger logger = Logger.getLogger (org.greenstone.gsdl3.service.GS2MGSearch.class.getName ());
49
50
[3755]51 /** constructor */
[14428]52 public GS2MGSearch () {
[29558]53 does_chunking = true;
[14633]54 if(this.mg_src == null){
55 this.mg_src = new MGSearchWrapper ();
56 }
[3649]57 }
[14428]58 public void cleanUp () {
59 super.cleanUp ();
60 this.mg_src.unloadIndexData ();
[9874]61 }
[14428]62
[13289]63 /** configure this service */
[14428]64 public boolean configure (Element info, Element extra_info) {
65 if (!super.configure (info, extra_info)){
66 return false;
67 }
[29558]68 // internally mg uses 50, so set this here
69 paramDefaults.put(MAXDOCS_PARAM, "50");
[29630]70 this.mg_src.setMaxNumeric (this.maxnumeric);
[14428]71 return true;
[13289]72 }
73
[14428]74
75
[13238]76 /** do the actual query */
[14428]77 protected Element processTextQuery (Element request) {
[14633]78 synchronized(this.mg_src){
[14428]79 // Create a new (empty) result message ('doc' is in ServiceRack.java)
[28966]80 Document result_doc = XMLConverter.newDOM();
81 Element result = result_doc.createElement (GSXML.RESPONSE_ELEM);
[24394]82 result.setAttribute (GSXML.FROM_ATT, QUERY_SERVICE);
[14428]83 result.setAttribute (GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
84
85 // Get the parameters of the request
86 Element param_list = (Element) GSXML.getChildByTagName (request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
87 if (param_list == null) {
88 logger.error ("TextQuery request had no paramList.");
89 return result; // Return the empty result
90 }
91
92 // Process the request parameters
[25635]93 HashMap<String, Serializable> params = GSXML.extractParams (param_list, false);
[14428]94
95 // Make sure a query has been specified
96 String query = (String) params.get (QUERY_PARAM);
97 if (query == null || query.equals ("")) {
98 return result; // Return the empty result
99 }
100
101 // If an index hasn't been specified, use the default
102 String index = (String) params.get (INDEX_PARAM);
103 if (index == null) {
104 index = this.default_index;
105 }
106
107 // If a subcollection index has been specified, use it
108 String indexSub = (String) params.get (INDEX_SUBCOLLECTION_PARAM);
109 if (indexSub != null) {
110 index += indexSub;
111 }
112 else{
113 if (!this.default_index_subcollection.equals ("")){
114 index += this.default_index_subcollection;
115 }
116 }
117
118 // If a subcollection index has been specified, use it
119 String indexLang = (String) params.get (INDEX_LANGUAGE_PARAM);
120 if (indexLang != null) {
121 index += indexLang;
122 }
123 else{
124 if (!this.default_index_language.equals ("")){
125 index += this.default_index_language;
126 }
127 }
128
129 // The location of the MG index and text files
130 String basedir = GSFile.collectionBaseDir (this.site_home, this.cluster_name) + File.separatorChar; // Needed for MG
131 String textdir = GSFile.collectionTextPath (this.index_stem);
132 String indexpath = GSFile.collectionIndexPath (this.index_stem, index);
133 this.mg_src.setIndex (indexpath);
[20296]134
[14428]135 // set the mg query parameters to the values the user has specified
136 setStandardQueryParams (params);
137 this.mg_src.runQuery (basedir, textdir, query);
138 MGQueryResult mqr = this.mg_src.getQueryResult ();
139 if (mqr.isClear ()) {
140 // something has gone wrong
[28966]141 GSXML.addError (result, "Couldn't query the mg database", GSXML.ERROR_TYPE_SYSTEM);
[14428]142 return result;
143 }
144 long totalDocs = mqr.getTotalDocs ();
145
146 // Get the docnums out, and convert to HASH ids
147 Vector docs = mqr.getDocs ();
148 if (docs.size () == 0) {
149 logger.error ("No results found...\n");
150 }
151
152 // Create a metadata list to store information about the query results
[28966]153 Element metadata_list = result_doc.createElement (GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
[14428]154 result.appendChild (metadata_list);
155
156 // Add a metadata element specifying the number of matching documents
157 // because teh total number is just the number returned, use numDocsReturned, not numDocsMatched
[28966]158 GSXML.addMetadata (metadata_list, "numDocsReturned", ""+totalDocs);
[14428]159 // 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
[28966]160 GSXML.addMetadata (metadata_list, "query", query);
[14428]161
162 if (docs.size () > 0) {
163 // Create a document list to store the matching documents, and add them
[28966]164 Element document_list = result_doc.createElement (GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
[14428]165 result.appendChild (document_list);
166 for (int d = 0; d < docs.size (); d++) {
167 long docnum = ((MGDocInfo) docs.elementAt (d)).num_;
168 float rank = ((MGDocInfo) docs.elementAt (d)).rank_;
169 String doc_id = internalNum2OID (docnum);
[28966]170 Element doc_node = createDocNode (result_doc, doc_id, Float.toString (rank));
[14428]171 document_list.appendChild (doc_node);
172 }
173 }
174
175 // Create a term list to store the term information, and add it
[28966]176 Element term_list = result_doc.createElement (GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
[14428]177 result.appendChild (term_list);
178 Vector terms = mqr.getTerms ();
179 for (int t = 0; t < terms.size (); t++) {
180 MGTermInfo term_info = (MGTermInfo) terms.get (t);
181
182 String term = term_info.term_;
183 int stem_method = term_info.stem_method_;
184 Vector equiv_terms = term_info.equiv_terms_;
185
[28966]186 Element term_elem = result_doc.createElement (GSXML.TERM_ELEM);
[14428]187 term_elem.setAttribute (GSXML.NAME_ATT, term);
188 term_elem.setAttribute (STEM_ATT, "" + stem_method);
189
[28966]190 Element equiv_term_list = result_doc.createElement (EQUIV_TERM_ELEM+GSXML.LIST_MODIFIER);
[14428]191 term_elem.appendChild (equiv_term_list);
192
193 long total_term_freq = 0;
194 for (int et = 0; et < equiv_terms.size (); et++) {
195 MGEquivTermInfo equiv_term_info = (MGEquivTermInfo) equiv_terms.get (et);
196
[28966]197 Element equiv_term_elem = result_doc.createElement (GSXML.TERM_ELEM);
[14428]198 equiv_term_elem.setAttribute (GSXML.NAME_ATT, equiv_term_info.term_);
199 equiv_term_elem.setAttribute (NUM_DOCS_MATCH_ATT, "" + equiv_term_info.match_docs_);
200 equiv_term_elem.setAttribute (FREQ_ATT, "" + equiv_term_info.term_freq_);
201 equiv_term_list.appendChild (equiv_term_elem);
202
203 total_term_freq += equiv_term_info.term_freq_;
204 }
205
206 term_elem.setAttribute (FREQ_ATT, "" + total_term_freq);
207 term_list.appendChild (term_elem);
208 }
209 return result;
[14633]210 }//end of synchronized
[3755]211 }
[14428]212
[13238]213 // should probably use a list rather than map
[25635]214 protected boolean setStandardQueryParams(HashMap<String, Serializable> params)
[13238]215 {
[20238]216 // set the default settings that gs uses
[29637]217 this.mg_src.setMaxDocs(Integer.parseInt(paramDefaults.get(MAXDOCS_PARAM)));
[13238]218 this.mg_src.setReturnTerms(true);
[29630]219 this.mg_src.setCase(paramDefaults.get(CASE_PARAM).equals(BOOLEAN_PARAM_ON) ? true : false);
220 this.mg_src.setStem(paramDefaults.get(STEM_PARAM).equals(BOOLEAN_PARAM_ON) ? true : false);
[13238]221 Set entries = params.entrySet();
222 Iterator i = entries.iterator();
223 while (i.hasNext()) {
224 Map.Entry m = (Map.Entry)i.next();
225 String name = (String)m.getKey();
226 String value = (String)m.getValue();
227
[14378]228 if (name.equals(CASE_PARAM) && this.does_case) {
[13238]229 boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
230 this.mg_src.setCase(val);
231 }
[14378]232 else if (name.equals(STEM_PARAM) && this.does_stem) {
[13238]233 boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
234 this.mg_src.setStem(val);
235 }
236 else if (name.equals(MATCH_PARAM)) {
237 int mode = (value.equals(MATCH_PARAM_ALL) ? 1 : 0);
238 this.mg_src.setMatchMode(mode);
239 }
240 else if (name.equals(MAXDOCS_PARAM)) {
241 int docs = Integer.parseInt(value);
242 this.mg_src.setMaxDocs(docs);
243 } // ignore any others
[9002]244 }
245 return true;
246 }
[14428]247
248
[3755]249}
[9002]250
251
Note: See TracBrowser for help on using the repository browser.