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

Last change on this file since 16873 was 16873, checked in by kjdon, 16 years ago

removed our email addresses from the code

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