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

Last change on this file since 25852 was 25849, checked in by sjm84, 12 years ago

Reformatting this file and tidying imports

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