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

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

Removed the Solr code from the main greenstone code

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