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

Last change on this file since 4903 was 4903, checked in by kjdon, 21 years ago

tidied up a lot of stuff, particularly the display text stuff, including how its formatted, and some of the service rack methods

  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 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: 4903 $
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,
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, lang);
70 createParameter(CASE_PARAM, param_list, lang);
71 createParameter(STEM_PARAM, param_list, lang);
72 createParameter(MATCH_PARAM, param_list, lang);
73 createParameter(MAXDOCS_PARAM, param_list, lang);
74 createParameter(QUERY_PARAM, param_list, lang);
75
76 return true;
77 }
78
79
80 /** Process a text query */
81 protected Element processTextQuery(Element request)
82 {
83 // Create a new (empty) result message
84 Element result = doc_.createElement(GSXML.RESPONSE_ELEM);
85 result.setAttribute(GSXML.FROM_ATT, TEXT_QUERY_SERVICE);
86 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
87
88 // Get the parameters of the request
89 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
90 if (param_list == null) {
91 System.err.println("Error: TextQuery request had no paramList.");
92 return result; // Return the empty result
93 }
94
95 // Process the request parameters
96 HashMap params = GSXML.extractParams(param_list, false);
97
98 // Make sure a query has been specified
99 String query = (String) params.get(QUERY_PARAM);
100 if (query == null || query.equals("")) {
101 return result; // Return the empty result
102 }
103
104 // If an index hasn't been specified, use the default
105 String index = (String) params.get(INDEX_PARAM);
106 if (index == null) {
107 index = default_index_;
108 }
109
110 // The location of the MG index and text files
111 String basedir = GSFile.collectionBaseDir(site_home_, cluster_name_) +
112 File.separatorChar; // Needed for MG
113 String textdir = GSFile.collectionTextPath(cluster_name_);
114 String indexpath = GSFile.collectionIndexPath(cluster_name_, index);
115 mg_src_.setIndex(indexpath);
116
117 // set the mg query parameters to the values the user has specified
118 setStandardQueryParams(params);
119 mg_src_.runQuery(basedir, textdir, query);
120 MGQueryResult mqr = mg_src_.getQueryResult();
121 long totalDocs = mqr.getTotalDocs();
122
123 // Get the docnums out, and convert to HASH ids
124 Vector docs = mqr.getDocs();
125 if (docs.size() == 0) {
126 System.err.println("GS2MGSearch: Warning: No results found...\n");
127 }
128
129 // Create a metadata list to store information about the query results
130 Element metadata_list = doc_.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
131 result.appendChild(metadata_list);
132
133 // Add a metadata element specifying the number of matching documents
134 Element num_matches_elem = doc_.createElement(GSXML.METADATA_ELEM);
135 num_matches_elem.setAttribute(GSXML.NAME_ATT, "numDocsMatched");
136 num_matches_elem.setAttribute(GSXML.VALUE_ATT, "" + totalDocs);
137 metadata_list.appendChild(num_matches_elem);
138
139 // Create a document list to store the matching documents, and add them
140 Element document_list = doc_.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
141 result.appendChild(document_list);
142 for (int d = 0; d < docs.size(); d++) {
143 long docnum = ((MGDocInfo) docs.elementAt(d)).num_;
144 String doc_id = gdbm_src_.docnum2Oid(docnum);
145 Element doc_node = createDocumentNodeElement(doc_id);
146 document_list.appendChild(doc_node);
147 }
148
149 // Create a term list to store the term information, and add it
150 Element term_list = doc_.createElement(GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
151 result.appendChild(term_list);
152 Vector terms = mqr.getTerms();
153 for (int t = 0; t < terms.size(); t++) {
154 MGTermInfo term_info = (MGTermInfo) terms.get(t);
155
156 String term = term_info.term_;
157 int stem_method = term_info.stem_method_;
158 Vector equiv_terms = term_info.equiv_terms_;
159
160 Element term_elem = doc_.createElement(GSXML.TERM_ELEM);
161 term_elem.setAttribute(GSXML.NAME_ATT, term);
162 term_elem.setAttribute(STEM_ATT, "" + stem_method);
163
164 Element equiv_term_list = doc_.createElement(EQUIV_TERM_ELEM+GSXML.LIST_MODIFIER);
165 term_elem.appendChild(equiv_term_list);
166
167 long total_term_freq = 0;
168 for (int et = 0; et < equiv_terms.size(); et++) {
169 MGEquivTermInfo equiv_term_info = (MGEquivTermInfo) equiv_terms.get(et);
170
171 Element equiv_term_elem = doc_.createElement(GSXML.TERM_ELEM);
172 equiv_term_elem.setAttribute(GSXML.NAME_ATT, equiv_term_info.term_);
173 equiv_term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + equiv_term_info.match_docs_);
174 equiv_term_elem.setAttribute(FREQ_ATT, "" + equiv_term_info.term_freq_);
175 equiv_term_list.appendChild(equiv_term_elem);
176
177 total_term_freq += equiv_term_info.term_freq_;
178 }
179
180 term_elem.setAttribute(FREQ_ATT, "" + total_term_freq);
181 term_list.appendChild(term_elem);
182 }
183
184 return result;
185 }
186
187
188 // should probably use a list rather than map
189 protected boolean setStandardQueryParams(HashMap params)
190 {
191 // set the default ones
192 mg_src_.setReturnTerms(true);
193 Set entries = params.entrySet();
194 Iterator i = entries.iterator();
195 while (i.hasNext()) {
196 Map.Entry m = (Map.Entry)i.next();
197 String name = (String)m.getKey();
198 String value = (String)m.getValue();
199
200 if (name.equals(CASE_PARAM)) {
201 boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
202 mg_src_.setCase(val);
203 }
204 else if (name.equals(STEM_PARAM)) {
205 boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
206 mg_src_.setStem(val);
207 }
208 else if (name.equals(MATCH_PARAM)) {
209 int mode = (value.equals(MATCH_PARAM_ALL) ? 1 : 0);
210 mg_src_.setMatchMode(mode);
211 }
212 else if (name.equals(MAXDOCS_PARAM)) {
213 int docs = Integer.parseInt(value);
214 mg_src_.setMaxDocs(docs);
215 } // ignore any others
216 }
217 return true;
218 }
219}
Note: See TracBrowser for help on using the repository browser.