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

Last change on this file since 32419 was 32084, checked in by kjdon, 7 years ago

getFacets now includes a lang arg, so we can get teh correct displayName for the facet.

  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
RevLine 
[13238]1/*
[25854]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 */
[13238]18
19package org.greenstone.gsdl3.service;
20
21// Greenstone classes
[13576]22import java.io.File;
[30049]23import java.io.Serializable;
[25885]24import java.util.ArrayList;
[25854]25import java.util.HashMap;
[13576]26import java.util.Iterator;
[30049]27import java.util.List;
[25854]28import java.util.Map;
[13576]29import java.util.Set;
30import java.util.Vector;
[13238]31
[13270]32import org.apache.log4j.Logger;
[29143]33import org.greenstone.LuceneWrapper4.GS2LuceneQuery;
34import org.greenstone.LuceneWrapper4.LuceneQueryResult;
[25885]35import org.greenstone.gsdl3.util.FacetWrapper;
[25854]36import org.greenstone.gsdl3.util.GSFile;
37import org.greenstone.gsdl3.util.GSXML;
[28966]38import org.greenstone.gsdl3.util.XMLConverter;
39import org.w3c.dom.Document;
[25854]40import org.w3c.dom.Element;
[13576]41
[24722]42public class GS2LuceneSearch extends SharedSoleneGS2FieldSearch
[13238]43{
[29558]44
[29544]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
[25854]49 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2LuceneSearch.class.getName());
[13576]50
[25854]51 private GS2LuceneQuery lucene_src = null;
[13238]52
[25854]53 public GS2LuceneSearch()
54 {
[29558]55 does_paging = true;
[29544]56 paramDefaults.put(SORT_ORDER_PARAM, SORT_ORDER_NORMAL);
[25854]57 this.lucene_src = new GS2LuceneQuery();
58 }
[13238]59
[25854]60 public void cleanUp()
61 {
62 super.cleanUp();
63 this.lucene_src.cleanUp();
64 }
[13992]65
[29544]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 }
[29558]103
[25854]104 /** methods to handle actually doing the query */
105
106 /** do any initialisation of the query object */
[25855]107 protected boolean setUpQueryer(HashMap params)
[25854]108 {
109 String indexdir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) + File.separatorChar + "index" + File.separatorChar;
110
111 String index = "didx";
[30145]112 if (this.default_level.toUpperCase().equals("SEC")) {
113 index = "sidx";
114 }
[25854]115 String physical_index_language_name = null;
116 String physical_sub_index_name = null;
[29428]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);
[29558]121
[25854]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
[29558]131 if (name.equals(HITS_PER_PAGE_PARAM))
[25854]132 {
[29558]133 if (value.equals("all")) {
134 hits_per_page = -1;
135 } else {
136 hits_per_page = Integer.parseInt(value);
137 }
[25854]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 {
[29428]157 sort_field = getLuceneSort(value);
158 this.lucene_src.setSortField(sort_field);
159
[25854]160 }
[27085]161 else if (name.equals(SORT_ORDER_PARAM)) {
[28029]162 sort_order = value;
163 }
[25854]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
[13576]183 }
[25854]184 // set up start and end results if necessary
[30147]185 // start results always start at 0
186 int start_results = 0;
[29558]187 if (start_page > 1 && hits_per_page > 0)
[25854]188 {
[30147]189 start_results = ((start_page - 1) * hits_per_page) ;
[13576]190 }
[29558]191 int end_results = Integer.MAX_VALUE;
192 if (hits_per_page > 0) {
193 end_results = hits_per_page * start_page;
194 }
[25854]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 }
[13576]208 }
[25854]209
[29544]210 if (sort_order.equals(SORT_ORDER_REVERSE)) {
211 this.lucene_src.setReverseSort(true);
[28029]212 } else {
[29544]213 this.lucene_src.setReverseSort(false);
[28029]214 }
[25854]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;
[24024]227 }
[25854]228 catch (Exception e)
229 {
230 logger.error("Exception happened in runQuery(): ", e);
231 }
232
233 return null;
[13576]234 }
[25854]235
236 /** get the total number of docs that match */
237 protected long numDocsMatched(Object query_result)
238 {
239 return ((LuceneQueryResult) query_result).getTotalDocs();
[24024]240 }
[25854]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;
[24024]253 }
[24722]254
[25854]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;
[13238]265 }
[24722]266
[25854]267 /** add in term info if available */
[25855]268 protected boolean addTermInfo(Element term_list, HashMap params, Object query_result)
[25854]269 {
[28966]270 Document doc = term_list.getOwnerDocument();
[25854]271 String query_level = (String) params.get(LEVEL_PARAM); // the current query level
[24722]272
[25854]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);
[18422]277
[28966]278 Element term_elem = doc.createElement(GSXML.TERM_ELEM);
[25854]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 }
[24722]285
[25854]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
[28966]291 Element stopword_elem = doc.createElement(GSXML.STOPWORD_ELEM);
[25854]292 stopword_elem.setAttribute(GSXML.NAME_ATT, stopword);
293 term_list.appendChild(stopword_elem);
294 }
295
296 return true;
[24024]297 }
[25885]298
[32084]299 protected ArrayList<FacetWrapper> getFacets(Object query_result, String lang)
[25885]300 {
301 return null;
302 }
[29428]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 }
[30049]314
315@Override
316protected Map<String, Map<String, List<String>>> getHighlightSnippets(
317 Object query_result) {
318 // TODO Auto-generated method stub
319 return null;
[13238]320}
[30049]321
322}
Note: See TracBrowser for help on using the repository browser.