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

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

ProtocolPortProperties.java constructors can throw an Exception now.

  • Property svn:executable set to *
File size: 6.9 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 String host = globalProperties.getProperty("tomcat.server", "localhost");
49 //String port = globalProperties.getProperty("tomcat.port.http", "8383");
50 //String protocol = globalProperties.getProperty("server.protocol", "http");
51 ProtocolPortProperties protocolPortProps = new ProtocolPortProperties(globalProperties); // can throw an Exception
52 String protocol = protocolPortProps.getProtocol();
53 String port = protocolPortProps.getPort();
54 String solrContext = globalProperties.getProperty("solr.context", "solr");
55
56 String portStr = port.equals("80") ? "" : ":"+port;
57 solr_servlet_base_url = protocol+"://"+host+portStr+"/"+solrContext;
58 } catch(Exception e) {
59 logger.error("Error reading greenstone's tomcat solr server properties from global.properties", e);
60 }
61 }
62
63
64 // Overriding the cleanUp() method here, so as to parallel the structure of GS2SolrSearch
65 // which also calls shutdown() on its CoreContainer object in GS2SolrSearch.cleanUp().
66 // However, this class has not yet been tested, so it's not certain that this method is
67 // required here.
68 // Adjusted to bring it up to speed with changes in GS2SolrSearch (for activate.pl) - not yet tested
69 public void cleanUp() {
70 super.cleanUp();
71
72 // clear the map keeping track of the SolrServers in this collection
73 solr_server.clear();
74 }
75
76
77 // adjusted configure to bring it up to speed with changes in GS2SolrSearch (for activate.pl) - not yet tested
78 public boolean configure(Element info, Element extra_info) {
79 boolean success = super.configure(info, extra_info);
80
81 // clear the map of solr cores for this collection added to the map upon querying
82 solr_server.clear();
83
84 if(!success) {
85 return false;
86 }
87
88 if(solr_servlet_base_url == null) {
89 logger.error("Unable to configure SolrSearch - solr_servlet_base_url is null because of issues with port/protocol in global.properties");
90 return false;
91 }
92 // initialize required number of SolrCores based on values
93 // in 'index_ids' that are set by LuceneSearch::configure()
94
95 String core_name_prefix = getCollectionCoreNamePrefix();
96
97 for (int i=0; i<index_ids.size(); i++) {
98
99 String idx_name = (String)index_ids.get(i);
100 String core_name = core_name_prefix + "-" + idx_name;
101
102 SolrServer solr_core = new HttpSolrServer(this.solr_servlet_base_url+"/"+core_name);
103 solr_server.put(core_name,solr_core);
104 }
105
106
107 return success;
108 }
109
110 protected void getIndexData(ArrayList index_ids, ArrayList index_names, String lang)
111 {
112 }
113
114 /** Process a text query - implemented by concrete subclasses */
115 protected Element processTextQuery(Element request) {
116
117 Document result_doc = XMLConverter.newDOM();
118 Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
119 Element doc_node_list = result_doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
120 Element metadata_list = result_doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
121 initResultElement(result,doc_node_list,metadata_list);
122
123 if (!hasParamList(request,metadata_list)) {
124 return result;
125 }
126
127 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
128 if (!hasQueryString(param_list,metadata_list)) {
129 return result;
130 }
131
132 HashMap params = GSXML.extractParams(param_list, false);
133 String query_string = (String) params.get(QUERY_PARAM);
134
135
136 // Get the index
137 String index = (String) params.get(INDEX_PARAM);
138 if (index == null || index.equals("")) {
139 index = this.default_index; // assume the default
140 }
141
142 try {
143
144 // Use SolrQuery with HttpSolrServer instead of ModifiableSolrParams,
145 // see http://stackoverflow.com/questions/13944567/querying-solr-server-using-solrj
146 SolrQuery solrParams = new SolrQuery(query_string); // initialised with q url-parameter
147 //solrparams.setRequestHandler("/select"); // default. Or try "select"
148
149 ///solrParams.set("start", start);
150 ///solrParams.set("rows", nbDocuments);
151
152
153 String core_name = getCollectionCoreNamePrefix() + "-" + index;
154
155 // http://stackoverflow.com/questions/17026530/accessing-a-cores-default-handler-through-solrj-using-setquerytype
156 // default request handler is /select, see http://wiki.apache.org/solr/CoreQueryParameters
157 SolrServer solr_core = solr_server.get(core_name);
158 QueryResponse solrResponse = solr_core.query(solrParams);
159
160 SolrDocumentList hits = solrResponse.getResults();
161
162 if (hits != null) {
163 // or should this be docs.getNumFound() ??
164 GSXML.addMetadata(metadata_list, "numDocsMatched", ""+hits.size());
165
166 System.err.println(hits.getNumFound() + " documents found, "
167 + hits.size() + " returned : ");
168
169 for (int i = 0; i < hits.size(); i++) {
170 SolrDocument solr_doc = hits.get(i);
171
172 String node_id = (String)solr_doc.get("docOID");
173 Element node = result_doc.createElement(GSXML.DOC_NODE_ELEM);
174 node.setAttribute(GSXML.NODE_ID_ATT, node_id);
175 doc_node_list.appendChild(node);
176
177 System.out.println("\t" + solr_doc.toString());
178 }
179 }
180 } catch (Exception e) {
181 e.printStackTrace();
182 }
183
184 return result;
185
186 }
187
188 protected String getCollectionCoreNamePrefix() {
189 String site_name = this.router.getSiteName();
190 String coll_name = this.cluster_name;
191 String collection_core_name_prefix = site_name + "-" + coll_name;
192 return collection_core_name_prefix;
193 }
194
195}
Note: See TracBrowser for help on using the repository browser.