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

Last change on this file since 27316 was 27085, checked in by kjdon, 11 years ago

added sortorder param to lucene searching

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