Revision 55643
Added by Katerina Iatropoulou almost 5 years ago
SolrResultSet.java | ||
---|---|---|
11 | 11 |
import gr.uoa.di.driver.enabling.resultset.ResultSet; |
12 | 12 |
import org.apache.commons.lang.StringEscapeUtils; |
13 | 13 |
import org.apache.log4j.Logger; |
14 |
import org.apache.solr.client.solrj.SolrClient; |
|
14 | 15 |
import org.apache.solr.client.solrj.SolrServerException; |
15 |
import org.apache.solr.client.solrj.impl.CloudSolrServer;
|
|
16 |
import org.apache.solr.client.solrj.impl.CloudSolrClient;
|
|
16 | 17 |
import org.apache.solr.client.solrj.response.FacetField; |
17 | 18 |
import org.apache.solr.client.solrj.response.QueryResponse; |
18 | 19 |
import org.apache.solr.common.SolrDocumentList; |
... | ... | |
23 | 24 |
import javax.ws.rs.core.MediaType; |
24 | 25 |
import java.io.IOException; |
25 | 26 |
import java.io.OutputStream; |
26 |
import java.util.ArrayList; |
|
27 |
import java.util.HashMap; |
|
28 |
import java.util.List; |
|
29 |
import java.util.Map; |
|
27 |
import java.util.*; |
|
30 | 28 |
|
31 | 29 |
/** |
32 | 30 |
* Created by antleb on 2/4/14. |
... | ... | |
36 | 34 |
private Logger logger = Logger.getLogger(getClass()); |
37 | 35 |
|
38 | 36 |
private EPR epr = null; |
39 |
private CloudSolrServer solrClient = null;
|
|
37 |
public SolrClient solrClient = null;
|
|
40 | 38 |
|
41 | 39 |
private NamedList<String> queryOpts = new NamedList<String>(); |
42 | 40 |
long size = -1; |
43 | 41 |
|
44 | 42 |
|
45 |
public SolrResultSet(EPR epr, CloudSolrServer solrClient) throws IOException, CQLParseException { |
|
43 |
public SolrResultSet(EPR epr, CloudSolrClient cloudSolrClient) throws IOException, CQLParseException { |
|
44 |
logger.debug("Setting solr client " + cloudSolrClient); |
|
46 | 45 |
this.epr = epr; |
47 |
this.solrClient = solrClient;
|
|
46 |
this.solrClient = cloudSolrClient;
|
|
48 | 47 |
this.queryOpts = SolrResultSetOptionsUtil.extractQueryOptions(epr.getParameter("query")); |
49 | 48 |
|
50 | 49 |
String layout = epr.getParameter("layout"); |
51 | 50 |
String mdformat = epr.getParameter("mdformat"); |
52 | 51 |
String interpretation = epr.getParameter("interpretation"); |
53 | 52 |
|
54 |
solrClient.setDefaultCollection(mdformat + "-" + layout + "-" + interpretation);
|
|
53 |
((CloudSolrClient)solrClient).setDefaultCollection(mdformat + "-" + layout + "-" + interpretation);
|
|
55 | 54 |
} |
56 | 55 |
|
57 | 56 |
@Override |
... | ... | |
66 | 65 |
|
67 | 66 |
@Override |
68 | 67 |
public void close() { |
69 |
//solrClient.shutdown(); |
|
68 |
/* |
|
69 |
try { |
|
70 |
logger.debug("!!!!!!!!! !!!!!!! CLOSING !!!!!!!!! !!!!!!!!!! "); |
|
71 |
solrClient.close(); |
|
72 |
|
|
73 |
} catch (IOException e) { |
|
74 |
logger.error("Error closing result set.", e); |
|
75 |
} |
|
76 |
*/ |
|
70 | 77 |
} |
71 | 78 |
|
72 | 79 |
@Override |
... | ... | |
133 | 140 |
logger.debug("docs: " + docs.size()); |
134 | 141 |
|
135 | 142 |
for (int i = 0; i < docs.size(); i++) { |
136 |
String result = ((ArrayList<String>) docs.get(i).get("__result")).get(0);
|
|
143 |
String result = (String) docs.get(i).get("__result");
|
|
137 | 144 |
res.add(result); |
138 | 145 |
} |
139 | 146 |
|
... | ... | |
141 | 148 |
|
142 | 149 |
} catch (SolrServerException sse) { |
143 | 150 |
logger.error("Fail to get results from Solr. ", sse); |
151 |
|
|
152 |
} catch (IOException ioe) { |
|
153 |
logger.error("Fail to get results from Solr. ", ioe); |
|
144 | 154 |
} |
145 | 155 |
|
146 | 156 |
return null; |
... | ... | |
155 | 165 |
List<String> refineSolrResults = new ArrayList<String>(); |
156 | 166 |
List<String> searchSolrResults = new ArrayList<String>(); |
157 | 167 |
|
158 |
logger.debug("format: " + format); |
|
159 |
|
|
160 | 168 |
QueryResponse rsp = null; |
161 | 169 |
HashMap<String, List<String>> map = new HashMap<String, List<String>>(); |
162 | 170 |
|
163 |
//logger.info("from: " + from*to); |
|
164 |
//logger.info("to: " + to); |
|
165 |
|
|
166 | 171 |
queryOpts.add("start", from*to + ""); |
167 | 172 |
queryOpts.add("rows", to +""); |
168 | 173 |
|
169 | 174 |
try { |
170 | 175 |
long startTime = System.nanoTime(); |
176 |
|
|
171 | 177 |
rsp = solrClient.query(SolrParams.toSolrParams(queryOpts)); |
172 | 178 |
long estimatedTime = System.nanoTime() - startTime; |
173 | 179 |
logger.info("Solrj time " + estimatedTime/1000000 + " milliseconds for query:" + queryOpts.get("q") + |
... | ... | |
181 | 187 |
this.size = docs.getNumFound(); |
182 | 188 |
|
183 | 189 |
if (facetFields!=null && !facetFields.isEmpty()) { |
184 |
|
|
185 |
logger.debug("Checking " + (format != null && format.equals(MediaType.APPLICATION_JSON))); |
|
186 |
|
|
187 | 190 |
if (format != null && format.equals(MediaType.APPLICATION_JSON)) { |
188 | 191 |
for (FacetField field : facetFields) { |
189 | 192 |
map.put(field.getName(), new ArrayList<String>()); |
... | ... | |
194 | 197 |
bf.setId(field.getValues().get(i).getName()); |
195 | 198 |
String[] facetedValues = field.getValues().get(i).getName().split("\\|\\|",2); |
196 | 199 |
|
200 |
|
|
201 |
logger.debug("faceted values " + Arrays.toString(facetedValues)); |
|
202 |
|
|
197 | 203 |
if (facetedValues.length > 1) { |
198 | 204 |
//bf.setName(org.apache.commons.lang3.StringEscapeUtils.escapeJson(facetedValues[1])); |
199 | 205 |
bf.setName(facetedValues[1]); |
206 |
logger.debug("faceted values [1] " + facetedValues[1]); |
|
200 | 207 |
|
201 | 208 |
} else if (field.getValues().get(i).getName().split("_\\:\\:",2).length > 1) { |
202 | 209 |
//bf.setName(org.apache.commons.lang3.StringEscapeUtils.escapeJson(field.getValues().get(i).getName().split("\\:\\:",2)[1]).replaceAll("\\:\\:", "\\|")); |
... | ... | |
222 | 229 |
refineSolrResults.add(builder.toString()); |
223 | 230 |
} |
224 | 231 |
|
225 |
} else { //the old implementation & xml as default //TODO check compatibility
|
|
232 |
} else { //the old implementation & xml as default |
|
226 | 233 |
logger.debug("Creating old browse results."); |
227 | 234 |
createXmlRefineFields(refineSolrResults, oldRefineTransformer); |
228 | 235 |
} |
229 | 236 |
} |
230 | 237 |
|
231 | 238 |
for (int i = 0; i < docs.size(); i++) { |
232 |
String result = ((ArrayList<String>) docs.get(i).get("__result")).get(0); |
|
239 |
String result = (String) docs.get(i).get("__result"); |
|
240 |
|
|
241 |
logger.debug("["+ i +"]: " + docs.get(i).get("__result")); |
|
242 |
|
|
233 | 243 |
try { |
234 | 244 |
if (transformer != null) { |
235 |
//logger.debug("1 >>>>>>" + result);
|
|
245 |
logger.debug("1 >>>>>>" + result); |
|
236 | 246 |
String xml = result.replaceAll("<em>","").replaceAll("</em>",""); |
237 | 247 |
result = transformer.transform(xml); |
238 |
//logger.debug("2 >>>>>>" + result);
|
|
248 |
logger.debug("2 >>>>>>" + result); |
|
239 | 249 |
} |
250 |
|
|
240 | 251 |
} catch (TransformerException te) { |
241 | 252 |
logger.warn("Error transforming " + result, te); |
242 | 253 |
continue; |
... | ... | |
251 | 262 |
|
252 | 263 |
Map<String,List<String>> response = new HashMap<String, List<String>>(); |
253 | 264 |
|
254 |
logger.debug("refine results " + refineSolrResults); |
|
265 |
//logger.debug("refine results " + refineSolrResults);
|
|
255 | 266 |
//logger.debug("search results " + searchSolrResults); |
256 | 267 |
|
257 | 268 |
response.put("refine",refineSolrResults); |
... | ... | |
261 | 272 |
|
262 | 273 |
} catch (SolrServerException sse) { |
263 | 274 |
logger.error("Error calling Solr.", sse); |
275 |
|
|
276 |
} catch (IOException ioe) { |
|
277 |
logger.error("Error calling Solr.", ioe); |
|
264 | 278 |
} |
265 | 279 |
return null; |
266 | 280 |
} |
... | ... | |
279 | 293 |
String nextCursorMark = ""; |
280 | 294 |
|
281 | 295 |
int curs = 0; |
282 |
QueryResponse resp = solrClient.query(SolrParams.toSolrParams(queryOpts)); |
|
296 |
try { |
|
297 |
QueryResponse resp = solrClient.query(SolrParams.toSolrParams(queryOpts)); |
|
283 | 298 |
|
284 |
while (!cursorMark.equals(nextCursorMark)) { |
|
285 |
resp = solrClient.query(SolrParams.toSolrParams(queryOpts)); |
|
286 |
cursorMark = nextCursorMark; |
|
287 |
nextCursorMark = resp.getNextCursorMark(); |
|
299 |
while (!cursorMark.equals(nextCursorMark)) {
|
|
300 |
resp = solrClient.query(SolrParams.toSolrParams(queryOpts));
|
|
301 |
cursorMark = nextCursorMark;
|
|
302 |
nextCursorMark = resp.getNextCursorMark();
|
|
288 | 303 |
|
289 |
for (int i = 0; i < resp.getResults().size(); i++) { |
|
290 |
if (transformer != null) { |
|
291 |
String result = null; |
|
292 |
try { |
|
293 |
result = transformer.transform(((ArrayList<String>) resp.getResults().get(i).get("__result")).get(0)); |
|
304 |
for (int i = 0; i < resp.getResults().size(); i++) { |
|
305 |
if (transformer != null) { |
|
306 |
String result = null; |
|
307 |
try { |
|
308 |
result = transformer.transform((String) resp.getResults().get(i).get("__result")); |
|
309 |
logger.debug("RESULT " + result); |
|
294 | 310 |
|
295 |
} catch (TransformerException te) { |
|
296 |
logger.warn("Error transforming " + result, te); |
|
297 |
continue; |
|
298 |
} |
|
311 |
} catch (TransformerException te) {
|
|
312 |
logger.warn("Error transforming " + result, te);
|
|
313 |
continue;
|
|
314 |
}
|
|
299 | 315 |
|
300 |
try { |
|
301 |
os.write(result.getBytes()); |
|
302 |
os.flush(); |
|
303 |
} catch (IOException e) { |
|
304 |
logger.error("Cursor get... ", e); |
|
305 |
continue; |
|
316 |
try { |
|
317 |
os.write(result.getBytes()); |
|
318 |
os.flush(); |
|
319 |
} catch (IOException e) { |
|
320 |
logger.error("Cursor get... ", e); |
|
321 |
continue; |
|
322 |
} |
|
306 | 323 |
} |
307 | 324 |
} |
325 |
|
|
326 |
queryOpts.remove("cursorMark"); |
|
327 |
queryOpts.add("cursorMark", nextCursorMark); |
|
328 |
curs++; |
|
308 | 329 |
} |
309 | 330 |
|
310 |
queryOpts.remove("cursorMark"); |
|
311 |
queryOpts.add("cursorMark", nextCursorMark); |
|
312 |
curs ++; |
|
331 |
} catch (IOException ioe) { |
|
332 |
logger.error("Error executing solr query. ", ioe); |
|
313 | 333 |
} |
314 | 334 |
|
315 | 335 |
logger.debug("CURS " + curs); |
Also available in: Unified diff
merge uoa-search solr7 branch 50700:55641 into trunk