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

Last change on this file since 28967 was 28967, checked in by kjdon, 10 years ago

no longer have this.doc to use. Need to create new Documents when needed

  • 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 Document result_doc = XMLConverter.newDOM();
182 Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
183 Element doc_node_list = result_doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
184 Element metadata_list = result_doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
185 initResultElement(result,doc_node_list,metadata_list);
186
187 if (!hasParamList(request,metadata_list)) {
188 return result;
189 }
190
191 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
192 if (!hasQueryString(param_list,metadata_list)) {
193 return result;
194 }
195
196 HashMap params = GSXML.extractParams(param_list, false);
197 String query_string = (String) params.get(QUERY_PARAM);
198
199
200 // Get the index
201 String index = (String) params.get(INDEX_PARAM);
202 if (index == null || index.equals("")) {
203 index = this.default_index; // assume the default
204 }
205
206 try {
207
208 ModifiableSolrParams solrParams = new ModifiableSolrParams();
209 solrParams.set("q", query_string);
210 //solrParams.set("start", start);
211 //solrParams.set("rows", nbDocuments);
212
213 String core_name = getCollectionCoreNamePrefix() + "-" + index;
214
215 EmbeddedSolrServer solr_core = (EmbeddedSolrServer)solr_server.get(core_name);
216
217 QueryResponse solrResponse = solr_core.query(solrParams);
218
219 SolrDocumentList hits = solrResponse.getResults();
220
221 if (hits != null) {
222 // or should this be docs.getNumFound() ??
223 GSXML.addMetadata(metadata_list, "numDocsMatched", ""+hits.size());
224
225 System.err.println(hits.getNumFound() + " documents found, "
226 + hits.size() + " returned : ");
227
228 for (int i = 0; i < hits.size(); i++) {
229 SolrDocument solr_doc = hits.get(i);
230
231 String node_id = (String)solr_doc.get("docOID");
232 Element node = result_doc.createElement(GSXML.DOC_NODE_ELEM);
233 node.setAttribute(GSXML.NODE_ID_ATT, node_id);
234 doc_node_list.appendChild(node);
235
236 System.out.println("\t" + solr_doc.toString());
237 }
238 }
239 } catch (Exception e) {
240 e.printStackTrace();
241 }
242
243 return result;
244
245 }
246
247 protected String getCollectionCoreNamePrefix() {
248 String site_name = this.router.getSiteName();
249 String coll_name = this.cluster_name;
250 String collection_core_name_prefix = site_name + "-" + coll_name;
251 return collection_core_name_prefix;
252 }
253
254}
Note: See TracBrowser for help on using the repository browser.