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

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

Fixing Greenstone 3's use (or lack thereof) of generics, this was done automatically so we may want to change it over time. This change will also auto-format any files that have not already been formatted.

  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 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
21// Greenstone classes
22import org.greenstone.mgpp.MGPPSearchWrapper;
23import org.greenstone.mgpp.MGPPTermInfo;
24import org.greenstone.mgpp.MGPPQueryResult;
25import org.greenstone.mgpp.MGPPDocInfo;
26
27import org.greenstone.gsdl3.util.GSFile;
28import org.greenstone.gsdl3.util.GSXML;
29
30
31// XML classes
32import org.w3c.dom.Document;
33import org.w3c.dom.Element;
34import org.w3c.dom.NodeList;
35
36// java classes
37import java.util.Iterator;
38import java.util.Set;
39import java.util.HashMap;
40import java.util.Map;
41import java.util.ArrayList;
42import java.util.Vector;
43import java.io.File;
44import java.io.Serializable;
45
46import org.apache.log4j.*;
47
48
49public class GS2MGPPSearch
50 extends AbstractGS2FieldSearch {
51 private static MGPPSearchWrapper mgpp_src=null;
52
53 private String physical_index_name = "idx";
54
55 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2MGPPSearch.class.getName());
56
57 /** constructor */
58 public GS2MGPPSearch() {
59 if(mgpp_src == null) {
60 mgpp_src = new MGPPSearchWrapper();
61 }
62 }
63
64 public void cleanUp() {
65 super.cleanUp();
66 mgpp_src.unloadIndexData();
67 }
68 /** process a query */
69 protected Element processAnyQuery(Element request, int query_type) {
70 synchronized (mgpp_src) {
71 return super.processAnyQuery(request, query_type);
72 }
73 }
74 /** configure this service */
75 public boolean configure(Element info, Element extra_info) {
76 if (!super.configure(info, extra_info)){
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 super.addCustomQueryParams(param_list, lang);
93 createParameter(RANK_PARAM, param_list, lang);
94 }
95
96 protected boolean setUpQueryer(HashMap<String, Serializable> params) {
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 Map.Entry m = (Map.Entry)i.next();
110 String name = (String)m.getKey();
111 String value = (String)m.getValue();
112
113 if (name.equals(CASE_PARAM)) {
114 boolean val = (value.equals(BOOLEAN_PARAM_ON)?true:false);
115 mgpp_src.setCase(val);
116 } else if (name.equals(STEM_PARAM)) {
117 boolean val = (value.equals(BOOLEAN_PARAM_ON)?true:false);
118 mgpp_src.setStem(val);
119 } else if (name.equals(ACCENT_PARAM)) {
120 boolean val = (value.equals(BOOLEAN_PARAM_ON)?true:false);
121 mgpp_src.setAccentFold(val);
122 } else if (name.equals(MAXDOCS_PARAM)&& !value.equals("")) {
123 int docs = Integer.parseInt(value);
124 mgpp_src.setMaxDocs(docs);
125 } else if (name.equals(LEVEL_PARAM)) {
126 mgpp_src.setQueryLevel(value);
127 } else if (name.equals(MATCH_PARAM)) {
128 int mode;
129 if (value.equals(MATCH_PARAM_ALL)) mode=1;
130 else mode=0;
131 mgpp_src.setMatchMode(mode);
132 } else if (name.equals(RANK_PARAM)) {
133 if (value.equals(RANK_PARAM_RANK)) {
134 mgpp_src.setSortByRank(true);
135 } else if (value.equals(RANK_PARAM_NONE)) {
136 mgpp_src.setSortByRank(false);
137 }
138 } else if (name.equals(INDEX_SUBCOLLECTION_PARAM)) {
139 physical_sub_index_name=value;
140 }else if (name.equals(INDEX_LANGUAGE_PARAM)){
141 physical_index_language_name=value;
142 } // ignore any others
143 }
144
145 if (physical_index_name.equals("idx")){
146 if (physical_sub_index_name!=null) {
147 physical_index_name+=physical_sub_index_name;
148 }
149 if (physical_index_language_name!=null){
150 physical_index_name+=physical_index_language_name;
151 }
152 }
153
154 // set up mgpp_src
155 String indexdir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) + File.separatorChar + GSFile.collectionIndexPath(this.index_stem, physical_index_name);
156 mgpp_src.loadIndexData(indexdir);
157
158 return true;
159 }
160
161 protected Object runQuery(String query) {
162 mgpp_src.runQuery(query);
163 MGPPQueryResult mqr= mgpp_src.getQueryResult();
164 return mqr;
165
166 }
167
168 protected long numDocsMatched(Object query_result) {
169 return ((MGPPQueryResult)query_result).getTotalDocs();
170 }
171
172 protected String [] getDocIDs(Object query_result) {
173
174 Vector docs = ((MGPPQueryResult)query_result).getDocs();
175 String [] doc_nums = new String [docs.size()];
176 for (int d = 0; d < docs.size(); d++) {
177 doc_nums[d] = Long.toString((((MGPPDocInfo) docs.elementAt(d)).num_));
178 }
179 return doc_nums;
180 }
181
182 protected String [] getDocRanks(Object query_result) {
183
184 Vector docs = ((MGPPQueryResult)query_result).getDocs();
185 String [] doc_ranks = new String [docs.size()];
186 for (int d = 0; d < docs.size(); d++) {
187 doc_ranks[d] = Float.toString(((MGPPDocInfo) docs.elementAt(d)).rank_);
188 }
189 return doc_ranks;
190 }
191
192 protected boolean addTermInfo(Element term_list, HashMap<String, Serializable> params,
193 Object query_result) {
194
195 String query_level = (String)params.get(LEVEL_PARAM); // the current query level
196
197 Vector terms = ((MGPPQueryResult)query_result).getTerms();
198 for (int t = 0; t < terms.size(); t++) {
199 MGPPTermInfo term_info = (MGPPTermInfo) terms.get(t);
200
201 Element term_elem = this.doc.createElement(GSXML.TERM_ELEM);
202 term_elem.setAttribute(GSXML.NAME_ATT, term_info.term_);
203 term_elem.setAttribute(STEM_ATT, "" + term_info.stem_method_);
204 term_elem.setAttribute(FREQ_ATT, "" + term_info.term_freq_);
205 term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + term_info.match_docs_);
206 String field = term_info.tag_;
207 if (field.equals(query_level)) {
208 // ignore
209 field = "";
210 }
211 term_elem.setAttribute(FIELD_ATT, field);
212
213 Vector equiv_terms = term_info.equiv_terms_;
214 Element equiv_term_list = this.doc.createElement(EQUIV_TERM_ELEM+GSXML.LIST_MODIFIER);
215 term_elem.appendChild(equiv_term_list);
216
217 for (int et = 0; et < equiv_terms.size(); et++) {
218 String equiv_term = (String) equiv_terms.get(et);
219
220 Element equiv_term_elem = this.doc.createElement(GSXML.TERM_ELEM);
221 equiv_term_elem.setAttribute(GSXML.NAME_ATT, equiv_term);
222 equiv_term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "");
223 equiv_term_elem.setAttribute(FREQ_ATT, "");
224 equiv_term_list.appendChild(equiv_term_elem);
225 }
226
227 term_list.appendChild(term_elem);
228 }
229 return true;
230 }
231
232
233 protected String addFieldInfo(String query, String field) {
234 if (field.equals("") || field.equals("ZZ")) {
235 return query;
236 }
237 return "["+query+"]:"+field;
238 }
239 protected void addQueryElem(StringBuffer final_query, String query,
240 String field, String combine) {
241
242 String comb="";
243 if (final_query.length()>0) {
244 comb = " "+combine+" ";
245 }
246 final_query.append(comb+addFieldInfo(query,field));
247 }
248
249 protected String addStemOptions(String query, String stem,
250 String casef, String accent) {
251 String mods = "#";
252 if (casef != null) {
253 if (casef.equals("1")) {
254 mods += "i";
255 } else {
256 mods += "c";
257 }
258 }
259 if (stem != null) {
260 if (stem.equals("1")) {
261 mods += "s";
262 } else {
263 mods+= "u";
264 }
265 }
266 if (accent != null) {
267 if (accent.equals("1")) {
268 mods += "f";
269 } else {
270 mods += "a";
271 }
272 }
273
274 StringBuffer temp = new StringBuffer();
275 String [] terms = query.split(" ");
276 for (int i=0; i<terms.length; i++) {
277 String t = terms[i].trim();
278 // what is the TX bit about???
279 if (!t.equals("") && !t.equals("TX")) {
280 temp.append(" "+t+mods);
281 }
282 }
283 return temp.toString();
284 }
285
286}
287
288
Note: See TracBrowser for help on using the repository browser.