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