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

Last change on this file since 25909 was 25909, checked in by ak19, 12 years ago

Making SolrSearch uptodate with the changes to GS2SolrSearch. SolrSearch remains untested.

  • Property svn:executable set to *
File size: 8.5 KB
Line 
1package org.greenstone.gsdl3.service;
2
3
4// Based on LuceneSearch, but not thoroughly tested
5
6// Greenstone classes
7import org.greenstone.gsdl3.util.*;
8import org.greenstone.util.GlobalProperties;
9
10// XML classes
11import org.w3c.dom.Element;
12import org.w3c.dom.Document;
13import org.w3c.dom.NodeList;
14
15import java.util.HashMap;
16import java.util.ArrayList;
17import java.util.Iterator;
18
19
20import org.apache.solr.client.solrj.SolrQuery;
21import org.apache.solr.client.solrj.SolrServer;
22import org.apache.solr.client.solrj.SolrServerException;
23import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
24import org.apache.solr.client.solrj.response.QueryResponse;
25import org.apache.solr.common.SolrDocument;
26import org.apache.solr.common.SolrDocumentList;
27import org.apache.solr.common.params.ModifiableSolrParams;
28import org.apache.solr.common.params.SolrParams;
29import org.apache.solr.common.util.NamedList;
30import org.apache.solr.servlet.SolrRequestParsers;
31import org.apache.solr.core.CoreContainer;
32import org.apache.solr.core.SolrCore;
33
34import java.io.File;
35import java.util.Collection;
36
37
38import org.apache.log4j.*;
39
40
41public class SolrSearch extends LuceneSearch {
42
43 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.SolrSearch.class.getName());
44
45 static protected CoreContainer solr_cores = null;
46 protected HashMap solr_server;
47
48 public SolrSearch()
49 {
50 solr_server = new HashMap();
51
52 if (solr_cores == null) {
53 // Share one CoreContainer across all sites/collections
54 try {
55
56 String gsdl3_home = GlobalProperties.getGSDL3Home();
57 String solr_ext_name = GlobalProperties.getProperty("gsdlext.solr.dirname","solr");
58
59 String solr_home_str = GSFile.extHome(gsdl3_home,solr_ext_name);
60
61 solr_cores = new CoreContainer(solr_home_str);
62 }
63 catch (Exception e) {
64 e.printStackTrace();
65 }
66 }
67 }
68
69
70 // Overriding the cleanUp() method here, so as to parallel the structure of GS2SolrSearch
71 // which also calls shutdown() on its CoreContainer object in GS2SolrSearch.cleanUp().
72 // However, this class has not yet been tested, so it's not certain that this method is
73 // required here.
74 // Adjusted to bring it up to speed with changes in GS2SolrSearch (for activate.pl) - not yet tested
75 public void cleanUp() {
76 super.cleanUp();
77
78 // 1. clear the map keeping track of the solrcores' EmbeddedSolrServers in this collection
79 solr_server.clear();
80
81 // 2. Remove all SolrCores in the CoreContainer (solr_cores) that are specific to this collection
82 String collection_core_name_prefix = getCollectionCoreNamePrefix();
83
84 Collection<String> coreNames = solr_cores.getCoreNames();
85 if(!coreNames.isEmpty()) {
86 Iterator<String> coreIterator = coreNames.iterator();
87 while(coreIterator.hasNext()) {
88
89 String solrCoreName = coreIterator.next();
90 if(solrCoreName.startsWith(collection_core_name_prefix)) {
91
92 logger.error("**** Removing collection-specific core: " + solrCoreName + " from CoreContainer");
93
94 // CoreContainer.remove(String name): removes and returns registered core w/o decrementing it's reference count
95 // http://lucene.apache.org/solr/api/index.html?org/apache/solr/core/CoreContainer.html
96 SolrCore solr_core = solr_cores.remove(solrCoreName);
97 while(!solr_core.isClosed()) {
98 logger.error("@@@@@@ " + solrCoreName + " was not closed. Closing....");
99 solr_core.close(); // http://lucene.apache.org/solr/api/org/apache/solr/core/SolrCore.html
100 }
101 if(solr_core.isClosed()) {
102 logger.error("@@@@@@ " + solrCoreName + " is closed.");
103 }
104 solr_core = null;
105 }
106 }
107 }
108
109 // 3. if there are no more solr cores in Greenstone, then solr_cores will be empty, null the CoreContainer
110 // All going well, this will happen when we're ant stopping the Greenstone server and the last Solr collection
111 // is being deactivated
112 Collection<String> coreNamesRemaining = solr_cores.getCoreNames();
113 if(coreNamesRemaining.isEmpty()) {
114 logger.error("**** CoreContainer contains 0 solrCores. Shutting down...");
115
116 solr_cores.shutdown(); // wouldn't do anything anyway for 0 cores I think
117 solr_cores = null;
118 }
119 else { // else part is just for debugging
120 Iterator coreIterator = coreNamesRemaining.iterator();
121 while(coreIterator.hasNext()) {
122 logger.error("**** Core: " + coreIterator.next() + " still exists in CoreContainer");
123 }
124 }
125
126 }
127
128
129 // adjusted configure to bring it up to speed with changes in GS2SolrSearch (for activate.pl) - not yet tested
130 public boolean configure(Element info, Element extra_info) {
131 boolean success = super.configure(info, extra_info);
132
133 // 1. Make the CoreContainer reload solr.xml
134 // This is particularly needed for when activate.pl is executed during
135 // a running GS3 server. At that point, the solr collection is reactivated and
136 // we need to tell Greenstone that the solr index has changed. This requires
137 // the CoreContainer to reload the solr.xml file, and it all works again.
138
139 solr_server.clear(); // clear the map of solr cores for this collection added to the map upon querying
140
141 // Reload the updated solr.xml into the CoreContainer
142 // (Doing a solr_cores.shutdown() first doesn't seem to be required)
143 try {
144 String solr_home_str = solr_cores.getSolrHome();
145 File solr_home = new File(solr_home_str);
146 File solr_xml = new File( solr_home,"solr.xml" );
147
148 solr_cores.load(solr_home_str,solr_xml);
149 } catch (Exception e) {
150 logger.error("Exception in SolrSearch.configure(): " + e.getMessage());
151 e.printStackTrace();
152 return false;
153 }
154
155 // initialize required number of SolrCores based on values
156 // in 'index_ids' that are set by LuceneSearch::configure()
157
158 String core_name_prefix = getCollectionCoreNamePrefix();
159
160 for (int i=0; i<index_ids.size(); i++) {
161
162 String idx_name = (String)index_ids.get(i);
163 String core_name = core_name_prefix + "-" + idx_name;
164
165 EmbeddedSolrServer solr_core
166 = new EmbeddedSolrServer(solr_cores,core_name);
167
168 solr_server.put(core_name,solr_core);
169 }
170
171 return success;
172 }
173
174 protected void getIndexData(ArrayList index_ids, ArrayList index_names, String lang)
175 {
176 }
177
178 /** Process a text query - implemented by concrete subclasses */
179 protected Element processTextQuery(Element request) {
180
181 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
182 Element doc_node_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
183 Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
184 initResultElement(result,doc_node_list,metadata_list);
185
186 if (!hasParamList(request,metadata_list)) {
187 return result;
188 }
189
190 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
191 if (!hasQueryString(param_list,metadata_list)) {
192 return result;
193 }
194
195 HashMap params = GSXML.extractParams(param_list, false);
196 String query_string = (String) params.get(QUERY_PARAM);
197
198
199 // Get the index
200 String index = (String) params.get(INDEX_PARAM);
201 if (index == null || index.equals("")) {
202 index = this.default_index; // assume the default
203 }
204
205 try {
206
207 ModifiableSolrParams solrParams = new ModifiableSolrParams();
208 solrParams.set("q", query_string);
209 //solrParams.set("start", start);
210 //solrParams.set("rows", nbDocuments);
211
212 String core_name = getCollectionCoreNamePrefix() + "-" + index;
213
214 EmbeddedSolrServer solr_core = (EmbeddedSolrServer)solr_server.get(core_name);
215
216 QueryResponse solrResponse = solr_core.query(solrParams);
217
218 SolrDocumentList hits = solrResponse.getResults();
219
220 if (hits != null) {
221 // or should this be docs.getNumFound() ??
222 GSXML.addMetadata(this.doc, metadata_list, "numDocsMatched", ""+hits.size());
223
224 System.err.println(hits.getNumFound() + " documents found, "
225 + hits.size() + " returned : ");
226
227 for (int i = 0; i < hits.size(); i++) {
228 SolrDocument solr_doc = hits.get(i);
229
230 String node_id = (String)solr_doc.get("docOID");
231 Element node = this.doc.createElement(GSXML.DOC_NODE_ELEM);
232 node.setAttribute(GSXML.NODE_ID_ATT, node_id);
233 doc_node_list.appendChild(node);
234
235 System.out.println("\t" + solr_doc.toString());
236 }
237 }
238 } catch (Exception e) {
239 e.printStackTrace();
240 }
241
242 return result;
243
244 }
245
246 protected String getCollectionCoreNamePrefix() {
247 String site_name = this.router.getSiteName();
248 String coll_name = this.cluster_name;
249 String collection_core_name_prefix = site_name + "-" + coll_name;
250 return collection_core_name_prefix;
251 }
252
253}
Note: See TracBrowser for help on using the repository browser.