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 |
|
---|
10 | // XML classes
|
---|
11 | import org.w3c.dom.Element;
|
---|
12 | import org.w3c.dom.Document;
|
---|
13 | import org.w3c.dom.NodeList;
|
---|
14 |
|
---|
15 | import java.util.ArrayList;
|
---|
16 | import java.util.HashMap;
|
---|
17 | import java.util.Properties;
|
---|
18 |
|
---|
19 | import org.apache.log4j.Logger;
|
---|
20 | import org.apache.solr.client.solrj.SolrQuery;
|
---|
21 | import org.apache.solr.client.solrj.SolrServer;
|
---|
22 | import org.apache.solr.client.solrj.SolrServerException;
|
---|
23 | import org.apache.solr.client.solrj.impl.HttpSolrServer;
|
---|
24 | import org.apache.solr.client.solrj.response.QueryResponse;
|
---|
25 | import org.apache.solr.common.SolrDocument;
|
---|
26 | import org.apache.solr.common.SolrDocumentList;
|
---|
27 | import org.apache.solr.common.util.NamedList;
|
---|
28 | import org.apache.solr.servlet.SolrRequestParsers;
|
---|
29 |
|
---|
30 |
|
---|
31 | public class SolrSearch extends LuceneSearch {
|
---|
32 |
|
---|
33 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.SolrSearch.class.getName());
|
---|
34 |
|
---|
35 | protected String solr_servlet_base_url;
|
---|
36 | protected HashMap<String, SolrServer> solr_server;
|
---|
37 |
|
---|
38 | public SolrSearch()
|
---|
39 | {
|
---|
40 | solr_server = new HashMap<String, SolrServer>();
|
---|
41 |
|
---|
42 | // Create the solr servlet url on GS3's tomcat. By default it's "http://localhost:8383/solr"
|
---|
43 | // Don't do this in configure(), since the tomcat url will remain unchanged while tomcat is running
|
---|
44 | try {
|
---|
45 | Properties globalProperties = new Properties();
|
---|
46 | globalProperties.load(Class.forName("org.greenstone.util.GlobalProperties").getClassLoader().getResourceAsStream("global.properties"));
|
---|
47 | String host = globalProperties.getProperty("tomcat.server", "localhost");
|
---|
48 | String port = globalProperties.getProperty("tomcat.port.http", "8383");
|
---|
49 | String protocol = globalProperties.getProperty("tomcat.protocol", "http");
|
---|
50 | String solrContext = globalProperties.getProperty("solr.context", "solr");
|
---|
51 |
|
---|
52 | String portStr = port.equals("80") ? "" : ":"+port;
|
---|
53 | solr_servlet_base_url = protocol+"://"+host+portStr+"/"+solrContext;
|
---|
54 | } catch(Exception e) {
|
---|
55 | logger.error("Error reading greenstone's tomcat solr server properties from global.properties", e);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | // Overriding the cleanUp() method here, so as to parallel the structure of GS2SolrSearch
|
---|
61 | // which also calls shutdown() on its CoreContainer object in GS2SolrSearch.cleanUp().
|
---|
62 | // However, this class has not yet been tested, so it's not certain that this method is
|
---|
63 | // required here.
|
---|
64 | // Adjusted to bring it up to speed with changes in GS2SolrSearch (for activate.pl) - not yet tested
|
---|
65 | public void cleanUp() {
|
---|
66 | super.cleanUp();
|
---|
67 |
|
---|
68 | // clear the map keeping track of the SolrServers in this collection
|
---|
69 | solr_server.clear();
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | // adjusted configure to bring it up to speed with changes in GS2SolrSearch (for activate.pl) - not yet tested
|
---|
74 | public boolean configure(Element info, Element extra_info) {
|
---|
75 | boolean success = super.configure(info, extra_info);
|
---|
76 |
|
---|
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 | }
|
---|
83 |
|
---|
84 | // initialize required number of SolrCores based on values
|
---|
85 | // in 'index_ids' that are set by LuceneSearch::configure()
|
---|
86 |
|
---|
87 | String core_name_prefix = getCollectionCoreNamePrefix();
|
---|
88 |
|
---|
89 | for (int i=0; i<index_ids.size(); i++) {
|
---|
90 |
|
---|
91 | String idx_name = (String)index_ids.get(i);
|
---|
92 | String core_name = core_name_prefix + "-" + idx_name;
|
---|
93 |
|
---|
94 | SolrServer solr_core = new HttpSolrServer(this.solr_servlet_base_url+"/"+core_name);
|
---|
95 | solr_server.put(core_name,solr_core);
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | return success;
|
---|
100 | }
|
---|
101 |
|
---|
102 | protected void getIndexData(ArrayList index_ids, ArrayList index_names, String lang)
|
---|
103 | {
|
---|
104 | }
|
---|
105 |
|
---|
106 | /** Process a text query - implemented by concrete subclasses */
|
---|
107 | protected Element processTextQuery(Element request) {
|
---|
108 |
|
---|
109 | Document result_doc = XMLConverter.newDOM();
|
---|
110 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
111 | Element doc_node_list = result_doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
112 | Element metadata_list = result_doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
113 | initResultElement(result,doc_node_list,metadata_list);
|
---|
114 |
|
---|
115 | if (!hasParamList(request,metadata_list)) {
|
---|
116 | return result;
|
---|
117 | }
|
---|
118 |
|
---|
119 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
120 | if (!hasQueryString(param_list,metadata_list)) {
|
---|
121 | return result;
|
---|
122 | }
|
---|
123 |
|
---|
124 | HashMap params = GSXML.extractParams(param_list, false);
|
---|
125 | String query_string = (String) params.get(QUERY_PARAM);
|
---|
126 |
|
---|
127 |
|
---|
128 | // Get the index
|
---|
129 | String index = (String) params.get(INDEX_PARAM);
|
---|
130 | if (index == null || index.equals("")) {
|
---|
131 | index = this.default_index; // assume the default
|
---|
132 | }
|
---|
133 |
|
---|
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"
|
---|
140 |
|
---|
141 | ///solrParams.set("start", start);
|
---|
142 | ///solrParams.set("rows", nbDocuments);
|
---|
143 |
|
---|
144 |
|
---|
145 | String core_name = getCollectionCoreNamePrefix() + "-" + index;
|
---|
146 |
|
---|
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);
|
---|
150 | QueryResponse solrResponse = solr_core.query(solrParams);
|
---|
151 |
|
---|
152 | SolrDocumentList hits = solrResponse.getResults();
|
---|
153 |
|
---|
154 | if (hits != null) {
|
---|
155 | // or should this be docs.getNumFound() ??
|
---|
156 | GSXML.addMetadata(metadata_list, "numDocsMatched", ""+hits.size());
|
---|
157 |
|
---|
158 | System.err.println(hits.getNumFound() + " documents found, "
|
---|
159 | + hits.size() + " returned : ");
|
---|
160 |
|
---|
161 | for (int i = 0; i < hits.size(); i++) {
|
---|
162 | SolrDocument solr_doc = hits.get(i);
|
---|
163 |
|
---|
164 | String node_id = (String)solr_doc.get("docOID");
|
---|
165 | Element node = result_doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
166 | node.setAttribute(GSXML.NODE_ID_ATT, node_id);
|
---|
167 | doc_node_list.appendChild(node);
|
---|
168 |
|
---|
169 | System.out.println("\t" + solr_doc.toString());
|
---|
170 | }
|
---|
171 | }
|
---|
172 | } catch (Exception e) {
|
---|
173 | e.printStackTrace();
|
---|
174 | }
|
---|
175 |
|
---|
176 | return result;
|
---|
177 |
|
---|
178 | }
|
---|
179 |
|
---|
180 | protected String getCollectionCoreNamePrefix() {
|
---|
181 | String site_name = this.router.getSiteName();
|
---|
182 | String coll_name = this.cluster_name;
|
---|
183 | String collection_core_name_prefix = site_name + "-" + coll_name;
|
---|
184 | return collection_core_name_prefix;
|
---|
185 | }
|
---|
186 |
|
---|
187 | }
|
---|