[3649] | 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
|
---|
[9002] | 6 | * the Free Software Foundation; either version 2 of the License, or
|
---|
[3649] | 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 | */
|
---|
| 18 | package org.greenstone.gsdl3.service;
|
---|
| 19 |
|
---|
[3824] | 20 | // Greenstone classes
|
---|
[25849] | 21 | import java.io.File;
|
---|
| 22 | import java.io.Serializable;
|
---|
[25885] | 23 | import java.util.ArrayList;
|
---|
[25849] | 24 | import java.util.HashMap;
|
---|
| 25 | import java.util.Iterator;
|
---|
| 26 | import java.util.Map;
|
---|
| 27 | import java.util.Set;
|
---|
| 28 | import java.util.Vector;
|
---|
[3824] | 29 |
|
---|
[25849] | 30 | import org.apache.log4j.Logger;
|
---|
[25885] | 31 | import org.greenstone.gsdl3.util.FacetWrapper;
|
---|
[15326] | 32 | import org.greenstone.gsdl3.util.GSFile;
|
---|
| 33 | import org.greenstone.gsdl3.util.GSXML;
|
---|
[25849] | 34 | import org.greenstone.mgpp.MGPPDocInfo;
|
---|
| 35 | import org.greenstone.mgpp.MGPPQueryResult;
|
---|
| 36 | import org.greenstone.mgpp.MGPPSearchWrapper;
|
---|
| 37 | import org.greenstone.mgpp.MGPPTermInfo;
|
---|
| 38 | import org.w3c.dom.Element;
|
---|
[15326] | 39 |
|
---|
[25849] | 40 | public class GS2MGPPSearch extends AbstractGS2FieldSearch
|
---|
| 41 | {
|
---|
| 42 | private static MGPPSearchWrapper mgpp_src = null;
|
---|
[15326] | 43 |
|
---|
[25849] | 44 | private String physical_index_name = "idx";
|
---|
[3824] | 45 |
|
---|
[25849] | 46 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2MGPPSearch.class.getName());
|
---|
[13238] | 47 |
|
---|
[25849] | 48 | /** constructor */
|
---|
| 49 | public GS2MGPPSearch()
|
---|
| 50 | {
|
---|
| 51 | if (mgpp_src == null)
|
---|
| 52 | {
|
---|
| 53 | mgpp_src = new MGPPSearchWrapper();
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
[13124] | 56 |
|
---|
[25849] | 57 | public void cleanUp()
|
---|
| 58 | {
|
---|
| 59 | super.cleanUp();
|
---|
| 60 | mgpp_src.unloadIndexData();
|
---|
| 61 | }
|
---|
[9002] | 62 |
|
---|
[25849] | 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 | }
|
---|
[20239] | 71 |
|
---|
[25849] | 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 | }
|
---|
[3649] | 79 |
|
---|
[25849] | 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 | }
|
---|
[3649] | 89 |
|
---|
[25849] | 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
|
---|
[26352] | 103 | mgpp_src.setCase(case_default.equals(BOOLEAN_PARAM_ON) ? true : false);
|
---|
| 104 | mgpp_src.setStem(stem_default.equals(BOOLEAN_PARAM_ON) ? true : false);
|
---|
| 105 | mgpp_src.setAccentFold(accent_default.equals(BOOLEAN_PARAM_ON) ? true : false);
|
---|
[25849] | 106 |
|
---|
| 107 | // set up the query params
|
---|
| 108 | Set entries = params.entrySet();
|
---|
| 109 | Iterator i = entries.iterator();
|
---|
[26103] | 110 | String current_physical_index_name = this.physical_index_name;
|
---|
[25849] | 111 | String physical_sub_index_name = this.default_index_subcollection;
|
---|
| 112 | String physical_index_language_name = this.default_index_language;
|
---|
| 113 | while (i.hasNext())
|
---|
| 114 | {
|
---|
| 115 | Map.Entry m = (Map.Entry) i.next();
|
---|
| 116 | String name = (String) m.getKey();
|
---|
| 117 | String value = (String) m.getValue();
|
---|
| 118 |
|
---|
| 119 | if (name.equals(CASE_PARAM))
|
---|
| 120 | {
|
---|
| 121 | boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
|
---|
| 122 | mgpp_src.setCase(val);
|
---|
| 123 | }
|
---|
| 124 | else if (name.equals(STEM_PARAM))
|
---|
| 125 | {
|
---|
| 126 | boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
|
---|
| 127 | mgpp_src.setStem(val);
|
---|
| 128 | }
|
---|
| 129 | else if (name.equals(ACCENT_PARAM))
|
---|
| 130 | {
|
---|
| 131 | boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
|
---|
| 132 | mgpp_src.setAccentFold(val);
|
---|
| 133 | }
|
---|
| 134 | else if (name.equals(MAXDOCS_PARAM) && !value.equals(""))
|
---|
| 135 | {
|
---|
| 136 | int docs = Integer.parseInt(value);
|
---|
| 137 | mgpp_src.setMaxDocs(docs);
|
---|
| 138 | }
|
---|
| 139 | else if (name.equals(LEVEL_PARAM))
|
---|
| 140 | {
|
---|
| 141 | mgpp_src.setQueryLevel(value);
|
---|
| 142 | }
|
---|
| 143 | else if (name.equals(MATCH_PARAM))
|
---|
| 144 | {
|
---|
| 145 | int mode;
|
---|
| 146 | if (value.equals(MATCH_PARAM_ALL))
|
---|
| 147 | mode = 1;
|
---|
| 148 | else
|
---|
| 149 | mode = 0;
|
---|
| 150 | mgpp_src.setMatchMode(mode);
|
---|
| 151 | }
|
---|
| 152 | else if (name.equals(RANK_PARAM))
|
---|
| 153 | {
|
---|
| 154 | if (value.equals(RANK_PARAM_RANK))
|
---|
| 155 | {
|
---|
| 156 | mgpp_src.setSortByRank(true);
|
---|
| 157 | }
|
---|
| 158 | else if (value.equals(RANK_PARAM_NONE))
|
---|
| 159 | {
|
---|
| 160 | mgpp_src.setSortByRank(false);
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | else if (name.equals(INDEX_SUBCOLLECTION_PARAM))
|
---|
| 164 | {
|
---|
| 165 | physical_sub_index_name = value;
|
---|
| 166 | }
|
---|
| 167 | else if (name.equals(INDEX_LANGUAGE_PARAM))
|
---|
| 168 | {
|
---|
| 169 | physical_index_language_name = value;
|
---|
| 170 | } // ignore any others
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | if (physical_index_name.equals("idx"))
|
---|
| 174 | {
|
---|
| 175 | if (physical_sub_index_name != null)
|
---|
| 176 | {
|
---|
[26103] | 177 | current_physical_index_name += physical_sub_index_name;
|
---|
[25849] | 178 | }
|
---|
| 179 | if (physical_index_language_name != null)
|
---|
| 180 | {
|
---|
[26103] | 181 | current_physical_index_name += physical_index_language_name;
|
---|
[25849] | 182 | }
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | // set up mgpp_src
|
---|
[26103] | 186 | String indexdir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) + File.separatorChar + GSFile.collectionIndexPath(this.index_stem, current_physical_index_name);
|
---|
[25849] | 187 | mgpp_src.loadIndexData(indexdir);
|
---|
| 188 |
|
---|
| 189 | return true;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | protected Object runQuery(String query)
|
---|
| 193 | {
|
---|
| 194 | mgpp_src.runQuery(query);
|
---|
| 195 | MGPPQueryResult mqr = mgpp_src.getQueryResult();
|
---|
| 196 | return mqr;
|
---|
| 197 |
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | protected long numDocsMatched(Object query_result)
|
---|
| 201 | {
|
---|
| 202 | return ((MGPPQueryResult) query_result).getTotalDocs();
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | protected String[] getDocIDs(Object query_result)
|
---|
| 206 | {
|
---|
| 207 |
|
---|
| 208 | Vector docs = ((MGPPQueryResult) query_result).getDocs();
|
---|
| 209 | String[] doc_nums = new String[docs.size()];
|
---|
| 210 | for (int d = 0; d < docs.size(); d++)
|
---|
| 211 | {
|
---|
| 212 | doc_nums[d] = Long.toString((((MGPPDocInfo) docs.elementAt(d)).num_));
|
---|
| 213 | }
|
---|
| 214 | return doc_nums;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | protected String[] getDocRanks(Object query_result)
|
---|
| 218 | {
|
---|
| 219 |
|
---|
| 220 | Vector docs = ((MGPPQueryResult) query_result).getDocs();
|
---|
| 221 | String[] doc_ranks = new String[docs.size()];
|
---|
| 222 | for (int d = 0; d < docs.size(); d++)
|
---|
| 223 | {
|
---|
| 224 | doc_ranks[d] = Float.toString(((MGPPDocInfo) docs.elementAt(d)).rank_);
|
---|
| 225 | }
|
---|
| 226 | return doc_ranks;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | protected boolean addTermInfo(Element term_list, HashMap<String, Serializable> params, Object query_result)
|
---|
| 230 | {
|
---|
| 231 |
|
---|
| 232 | String query_level = (String) params.get(LEVEL_PARAM); // the current query level
|
---|
| 233 |
|
---|
| 234 | Vector terms = ((MGPPQueryResult) query_result).getTerms();
|
---|
| 235 | for (int t = 0; t < terms.size(); t++)
|
---|
| 236 | {
|
---|
| 237 | MGPPTermInfo term_info = (MGPPTermInfo) terms.get(t);
|
---|
| 238 |
|
---|
| 239 | Element term_elem = this.doc.createElement(GSXML.TERM_ELEM);
|
---|
| 240 | term_elem.setAttribute(GSXML.NAME_ATT, term_info.term_);
|
---|
| 241 | term_elem.setAttribute(STEM_ATT, "" + term_info.stem_method_);
|
---|
| 242 | term_elem.setAttribute(FREQ_ATT, "" + term_info.term_freq_);
|
---|
| 243 | term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + term_info.match_docs_);
|
---|
| 244 | String field = term_info.tag_;
|
---|
| 245 | if (field.equals(query_level))
|
---|
| 246 | {
|
---|
| 247 | // ignore
|
---|
| 248 | field = "";
|
---|
| 249 | }
|
---|
| 250 | term_elem.setAttribute(FIELD_ATT, field);
|
---|
| 251 |
|
---|
| 252 | Vector equiv_terms = term_info.equiv_terms_;
|
---|
| 253 | Element equiv_term_list = this.doc.createElement(EQUIV_TERM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 254 | term_elem.appendChild(equiv_term_list);
|
---|
| 255 |
|
---|
| 256 | for (int et = 0; et < equiv_terms.size(); et++)
|
---|
| 257 | {
|
---|
| 258 | String equiv_term = (String) equiv_terms.get(et);
|
---|
| 259 |
|
---|
| 260 | Element equiv_term_elem = this.doc.createElement(GSXML.TERM_ELEM);
|
---|
| 261 | equiv_term_elem.setAttribute(GSXML.NAME_ATT, equiv_term);
|
---|
| 262 | equiv_term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "");
|
---|
| 263 | equiv_term_elem.setAttribute(FREQ_ATT, "");
|
---|
| 264 | equiv_term_list.appendChild(equiv_term_elem);
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | term_list.appendChild(term_elem);
|
---|
| 268 | }
|
---|
| 269 | return true;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | protected String addFieldInfo(String query, String field)
|
---|
| 273 | {
|
---|
| 274 | if (field.equals("") || field.equals("ZZ"))
|
---|
| 275 | {
|
---|
| 276 | return query;
|
---|
| 277 | }
|
---|
| 278 | return "[" + query + "]:" + field;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | protected void addQueryElem(StringBuffer final_query, String query, String field, String combine)
|
---|
| 282 | {
|
---|
| 283 |
|
---|
| 284 | String comb = "";
|
---|
| 285 | if (final_query.length() > 0)
|
---|
| 286 | {
|
---|
| 287 | comb = " " + combine + " ";
|
---|
| 288 | }
|
---|
| 289 | final_query.append(comb + addFieldInfo(query, field));
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | protected String addStemOptions(String query, String stem, String casef, String accent)
|
---|
| 293 | {
|
---|
| 294 | String mods = "#";
|
---|
| 295 | if (casef != null)
|
---|
| 296 | {
|
---|
| 297 | if (casef.equals("1"))
|
---|
| 298 | {
|
---|
| 299 | mods += "i";
|
---|
| 300 | }
|
---|
| 301 | else
|
---|
| 302 | {
|
---|
| 303 | mods += "c";
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
| 306 | if (stem != null)
|
---|
| 307 | {
|
---|
| 308 | if (stem.equals("1"))
|
---|
| 309 | {
|
---|
| 310 | mods += "s";
|
---|
| 311 | }
|
---|
| 312 | else
|
---|
| 313 | {
|
---|
| 314 | mods += "u";
|
---|
| 315 | }
|
---|
| 316 | }
|
---|
| 317 | if (accent != null)
|
---|
| 318 | {
|
---|
| 319 | if (accent.equals("1"))
|
---|
| 320 | {
|
---|
| 321 | mods += "f";
|
---|
| 322 | }
|
---|
| 323 | else
|
---|
| 324 | {
|
---|
| 325 | mods += "a";
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | StringBuffer temp = new StringBuffer();
|
---|
| 330 | String[] terms = query.split(" ");
|
---|
| 331 | for (int i = 0; i < terms.length; i++)
|
---|
| 332 | {
|
---|
| 333 | String t = terms[i].trim();
|
---|
| 334 | // what is the TX bit about???
|
---|
| 335 | if (!t.equals("") && !t.equals("TX"))
|
---|
| 336 | {
|
---|
| 337 | temp.append(" " + t + mods);
|
---|
| 338 | }
|
---|
| 339 | }
|
---|
| 340 | return temp.toString();
|
---|
| 341 | }
|
---|
| 342 |
|
---|
[25885] | 343 | protected ArrayList<FacetWrapper> getFacets(Object query_result)
|
---|
| 344 | {
|
---|
| 345 | return null;
|
---|
| 346 | }
|
---|
[25849] | 347 | }
|
---|