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

Last change on this file since 4012 was 4012, checked in by mdewsnip, 21 years ago

Changed string comparison to use .equals().

  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 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.Element;
27import org.w3c.dom.Node;
28import org.w3c.dom.NodeList;
29
30// General Java classes
31import java.io.File;
32import java.util.HashMap;
33import java.util.Iterator;
34import java.util.Map;
35import java.util.Set;
36import java.util.Vector;
37
38
39/**
40 *
41 * @author <a href="mailto:[email protected]">Katherine Don</a>
42 * @author <a href="mailto:[email protected]">Michael Dewsnip</a>
43 * @version $Revision: 4012 $
44 */
45
46public class GS2MGSearch
47 extends GS2Search {
48
49
50 private MGWrapper mg_src_ = null;
51
52
53 /** constructor */
54 public GS2MGSearch()
55 {
56 mg_src_ = new MGWrapper();
57 }
58
59
60 /** this creates all teh params and appends them to param_list.
61 * if display=true it creates the text strings version
62 * otherwise it creates the description version
63 */
64 protected boolean createTextQueryParamList(Element param_list, boolean display,
65 String lang)
66 {
67 // the order they are specified here is the order they appear on
68 // the query form
69 createParameter(INDEX_PARAM, param_list, display, lang);
70 createParameter(CASE_PARAM, param_list, display, lang);
71 createParameter(STEM_PARAM, param_list, display, lang);
72 createParameter(MATCH_PARAM, param_list, display, lang);
73 createParameter(MAXDOCS_PARAM, param_list, display, lang);
74 createParameter(QUERY_PARAM, param_list, display, lang);
75
76 return true;
77 }
78
79
80 /** Creates a display element containing all the text strings needed to display
81 the service page, in the language specified */
82 protected Element createServiceDisplay(String service, String lang)
83 {
84 // Create a service display for the basic text query service
85 return super.createServiceDisplay(service, lang);
86 }
87
88
89 /** Process a text query */
90 protected Element processTextQuery(Element request)
91 {
92 // Create a new (empty) result message
93 Element result = doc_.createElement(GSXML.RESPONSE_ELEM);
94 result.setAttribute(GSXML.FROM_ATT, TEXT_QUERY_SERVICE);
95 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
96
97 // Get the parameters of the request
98 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
99 if (param_list == null) {
100 System.err.println("Error: TextQuery request had no paramList.");
101 return result; // Return the empty result
102 }
103
104 // Process the request parameters
105 HashMap params = GSXML.extractParams(param_list, false);
106
107 // Make sure a query has been specified
108 String query = (String) params.get(QUERY_PARAM);
109 if (query == null || query.equals("")) {
110 return result; // Return the empty result
111 }
112
113 // If an index hasn't been specified, use the default
114 String index = (String) params.get(INDEX_PARAM);
115 if (index == null) {
116 index = default_index_;
117 }
118
119 // The location of the MG index and text files
120 String basedir = GSFile.collectionBaseDir(site_home_, cluster_name_) +
121 File.separatorChar; // Needed for MG
122 String textdir = GSFile.collectionTextPath(cluster_name_);
123 String indexpath = GSFile.collectionIndexPath(cluster_name_, index);
124 mg_src_.setIndex(indexpath);
125
126 // set the mg query parameters to the values the user has specified
127 setStandardQueryParams(params);
128 mg_src_.runQuery(basedir, textdir, query);
129 MGQueryResult mqr = mg_src_.getQueryResult();
130 long totalDocs = mqr.getTotalDocs();
131
132 // Get the docnums out, and convert to HASH ids
133 Vector docs = mqr.getDocs();
134 if (docs.size() == 0) {
135 System.err.println("GS2MGSearch: Warning: No results found...\n");
136 }
137
138 // Create a metadata list to store information about the query results
139 Element metadata_list = doc_.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
140 result.appendChild(metadata_list);
141
142 // Add a metadata element specifying the number of matching documents
143 Element num_matches_elem = doc_.createElement(GSXML.METADATA_ELEM);
144 num_matches_elem.setAttribute(GSXML.NAME_ATT, "numDocsMatched");
145 num_matches_elem.setAttribute(GSXML.VALUE_ATT, "" + totalDocs);
146 metadata_list.appendChild(num_matches_elem);
147
148 // Create a document list to store the matching documents, and add them
149 Element document_list = doc_.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
150 result.appendChild(document_list);
151 for (int d = 0; d < docs.size(); d++) {
152 long docnum = ((MGDocInfo) docs.elementAt(d)).num_;
153 String doc_id = gdbm_src_.docnum2Oid(docnum);
154 Element doc_node = createDocumentNodeElement(doc_id);
155 document_list.appendChild(doc_node);
156 }
157
158 // Create a term list to store the term information, and add it
159 Element term_list = doc_.createElement(GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
160 result.appendChild(term_list);
161 Vector terms = mqr.getTerms();
162 for (int t = 0; t < terms.size(); t++) {
163 MGTermInfo term_info = (MGTermInfo) terms.get(t);
164
165 String term = term_info.term_;
166 int stem_method = term_info.stem_method_;
167 Vector equiv_terms = term_info.equiv_terms_;
168
169 Element term_elem = doc_.createElement(GSXML.TERM_ELEM);
170 term_elem.setAttribute(GSXML.NAME_ATT, term);
171 term_elem.setAttribute(STEM_ATT, "" + stem_method);
172
173 Element equiv_term_list = doc_.createElement(EQUIV_TERM_ELEM+GSXML.LIST_MODIFIER);
174 term_elem.appendChild(equiv_term_list);
175
176 long total_term_freq = 0;
177 for (int et = 0; et < equiv_terms.size(); et++) {
178 MGEquivTermInfo equiv_term_info = (MGEquivTermInfo) equiv_terms.get(et);
179
180 Element equiv_term_elem = doc_.createElement(GSXML.TERM_ELEM);
181 equiv_term_elem.setAttribute(GSXML.NAME_ATT, equiv_term_info.term_);
182 equiv_term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + equiv_term_info.match_docs_);
183 equiv_term_elem.setAttribute(FREQ_ATT, "" + equiv_term_info.term_freq_);
184 equiv_term_list.appendChild(equiv_term_elem);
185
186 total_term_freq += equiv_term_info.term_freq_;
187 }
188
189 term_elem.setAttribute(FREQ_ATT, "" + total_term_freq);
190 term_list.appendChild(term_elem);
191 }
192
193 return result;
194 }
195
196
197 // should probably use a list rather than map
198 protected boolean setStandardQueryParams(HashMap params)
199 {
200 // set the default ones
201 mg_src_.setReturnTerms(true);
202 Set entries = params.entrySet();
203 Iterator i = entries.iterator();
204 while (i.hasNext()) {
205 Map.Entry m = (Map.Entry)i.next();
206 String name = (String)m.getKey();
207 String value = (String)m.getValue();
208
209 if (name.equals(CASE_PARAM)) {
210 boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
211 mg_src_.setCase(val);
212 }
213 else if (name.equals(STEM_PARAM)) {
214 boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
215 mg_src_.setStem(val);
216 }
217 else if (name.equals(MATCH_PARAM)) {
218 int mode = (value.equals(MATCH_PARAM_ALL) ? 1 : 0);
219 mg_src_.setMatchMode(mode);
220 }
221 else if (name.equals(MAXDOCS_PARAM)) {
222 int docs = Integer.parseInt(value);
223 mg_src_.setMaxDocs(docs);
224 } // ignore any others
225 }
226 return true;
227 }
228}
Note: See TracBrowser for help on using the repository browser.