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

Last change on this file since 26103 was 26103, checked in by kjdon, 12 years ago

when concatenating subcoll and language info onto index name, need to use a temporary variable. Don't use the class variable or next time it won't match idx and you won't be able to change to the new subindex.

  • Property svn:keywords set to Author Date Id Revision
File size: 8.7 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 mgpp_src.setCase(true);
104
105 // set up the query params
106 Set entries = params.entrySet();
107 Iterator i = entries.iterator();
108 String current_physical_index_name = this.physical_index_name;
109 String physical_sub_index_name = this.default_index_subcollection;
110 String physical_index_language_name = this.default_index_language;
111 while (i.hasNext())
112 {
113 Map.Entry m = (Map.Entry) i.next();
114 String name = (String) m.getKey();
115 String value = (String) m.getValue();
116
117 if (name.equals(CASE_PARAM))
118 {
119 boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
120 mgpp_src.setCase(val);
121 }
122 else if (name.equals(STEM_PARAM))
123 {
124 boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
125 mgpp_src.setStem(val);
126 }
127 else if (name.equals(ACCENT_PARAM))
128 {
129 boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
130 mgpp_src.setAccentFold(val);
131 }
132 else if (name.equals(MAXDOCS_PARAM) && !value.equals(""))
133 {
134 int docs = Integer.parseInt(value);
135 mgpp_src.setMaxDocs(docs);
136 }
137 else if (name.equals(LEVEL_PARAM))
138 {
139 mgpp_src.setQueryLevel(value);
140 }
141 else if (name.equals(MATCH_PARAM))
142 {
143 int mode;
144 if (value.equals(MATCH_PARAM_ALL))
145 mode = 1;
146 else
147 mode = 0;
148 mgpp_src.setMatchMode(mode);
149 }
150 else if (name.equals(RANK_PARAM))
151 {
152 if (value.equals(RANK_PARAM_RANK))
153 {
154 mgpp_src.setSortByRank(true);
155 }
156 else if (value.equals(RANK_PARAM_NONE))
157 {
158 mgpp_src.setSortByRank(false);
159 }
160 }
161 else if (name.equals(INDEX_SUBCOLLECTION_PARAM))
162 {
163 physical_sub_index_name = value;
164 }
165 else if (name.equals(INDEX_LANGUAGE_PARAM))
166 {
167 physical_index_language_name = value;
168 } // ignore any others
169 }
170
171 if (physical_index_name.equals("idx"))
172 {
173 if (physical_sub_index_name != null)
174 {
175 current_physical_index_name += physical_sub_index_name;
176 }
177 if (physical_index_language_name != null)
178 {
179 current_physical_index_name += physical_index_language_name;
180 }
181 }
182
183 // set up mgpp_src
184 String indexdir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) + File.separatorChar + GSFile.collectionIndexPath(this.index_stem, current_physical_index_name);
185 mgpp_src.loadIndexData(indexdir);
186
187 return true;
188 }
189
190 protected Object runQuery(String query)
191 {
192 mgpp_src.runQuery(query);
193 MGPPQueryResult mqr = mgpp_src.getQueryResult();
194 return mqr;
195
196 }
197
198 protected long numDocsMatched(Object query_result)
199 {
200 return ((MGPPQueryResult) query_result).getTotalDocs();
201 }
202
203 protected String[] getDocIDs(Object query_result)
204 {
205
206 Vector docs = ((MGPPQueryResult) query_result).getDocs();
207 String[] doc_nums = new String[docs.size()];
208 for (int d = 0; d < docs.size(); d++)
209 {
210 doc_nums[d] = Long.toString((((MGPPDocInfo) docs.elementAt(d)).num_));
211 }
212 return doc_nums;
213 }
214
215 protected String[] getDocRanks(Object query_result)
216 {
217
218 Vector docs = ((MGPPQueryResult) query_result).getDocs();
219 String[] doc_ranks = new String[docs.size()];
220 for (int d = 0; d < docs.size(); d++)
221 {
222 doc_ranks[d] = Float.toString(((MGPPDocInfo) docs.elementAt(d)).rank_);
223 }
224 return doc_ranks;
225 }
226
227 protected boolean addTermInfo(Element term_list, HashMap<String, Serializable> params, Object query_result)
228 {
229
230 String query_level = (String) params.get(LEVEL_PARAM); // the current query level
231
232 Vector terms = ((MGPPQueryResult) query_result).getTerms();
233 for (int t = 0; t < terms.size(); t++)
234 {
235 MGPPTermInfo term_info = (MGPPTermInfo) terms.get(t);
236
237 Element term_elem = this.doc.createElement(GSXML.TERM_ELEM);
238 term_elem.setAttribute(GSXML.NAME_ATT, term_info.term_);
239 term_elem.setAttribute(STEM_ATT, "" + term_info.stem_method_);
240 term_elem.setAttribute(FREQ_ATT, "" + term_info.term_freq_);
241 term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + term_info.match_docs_);
242 String field = term_info.tag_;
243 if (field.equals(query_level))
244 {
245 // ignore
246 field = "";
247 }
248 term_elem.setAttribute(FIELD_ATT, field);
249
250 Vector equiv_terms = term_info.equiv_terms_;
251 Element equiv_term_list = this.doc.createElement(EQUIV_TERM_ELEM + GSXML.LIST_MODIFIER);
252 term_elem.appendChild(equiv_term_list);
253
254 for (int et = 0; et < equiv_terms.size(); et++)
255 {
256 String equiv_term = (String) equiv_terms.get(et);
257
258 Element equiv_term_elem = this.doc.createElement(GSXML.TERM_ELEM);
259 equiv_term_elem.setAttribute(GSXML.NAME_ATT, equiv_term);
260 equiv_term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "");
261 equiv_term_elem.setAttribute(FREQ_ATT, "");
262 equiv_term_list.appendChild(equiv_term_elem);
263 }
264
265 term_list.appendChild(term_elem);
266 }
267 return true;
268 }
269
270 protected String addFieldInfo(String query, String field)
271 {
272 if (field.equals("") || field.equals("ZZ"))
273 {
274 return query;
275 }
276 return "[" + query + "]:" + field;
277 }
278
279 protected void addQueryElem(StringBuffer final_query, String query, String field, String combine)
280 {
281
282 String comb = "";
283 if (final_query.length() > 0)
284 {
285 comb = " " + combine + " ";
286 }
287 final_query.append(comb + addFieldInfo(query, field));
288 }
289
290 protected String addStemOptions(String query, String stem, String casef, String accent)
291 {
292 String mods = "#";
293 if (casef != null)
294 {
295 if (casef.equals("1"))
296 {
297 mods += "i";
298 }
299 else
300 {
301 mods += "c";
302 }
303 }
304 if (stem != null)
305 {
306 if (stem.equals("1"))
307 {
308 mods += "s";
309 }
310 else
311 {
312 mods += "u";
313 }
314 }
315 if (accent != null)
316 {
317 if (accent.equals("1"))
318 {
319 mods += "f";
320 }
321 else
322 {
323 mods += "a";
324 }
325 }
326
327 StringBuffer temp = new StringBuffer();
328 String[] terms = query.split(" ");
329 for (int i = 0; i < terms.length; i++)
330 {
331 String t = terms[i].trim();
332 // what is the TX bit about???
333 if (!t.equals("") && !t.equals("TX"))
334 {
335 temp.append(" " + t + mods);
336 }
337 }
338 return temp.toString();
339 }
340
341 protected ArrayList<FacetWrapper> getFacets(Object query_result)
342 {
343 return null;
344 }
345}
Note: See TracBrowser for help on using the repository browser.