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

Last change on this file since 14440 was 14440, checked in by xiao, 17 years ago

make MGPPSearchWrapper a static variable; add a synchronized method processAnyQuery() to override that in its superclass.

  • Property svn:keywords set to Author Date Id Revision
File size: 8.7 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.*;
23import org.greenstone.gsdl3.util.*;
24
25// XML classes
26import org.w3c.dom.Document;
27import org.w3c.dom.Element;
28import org.w3c.dom.NodeList;
29
30// java classes
31import java.util.Iterator;
32import java.util.Set;
33import java.util.HashMap;
34import java.util.Map;
35import java.util.ArrayList;
36import java.util.Vector;
37import java.io.File;
38
39import org.apache.log4j.*;
40
41/**
42 *
43 * @author <a href="mailto:[email protected]">Katherine Don</a>
44 * @author <a href="mailto:[email protected]">Michael Dewsnip</a>
45 */
46
47public class GS2MGPPSearch
48 extends AbstractGS2FieldSearch {
49 private static MGPPSearchWrapper mgpp_src=null;
50
51 private String physical_index_name = "idx";
52
53 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2MGPPSearch.class.getName());
54
55 /** constructor */
56 public GS2MGPPSearch() {
57 if(mgpp_src == null) {
58 mgpp_src = new MGPPSearchWrapper();
59 }
60 }
61
62 public void cleanUp() {
63 super.cleanUp();
64 mgpp_src.unloadIndexData();
65 }
66 /** process a query */
67 protected Element processAnyQuery(Element request, int query_type) {
68 synchronized (mgpp_src) {
69 return super.processAnyQuery(request, query_type);
70 }
71 }
72 /** configure this service */
73 public boolean configure(Element info, Element extra_info) {
74 if (!super.configure(info, extra_info)){
75 return false;
76 }
77
78 // the default level is also the level which gdbm is expecting
79 // this must not be overwritten
80 mgpp_src.setReturnLevel(this.default_gdbm_level);
81 // return term info
82 mgpp_src.setReturnTerms(true);
83 // set the default - this may be overwritten by query params
84 mgpp_src.setQueryLevel(this.default_level);
85 mgpp_src.setMaxNumeric(this.maxnumeric);
86
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 params) {
97
98 // set up the query params
99 Set entries = params.entrySet();
100 Iterator i = entries.iterator();
101 String physical_sub_index_name=null;
102 String physical_index_language_name=null;
103 while (i.hasNext()) {
104 Map.Entry m = (Map.Entry)i.next();
105 String name = (String)m.getKey();
106 String value = (String)m.getValue();
107
108 if (name.equals(CASE_PARAM)) {
109 boolean val = (value.equals(BOOLEAN_PARAM_ON)?true:false);
110 mgpp_src.setCase(val);
111 } else if (name.equals(STEM_PARAM)) {
112 boolean val = (value.equals(BOOLEAN_PARAM_ON)?true:false);
113 mgpp_src.setStem(val);
114 } else if (name.equals(ACCENT_PARAM)) {
115 boolean val = (value.equals(BOOLEAN_PARAM_ON)?true:false);
116 mgpp_src.setAccentFold(val);
117 } else if (name.equals(MAXDOCS_PARAM)&& !value.equals("")) {
118 int docs = Integer.parseInt(value);
119 mgpp_src.setMaxDocs(docs);
120 } else if (name.equals(LEVEL_PARAM)) {
121 mgpp_src.setQueryLevel(value);
122 } else if (name.equals(MATCH_PARAM)) {
123 int mode;
124 if (value.equals(MATCH_PARAM_ALL)) mode=1;
125 else mode=0;
126 mgpp_src.setMatchMode(mode);
127 } else if (name.equals(RANK_PARAM)) {
128 if (value.equals(RANK_PARAM_RANK)) {
129 mgpp_src.setSortByRank(true);
130 } else if (value.equals(RANK_PARAM_NONE)) {
131 mgpp_src.setSortByRank(false);
132 }
133 } else if (name.equals(INDEX_SUBCOLLECTION_PARAM)) {
134 physical_sub_index_name=value;
135 }else if (name.equals(INDEX_LANGUAGE_PARAM)){
136 physical_index_language_name=value;
137 } // ignore any others
138 }
139
140 if (physical_index_name.equals("idx")){
141 if (physical_sub_index_name!=null) {
142 physical_index_name+=physical_sub_index_name;
143 }
144 if (physical_index_language_name!=null){
145 physical_index_name+=physical_index_language_name;
146 }
147 }
148
149 // set up mgpp_src
150 String indexdir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) + File.separatorChar + GSFile.collectionIndexPath(this.index_stem, physical_index_name);
151 mgpp_src.loadIndexData(indexdir);
152
153 physical_index_name="idx";
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.