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

Last change on this file since 29637 was 29637, checked in by Jeremy Symon, 9 years ago

Convert MAXDOCS_PARAM to int when calling setMaxDocs. Fixes error introduced in r29630. Now it compiles again.

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