source: gs3-extensions/solr/trunk/src/src/java/org/greenstone/gsdl3/service/GS2SolrSearch.java@ 25886

Last change on this file since 25886 was 25886, checked in by sjm84, 12 years ago

Some fixes to remove the Solr code from the main Greenstone code

  • Property svn:executable set to *
File size: 10.3 KB
Line 
1/*
2 * GS2SolrSearch.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.List;
27import java.util.Map;
28import java.util.Set;
29import java.util.Vector;
30
31import org.apache.log4j.Logger;
32import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
33import org.apache.solr.client.solrj.response.FacetField;
34import org.apache.solr.core.CoreContainer;
35import org.greenstone.LuceneWrapper3.SharedSoleneQueryResult;
36import org.greenstone.gsdl3.util.FacetWrapper;
37import org.greenstone.gsdl3.util.GSFile;
38import org.greenstone.gsdl3.util.GSXML;
39import org.greenstone.gsdl3.util.SolrFacetWrapper;
40import org.greenstone.gsdl3.util.SolrQueryResult;
41import org.greenstone.gsdl3.util.SolrQueryWrapper;
42import org.greenstone.util.GlobalProperties;
43import org.w3c.dom.Element;
44import org.w3c.dom.NodeList;
45
46public class GS2SolrSearch extends SharedSoleneGS2FieldSearch
47{
48 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2SolrSearch.class.getName());
49
50 static protected CoreContainer all_solr_cores = null;
51
52 protected HashMap solr_core_cache;
53 protected SolrQueryWrapper solr_src = null;
54
55 protected ArrayList<String> _facets = new ArrayList<String>();
56
57 public GS2SolrSearch()
58 {
59 does_faceting = true;
60 // Used to store the solr cores that match the required 'level'
61 // of search (e.g. either document-level=>didx, or
62 // section-level=>sidx. The hashmap is filled out on demand
63 // based on 'level' parameter passed in to 'setUpQueryer()'
64
65 solr_core_cache = new HashMap();
66
67 if (all_solr_cores == null)
68 {
69 // Share one CoreContainer across all sites/collections
70 try
71 {
72 String gsdl3_home = GlobalProperties.getGSDL3Home();
73 String solr_ext_name = GlobalProperties.getProperty("gsdlext.solr.dirname", "solr");
74
75 String solr_home_str = GSFile.extHome(gsdl3_home, solr_ext_name);
76 File solr_home = new File(solr_home_str);
77 File solr_xml = new File(solr_home, "solr.xml");
78
79 all_solr_cores = new CoreContainer(solr_home_str, solr_xml);
80 }
81 catch (Exception e)
82 {
83 e.printStackTrace();
84 }
85 }
86
87 this.solr_src = new SolrQueryWrapper();
88 }
89
90 /** configure this service */
91 public boolean configure(Element info, Element extra_info)
92 {
93 if (!super.configure(info, extra_info))
94 {
95 return false;
96 }
97
98 Element searchElem = (Element) GSXML.getChildByTagName(extra_info, GSXML.SEARCH_ELEM);
99 NodeList configIndexElems = searchElem.getElementsByTagName(GSXML.INDEX_ELEM);
100
101 ArrayList<String> chosenFacets = new ArrayList<String>();
102 for (int i = 0; i < configIndexElems.getLength(); i++)
103 {
104 Element current = (Element) configIndexElems.item(i);
105 if (current.getAttribute(GSXML.FACET_ATT).equals("true"))
106 {
107 chosenFacets.add(current.getAttribute(GSXML.NAME_ATT));
108 }
109 }
110
111 Element indexListElem = (Element) GSXML.getChildByTagName(info, GSXML.INDEX_ELEM + GSXML.LIST_MODIFIER);
112 NodeList buildIndexElems = indexListElem.getElementsByTagName(GSXML.INDEX_ELEM);
113
114 for (int j = 0; j < buildIndexElems.getLength(); j++)
115 {
116 Element current = (Element) buildIndexElems.item(j);
117 for (int i = 0; i < chosenFacets.size(); i++)
118 {
119 if (current.getAttribute(GSXML.NAME_ATT).equals(chosenFacets.get(i)))
120 {
121 _facets.add(current.getAttribute(GSXML.SHORTNAME_ATT));
122 }
123 }
124 }
125
126 return true;
127 }
128
129 public void cleanUp()
130 {
131 super.cleanUp();
132 this.solr_src.cleanUp();
133 all_solr_cores.shutdown();
134 }
135
136 /** methods to handle actually doing the query */
137
138 /** do any initialisation of the query object */
139 protected boolean setUpQueryer(HashMap params)
140 {
141 this.solr_src.clearFacets();
142 this.solr_src.clearFacetQueries();
143
144 for (int i = 0; i < _facets.size(); i++)
145 {
146 this.solr_src.addFacet(_facets.get(i));
147 }
148
149 String index = "didx";
150 String physical_index_language_name = null;
151 String physical_sub_index_name = null;
152 int maxdocs = 100;
153 int hits_per_page = 20;
154 int start_page = 1;
155 // set up the query params
156 Set entries = params.entrySet();
157 Iterator i = entries.iterator();
158 while (i.hasNext())
159 {
160 Map.Entry m = (Map.Entry) i.next();
161 String name = (String) m.getKey();
162 String value = (String) m.getValue();
163
164 if (name.equals(MAXDOCS_PARAM) && !value.equals(""))
165 {
166 maxdocs = Integer.parseInt(value);
167 }
168 else if (name.equals(HITS_PER_PAGE_PARAM))
169 {
170 hits_per_page = Integer.parseInt(value);
171 }
172 else if (name.equals(START_PAGE_PARAM))
173 {
174 start_page = Integer.parseInt(value);
175 }
176 else if (name.equals(MATCH_PARAM))
177 {
178 if (value.equals(MATCH_PARAM_ALL))
179 {
180 this.solr_src.setDefaultConjunctionOperator("AND");
181 }
182 else
183 {
184 this.solr_src.setDefaultConjunctionOperator("OR");
185 }
186 }
187 else if (name.equals(RANK_PARAM))
188 {
189 if (value.equals(RANK_PARAM_RANK_VALUE))
190 {
191 value = null;
192 }
193 this.solr_src.setSortField(value);
194 }
195 else if (name.equals(LEVEL_PARAM))
196 {
197 if (value.toUpperCase().equals("SEC"))
198 {
199 index = "sidx";
200 }
201 else
202 {
203 index = "didx";
204 }
205 }
206 else if (name.equals("facets") && value.length() > 0)
207 {
208 String[] facets = value.split(",");
209
210 for (String facet : facets)
211 {
212 this.solr_src.addFacet(facet);
213 }
214 }
215 else if (name.equals("facetQueries") && value.length() > 0)
216 {
217 this.solr_src.addFacetQuery(value);
218 }
219 else if (name.equals(INDEX_SUBCOLLECTION_PARAM))
220 {
221 physical_sub_index_name = value;
222 }
223 else if (name.equals(INDEX_LANGUAGE_PARAM))
224 {
225 physical_index_language_name = value;
226 } // ignore any others
227 }
228 // set up start and end results if necessary
229 int start_results = 1;
230 if (start_page != 1)
231 {
232 start_results = ((start_page - 1) * hits_per_page) + 1;
233 }
234 int end_results = hits_per_page * start_page;
235 this.solr_src.setStartResults(start_results);
236 this.solr_src.setEndResults(end_results);
237 this.solr_src.setMaxDocs(maxdocs);
238
239 if (index.equals("sidx") || index.equals("didx"))
240 {
241 if (physical_sub_index_name != null)
242 {
243 index += physical_sub_index_name;
244 }
245 if (physical_index_language_name != null)
246 {
247 index += physical_index_language_name;
248 }
249 }
250
251 // now we know the index level, we can dig out the required
252 // solr-core, (caching the result in 'solr_core_cache')
253
254 String site_name = this.router.getSiteName();
255 String coll_name = this.cluster_name;
256
257 String core_name = site_name + "-" + coll_name + "-" + index;
258
259 EmbeddedSolrServer solr_core = null;
260
261 if (!solr_core_cache.containsKey(core_name))
262 {
263 solr_core = new EmbeddedSolrServer(all_solr_cores, core_name);
264
265 solr_core_cache.put(core_name, solr_core);
266 }
267 else
268 {
269 solr_core = (EmbeddedSolrServer) solr_core_cache.get(core_name);
270 }
271
272 this.solr_src.setSolrCore(solr_core);
273 this.solr_src.initialise();
274 return true;
275 }
276
277 /** do the query */
278 protected Object runQuery(String query)
279 {
280 try
281 {
282 //SharedSoleneQueryResult sqr = this.solr_src.runQuery(query);
283 SharedSoleneQueryResult sqr = this.solr_src.runQuery(query);
284
285 return sqr;
286 }
287 catch (Exception e)
288 {
289 logger.error("Exception happened in run query: ", e);
290 }
291
292 return null;
293 }
294
295 /** get the total number of docs that match */
296 protected long numDocsMatched(Object query_result)
297 {
298 return ((SharedSoleneQueryResult) query_result).getTotalDocs();
299
300 }
301
302 /** get the list of doc ids */
303 protected String[] getDocIDs(Object query_result)
304 {
305 Vector docs = ((SharedSoleneQueryResult) query_result).getDocs();
306 String[] doc_nums = new String[docs.size()];
307 for (int d = 0; d < docs.size(); d++)
308 {
309 String doc_num = ((SharedSoleneQueryResult.DocInfo) docs.elementAt(d)).id_;
310 doc_nums[d] = doc_num;
311 }
312 return doc_nums;
313 }
314
315 /** get the list of doc ranks */
316 protected String[] getDocRanks(Object query_result)
317 {
318 Vector docs = ((SharedSoleneQueryResult) query_result).getDocs();
319 String[] doc_ranks = new String[docs.size()];
320 for (int d = 0; d < docs.size(); d++)
321 {
322 doc_ranks[d] = Float.toString(((SharedSoleneQueryResult.DocInfo) docs.elementAt(d)).rank_);
323 }
324 return doc_ranks;
325 }
326
327 /** add in term info if available */
328 protected boolean addTermInfo(Element term_list, HashMap params, Object query_result)
329 {
330 String query_level = (String) params.get(LEVEL_PARAM); // the current query level
331
332 Vector terms = ((SharedSoleneQueryResult) query_result).getTerms();
333 for (int t = 0; t < terms.size(); t++)
334 {
335 SharedSoleneQueryResult.TermInfo term_info = (SharedSoleneQueryResult.TermInfo) terms.get(t);
336
337 Element term_elem = this.doc.createElement(GSXML.TERM_ELEM);
338 term_elem.setAttribute(GSXML.NAME_ATT, term_info.term_);
339 term_elem.setAttribute(FREQ_ATT, "" + term_info.term_freq_);
340 term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + term_info.match_docs_);
341 term_elem.setAttribute(FIELD_ATT, term_info.field_);
342 term_list.appendChild(term_elem);
343 }
344
345 Vector stopwords = ((SharedSoleneQueryResult) query_result).getStopWords();
346 for (int t = 0; t < stopwords.size(); t++)
347 {
348 String stopword = (String) stopwords.get(t);
349
350 Element stopword_elem = this.doc.createElement(GSXML.STOPWORD_ELEM);
351 stopword_elem.setAttribute(GSXML.NAME_ATT, stopword);
352 term_list.appendChild(stopword_elem);
353 }
354
355 return true;
356 }
357
358 protected ArrayList<FacetWrapper> getFacets(Object query_result)
359 {
360 if (!(query_result instanceof SolrQueryResult))
361 {
362 return null;
363 }
364
365 SolrQueryResult result = (SolrQueryResult) query_result;
366 List<FacetField> facets = result.getFacetResults();
367
368 if (facets == null)
369 {
370 return null;
371 }
372
373 ArrayList<FacetWrapper> newFacetList = new ArrayList<FacetWrapper>();
374
375 for (FacetField facet : facets)
376 {
377 newFacetList.add(new SolrFacetWrapper(facet));
378 }
379
380 return newFacetList;
381 }
382}
Note: See TracBrowser for help on using the repository browser.