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

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

adding in sort by none, and sort order asc/desc

  • Property svn:executable set to *
File size: 15.1 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.Collection;
25import java.util.HashMap;
26import java.util.Iterator;
27import java.util.List;
28import java.util.Map;
29import java.util.Set;
30import java.util.Vector;
31
32import org.apache.log4j.Logger;
33import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
34import org.apache.solr.client.solrj.response.FacetField;
35import org.apache.solr.core.CoreContainer;
36import org.apache.solr.core.SolrCore;
37import org.greenstone.LuceneWrapper3.SharedSoleneQueryResult;
38import org.greenstone.gsdl3.util.FacetWrapper;
39import org.greenstone.gsdl3.util.GSFile;
40import org.greenstone.gsdl3.util.GSXML;
41import org.greenstone.gsdl3.util.SolrFacetWrapper;
42import org.greenstone.gsdl3.util.SolrQueryResult;
43import org.greenstone.gsdl3.util.SolrQueryWrapper;
44import org.greenstone.util.GlobalProperties;
45import org.w3c.dom.Element;
46import org.w3c.dom.NodeList;
47
48public class GS2SolrSearch extends SharedSoleneGS2FieldSearch
49{
50 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2SolrSearch.class.getName());
51
52 static protected CoreContainer all_solr_cores = null;
53
54 protected HashMap solr_core_cache;
55 protected SolrQueryWrapper solr_src = null;
56
57 protected ArrayList<String> _facets = new ArrayList<String>();
58
59 public GS2SolrSearch()
60 {
61 does_faceting = true;
62 // Used to store the solr cores that match the required 'level'
63 // of search (e.g. either document-level=>didx, or
64 // section-level=>sidx. The hashmap is filled out on demand
65 // based on 'level' parameter passed in to 'setUpQueryer()'
66
67 solr_core_cache = new HashMap();
68
69 if (all_solr_cores == null)
70 {
71 // Share one CoreContainer across all sites/collections
72 try
73 {
74 String gsdl3_writablehome = GlobalProperties.getGSDL3WritableHome();
75 String solr_ext_name = GlobalProperties.getProperty("gsdlext.solr.dirname", "solr");
76
77 String solr_home_str = GSFile.extHome(gsdl3_writablehome, solr_ext_name);
78
79 all_solr_cores = new CoreContainer(solr_home_str);
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 boolean success = super.configure(info, extra_info);
94
95 // 1. Make the CoreContainer reload solr.xml
96 // This is particularly needed for when activate.pl is executed during
97 // a running GS3 server. At that point, the solr collection is reactivated and
98 // we need to tell Greenstone that the solr index has changed. This requires
99 // the CoreContainer to reload the solr.xml file, and it all works again.
100
101 solr_core_cache.clear(); // clear the map of solr cores for this collection added to the map upon querying
102
103 // Reload the updated solr.xml into the CoreContainer
104 // (Doing an all_solr_cores.shutdown() first doesn't seem to be required)
105 try {
106 String solr_home_str = all_solr_cores.getSolrHome();
107 File solr_home = new File(solr_home_str);
108 File solr_xml = new File( solr_home,"solr.xml" );
109
110 all_solr_cores.load(solr_home_str,solr_xml);
111
112 } catch (Exception e) {
113 logger.error("Exception in GS2SolrSearch.configure(): " + e.getMessage());
114 e.printStackTrace();
115 return false;
116 }
117
118 if(!success) {
119 return false;
120 }
121
122 // 2. Setting up facets
123 // TODO - get these from build config, in case some haven't built
124 Element searchElem = (Element) GSXML.getChildByTagName(extra_info, GSXML.SEARCH_ELEM);
125 NodeList facet_list = info.getElementsByTagName("facet");
126 for (int i=0; i<facet_list.getLength(); i++) {
127 _facets.add(((Element)facet_list.item(i)).getAttribute(GSXML.SHORTNAME_ATT));
128 }
129 // NodeList configIndexElems = searchElem.getElementsByTagName(GSXML.INDEX_ELEM);
130
131 // ArrayList<String> chosenFacets = new ArrayList<String>();
132 // for (int i = 0; i < configIndexElems.getLength(); i++)
133 // {
134 // Element current = (Element) configIndexElems.item(i);
135 // if (current.getAttribute(GSXML.FACET_ATT).equals("true"))
136 // {
137 // chosenFacets.add(current.getAttribute(GSXML.NAME_ATT));
138 // }
139 // }
140
141 // Element indexListElem = (Element) GSXML.getChildByTagName(info, GSXML.INDEX_ELEM + GSXML.LIST_MODIFIER);
142 // NodeList buildIndexElems = indexListElem.getElementsByTagName(GSXML.INDEX_ELEM);
143
144 // for (int j = 0; j < buildIndexElems.getLength(); j++)
145 // {
146 // Element current = (Element) buildIndexElems.item(j);
147 // for (int i = 0; i < chosenFacets.size(); i++)
148 // {
149 // if (current.getAttribute(GSXML.NAME_ATT).equals(chosenFacets.get(i)))
150 // {
151 // _facets.add(current.getAttribute(GSXML.SHORTNAME_ATT));
152 // }
153 // }
154 // }
155
156 return true;
157 }
158
159 public void cleanUp()
160 {
161 super.cleanUp();
162 this.solr_src.cleanUp();
163
164 // When cleaning up, not only do we need to empty the solr_core_cache map, but we also need to remove all
165 // references to this collection's sorlcores in the CoreContainer object, which can be more SolrCores than
166 // the EmbeddedSolrServers instantiated and added to the solr_core_cache, since the cache does lazy loading
167 // while the CoreContainer contains all the cores defined in solr.xml, which includes all *possible* cores
168 // for this collection even if EmbeddedSolrServers for these were not added to the solr_core_cache_map.
169
170 // 1. clear the map keeping track of the solrcores' EmbeddedSolrServers in this collection
171 solr_core_cache.clear();
172
173 // 2. Remove all SolrCores in the CoreContainer (all_solr_cores) that are specific to this collection
174 String collection_core_name_prefix = getCollectionCoreNamePrefix();
175
176 if (all_solr_cores!=null) {
177 Collection<String> coreNames = all_solr_cores.getCoreNames();
178 if(!coreNames.isEmpty()) {
179 Iterator<String> coreIterator = coreNames.iterator();
180 while(coreIterator.hasNext()) {
181
182 String solrCoreName = coreIterator.next();
183 if(solrCoreName.startsWith(collection_core_name_prefix)) {
184
185 logger.error("**** Removing collection-specific core: " + solrCoreName + " from CoreContainer");
186
187 // CoreContainer.remove(String name): removes and returns registered core w/o decrementing it's reference count
188 // http://lucene.apache.org/solr/api/index.html?org/apache/solr/core/CoreContainer.html
189 SolrCore solr_core = all_solr_cores.remove(solrCoreName);
190 while(!solr_core.isClosed()) {
191 logger.error("@@@@@@ " + solrCoreName + " was not closed. Closing....");
192 solr_core.close(); // http://lucene.apache.org/solr/api/org/apache/solr/core/SolrCore.html
193 }
194 if(solr_core.isClosed()) {
195 logger.error("@@@@@@ " + solrCoreName + " is closed.");
196 }
197 solr_core = null;
198 }
199 }
200 }
201 }
202
203 // 3. if there are no more solr cores in Greenstone, then all_solr_cores will be empty, null the CoreContainer
204 // All going well, this will happen when we're ant stopping the Greenstone server and the last Solr collection
205 // is being deactivated
206 if (all_solr_cores!=null) {
207 Collection<String> coreNamesRemaining = all_solr_cores.getCoreNames();
208 if(coreNamesRemaining.isEmpty()) {
209 logger.error("**** CoreContainer contains 0 solrCores. Shutting down...");
210
211 all_solr_cores.shutdown(); // wouldn't do anything anyway for 0 cores I think
212 all_solr_cores = null;
213 }
214 else { // else part is just for debugging
215 Iterator coreIterator = coreNamesRemaining.iterator();
216 while(coreIterator.hasNext()) {
217 logger.error("**** Core: " + coreIterator.next() + " still exists in CoreContainer");
218 }
219 }
220 }
221 }
222
223 /** methods to handle actually doing the query */
224
225 /** do any initialisation of the query object */
226 protected boolean setUpQueryer(HashMap params)
227 {
228 this.solr_src.clearFacets();
229 this.solr_src.clearFacetQueries();
230
231 for (int i = 0; i < _facets.size(); i++)
232 {
233 this.solr_src.addFacet(_facets.get(i));
234 }
235
236 String index = "didx";
237 String physical_index_language_name = null;
238 String physical_sub_index_name = null;
239 int maxdocs = 100;
240 int hits_per_page = 20;
241 int start_page = 1;
242 // set up the query params
243 Set entries = params.entrySet();
244 Iterator i = entries.iterator();
245 while (i.hasNext())
246 {
247 Map.Entry m = (Map.Entry) i.next();
248 String name = (String) m.getKey();
249 String value = (String) m.getValue();
250
251 if (name.equals(MAXDOCS_PARAM) && !value.equals(""))
252 {
253 maxdocs = Integer.parseInt(value);
254 }
255 else if (name.equals(HITS_PER_PAGE_PARAM))
256 {
257 hits_per_page = Integer.parseInt(value);
258 }
259 else if (name.equals(START_PAGE_PARAM))
260 {
261 start_page = Integer.parseInt(value);
262 }
263 else if (name.equals(MATCH_PARAM))
264 {
265 if (value.equals(MATCH_PARAM_ALL))
266 {
267 this.solr_src.setDefaultConjunctionOperator("AND");
268 }
269 else
270 {
271 this.solr_src.setDefaultConjunctionOperator("OR");
272 }
273 }
274 else if (name.equals(RANK_PARAM))
275 {
276 if (value.equals(RANK_PARAM_RANK))
277 {
278 value = SolrQueryWrapper.SORT_BY_RANK;
279 } else if (value.equals(RANK_PARAM_NONE)) {
280 value = SolrQueryWrapper.SORT_BY_INDEX_ORDER;
281 }
282
283 this.solr_src.setSortField(value);
284 }
285 else if (name.equals(SORT_ORDER_PARAM)) {
286 if (value.equals(SORT_ORDER_DESCENDING)) {
287 this.solr_src.setSortOrder(SolrQueryWrapper.SORT_DESCENDING);
288 } else {
289 this.solr_src.setSortOrder(SolrQueryWrapper.SORT_ASCENDING);
290 }
291 }
292 else if (name.equals(LEVEL_PARAM))
293 {
294 if (value.toUpperCase().equals("SEC"))
295 {
296 index = "sidx";
297 }
298 else
299 {
300 index = "didx";
301 }
302 }
303 // Would facets ever come in through params???
304 else if (name.equals("facets") && value.length() > 0)
305 {
306 String[] facets = value.split(",");
307
308 for (String facet : facets)
309 {
310 this.solr_src.addFacet(facet);
311 }
312 }
313 else if (name.equals("facetQueries") && value.length() > 0)
314 {
315 this.solr_src.addFacetQuery(value);
316 }
317 else if (name.equals(INDEX_SUBCOLLECTION_PARAM))
318 {
319 physical_sub_index_name = value;
320 }
321 else if (name.equals(INDEX_LANGUAGE_PARAM))
322 {
323 physical_index_language_name = value;
324 } // ignore any others
325 }
326 // set up start and end results if necessary
327 int start_results = 1;
328 if (start_page != 1)
329 {
330 start_results = ((start_page - 1) * hits_per_page) + 1;
331 }
332 int end_results = hits_per_page * start_page;
333 this.solr_src.setStartResults(start_results);
334 this.solr_src.setEndResults(end_results);
335 this.solr_src.setMaxDocs(maxdocs);
336
337 if (index.equals("sidx") || index.equals("didx"))
338 {
339 if (physical_sub_index_name != null)
340 {
341 index += physical_sub_index_name;
342 }
343 if (physical_index_language_name != null)
344 {
345 index += physical_index_language_name;
346 }
347 }
348
349 // now we know the index level, we can dig out the required
350 // solr-core, (caching the result in 'solr_core_cache')
351 String core_name = getCollectionCoreNamePrefix() + "-" + index;
352
353 EmbeddedSolrServer solr_core = null;
354
355 if (!solr_core_cache.containsKey(core_name))
356 {
357 solr_core = new EmbeddedSolrServer(all_solr_cores, core_name);
358
359 solr_core_cache.put(core_name, solr_core);
360 }
361 else
362 {
363 solr_core = (EmbeddedSolrServer) solr_core_cache.get(core_name);
364 }
365
366 this.solr_src.setSolrCore(solr_core);
367 this.solr_src.initialise();
368 return true;
369 }
370
371 /** do the query */
372 protected Object runQuery(String query)
373 {
374 try
375 {
376 //SharedSoleneQueryResult sqr = this.solr_src.runQuery(query);
377 SharedSoleneQueryResult sqr = this.solr_src.runQuery(query);
378
379 return sqr;
380 }
381 catch (Exception e)
382 {
383 logger.error("Exception happened in run query: ", e);
384 }
385
386 return null;
387 }
388
389 /** get the total number of docs that match */
390 protected long numDocsMatched(Object query_result)
391 {
392 return ((SharedSoleneQueryResult) query_result).getTotalDocs();
393
394 }
395
396 /** get the list of doc ids */
397 protected String[] getDocIDs(Object query_result)
398 {
399 Vector docs = ((SharedSoleneQueryResult) query_result).getDocs();
400 String[] doc_nums = new String[docs.size()];
401 for (int d = 0; d < docs.size(); d++)
402 {
403 String doc_num = ((SharedSoleneQueryResult.DocInfo) docs.elementAt(d)).id_;
404 doc_nums[d] = doc_num;
405 }
406 return doc_nums;
407 }
408
409 /** get the list of doc ranks */
410 protected String[] getDocRanks(Object query_result)
411 {
412 Vector docs = ((SharedSoleneQueryResult) query_result).getDocs();
413 String[] doc_ranks = new String[docs.size()];
414 for (int d = 0; d < docs.size(); d++)
415 {
416 doc_ranks[d] = Float.toString(((SharedSoleneQueryResult.DocInfo) docs.elementAt(d)).rank_);
417 }
418 return doc_ranks;
419 }
420
421 /** add in term info if available */
422 protected boolean addTermInfo(Element term_list, HashMap params, Object query_result)
423 {
424 String query_level = (String) params.get(LEVEL_PARAM); // the current query level
425
426 Vector terms = ((SharedSoleneQueryResult) query_result).getTerms();
427 for (int t = 0; t < terms.size(); t++)
428 {
429 SharedSoleneQueryResult.TermInfo term_info = (SharedSoleneQueryResult.TermInfo) terms.get(t);
430
431 Element term_elem = this.doc.createElement(GSXML.TERM_ELEM);
432 term_elem.setAttribute(GSXML.NAME_ATT, term_info.term_);
433 term_elem.setAttribute(FREQ_ATT, "" + term_info.term_freq_);
434 term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + term_info.match_docs_);
435 term_elem.setAttribute(FIELD_ATT, term_info.field_);
436 term_list.appendChild(term_elem);
437 }
438
439 Vector stopwords = ((SharedSoleneQueryResult) query_result).getStopWords();
440 for (int t = 0; t < stopwords.size(); t++)
441 {
442 String stopword = (String) stopwords.get(t);
443
444 Element stopword_elem = this.doc.createElement(GSXML.STOPWORD_ELEM);
445 stopword_elem.setAttribute(GSXML.NAME_ATT, stopword);
446 term_list.appendChild(stopword_elem);
447 }
448
449 return true;
450 }
451
452 protected ArrayList<FacetWrapper> getFacets(Object query_result)
453 {
454 if (!(query_result instanceof SolrQueryResult))
455 {
456 return null;
457 }
458
459 SolrQueryResult result = (SolrQueryResult) query_result;
460 List<FacetField> facets = result.getFacetResults();
461
462 if (facets == null)
463 {
464 return null;
465 }
466
467 ArrayList<FacetWrapper> newFacetList = new ArrayList<FacetWrapper>();
468
469 for (FacetField facet : facets)
470 {
471 SolrFacetWrapper wrap = new SolrFacetWrapper(facet);
472 // String name = wrap.getName();
473 // String display_name = "Poo";
474 // wrap.setDisplayName(display_name);
475
476 newFacetList.add(wrap);
477 }
478
479 return newFacetList;
480 }
481
482
483 protected String getCollectionCoreNamePrefix() {
484 String site_name = this.router.getSiteName();
485 String coll_name = this.cluster_name;
486 String collection_core_name_prefix = site_name + "-" + coll_name;
487 return collection_core_name_prefix;
488 }
489}
Note: See TracBrowser for help on using the repository browser.