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 | /** 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 | String physical_index_language_name = null;
|
---|
113 | String physical_sub_index_name = null;
|
---|
114 | int hits_per_page = Integer.parseInt(paramDefaults.get(HITS_PER_PAGE_PARAM));
|
---|
115 | int start_page = Integer.parseInt(paramDefaults.get(START_PAGE_PARAM));
|
---|
116 | String sort_field = getLuceneSort(default_sort);
|
---|
117 | String sort_order = paramDefaults.get(SORT_ORDER_PARAM);
|
---|
118 |
|
---|
119 | // set up the query params
|
---|
120 | Set entries = params.entrySet();
|
---|
121 | Iterator i = entries.iterator();
|
---|
122 | while (i.hasNext())
|
---|
123 | {
|
---|
124 | Map.Entry m = (Map.Entry) i.next();
|
---|
125 | String name = (String) m.getKey();
|
---|
126 | String value = (String) m.getValue();
|
---|
127 |
|
---|
128 | if (name.equals(HITS_PER_PAGE_PARAM))
|
---|
129 | {
|
---|
130 | if (value.equals("all")) {
|
---|
131 | hits_per_page = -1;
|
---|
132 | } else {
|
---|
133 | hits_per_page = Integer.parseInt(value);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | else if (name.equals(START_PAGE_PARAM))
|
---|
137 | {
|
---|
138 | start_page = Integer.parseInt(value);
|
---|
139 |
|
---|
140 | }
|
---|
141 | else if (name.equals(MATCH_PARAM))
|
---|
142 | {
|
---|
143 | if (value.equals(MATCH_PARAM_ALL))
|
---|
144 | {
|
---|
145 | this.lucene_src.setDefaultConjunctionOperator("AND");
|
---|
146 | }
|
---|
147 | else
|
---|
148 | {
|
---|
149 | this.lucene_src.setDefaultConjunctionOperator("OR");
|
---|
150 | }
|
---|
151 | }
|
---|
152 | else if (name.equals(RANK_PARAM))
|
---|
153 | {
|
---|
154 | sort_field = getLuceneSort(value);
|
---|
155 | this.lucene_src.setSortField(sort_field);
|
---|
156 |
|
---|
157 | }
|
---|
158 | else if (name.equals(SORT_ORDER_PARAM)) {
|
---|
159 | sort_order = value;
|
---|
160 | }
|
---|
161 | else if (name.equals(LEVEL_PARAM))
|
---|
162 | {
|
---|
163 | if (value.toUpperCase().equals("SEC"))
|
---|
164 | {
|
---|
165 | index = "sidx";
|
---|
166 | }
|
---|
167 | else
|
---|
168 | {
|
---|
169 | index = "didx";
|
---|
170 | }
|
---|
171 | }
|
---|
172 | else if (name.equals(INDEX_SUBCOLLECTION_PARAM))
|
---|
173 | {
|
---|
174 | physical_sub_index_name = value;
|
---|
175 | }
|
---|
176 | else if (name.equals(INDEX_LANGUAGE_PARAM))
|
---|
177 | {
|
---|
178 | physical_index_language_name = value;
|
---|
179 | } // ignore any others
|
---|
180 | }
|
---|
181 | // set up start and end results if necessary
|
---|
182 | int start_results = 1;
|
---|
183 | if (start_page > 1 && hits_per_page > 0)
|
---|
184 | {
|
---|
185 | start_results = ((start_page - 1) * hits_per_page) + 1;
|
---|
186 | }
|
---|
187 | int end_results = Integer.MAX_VALUE;
|
---|
188 | if (hits_per_page > 0) {
|
---|
189 | end_results = hits_per_page * start_page;
|
---|
190 | }
|
---|
191 | this.lucene_src.setStartResults(start_results);
|
---|
192 | this.lucene_src.setEndResults(end_results);
|
---|
193 |
|
---|
194 | if (index.equals("sidx") || index.equals("didx"))
|
---|
195 | {
|
---|
196 | if (physical_sub_index_name != null)
|
---|
197 | {
|
---|
198 | index += physical_sub_index_name;
|
---|
199 | }
|
---|
200 | if (physical_index_language_name != null)
|
---|
201 | {
|
---|
202 | index += physical_index_language_name;
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | if (sort_order.equals(SORT_ORDER_REVERSE)) {
|
---|
207 | this.lucene_src.setReverseSort(true);
|
---|
208 | } else {
|
---|
209 | this.lucene_src.setReverseSort(false);
|
---|
210 | }
|
---|
211 | this.lucene_src.setIndexDir(indexdir + index);
|
---|
212 | this.lucene_src.initialise();
|
---|
213 | return true;
|
---|
214 | }
|
---|
215 |
|
---|
216 | /** do the query */
|
---|
217 | protected Object runQuery(String query)
|
---|
218 | {
|
---|
219 | try
|
---|
220 | {
|
---|
221 | LuceneQueryResult lqr = this.lucene_src.runQuery(query);
|
---|
222 | return lqr;
|
---|
223 | }
|
---|
224 | catch (Exception e)
|
---|
225 | {
|
---|
226 | logger.error("Exception happened in runQuery(): ", e);
|
---|
227 | }
|
---|
228 |
|
---|
229 | return null;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /** get the total number of docs that match */
|
---|
233 | protected long numDocsMatched(Object query_result)
|
---|
234 | {
|
---|
235 | return ((LuceneQueryResult) query_result).getTotalDocs();
|
---|
236 | }
|
---|
237 |
|
---|
238 | /** get the list of doc ids */
|
---|
239 | protected String[] getDocIDs(Object query_result)
|
---|
240 | {
|
---|
241 | Vector docs = ((LuceneQueryResult) query_result).getDocs();
|
---|
242 | String[] doc_nums = new String[docs.size()];
|
---|
243 | for (int d = 0; d < docs.size(); d++)
|
---|
244 | {
|
---|
245 | String doc_num = ((LuceneQueryResult.DocInfo) docs.elementAt(d)).id_;
|
---|
246 | doc_nums[d] = doc_num;
|
---|
247 | }
|
---|
248 | return doc_nums;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /** get the list of doc ranks */
|
---|
252 | protected String[] getDocRanks(Object query_result)
|
---|
253 | {
|
---|
254 | Vector docs = ((LuceneQueryResult) query_result).getDocs();
|
---|
255 | String[] doc_ranks = new String[docs.size()];
|
---|
256 | for (int d = 0; d < docs.size(); d++)
|
---|
257 | {
|
---|
258 | doc_ranks[d] = Float.toString(((LuceneQueryResult.DocInfo) docs.elementAt(d)).rank_);
|
---|
259 | }
|
---|
260 | return doc_ranks;
|
---|
261 | }
|
---|
262 |
|
---|
263 | /** add in term info if available */
|
---|
264 | protected boolean addTermInfo(Element term_list, HashMap params, Object query_result)
|
---|
265 | {
|
---|
266 | Document doc = term_list.getOwnerDocument();
|
---|
267 | String query_level = (String) params.get(LEVEL_PARAM); // the current query level
|
---|
268 |
|
---|
269 | Vector terms = ((LuceneQueryResult) query_result).getTerms();
|
---|
270 | for (int t = 0; t < terms.size(); t++)
|
---|
271 | {
|
---|
272 | LuceneQueryResult.TermInfo term_info = (LuceneQueryResult.TermInfo) terms.get(t);
|
---|
273 |
|
---|
274 | Element term_elem = doc.createElement(GSXML.TERM_ELEM);
|
---|
275 | term_elem.setAttribute(GSXML.NAME_ATT, term_info.term_);
|
---|
276 | term_elem.setAttribute(FREQ_ATT, "" + term_info.term_freq_);
|
---|
277 | term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + term_info.match_docs_);
|
---|
278 | term_elem.setAttribute(FIELD_ATT, term_info.field_);
|
---|
279 | term_list.appendChild(term_elem);
|
---|
280 | }
|
---|
281 |
|
---|
282 | Vector stopwords = ((LuceneQueryResult) query_result).getStopWords();
|
---|
283 | for (int t = 0; t < stopwords.size(); t++)
|
---|
284 | {
|
---|
285 | String stopword = (String) stopwords.get(t);
|
---|
286 |
|
---|
287 | Element stopword_elem = doc.createElement(GSXML.STOPWORD_ELEM);
|
---|
288 | stopword_elem.setAttribute(GSXML.NAME_ATT, stopword);
|
---|
289 | term_list.appendChild(stopword_elem);
|
---|
290 | }
|
---|
291 |
|
---|
292 | return true;
|
---|
293 | }
|
---|
294 |
|
---|
295 | protected ArrayList<FacetWrapper> getFacets(Object query_result)
|
---|
296 | {
|
---|
297 | return null;
|
---|
298 | }
|
---|
299 |
|
---|
300 | protected String getLuceneSort(String gs3_sort) {
|
---|
301 |
|
---|
302 | if (gs3_sort.equals(RANK_PARAM_RANK)) {
|
---|
303 | return GS2LuceneQuery.SORT_RANK;
|
---|
304 | }
|
---|
305 | if (gs3_sort.equals(RANK_PARAM_NONE)) {
|
---|
306 | return GS2LuceneQuery.SORT_NATURAL;
|
---|
307 | }
|
---|
308 | return gs3_sort;
|
---|
309 | }
|
---|
310 |
|
---|
311 | @Override
|
---|
312 | protected Map<String, Map<String, List<String>>> getHighlightSnippets(
|
---|
313 | Object query_result) {
|
---|
314 | // TODO Auto-generated method stub
|
---|
315 | return null;
|
---|
316 | }
|
---|
317 |
|
---|
318 | }
|
---|