1
|
package eu.dnetlib.data.search.solr;
|
2
|
|
3
|
import com.google.gson.Gson;
|
4
|
import eu.dnetlib.data.search.transform.Transformer;
|
5
|
import eu.dnetlib.data.search.transform.TransformerException;
|
6
|
import eu.dnetlib.data.search.utils.solr.SolrResultSetOptionsUtil;
|
7
|
import eu.dnetlib.data.search.utils.solr.SolrResultsFormatter;
|
8
|
import eu.dnetlib.domain.EPR;
|
9
|
import gr.uoa.di.driver.enabling.resultset.ResultSet;
|
10
|
import org.apache.commons.lang.StringEscapeUtils;
|
11
|
import org.apache.log4j.Logger;
|
12
|
import org.apache.solr.client.solrj.SolrServerException;
|
13
|
import org.apache.solr.client.solrj.impl.CloudSolrServer;
|
14
|
import org.apache.solr.client.solrj.response.FacetField;
|
15
|
import org.apache.solr.client.solrj.response.QueryResponse;
|
16
|
import org.apache.solr.common.SolrDocumentList;
|
17
|
import org.apache.solr.common.params.SolrParams;
|
18
|
import org.apache.solr.common.util.NamedList;
|
19
|
import org.z3950.zing.cql.CQLParseException;
|
20
|
|
21
|
import javax.ws.rs.core.MediaType;
|
22
|
import java.io.IOException;
|
23
|
import java.util.ArrayList;
|
24
|
import java.util.HashMap;
|
25
|
import java.util.List;
|
26
|
import java.util.Map;
|
27
|
|
28
|
/**
|
29
|
* Created by antleb on 2/4/14.
|
30
|
*/
|
31
|
public class SolrResultSet implements ResultSet<String> {
|
32
|
|
33
|
private Logger logger = Logger.getLogger(getClass());
|
34
|
|
35
|
private EPR epr = null;
|
36
|
private CloudSolrServer solrClient = null;
|
37
|
|
38
|
private NamedList<String> queryOpts = new NamedList<String>();
|
39
|
long size = -1;
|
40
|
|
41
|
|
42
|
public SolrResultSet(EPR epr, CloudSolrServer solrClient) throws IOException, CQLParseException {
|
43
|
this.epr = epr;
|
44
|
this.solrClient = solrClient;
|
45
|
this.queryOpts = SolrResultSetOptionsUtil.extractQueryOptions(epr.getParameter("query"));
|
46
|
|
47
|
String layout = epr.getParameter("layout");
|
48
|
String mdformat = epr.getParameter("mdformat");
|
49
|
String interpretation = epr.getParameter("interpretation");
|
50
|
|
51
|
solrClient.setDefaultCollection(mdformat + "-" + layout + "-" + interpretation);
|
52
|
}
|
53
|
|
54
|
|
55
|
|
56
|
@Override
|
57
|
public boolean isOpen() {
|
58
|
return true;
|
59
|
}
|
60
|
|
61
|
@Override
|
62
|
public boolean isAlive() {
|
63
|
return true;
|
64
|
}
|
65
|
|
66
|
@Override
|
67
|
public void close() {
|
68
|
//solrClient.shutdown();
|
69
|
}
|
70
|
|
71
|
@Override
|
72
|
public int size() {
|
73
|
return (int) size;
|
74
|
}
|
75
|
|
76
|
@Override
|
77
|
@Deprecated
|
78
|
public List<String> getElements(int from, int to) {
|
79
|
return get(from, to);
|
80
|
}
|
81
|
|
82
|
List<FacetField> facetFields = null;
|
83
|
|
84
|
@Override
|
85
|
@Deprecated
|
86
|
public List<String> get(int from, int to) {
|
87
|
List<String> res = new ArrayList<String>();
|
88
|
|
89
|
QueryResponse rsp = null;
|
90
|
|
91
|
HashMap<String, List<String>> map = new HashMap<String, List<String>>();
|
92
|
|
93
|
logger.debug("from: " + from);
|
94
|
logger.debug("to: " + to);
|
95
|
|
96
|
|
97
|
queryOpts.add("start", from+1 + "");
|
98
|
queryOpts.add("rows", to + 1+"");
|
99
|
|
100
|
try {
|
101
|
rsp = solrClient.query(SolrParams.toSolrParams(queryOpts));
|
102
|
facetFields = rsp.getFacetFields();
|
103
|
SolrDocumentList docs = rsp.getResults();
|
104
|
|
105
|
if (facetFields!=null && !facetFields.isEmpty()) {
|
106
|
for (int i = from - 1; i < to; i++) {
|
107
|
for (FacetField field : facetFields) {
|
108
|
if (field.getValueCount() > i) {
|
109
|
BrowseField bf = new BrowseField();
|
110
|
bf.setId(field.getValues().get(i).getName());
|
111
|
bf.setName(field.getValues().get(i).getName());
|
112
|
bf.setCount(field.getValues().get(i).getCount() + "");
|
113
|
if (map.get(field.getName()) == null) {
|
114
|
map.put(field.getName(), new ArrayList<String>());
|
115
|
}
|
116
|
|
117
|
map.get(field.getName()).add(new Gson().toJson(bf));
|
118
|
}
|
119
|
}
|
120
|
}
|
121
|
|
122
|
for (Map.Entry<String, List<String>> facetEntry : map.entrySet()) {
|
123
|
StringBuilder builder = new StringBuilder();
|
124
|
builder.append("\"" + facetEntry.getKey() + "\"" + " : ");
|
125
|
builder.append(facetEntry.getValue());
|
126
|
res.add(builder.toString());
|
127
|
}
|
128
|
}
|
129
|
|
130
|
logger.debug("time: " + rsp.getElapsedTime());
|
131
|
logger.debug("found: " + docs.getNumFound());
|
132
|
logger.debug("docs: " + docs.size());
|
133
|
|
134
|
for (int i = 0; i < docs.size(); i++) {
|
135
|
String result = ((ArrayList<String>) docs.get(i).get("__result")).get(0);
|
136
|
res.add(result);
|
137
|
}
|
138
|
|
139
|
return res;
|
140
|
|
141
|
} catch (SolrServerException sse) {
|
142
|
logger.error("Fail to get results from Solr. ", sse);
|
143
|
}
|
144
|
|
145
|
return null;
|
146
|
}
|
147
|
|
148
|
public Map<String,List<String>> newGet(int from, int to, String format, Transformer transformer, Transformer oldRefineTransformer) {
|
149
|
List<String> refineSolrResults = new ArrayList<String>();
|
150
|
List<String> searchSolrResults = new ArrayList<String>();
|
151
|
|
152
|
logger.debug("format: " + format);
|
153
|
|
154
|
QueryResponse rsp = null;
|
155
|
HashMap<String, List<String>> map = new HashMap<String, List<String>>();
|
156
|
|
157
|
//logger.info("from: " + from*to);
|
158
|
//logger.info("to: " + to);
|
159
|
|
160
|
queryOpts.add("start", from*to + "");
|
161
|
queryOpts.add("rows", to +"");
|
162
|
|
163
|
try {
|
164
|
long startTime = System.nanoTime();
|
165
|
rsp = solrClient.query(SolrParams.toSolrParams(queryOpts));
|
166
|
long estimatedTime = System.nanoTime() - startTime;
|
167
|
logger.info("Solrj time " + estimatedTime/1000000 + " milliseconds for query:" + queryOpts.get("q") +
|
168
|
" and facets " + queryOpts.getAll("facet.field") + " and fq " + queryOpts.getAll("fq") + " from: "
|
169
|
+ from + " and size " + to);
|
170
|
|
171
|
facetFields = rsp.getFacetFields();
|
172
|
|
173
|
SolrDocumentList docs = rsp.getResults();
|
174
|
|
175
|
this.size = docs.getNumFound();
|
176
|
|
177
|
if (facetFields!=null && !facetFields.isEmpty()) {
|
178
|
|
179
|
logger.debug("Checking " + (format != null && format.equals(MediaType.APPLICATION_JSON)));
|
180
|
|
181
|
if (format != null && format.equals(MediaType.APPLICATION_JSON)) {
|
182
|
for (FacetField field : facetFields) {
|
183
|
map.put(field.getName(), new ArrayList<String>());
|
184
|
BrowseField bf = null;
|
185
|
for (int i = 0; i < field.getValueCount(); i++) {
|
186
|
bf = new BrowseField();
|
187
|
//bf.setId(org.apache.commons.lang3.StringEscapeUtils.escapeJson(field.getValues().get(i).getName()));
|
188
|
bf.setId(field.getValues().get(i).getName());
|
189
|
String[] facetedValues = field.getValues().get(i).getName().split("\\|\\|",2);
|
190
|
|
191
|
if (facetedValues.length > 1) {
|
192
|
//bf.setName(org.apache.commons.lang3.StringEscapeUtils.escapeJson(facetedValues[1]));
|
193
|
bf.setName(facetedValues[1]);
|
194
|
|
195
|
} else if (field.getValues().get(i).getName().split("_\\:\\:",2).length > 1) {
|
196
|
//bf.setName(org.apache.commons.lang3.StringEscapeUtils.escapeJson(field.getValues().get(i).getName().split("\\:\\:",2)[1]).replaceAll("\\:\\:", "\\|"));
|
197
|
bf.setName(field.getValues().get(i).getName().split("\\:\\:",2)[1].replaceAll("\\:\\:", "\\|"));
|
198
|
|
199
|
} else {
|
200
|
//bf.setName(org.apache.commons.lang3.StringEscapeUtils.escapeJson(field.getValues().get(i).getName()));
|
201
|
bf.setName(field.getValues().get(i).getName());
|
202
|
}
|
203
|
|
204
|
bf.setCount(field.getValues().get(i).getCount() + "");
|
205
|
map.get(field.getName()).add(new Gson().toJson(bf));
|
206
|
}
|
207
|
|
208
|
}
|
209
|
|
210
|
StringBuilder builder = null;
|
211
|
|
212
|
for (Map.Entry<String, List<String>> facetEntry : map.entrySet()) {
|
213
|
builder = new StringBuilder();
|
214
|
builder.append("\"" + facetEntry.getKey() + "\"" + " : ");
|
215
|
builder.append(facetEntry.getValue());
|
216
|
refineSolrResults.add(builder.toString());
|
217
|
}
|
218
|
|
219
|
} else { //the old implementation & xml as default //TODO check compatibility
|
220
|
logger.debug("Creating old browse results.");
|
221
|
createXmlRefineFields(refineSolrResults, oldRefineTransformer);
|
222
|
}
|
223
|
}
|
224
|
|
225
|
for (int i = 0; i < docs.size(); i++) {
|
226
|
String result = ((ArrayList<String>) docs.get(i).get("__result")).get(0);
|
227
|
try {
|
228
|
if (transformer != null) {
|
229
|
//logger.debug("1 >>>>>>" + result);
|
230
|
String xml = result.replaceAll("<em>","").replaceAll("</em>","");
|
231
|
result = transformer.transform(xml);
|
232
|
//logger.debug("2 >>>>>>" + result);
|
233
|
}
|
234
|
} catch (TransformerException te) {
|
235
|
logger.warn("Error transforming " + result, te);
|
236
|
continue;
|
237
|
}
|
238
|
|
239
|
if (format != null && format.equals(MediaType.APPLICATION_JSON)) {
|
240
|
searchSolrResults.add(SolrResultsFormatter.xml2Json(result));
|
241
|
} else { // default xml
|
242
|
searchSolrResults.add(result);
|
243
|
}
|
244
|
}
|
245
|
|
246
|
Map<String,List<String>> response = new HashMap<String, List<String>>();
|
247
|
|
248
|
logger.debug("refine results " + refineSolrResults);
|
249
|
//logger.debug("search results " + searchSolrResults);
|
250
|
|
251
|
response.put("refine",refineSolrResults);
|
252
|
response.put("search", searchSolrResults);
|
253
|
|
254
|
return response;
|
255
|
|
256
|
} catch (SolrServerException sse) {
|
257
|
logger.error("Error calling Solr.", sse);
|
258
|
}
|
259
|
return null;
|
260
|
}
|
261
|
|
262
|
|
263
|
//TODO get rid of this as soon as Joomla portal is out
|
264
|
//Just copied and refactored the old one...
|
265
|
@Deprecated
|
266
|
private void createXmlRefineFields(List<String> res, Transformer oldRefineTransformer) {
|
267
|
int max = -12;
|
268
|
|
269
|
for (FacetField field:facetFields) {
|
270
|
logger.debug("field " + field.getName() + " has count " + field.getValueCount());
|
271
|
|
272
|
if (field.getValueCount() > max) {
|
273
|
max = field.getValueCount();
|
274
|
}
|
275
|
}
|
276
|
|
277
|
logger.debug("max " + max);
|
278
|
|
279
|
for (int i = 0; i < max; i++) {
|
280
|
StringBuilder sb = new StringBuilder();
|
281
|
|
282
|
sb.append("<row>");
|
283
|
for (FacetField field:facetFields) {
|
284
|
if (field.getValueCount() > i) {
|
285
|
sb.append("<groupresult field=\"").append(field.getName()).append("\">");
|
286
|
sb.append("<count>").append(field.getValues().get(i).getCount()).append("</count>");
|
287
|
sb.append("<originalValue>").append(StringEscapeUtils.escapeXml(field.getValues().get(i).getName())).append("</originalValue>");
|
288
|
|
289
|
String[] facetValues = field.getValues().get(i).getName().split("\\|\\|");
|
290
|
if(facetValues.length > 1) {
|
291
|
sb.append("<value>").append(StringEscapeUtils.escapeXml(facetValues[1])).append("</value>");
|
292
|
} else {
|
293
|
sb.append("<value>").append(StringEscapeUtils.escapeXml(facetValues[0])).append("</value>");
|
294
|
}
|
295
|
sb.append("</groupresult>");
|
296
|
}
|
297
|
}
|
298
|
sb.append("</row>");
|
299
|
|
300
|
try {
|
301
|
//logger.debug("row: " + sb.toString());
|
302
|
//logger.debug("row2: " + oldRefineTransformer.transform(sb.toString()));
|
303
|
|
304
|
//TODO remove
|
305
|
res.add(oldRefineTransformer.transform(sb.toString()));
|
306
|
|
307
|
} catch (TransformerException te) {
|
308
|
logger.error("Cannot transform refine for: " + sb.toString(), te);
|
309
|
}
|
310
|
}
|
311
|
}
|
312
|
|
313
|
/*
|
314
|
public static void main(String[] args) throws IOException, CQLParseException, SolrServerException {
|
315
|
CloudSolrServer solrClient = new CloudSolrServer("beta.solr.openaire.eu:9983");
|
316
|
NamedList<String> queryOpts = new NamedList<String>();
|
317
|
|
318
|
// queryOpts.add("q", new CqlTranslatorImpl().getTranslatedQuery("oaftype exact project").asLucene());
|
319
|
NamedList<String> extraOpts = new NamedList<String>();
|
320
|
|
321
|
//extraOpts.add("start", "1");
|
322
|
// extraOpts.add("rows", "10");
|
323
|
// extraOpts.addAll(queryOpts);
|
324
|
|
325
|
queryOpts.add("facet", "true");
|
326
|
//TranslatedQuery translatedQuery = new CqlTranslatorImpl().getTranslatedQuery("oaftype=result sortBy resultdateofacceptance/sort.descending");
|
327
|
|
328
|
// queryOpts.add("q", "oaftype=project");
|
329
|
queryOpts.add("facet", "true");
|
330
|
queryOpts.add("facet.mincount", "1");
|
331
|
queryOpts.add("fq", "popularity");
|
332
|
|
333
|
|
334
|
|
335
|
// queryOpts.put("fq", new CqlTranslatorImpl().getTranslatedQuery("").asLucene());
|
336
|
// queryOpts.add("facet.field", "contextid");
|
337
|
// queryOpts.add("facet.field", "contextname");
|
338
|
// queryOpts.add("facet.mincount", "1");
|
339
|
// queryOpts.add("facet.threads", "10");
|
340
|
// System.out.println(translatedQuery.getOptions().getSort().getMode());
|
341
|
// System.out.println(translatedQuery.getOptions().getSort().getField());
|
342
|
|
343
|
//queryOpts.add("sort", translatedQuery.getOptions().getSort().getField() + " " + translatedQuery.getOptions().getSort().getMode() );
|
344
|
|
345
|
solrClient.setDefaultCollection("DMF-index-openaire");
|
346
|
|
347
|
/* QueryResponse resp = null;
|
348
|
synchronized (solrClient) {
|
349
|
resp = solrClient.query(SolrParams.toSolrParams(extraOpts));
|
350
|
}*/
|
351
|
// System.out.println("time: " + resp.getElapsedTime());
|
352
|
//System.out.println("results: " + resp.getResults());
|
353
|
|
354
|
/* System.out.println(resp.getFacetField("contextname").getValueCount());
|
355
|
|
356
|
for (FacetField.Count count:resp.getFacetField("contextname").getValues())
|
357
|
System.out.println(count.getName() + " : " + count.getCount());
|
358
|
|
359
|
|
360
|
int max = -12;
|
361
|
|
362
|
for (FacetField field:resp.getFacetFields()) {
|
363
|
if (field.getValueCount() > max)
|
364
|
max = field.getValueCount();
|
365
|
|
366
|
}
|
367
|
|
368
|
System.out.println("max: " + max);
|
369
|
*/
|
370
|
// }
|
371
|
|
372
|
@Override
|
373
|
public EPR getEpr() {
|
374
|
return epr;
|
375
|
}
|
376
|
}
|
377
|
|
378
|
class BrowseField {
|
379
|
String name;
|
380
|
|
381
|
public String getName() {
|
382
|
return name;
|
383
|
}
|
384
|
|
385
|
public void setName(String name) {
|
386
|
this.name = name;
|
387
|
}
|
388
|
|
389
|
public String getId() {
|
390
|
return id;
|
391
|
}
|
392
|
|
393
|
public void setId(String id) {
|
394
|
this.id = id;
|
395
|
}
|
396
|
|
397
|
public String getCount() {
|
398
|
return count;
|
399
|
}
|
400
|
|
401
|
public void setCount(String count) {
|
402
|
this.count = count;
|
403
|
}
|
404
|
|
405
|
String id;
|
406
|
String count;
|
407
|
|
408
|
|
409
|
}
|