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.HashMap;
|
---|
16 | import java.util.ArrayList;
|
---|
17 | import java.util.Iterator;
|
---|
18 |
|
---|
19 |
|
---|
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.embedded.EmbeddedSolrServer;
|
---|
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.params.ModifiableSolrParams;
|
---|
28 | import org.apache.solr.common.params.SolrParams;
|
---|
29 | import org.apache.solr.common.util.NamedList;
|
---|
30 | import org.apache.solr.servlet.SolrRequestParsers;
|
---|
31 | import org.apache.solr.core.CoreContainer;
|
---|
32 | import org.apache.solr.core.SolrCore;
|
---|
33 |
|
---|
34 | import java.io.File;
|
---|
35 | import java.util.Collection;
|
---|
36 |
|
---|
37 |
|
---|
38 | import org.apache.log4j.*;
|
---|
39 |
|
---|
40 |
|
---|
41 | public class SolrSearch extends LuceneSearch {
|
---|
42 |
|
---|
43 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.SolrSearch.class.getName());
|
---|
44 |
|
---|
45 | static protected CoreContainer solr_cores = null;
|
---|
46 | protected HashMap solr_server;
|
---|
47 |
|
---|
48 | public SolrSearch()
|
---|
49 | {
|
---|
50 | solr_server = new HashMap();
|
---|
51 |
|
---|
52 | if (solr_cores == null) {
|
---|
53 | // Share one CoreContainer across all sites/collections
|
---|
54 | try {
|
---|
55 |
|
---|
56 | String gsdl3_home = GlobalProperties.getGSDL3Home();
|
---|
57 | String solr_ext_name = GlobalProperties.getProperty("gsdlext.solr.dirname","solr");
|
---|
58 |
|
---|
59 | String solr_home_str = GSFile.extHome(gsdl3_home,solr_ext_name);
|
---|
60 |
|
---|
61 | solr_cores = new CoreContainer(solr_home_str);
|
---|
62 | }
|
---|
63 | catch (Exception e) {
|
---|
64 | e.printStackTrace();
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | // Overriding the cleanUp() method here, so as to parallel the structure of GS2SolrSearch
|
---|
71 | // which also calls shutdown() on its CoreContainer object in GS2SolrSearch.cleanUp().
|
---|
72 | // However, this class has not yet been tested, so it's not certain that this method is
|
---|
73 | // required here.
|
---|
74 | // Adjusted to bring it up to speed with changes in GS2SolrSearch (for activate.pl) - not yet tested
|
---|
75 | public void cleanUp() {
|
---|
76 | super.cleanUp();
|
---|
77 |
|
---|
78 | // 1. clear the map keeping track of the solrcores' EmbeddedSolrServers in this collection
|
---|
79 | solr_server.clear();
|
---|
80 |
|
---|
81 | // 2. Need GS3 server (tomcat) to release the cores, else a part of tomcat is still running in the background
|
---|
82 | // on ant stop, holding a lock on the cores. Doing shutdown() preserves core descriptions in solr.xml
|
---|
83 | solr_cores.shutdown();
|
---|
84 | solr_cores = null;
|
---|
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 | // 1. Make the CoreContainer reload solr.xml
|
---|
93 | // This is particularly needed for when activate.pl is executed during
|
---|
94 | // a running GS3 server. At that point, the solr collection is reactivated and
|
---|
95 | // we need to tell Greenstone that the solr index has changed. This requires
|
---|
96 | // the CoreContainer to reload the solr.xml file, and it all works again.
|
---|
97 |
|
---|
98 | solr_server.clear(); // clear the map of solr cores for this collection added to the map upon querying
|
---|
99 |
|
---|
100 | // Reload the updated solr.xml into the CoreContainer
|
---|
101 | // (Doing a solr_cores.shutdown() first doesn't seem to be required)
|
---|
102 | try {
|
---|
103 | String solr_home_str = solr_cores.getSolrHome();
|
---|
104 | File solr_home = new File(solr_home_str);
|
---|
105 | File solr_xml = new File( solr_home,"solr.xml" );
|
---|
106 |
|
---|
107 | //solr_cores.load(solr_home_str,solr_xml);
|
---|
108 | solr_cores.load();
|
---|
109 | } catch (Exception e) {
|
---|
110 | logger.error("Exception in SolrSearch.configure(): " + e.getMessage());
|
---|
111 | e.printStackTrace();
|
---|
112 | return false;
|
---|
113 | }
|
---|
114 |
|
---|
115 | // initialize required number of SolrCores based on values
|
---|
116 | // in 'index_ids' that are set by LuceneSearch::configure()
|
---|
117 |
|
---|
118 | String core_name_prefix = getCollectionCoreNamePrefix();
|
---|
119 |
|
---|
120 | for (int i=0; i<index_ids.size(); i++) {
|
---|
121 |
|
---|
122 | String idx_name = (String)index_ids.get(i);
|
---|
123 | String core_name = core_name_prefix + "-" + idx_name;
|
---|
124 |
|
---|
125 | EmbeddedSolrServer solr_core
|
---|
126 | = new EmbeddedSolrServer(solr_cores,core_name);
|
---|
127 |
|
---|
128 | solr_server.put(core_name,solr_core);
|
---|
129 | }
|
---|
130 |
|
---|
131 | return success;
|
---|
132 | }
|
---|
133 |
|
---|
134 | protected void getIndexData(ArrayList index_ids, ArrayList index_names, String lang)
|
---|
135 | {
|
---|
136 | }
|
---|
137 |
|
---|
138 | /** Process a text query - implemented by concrete subclasses */
|
---|
139 | protected Element processTextQuery(Element request) {
|
---|
140 |
|
---|
141 | Document result_doc = XMLConverter.newDOM();
|
---|
142 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
143 | Element doc_node_list = result_doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
144 | Element metadata_list = result_doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
145 | initResultElement(result,doc_node_list,metadata_list);
|
---|
146 |
|
---|
147 | if (!hasParamList(request,metadata_list)) {
|
---|
148 | return result;
|
---|
149 | }
|
---|
150 |
|
---|
151 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
152 | if (!hasQueryString(param_list,metadata_list)) {
|
---|
153 | return result;
|
---|
154 | }
|
---|
155 |
|
---|
156 | HashMap params = GSXML.extractParams(param_list, false);
|
---|
157 | String query_string = (String) params.get(QUERY_PARAM);
|
---|
158 |
|
---|
159 |
|
---|
160 | // Get the index
|
---|
161 | String index = (String) params.get(INDEX_PARAM);
|
---|
162 | if (index == null || index.equals("")) {
|
---|
163 | index = this.default_index; // assume the default
|
---|
164 | }
|
---|
165 |
|
---|
166 | try {
|
---|
167 |
|
---|
168 | ModifiableSolrParams solrParams = new ModifiableSolrParams();
|
---|
169 | solrParams.set("q", query_string);
|
---|
170 | //solrParams.set("start", start);
|
---|
171 | //solrParams.set("rows", nbDocuments);
|
---|
172 |
|
---|
173 | String core_name = getCollectionCoreNamePrefix() + "-" + index;
|
---|
174 |
|
---|
175 | EmbeddedSolrServer solr_core = (EmbeddedSolrServer)solr_server.get(core_name);
|
---|
176 |
|
---|
177 | QueryResponse solrResponse = solr_core.query(solrParams);
|
---|
178 |
|
---|
179 | SolrDocumentList hits = solrResponse.getResults();
|
---|
180 |
|
---|
181 | if (hits != null) {
|
---|
182 | // or should this be docs.getNumFound() ??
|
---|
183 | GSXML.addMetadata(metadata_list, "numDocsMatched", ""+hits.size());
|
---|
184 |
|
---|
185 | System.err.println(hits.getNumFound() + " documents found, "
|
---|
186 | + hits.size() + " returned : ");
|
---|
187 |
|
---|
188 | for (int i = 0; i < hits.size(); i++) {
|
---|
189 | SolrDocument solr_doc = hits.get(i);
|
---|
190 |
|
---|
191 | String node_id = (String)solr_doc.get("docOID");
|
---|
192 | Element node = result_doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
193 | node.setAttribute(GSXML.NODE_ID_ATT, node_id);
|
---|
194 | doc_node_list.appendChild(node);
|
---|
195 |
|
---|
196 | System.out.println("\t" + solr_doc.toString());
|
---|
197 | }
|
---|
198 | }
|
---|
199 | } catch (Exception e) {
|
---|
200 | e.printStackTrace();
|
---|
201 | }
|
---|
202 |
|
---|
203 | return result;
|
---|
204 |
|
---|
205 | }
|
---|
206 |
|
---|
207 | protected String getCollectionCoreNamePrefix() {
|
---|
208 | String site_name = this.router.getSiteName();
|
---|
209 | String coll_name = this.cluster_name;
|
---|
210 | String collection_core_name_prefix = site_name + "-" + coll_name;
|
---|
211 | return collection_core_name_prefix;
|
---|
212 | }
|
---|
213 |
|
---|
214 | }
|
---|