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