source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/GS2LuceneSearch.java@ 30147

Last change on this file since 30147 was 30147, checked in by kjdon, 9 years ago

making start_results always start at 0, as sometimes it was 1, sometimes it was 0, and it was too confusing

  • Property svn:keywords set to Author Date Id Revision
File size: 9.1 KB
Line 
1/*
2 * GS2LuceneSearch.java
3 * Copyright (C) 2006 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 */
18
19package org.greenstone.gsdl3.service;
20
21// Greenstone classes
22import java.io.File;
23import java.io.Serializable;
24import java.util.ArrayList;
25import java.util.HashMap;
26import java.util.Iterator;
27import java.util.List;
28import java.util.Map;
29import java.util.Set;
30import java.util.Vector;
31
32import org.apache.log4j.Logger;
33import org.greenstone.LuceneWrapper4.GS2LuceneQuery;
34import org.greenstone.LuceneWrapper4.LuceneQueryResult;
35import org.greenstone.gsdl3.util.FacetWrapper;
36import org.greenstone.gsdl3.util.GSFile;
37import org.greenstone.gsdl3.util.GSXML;
38import org.greenstone.gsdl3.util.XMLConverter;
39import org.w3c.dom.Document;
40import org.w3c.dom.Element;
41
42public class GS2LuceneSearch extends SharedSoleneGS2FieldSearch
43{
44
45 protected static final String SORT_ORDER_PARAM = "reverseSort";
46 protected static final String SORT_ORDER_REVERSE = "1";
47 protected static final String SORT_ORDER_NORMAL = "0";
48
49 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2LuceneSearch.class.getName());
50
51 private GS2LuceneQuery lucene_src = null;
52
53 public GS2LuceneSearch()
54 {
55 does_paging = true;
56 paramDefaults.put(SORT_ORDER_PARAM, SORT_ORDER_NORMAL);
57 this.lucene_src = new GS2LuceneQuery();
58 }
59
60 public void cleanUp()
61 {
62 super.cleanUp();
63 this.lucene_src.cleanUp();
64 }
65
66 /** add in the Lucene specific params to TextQuery */
67 protected void addCustomQueryParams(Element param_list, String lang)
68 {
69 super.addCustomQueryParams(param_list, lang);
70 /** Add in the reverse sort on/off param */
71 createParameter(SORT_ORDER_PARAM, param_list, lang);
72 }
73 /** add in Lucene specific params for AdvancedFieldQuery */
74 protected void addCustomQueryParamsAdvField(Element param_list, String lang)
75 {
76 super.addCustomQueryParamsAdvField(param_list, lang);
77 createParameter(SORT_ORDER_PARAM, param_list, lang);
78
79 }
80 /** create a param and add to the list */
81 protected void createParameter(String name, Element param_list, String lang)
82 {
83 Document doc = param_list.getOwnerDocument();
84 Element param = null;
85 String param_default = paramDefaults.get(name);
86 if (name.equals(SORT_ORDER_PARAM)) {
87 String[] vals = { SORT_ORDER_REVERSE, SORT_ORDER_NORMAL };
88 String[] vals_texts = { getTextString("param." + SORT_ORDER_PARAM + "." + SORT_ORDER_REVERSE, lang), getTextString("param." + SORT_ORDER_PARAM + "." + SORT_ORDER_NORMAL, lang) };
89
90 param = GSXML.createParameterDescription(doc, SORT_ORDER_PARAM, getTextString("param." + SORT_ORDER_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, param_default, vals, vals_texts);
91 }
92
93 if (param != null)
94 {
95 param_list.appendChild(param);
96 }
97 else
98 {
99 super.createParameter(name, param_list, lang);
100 }
101
102 }
103
104 /** methods to handle actually doing the query */
105
106 /** do any initialisation of the query object */
107 protected boolean setUpQueryer(HashMap params)
108 {
109 String indexdir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) + File.separatorChar + "index" + File.separatorChar;
110
111 String index = "didx";
112 if (this.default_level.toUpperCase().equals("SEC")) {
113 index = "sidx";
114 }
115 String physical_index_language_name = null;
116 String physical_sub_index_name = null;
117 int hits_per_page = Integer.parseInt(paramDefaults.get(HITS_PER_PAGE_PARAM));
118 int start_page = Integer.parseInt(paramDefaults.get(START_PAGE_PARAM));
119 String sort_field = getLuceneSort(default_sort);
120 String sort_order = paramDefaults.get(SORT_ORDER_PARAM);
121
122 // set up the query params
123 Set entries = params.entrySet();
124 Iterator i = entries.iterator();
125 while (i.hasNext())
126 {
127 Map.Entry m = (Map.Entry) i.next();
128 String name = (String) m.getKey();
129 String value = (String) m.getValue();
130
131 if (name.equals(HITS_PER_PAGE_PARAM))
132 {
133 if (value.equals("all")) {
134 hits_per_page = -1;
135 } else {
136 hits_per_page = Integer.parseInt(value);
137 }
138 }
139 else if (name.equals(START_PAGE_PARAM))
140 {
141 start_page = Integer.parseInt(value);
142
143 }
144 else if (name.equals(MATCH_PARAM))
145 {
146 if (value.equals(MATCH_PARAM_ALL))
147 {
148 this.lucene_src.setDefaultConjunctionOperator("AND");
149 }
150 else
151 {
152 this.lucene_src.setDefaultConjunctionOperator("OR");
153 }
154 }
155 else if (name.equals(RANK_PARAM))
156 {
157 sort_field = getLuceneSort(value);
158 this.lucene_src.setSortField(sort_field);
159
160 }
161 else if (name.equals(SORT_ORDER_PARAM)) {
162 sort_order = value;
163 }
164 else if (name.equals(LEVEL_PARAM))
165 {
166 if (value.toUpperCase().equals("SEC"))
167 {
168 index = "sidx";
169 }
170 else
171 {
172 index = "didx";
173 }
174 }
175 else if (name.equals(INDEX_SUBCOLLECTION_PARAM))
176 {
177 physical_sub_index_name = value;
178 }
179 else if (name.equals(INDEX_LANGUAGE_PARAM))
180 {
181 physical_index_language_name = value;
182 } // ignore any others
183 }
184 // set up start and end results if necessary
185 // start results always start at 0
186 int start_results = 0;
187 if (start_page > 1 && hits_per_page > 0)
188 {
189 start_results = ((start_page - 1) * hits_per_page) ;
190 }
191 int end_results = Integer.MAX_VALUE;
192 if (hits_per_page > 0) {
193 end_results = hits_per_page * start_page;
194 }
195 this.lucene_src.setStartResults(start_results);
196 this.lucene_src.setEndResults(end_results);
197
198 if (index.equals("sidx") || index.equals("didx"))
199 {
200 if (physical_sub_index_name != null)
201 {
202 index += physical_sub_index_name;
203 }
204 if (physical_index_language_name != null)
205 {
206 index += physical_index_language_name;
207 }
208 }
209
210 if (sort_order.equals(SORT_ORDER_REVERSE)) {
211 this.lucene_src.setReverseSort(true);
212 } else {
213 this.lucene_src.setReverseSort(false);
214 }
215 this.lucene_src.setIndexDir(indexdir + index);
216 this.lucene_src.initialise();
217 return true;
218 }
219
220 /** do the query */
221 protected Object runQuery(String query)
222 {
223 try
224 {
225 LuceneQueryResult lqr = this.lucene_src.runQuery(query);
226 return lqr;
227 }
228 catch (Exception e)
229 {
230 logger.error("Exception happened in runQuery(): ", e);
231 }
232
233 return null;
234 }
235
236 /** get the total number of docs that match */
237 protected long numDocsMatched(Object query_result)
238 {
239 return ((LuceneQueryResult) query_result).getTotalDocs();
240 }
241
242 /** get the list of doc ids */
243 protected String[] getDocIDs(Object query_result)
244 {
245 Vector docs = ((LuceneQueryResult) query_result).getDocs();
246 String[] doc_nums = new String[docs.size()];
247 for (int d = 0; d < docs.size(); d++)
248 {
249 String doc_num = ((LuceneQueryResult.DocInfo) docs.elementAt(d)).id_;
250 doc_nums[d] = doc_num;
251 }
252 return doc_nums;
253 }
254
255 /** get the list of doc ranks */
256 protected String[] getDocRanks(Object query_result)
257 {
258 Vector docs = ((LuceneQueryResult) query_result).getDocs();
259 String[] doc_ranks = new String[docs.size()];
260 for (int d = 0; d < docs.size(); d++)
261 {
262 doc_ranks[d] = Float.toString(((LuceneQueryResult.DocInfo) docs.elementAt(d)).rank_);
263 }
264 return doc_ranks;
265 }
266
267 /** add in term info if available */
268 protected boolean addTermInfo(Element term_list, HashMap params, Object query_result)
269 {
270 Document doc = term_list.getOwnerDocument();
271 String query_level = (String) params.get(LEVEL_PARAM); // the current query level
272
273 Vector terms = ((LuceneQueryResult) query_result).getTerms();
274 for (int t = 0; t < terms.size(); t++)
275 {
276 LuceneQueryResult.TermInfo term_info = (LuceneQueryResult.TermInfo) terms.get(t);
277
278 Element term_elem = doc.createElement(GSXML.TERM_ELEM);
279 term_elem.setAttribute(GSXML.NAME_ATT, term_info.term_);
280 term_elem.setAttribute(FREQ_ATT, "" + term_info.term_freq_);
281 term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + term_info.match_docs_);
282 term_elem.setAttribute(FIELD_ATT, term_info.field_);
283 term_list.appendChild(term_elem);
284 }
285
286 Vector stopwords = ((LuceneQueryResult) query_result).getStopWords();
287 for (int t = 0; t < stopwords.size(); t++)
288 {
289 String stopword = (String) stopwords.get(t);
290
291 Element stopword_elem = doc.createElement(GSXML.STOPWORD_ELEM);
292 stopword_elem.setAttribute(GSXML.NAME_ATT, stopword);
293 term_list.appendChild(stopword_elem);
294 }
295
296 return true;
297 }
298
299 protected ArrayList<FacetWrapper> getFacets(Object query_result)
300 {
301 return null;
302 }
303
304 protected String getLuceneSort(String gs3_sort) {
305
306 if (gs3_sort.equals(RANK_PARAM_RANK)) {
307 return GS2LuceneQuery.SORT_RANK;
308 }
309 if (gs3_sort.equals(RANK_PARAM_NONE)) {
310 return GS2LuceneQuery.SORT_NATURAL;
311 }
312 return gs3_sort;
313 }
314
315@Override
316protected Map<String, Map<String, List<String>>> getHighlightSnippets(
317 Object query_result) {
318 // TODO Auto-generated method stub
319 return null;
320}
321
322}
Note: See TracBrowser for help on using the repository browser.