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

Last change on this file was 38107, checked in by kjdon, 8 months ago

removed a comment

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