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