[24641] | 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;
|
---|
[32411] | 9 | import org.greenstone.util.ProtocolPortProperties;
|
---|
[24641] | 10 |
|
---|
| 11 | // XML classes
|
---|
| 12 | import org.w3c.dom.Element;
|
---|
| 13 | import org.w3c.dom.Document;
|
---|
| 14 | import org.w3c.dom.NodeList;
|
---|
| 15 |
|
---|
[29711] | 16 | import java.util.ArrayList;
|
---|
[24641] | 17 | import java.util.HashMap;
|
---|
[29711] | 18 | import java.util.Properties;
|
---|
[24641] | 19 |
|
---|
[29711] | 20 | import org.apache.log4j.Logger;
|
---|
[24641] | 21 | import org.apache.solr.client.solrj.SolrQuery;
|
---|
| 22 | import org.apache.solr.client.solrj.SolrServer;
|
---|
| 23 | import org.apache.solr.client.solrj.SolrServerException;
|
---|
[29711] | 24 | import org.apache.solr.client.solrj.impl.HttpSolrServer;
|
---|
[24641] | 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 |
|
---|
[29711] | 36 | protected String solr_servlet_base_url;
|
---|
| 37 | protected HashMap<String, SolrServer> solr_server;
|
---|
[24641] | 38 |
|
---|
| 39 | public SolrSearch()
|
---|
| 40 | {
|
---|
[29711] | 41 | solr_server = new HashMap<String, SolrServer>();
|
---|
[24641] | 42 |
|
---|
[29711] | 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");
|
---|
[32411] | 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();
|
---|
[32378] | 57 | String solrContext = globalProperties.getProperty("solr.context", "solr");
|
---|
[29711] | 58 |
|
---|
| 59 | String portStr = port.equals("80") ? "" : ":"+port;
|
---|
[32378] | 60 | solr_servlet_base_url = protocol+"://"+host+portStr+"/"+solrContext;
|
---|
[29711] | 61 | } catch(Exception e) {
|
---|
| 62 | logger.error("Error reading greenstone's tomcat solr server properties from global.properties", e);
|
---|
[24641] | 63 | }
|
---|
| 64 | }
|
---|
[25909] | 65 |
|
---|
[24641] | 66 |
|
---|
[25808] | 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.
|
---|
[25909] | 71 | // Adjusted to bring it up to speed with changes in GS2SolrSearch (for activate.pl) - not yet tested
|
---|
[25808] | 72 | public void cleanUp() {
|
---|
| 73 | super.cleanUp();
|
---|
[25909] | 74 |
|
---|
[29711] | 75 | // clear the map keeping track of the SolrServers in this collection
|
---|
| 76 | solr_server.clear();
|
---|
[25808] | 77 | }
|
---|
[24641] | 78 |
|
---|
[25909] | 79 |
|
---|
| 80 | // adjusted configure to bring it up to speed with changes in GS2SolrSearch (for activate.pl) - not yet tested
|
---|
[24641] | 81 | public boolean configure(Element info, Element extra_info) {
|
---|
[29711] | 82 | boolean success = super.configure(info, extra_info);
|
---|
[25909] | 83 |
|
---|
[29711] | 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 | }
|
---|
[24641] | 90 |
|
---|
| 91 | // initialize required number of SolrCores based on values
|
---|
| 92 | // in 'index_ids' that are set by LuceneSearch::configure()
|
---|
| 93 |
|
---|
[25909] | 94 | String core_name_prefix = getCollectionCoreNamePrefix();
|
---|
[24641] | 95 |
|
---|
| 96 | for (int i=0; i<index_ids.size(); i++) {
|
---|
| 97 |
|
---|
| 98 | String idx_name = (String)index_ids.get(i);
|
---|
[25909] | 99 | String core_name = core_name_prefix + "-" + idx_name;
|
---|
[24641] | 100 |
|
---|
[29711] | 101 | SolrServer solr_core = new HttpSolrServer(this.solr_servlet_base_url+"/"+core_name);
|
---|
[24641] | 102 | solr_server.put(core_name,solr_core);
|
---|
| 103 | }
|
---|
[29711] | 104 |
|
---|
[24641] | 105 |
|
---|
[25909] | 106 | return success;
|
---|
[24641] | 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 |
|
---|
[28967] | 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);
|
---|
[24641] | 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 {
|
---|
[29711] | 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"
|
---|
[24641] | 147 |
|
---|
[29711] | 148 | ///solrParams.set("start", start);
|
---|
| 149 | ///solrParams.set("rows", nbDocuments);
|
---|
[24641] | 150 |
|
---|
[29711] | 151 |
|
---|
[25909] | 152 | String core_name = getCollectionCoreNamePrefix() + "-" + index;
|
---|
[24641] | 153 |
|
---|
[29711] | 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);
|
---|
[24641] | 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() ??
|
---|
[28967] | 163 | GSXML.addMetadata(metadata_list, "numDocsMatched", ""+hits.size());
|
---|
[24641] | 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");
|
---|
[28967] | 172 | Element node = result_doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
[24641] | 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 |
|
---|
[25909] | 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 | }
|
---|
[24641] | 193 |
|
---|
| 194 | }
|
---|