source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/GS2MGPPSearch.java@ 28181

Last change on this file since 28181 was 28181, checked in by kjdon, 11 years ago

making search param defaults able to be set in config file. uses <paramDefault name=xx value=yy> element. Now all defaults are set in paramDefaults HashMap instead of individual variables. have left index etc ones for now as they are more complicated.

  • Property svn:keywords set to Author Date Id Revision
File size: 9.1 KB
Line 
1/*
2 * GS2MGPPSearch.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// Greenstone classes
21import java.io.File;
22import java.io.Serializable;
23import java.util.ArrayList;
24import java.util.HashMap;
25import java.util.Iterator;
26import java.util.Map;
27import java.util.Set;
28import java.util.Vector;
29
30import org.apache.log4j.Logger;
31import org.greenstone.gsdl3.util.FacetWrapper;
32import org.greenstone.gsdl3.util.GSFile;
33import org.greenstone.gsdl3.util.GSXML;
34import org.greenstone.mgpp.MGPPDocInfo;
35import org.greenstone.mgpp.MGPPQueryResult;
36import org.greenstone.mgpp.MGPPSearchWrapper;
37import org.greenstone.mgpp.MGPPTermInfo;
38import org.w3c.dom.Element;
39
40public class GS2MGPPSearch extends AbstractGS2FieldSearch
41{
42 private static MGPPSearchWrapper mgpp_src = null;
43
44 private String physical_index_name = "idx";
45
46 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2MGPPSearch.class.getName());
47
48 /** constructor */
49 public GS2MGPPSearch()
50 {
51 if (mgpp_src == null)
52 {
53 mgpp_src = new MGPPSearchWrapper();
54 }
55 }
56
57 public void cleanUp()
58 {
59 super.cleanUp();
60 mgpp_src.unloadIndexData();
61 }
62
63 /** process a query */
64 protected Element processAnyQuery(Element request, int query_type)
65 {
66 synchronized (mgpp_src)
67 {
68 return super.processAnyQuery(request, query_type);
69 }
70 }
71
72 /** configure this service */
73 public boolean configure(Element info, Element extra_info)
74 {
75 if (!super.configure(info, extra_info))
76 {
77 return false;
78 }
79
80 // set up the defaults which are not dependent on query parameters
81 // the default level is also the level which the database is expecting
82 // this must not be overwritten
83 mgpp_src.setReturnLevel(this.default_db_level);
84 // return term info
85 mgpp_src.setReturnTerms(true);
86 mgpp_src.setMaxNumeric(this.maxnumeric);
87 return true;
88 }
89
90 /** add in the mgpp specific params to TextQuery */
91 protected void addCustomQueryParams(Element param_list, String lang)
92 {
93 super.addCustomQueryParams(param_list, lang);
94 createParameter(RANK_PARAM, param_list, lang);
95 }
96
97 protected boolean setUpQueryer(HashMap<String, Serializable> params)
98 {
99
100 // set up the defaults that may be changed by query params
101 mgpp_src.setQueryLevel(this.default_level);
102 // we have case folding on by default
103 if (this.does_case) {
104 mgpp_src.setCase(paramDefaults.get(CASE_PARAM).equals(BOOLEAN_PARAM_ON) ? true : false);
105 }
106 if (this.does_stem) {
107 mgpp_src.setStem(paramDefaults.get(STEM_PARAM).equals(BOOLEAN_PARAM_ON) ? true : false);
108 }
109 if (this.does_accent) {
110 mgpp_src.setAccentFold(paramDefaults.get(ACCENT_PARAM).equals(BOOLEAN_PARAM_ON) ? true : false);
111 }
112 // set up the query params
113 Set entries = params.entrySet();
114 Iterator i = entries.iterator();
115 String current_physical_index_name = this.physical_index_name;
116 String physical_sub_index_name = this.default_index_subcollection;
117 String physical_index_language_name = this.default_index_language;
118 while (i.hasNext())
119 {
120 Map.Entry m = (Map.Entry) i.next();
121 String name = (String) m.getKey();
122 String value = (String) m.getValue();
123
124 if (name.equals(CASE_PARAM) && this.does_case)
125 {
126 boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
127 mgpp_src.setCase(val);
128 }
129 else if (name.equals(STEM_PARAM) && this.does_stem)
130 {
131 boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
132 mgpp_src.setStem(val);
133 }
134 else if (name.equals(ACCENT_PARAM) && this.does_accent)
135 {
136 boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
137 mgpp_src.setAccentFold(val);
138 }
139 else if (name.equals(MAXDOCS_PARAM) && !value.equals(""))
140 {
141 int docs = Integer.parseInt(value);
142 mgpp_src.setMaxDocs(docs);
143 }
144 else if (name.equals(LEVEL_PARAM))
145 {
146 mgpp_src.setQueryLevel(value);
147 }
148 else if (name.equals(MATCH_PARAM))
149 {
150 int mode;
151 if (value.equals(MATCH_PARAM_ALL))
152 mode = 1;
153 else
154 mode = 0;
155 mgpp_src.setMatchMode(mode);
156 }
157 else if (name.equals(RANK_PARAM))
158 {
159 if (value.equals(RANK_PARAM_RANK))
160 {
161 mgpp_src.setSortByRank(true);
162 }
163 else if (value.equals(RANK_PARAM_NONE))
164 {
165 mgpp_src.setSortByRank(false);
166 }
167 }
168 else if (name.equals(INDEX_SUBCOLLECTION_PARAM))
169 {
170 physical_sub_index_name = value;
171 }
172 else if (name.equals(INDEX_LANGUAGE_PARAM))
173 {
174 physical_index_language_name = value;
175 } // ignore any others
176 }
177
178 if (physical_index_name.equals("idx"))
179 {
180 if (physical_sub_index_name != null)
181 {
182 current_physical_index_name += physical_sub_index_name;
183 }
184 if (physical_index_language_name != null)
185 {
186 current_physical_index_name += physical_index_language_name;
187 }
188 }
189
190 // set up mgpp_src
191 String indexdir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) + File.separatorChar + GSFile.collectionIndexPath(this.index_stem, current_physical_index_name);
192 mgpp_src.loadIndexData(indexdir);
193
194 return true;
195 }
196
197 protected Object runQuery(String query)
198 {
199 mgpp_src.runQuery(query);
200 MGPPQueryResult mqr = mgpp_src.getQueryResult();
201 return mqr;
202
203 }
204
205 protected long numDocsMatched(Object query_result)
206 {
207 return ((MGPPQueryResult) query_result).getTotalDocs();
208 }
209
210 protected String[] getDocIDs(Object query_result)
211 {
212
213 Vector docs = ((MGPPQueryResult) query_result).getDocs();
214 String[] doc_nums = new String[docs.size()];
215 for (int d = 0; d < docs.size(); d++)
216 {
217 doc_nums[d] = Long.toString((((MGPPDocInfo) docs.elementAt(d)).num_));
218 }
219 return doc_nums;
220 }
221
222 protected String[] getDocRanks(Object query_result)
223 {
224
225 Vector docs = ((MGPPQueryResult) query_result).getDocs();
226 String[] doc_ranks = new String[docs.size()];
227 for (int d = 0; d < docs.size(); d++)
228 {
229 doc_ranks[d] = Float.toString(((MGPPDocInfo) docs.elementAt(d)).rank_);
230 }
231 return doc_ranks;
232 }
233
234 protected boolean addTermInfo(Element term_list, HashMap<String, Serializable> params, Object query_result)
235 {
236
237 String query_level = (String) params.get(LEVEL_PARAM); // the current query level
238
239 Vector terms = ((MGPPQueryResult) query_result).getTerms();
240 for (int t = 0; t < terms.size(); t++)
241 {
242 MGPPTermInfo term_info = (MGPPTermInfo) terms.get(t);
243
244 Element term_elem = this.doc.createElement(GSXML.TERM_ELEM);
245 term_elem.setAttribute(GSXML.NAME_ATT, term_info.term_);
246 term_elem.setAttribute(STEM_ATT, "" + term_info.stem_method_);
247 term_elem.setAttribute(FREQ_ATT, "" + term_info.term_freq_);
248 term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + term_info.match_docs_);
249 String field = term_info.tag_;
250 if (field.equals(query_level))
251 {
252 // ignore
253 field = "";
254 }
255 term_elem.setAttribute(FIELD_ATT, field);
256
257 Vector equiv_terms = term_info.equiv_terms_;
258 Element equiv_term_list = this.doc.createElement(EQUIV_TERM_ELEM + GSXML.LIST_MODIFIER);
259 term_elem.appendChild(equiv_term_list);
260
261 for (int et = 0; et < equiv_terms.size(); et++)
262 {
263 String equiv_term = (String) equiv_terms.get(et);
264
265 Element equiv_term_elem = this.doc.createElement(GSXML.TERM_ELEM);
266 equiv_term_elem.setAttribute(GSXML.NAME_ATT, equiv_term);
267 equiv_term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "");
268 equiv_term_elem.setAttribute(FREQ_ATT, "");
269 equiv_term_list.appendChild(equiv_term_elem);
270 }
271
272 term_list.appendChild(term_elem);
273 }
274 return true;
275 }
276
277 protected String addFieldInfo(String query, String field)
278 {
279 if (field.equals("") || field.equals("ZZ"))
280 {
281 return query;
282 }
283 return "[" + query + "]:" + field;
284 }
285
286 protected void addQueryElem(StringBuffer final_query, String query, String field, String combine)
287 {
288
289 String comb = "";
290 if (final_query.length() > 0)
291 {
292 comb = " " + combine + " ";
293 }
294 final_query.append(comb + addFieldInfo(query, field));
295 }
296
297 protected String addStemOptions(String query, String stem, String casef, String accent)
298 {
299 String mods = "#";
300 if (casef != null)
301 {
302 if (casef.equals("1"))
303 {
304 mods += "i";
305 }
306 else
307 {
308 mods += "c";
309 }
310 }
311 if (stem != null)
312 {
313 if (stem.equals("1"))
314 {
315 mods += "s";
316 }
317 else
318 {
319 mods += "u";
320 }
321 }
322 if (accent != null)
323 {
324 if (accent.equals("1"))
325 {
326 mods += "f";
327 }
328 else
329 {
330 mods += "a";
331 }
332 }
333
334 StringBuffer temp = new StringBuffer();
335 String[] terms = query.split(" ");
336 for (int i = 0; i < terms.length; i++)
337 {
338 String t = terms[i].trim();
339 // what is the TX bit about???
340 if (!t.equals("") && !t.equals("TX"))
341 {
342 temp.append(" " + t + mods);
343 }
344 }
345 return temp.toString();
346 }
347
348 protected ArrayList<FacetWrapper> getFacets(Object query_result)
349 {
350 return null;
351 }
352}
Note: See TracBrowser for help on using the repository browser.