source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS2MGSearch.java@ 13289

Last change on this file since 13289 was 13289, checked in by shaoqun, 17 years ago

added its configure method to set maxnumeric to MGWrapper

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