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

Last change on this file since 29558 was 29558, checked in by kjdon, 9 years ago

work around does_paging, does_chunking. only add in maxdocs, hitsperpage params if the service actually uses them. lucnee/solr, don't use maxdocs any more. I haven't had a chance to clean up the changes, but I need to commit, so there may be extraneous debug statements still here.

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