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