Changeset 29711 for gs3-extensions/solr/trunk/src/src/java/org/greenstone/gsdl3/service/SolrSearch.java
- Timestamp:
- 2015-02-03T21:57:53+13:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gs3-extensions/solr/trunk/src/src/java/org/greenstone/gsdl3/service/SolrSearch.java
r29710 r29711 13 13 import org.w3c.dom.NodeList; 14 14 15 import java.util.ArrayList; 15 16 import java.util.HashMap; 16 import java.util.ArrayList; 17 import java.util.Iterator; 17 import java.util.Properties; 18 18 19 19 import org.apache.log4j.Logger; 20 20 import org.apache.solr.client.solrj.SolrQuery; 21 21 import org.apache.solr.client.solrj.SolrServer; 22 22 import org.apache.solr.client.solrj.SolrServerException; 23 import org.apache.solr.client.solrj. embedded.EmbeddedSolrServer;23 import org.apache.solr.client.solrj.impl.HttpSolrServer; 24 24 import org.apache.solr.client.solrj.response.QueryResponse; 25 25 import org.apache.solr.common.SolrDocument; 26 26 import org.apache.solr.common.SolrDocumentList; 27 import org.apache.solr.common.params.ModifiableSolrParams;28 import org.apache.solr.common.params.SolrParams;29 27 import org.apache.solr.common.util.NamedList; 30 28 import org.apache.solr.servlet.SolrRequestParsers; 31 import org.apache.solr.core.CoreContainer;32 import org.apache.solr.core.SolrCore;33 34 import java.io.File;35 import java.util.Collection;36 37 38 import org.apache.log4j.*;39 29 40 30 41 31 public class SolrSearch extends LuceneSearch { 42 32 33 public static final String SOLR_SERVLET_SUFFIX = "/solr"; 43 34 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.SolrSearch.class.getName()); 44 35 45 static protected CoreContainer solr_cores = null;46 protected HashMap solr_server;36 protected String solr_servlet_base_url; 37 protected HashMap<String, SolrServer> solr_server; 47 38 48 39 public SolrSearch() 49 40 { 50 solr_server = new HashMap ();41 solr_server = new HashMap<String, SolrServer>(); 51 42 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 } 43 // Create the solr servlet url on GS3's tomcat. By default it's "http://localhost:8383/solr" 44 // Don't do this in configure(), since the tomcat url will remain unchanged while tomcat is running 45 try { 46 Properties globalProperties = new Properties(); 47 globalProperties.load(Class.forName("org.greenstone.util.GlobalProperties").getClassLoader().getResourceAsStream("global.properties")); 48 String host = globalProperties.getProperty("tomcat.server", "localhost"); 49 String port = globalProperties.getProperty("tomcat.port", "8383"); 50 String protocol = globalProperties.getProperty("tomcat.protocol", "http"); 51 52 String portStr = port.equals("80") ? "" : ":"+port; 53 solr_servlet_base_url = protocol+"://"+host+portStr+SOLR_SERVLET_SUFFIX; 54 } catch(Exception e) { 55 logger.error("Error reading greenstone's tomcat solr server properties from global.properties", e); 66 56 } 67 57 } … … 76 66 super.cleanUp(); 77 67 78 // 1. clear the map keeping track of the solrcores' EmbeddedSolrServers in this collection 79 solr_server.clear(); 80 81 // 2. Need GS3 server (tomcat) to release the cores, else a part of tomcat is still running in the background 82 // on ant stop, holding a lock on the cores. Doing shutdown() preserves core descriptions in solr.xml 83 solr_cores.shutdown(); 84 solr_cores = null; 68 // clear the map keeping track of the SolrServers in this collection 69 solr_server.clear(); 85 70 } 86 71 … … 88 73 // adjusted configure to bring it up to speed with changes in GS2SolrSearch (for activate.pl) - not yet tested 89 74 public boolean configure(Element info, Element extra_info) { 90 75 boolean success = super.configure(info, extra_info); 91 76 92 // 1. Make the CoreContainer reload solr.xml 93 // This is particularly needed for when activate.pl is executed during 94 // a running GS3 server. At that point, the solr collection is reactivated and 95 // we need to tell Greenstone that the solr index has changed. This requires 96 // the CoreContainer to reload the solr.xml file, and it all works again. 97 98 solr_server.clear(); // clear the map of solr cores for this collection added to the map upon querying 99 100 // Reload the updated solr.xml into the CoreContainer 101 // (Doing a solr_cores.shutdown() first doesn't seem to be required) 102 try { 103 String solr_home_str = solr_cores.getSolrHome(); 104 File solr_home = new File(solr_home_str); 105 File solr_xml = new File( solr_home,"solr.xml" ); 106 107 //solr_cores.load(solr_home_str,solr_xml); 108 solr_cores.load(); 109 } catch (Exception e) { 110 logger.error("Exception in SolrSearch.configure(): " + e.getMessage()); 111 e.printStackTrace(); 112 return false; 113 } 77 // clear the map of solr cores for this collection added to the map upon querying 78 solr_server.clear(); 79 80 if(!success) { 81 return false; 82 } 114 83 115 84 // initialize required number of SolrCores based on values … … 123 92 String core_name = core_name_prefix + "-" + idx_name; 124 93 125 EmbeddedSolrServer solr_core 126 = new EmbeddedSolrServer(solr_cores,core_name); 127 94 SolrServer solr_core = new HttpSolrServer(this.solr_servlet_base_url+"/"+core_name); 128 95 solr_server.put(core_name,solr_core); 129 96 } 97 130 98 131 99 return success; … … 165 133 166 134 try { 135 136 // Use SolrQuery with HttpSolrServer instead of ModifiableSolrParams, 137 // see http://stackoverflow.com/questions/13944567/querying-solr-server-using-solrj 138 SolrQuery solrParams = new SolrQuery(query_string); // initialised with q url-parameter 139 //solrparams.setRequestHandler("/select"); // default. Or try "select" 167 140 168 ModifiableSolrParams solrParams = new ModifiableSolrParams(); 169 solrParams.set("q", query_string); 170 //solrParams.set("start", start); 171 //solrParams.set("rows", nbDocuments); 141 ///solrParams.set("start", start); 142 ///solrParams.set("rows", nbDocuments); 143 172 144 173 145 String core_name = getCollectionCoreNamePrefix() + "-" + index; 174 146 175 EmbeddedSolrServer solr_core = (EmbeddedSolrServer)solr_server.get(core_name); 176 147 // http://stackoverflow.com/questions/17026530/accessing-a-cores-default-handler-through-solrj-using-setquerytype 148 // default request handler is /select, see http://wiki.apache.org/solr/CoreQueryParameters 149 SolrServer solr_core = solr_server.get(core_name); 177 150 QueryResponse solrResponse = solr_core.query(solrParams); 178 151
Note:
See TracChangeset
for help on using the changeset viewer.