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

Last change on this file since 14752 was 14752, checked in by qq6, 16 years ago

set default_index_subcollection and default_index_language to physical_sub_index_name and physical_index_language_name respectively

  • 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= this.default_index_subcollection;
102 String physical_index_language_name= this.default_index_language;
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 return true;
154 }
155
156 protected Object runQuery(String query) {
157 mgpp_src.runQuery(query);
158 MGPPQueryResult mqr= mgpp_src.getQueryResult();
159 return mqr;
160
161 }
162
163 protected long numDocsMatched(Object query_result) {
164 return ((MGPPQueryResult)query_result).getTotalDocs();
165 }
166
167 protected String [] getDocIDs(Object query_result) {
168
169 Vector docs = ((MGPPQueryResult)query_result).getDocs();
170 String [] doc_nums = new String [docs.size()];
171 for (int d = 0; d < docs.size(); d++) {
172 doc_nums[d] = Long.toString((((MGPPDocInfo) docs.elementAt(d)).num_));
173 }
174 return doc_nums;
175 }
176
177 protected String [] getDocRanks(Object query_result) {
178
179 Vector docs = ((MGPPQueryResult)query_result).getDocs();
180 String [] doc_ranks = new String [docs.size()];
181 for (int d = 0; d < docs.size(); d++) {
182 doc_ranks[d] = Float.toString(((MGPPDocInfo) docs.elementAt(d)).rank_);
183 }
184 return doc_ranks;
185 }
186
187 protected boolean addTermInfo(Element term_list, HashMap params,
188 Object query_result) {
189
190 String query_level = (String)params.get(LEVEL_PARAM); // the current query level
191
192 Vector terms = ((MGPPQueryResult)query_result).getTerms();
193 for (int t = 0; t < terms.size(); t++) {
194 MGPPTermInfo term_info = (MGPPTermInfo) terms.get(t);
195
196 Element term_elem = this.doc.createElement(GSXML.TERM_ELEM);
197 term_elem.setAttribute(GSXML.NAME_ATT, term_info.term_);
198 term_elem.setAttribute(STEM_ATT, "" + term_info.stem_method_);
199 term_elem.setAttribute(FREQ_ATT, "" + term_info.term_freq_);
200 term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + term_info.match_docs_);
201 String field = term_info.tag_;
202 if (field.equals(query_level)) {
203 // ignore
204 field = "";
205 }
206 term_elem.setAttribute(FIELD_ATT, field);
207
208 Vector equiv_terms = term_info.equiv_terms_;
209 Element equiv_term_list = this.doc.createElement(EQUIV_TERM_ELEM+GSXML.LIST_MODIFIER);
210 term_elem.appendChild(equiv_term_list);
211
212 for (int et = 0; et < equiv_terms.size(); et++) {
213 String equiv_term = (String) equiv_terms.get(et);
214
215 Element equiv_term_elem = this.doc.createElement(GSXML.TERM_ELEM);
216 equiv_term_elem.setAttribute(GSXML.NAME_ATT, equiv_term);
217 equiv_term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "");
218 equiv_term_elem.setAttribute(FREQ_ATT, "");
219 equiv_term_list.appendChild(equiv_term_elem);
220 }
221
222 term_list.appendChild(term_elem);
223 }
224 return true;
225 }
226
227
228 protected String addFieldInfo(String query, String field) {
229 if (field.equals("") || field.equals("ZZ")) {
230 return query;
231 }
232 return "["+query+"]:"+field;
233 }
234 protected void addQueryElem(StringBuffer final_query, String query,
235 String field, String combine) {
236
237 String comb="";
238 if (final_query.length()>0) {
239 comb = " "+combine+" ";
240 }
241 final_query.append(comb+addFieldInfo(query,field));
242 }
243
244 protected String addStemOptions(String query, String stem,
245 String casef, String accent) {
246 String mods = "#";
247 if (casef != null) {
248 if (casef.equals("1")) {
249 mods += "i";
250 } else {
251 mods += "c";
252 }
253 }
254 if (stem != null) {
255 if (stem.equals("1")) {
256 mods += "s";
257 } else {
258 mods+= "u";
259 }
260 }
261 if (accent != null) {
262 if (accent.equals("1")) {
263 mods += "f";
264 } else {
265 mods += "a";
266 }
267 }
268
269 StringBuffer temp = new StringBuffer();
270 String [] terms = query.split(" ");
271 for (int i=0; i<terms.length; i++) {
272 String t = terms[i].trim();
273 // what is the TX bit about???
274 if (!t.equals("") && !t.equals("TX")) {
275 temp.append(" "+t+mods);
276 }
277 }
278 return temp.toString();
279 }
280
281}
282
283
Note: See TracBrowser for help on using the repository browser.