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

Last change on this file since 32430 was 32430, checked in by ak19, 6 years ago

solr should only be accessible locally (from localhost, specifically 127.0.0.1) which means over http. This conflicted with the previous design of the properties file for working with http and/or https. Now we have tomcat.port.https and localhost.port.http, both always set. In place of server.protocol that used to contain the default protocol, we now have server.protocols which can be set to a comma separated list of one or both of http and https. Drastic restructuring followed. I think I've tested all but https certification stuff.

  • Property svn:executable set to *
File size: 7.8 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;
9import org.greenstone.util.ProtocolPortProperties;
10
11// XML classes
12import org.w3c.dom.Element;
13import org.w3c.dom.Document;
14import org.w3c.dom.NodeList;
15
16import java.util.ArrayList;
17import java.util.HashMap;
18import java.util.Properties;
19
20import org.apache.log4j.Logger;
21import org.apache.solr.client.solrj.SolrQuery;
22import org.apache.solr.client.solrj.SolrServer;
23import org.apache.solr.client.solrj.SolrServerException;
24import org.apache.solr.client.solrj.impl.HttpSolrServer;
25import org.apache.solr.client.solrj.response.QueryResponse;
26import org.apache.solr.common.SolrDocument;
27import org.apache.solr.common.SolrDocumentList;
28import org.apache.solr.common.util.NamedList;
29import org.apache.solr.servlet.SolrRequestParsers;
30
31
32public class SolrSearch extends LuceneSearch {
33
34 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.SolrSearch.class.getName());
35
36 protected String solr_servlet_base_url;
37 protected HashMap<String, SolrServer> solr_server;
38
39 public SolrSearch()
40 {
41 solr_server = new HashMap<String, SolrServer>();
42
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
49 /*
50 String host = globalProperties.getProperty("tomcat.server", "localhost");
51 //String port = globalProperties.getProperty("tomcat.port.http", "8383");
52 //String protocol = globalProperties.getProperty("server.protocol", "http");
53 ProtocolPortProperties protocolPortProps = new ProtocolPortProperties(globalProperties); // can throw an Exception
54 String protocol = protocolPortProps.getProtocol();
55 String port = protocolPortProps.getPort();
56 String solrContext = globalProperties.getProperty("solr.context", "solr");
57
58 String portStr = port.equals("80") ? "" : ":"+port;
59 solr_servlet_base_url = protocol+"://"+host+portStr+"/"+solrContext;
60 */
61
62 // The solr servlet is only accessible locally (from "localhost", specifically 127.0.0.1).
63 // for security reasons, as we don't want non librarian users
64 // to go to the solr servlet and delete solr cores or something.
65 // The security Valve element in the tomcat solr.xml context file restricts
66 // access to 127.0.0.1, but here we ensure that the solr URL is the local http one
67 // and not any https with domain name and https port.
68 // Note that we use 127.0.0.1 instead of literally "localhost" since localhost is unsafe
69 ProtocolPortProperties protocolPortProps = new ProtocolPortProperties(globalProperties); // can throw an Exception
70 String solrContext = globalProperties.getProperty("solr.context", "solr");
71 solr_servlet_base_url = protocolPortProps.getLocalHttpBaseAddress()+"/"+solrContext;
72 } catch(Exception e) {
73 logger.error("Error reading greenstone's tomcat solr server properties from global.properties", e);
74 }
75 }
76
77
78 // Overriding the cleanUp() method here, so as to parallel the structure of GS2SolrSearch
79 // which also calls shutdown() on its CoreContainer object in GS2SolrSearch.cleanUp().
80 // However, this class has not yet been tested, so it's not certain that this method is
81 // required here.
82 // Adjusted to bring it up to speed with changes in GS2SolrSearch (for activate.pl) - not yet tested
83 public void cleanUp() {
84 super.cleanUp();
85
86 // clear the map keeping track of the SolrServers in this collection
87 solr_server.clear();
88 }
89
90
91 // adjusted configure to bring it up to speed with changes in GS2SolrSearch (for activate.pl) - not yet tested
92 public boolean configure(Element info, Element extra_info) {
93 boolean success = super.configure(info, extra_info);
94
95 // clear the map of solr cores for this collection added to the map upon querying
96 solr_server.clear();
97
98 if(!success) {
99 return false;
100 }
101
102 if(solr_servlet_base_url == null) {
103 logger.error("Unable to configure SolrSearch - solr_servlet_base_url is null because of issues with port/protocol in global.properties");
104 return false;
105 }
106 // initialize required number of SolrCores based on values
107 // in 'index_ids' that are set by LuceneSearch::configure()
108
109 String core_name_prefix = getCollectionCoreNamePrefix();
110
111 for (int i=0; i<index_ids.size(); i++) {
112
113 String idx_name = (String)index_ids.get(i);
114 String core_name = core_name_prefix + "-" + idx_name;
115
116 SolrServer solr_core = new HttpSolrServer(this.solr_servlet_base_url+"/"+core_name);
117 solr_server.put(core_name,solr_core);
118 }
119
120
121 return success;
122 }
123
124 protected void getIndexData(ArrayList index_ids, ArrayList index_names, String lang)
125 {
126 }
127
128 /** Process a text query - implemented by concrete subclasses */
129 protected Element processTextQuery(Element request) {
130
131 Document result_doc = XMLConverter.newDOM();
132 Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
133 Element doc_node_list = result_doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
134 Element metadata_list = result_doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
135 initResultElement(result,doc_node_list,metadata_list);
136
137 if (!hasParamList(request,metadata_list)) {
138 return result;
139 }
140
141 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
142 if (!hasQueryString(param_list,metadata_list)) {
143 return result;
144 }
145
146 HashMap params = GSXML.extractParams(param_list, false);
147 String query_string = (String) params.get(QUERY_PARAM);
148
149
150 // Get the index
151 String index = (String) params.get(INDEX_PARAM);
152 if (index == null || index.equals("")) {
153 index = this.default_index; // assume the default
154 }
155
156 try {
157
158 // Use SolrQuery with HttpSolrServer instead of ModifiableSolrParams,
159 // see http://stackoverflow.com/questions/13944567/querying-solr-server-using-solrj
160 SolrQuery solrParams = new SolrQuery(query_string); // initialised with q url-parameter
161 //solrparams.setRequestHandler("/select"); // default. Or try "select"
162
163 ///solrParams.set("start", start);
164 ///solrParams.set("rows", nbDocuments);
165
166
167 String core_name = getCollectionCoreNamePrefix() + "-" + index;
168
169 // http://stackoverflow.com/questions/17026530/accessing-a-cores-default-handler-through-solrj-using-setquerytype
170 // default request handler is /select, see http://wiki.apache.org/solr/CoreQueryParameters
171 SolrServer solr_core = solr_server.get(core_name);
172 QueryResponse solrResponse = solr_core.query(solrParams);
173
174 SolrDocumentList hits = solrResponse.getResults();
175
176 if (hits != null) {
177 // or should this be docs.getNumFound() ??
178 GSXML.addMetadata(metadata_list, "numDocsMatched", ""+hits.size());
179
180 System.err.println(hits.getNumFound() + " documents found, "
181 + hits.size() + " returned : ");
182
183 for (int i = 0; i < hits.size(); i++) {
184 SolrDocument solr_doc = hits.get(i);
185
186 String node_id = (String)solr_doc.get("docOID");
187 Element node = result_doc.createElement(GSXML.DOC_NODE_ELEM);
188 node.setAttribute(GSXML.NODE_ID_ATT, node_id);
189 doc_node_list.appendChild(node);
190
191 System.out.println("\t" + solr_doc.toString());
192 }
193 }
194 } catch (Exception e) {
195 e.printStackTrace();
196 }
197
198 return result;
199
200 }
201
202 protected String getCollectionCoreNamePrefix() {
203 String site_name = this.router.getSiteName();
204 String coll_name = this.cluster_name;
205 String collection_core_name_prefix = site_name + "-" + coll_name;
206 return collection_core_name_prefix;
207 }
208
209}
Note: See TracBrowser for help on using the repository browser.