1
|
package eu.dnetlib.index.query;
|
2
|
|
3
|
import java.io.ByteArrayInputStream;
|
4
|
import java.io.IOException;
|
5
|
import java.util.Collection;
|
6
|
import java.util.List;
|
7
|
import java.util.Map;
|
8
|
import java.util.function.Function;
|
9
|
import java.util.stream.Collectors;
|
10
|
import java.util.zip.GZIPInputStream;
|
11
|
|
12
|
import org.apache.commons.io.IOUtils;
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
import org.apache.solr.client.solrj.response.FacetField;
|
16
|
import org.apache.solr.client.solrj.response.FacetField.Count;
|
17
|
import org.apache.solr.client.solrj.response.QueryResponse;
|
18
|
import org.apache.solr.common.SolrDocument;
|
19
|
import org.apache.solr.common.SolrDocumentList;
|
20
|
|
21
|
import com.google.common.base.Predicate;
|
22
|
import com.google.common.collect.BiMap;
|
23
|
import com.google.common.collect.Iterables;
|
24
|
import com.google.common.collect.Lists;
|
25
|
|
26
|
import eu.dnetlib.clients.index.query.QueryResponseParser;
|
27
|
import eu.dnetlib.clients.index.utils.IndexFieldUtility;
|
28
|
import eu.dnetlib.rmi.provision.BrowsingRow;
|
29
|
import eu.dnetlib.rmi.provision.GroupResult;
|
30
|
|
31
|
/**
|
32
|
* The Class SolrResponseParser.
|
33
|
*/
|
34
|
public class SolrResponseParser extends QueryResponseParser {
|
35
|
|
36
|
/**
|
37
|
* logger.
|
38
|
*/
|
39
|
private static final Log log = LogFactory.getLog(SolrResponseParser.class);
|
40
|
|
41
|
/**
|
42
|
* Lower level response.
|
43
|
*/
|
44
|
private QueryResponse queryRsp = null;
|
45
|
|
46
|
/** The wrapper rank. */
|
47
|
protected final Function<SolrDocument, String> wrapperRank =
|
48
|
doc -> addRanking(
|
49
|
getSingleField(doc, IndexFieldUtility.RESULT),
|
50
|
getSingleField(doc, IndexFieldUtility.SCORE_FIELD));
|
51
|
|
52
|
/** The wrapper no rank. */
|
53
|
protected final Function<SolrDocument, String> wrapperNoRank =
|
54
|
doc -> wrap(getSingleField(doc, IndexFieldUtility.RESULT));
|
55
|
|
56
|
/**
|
57
|
* The Constructor.
|
58
|
*
|
59
|
* @param highlightUtils
|
60
|
* the highlight utils
|
61
|
* @param aliases
|
62
|
* the aliases
|
63
|
* @param returnEmptyFields
|
64
|
* the return empty fields
|
65
|
* @param includeRanking
|
66
|
* the include ranking
|
67
|
* @param response
|
68
|
* the response
|
69
|
*/
|
70
|
public SolrResponseParser(final Function<String, String> highlightUtils, final BiMap<String, String> aliases, final boolean returnEmptyFields,
|
71
|
final boolean includeRanking, final QueryResponse response) {
|
72
|
super(highlightUtils, aliases, returnEmptyFields, includeRanking);
|
73
|
this.queryRsp = response;
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* {@inheritDoc}
|
78
|
*
|
79
|
* @see QueryResponseParser#getNumFound()
|
80
|
*/
|
81
|
@Override
|
82
|
public long getNumFound() {
|
83
|
|
84
|
return this.queryRsp.getResults().getNumFound();
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* {@inheritDoc}
|
89
|
*
|
90
|
* @see QueryResponseParser#getQueryTime()
|
91
|
*/
|
92
|
@Override
|
93
|
public int getQueryTime() {
|
94
|
return queryRsp.getQTime();
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* {@inheritDoc}
|
99
|
*
|
100
|
* @see QueryResponseParser#getElapsedTime()
|
101
|
*/
|
102
|
@Override
|
103
|
public long getElapsedTime() {
|
104
|
return queryRsp.getElapsedTime();
|
105
|
}
|
106
|
|
107
|
/**
|
108
|
* {@inheritDoc}
|
109
|
*
|
110
|
* @see QueryResponseParser#getStatus()
|
111
|
*/
|
112
|
@Override
|
113
|
public String getStatus() {
|
114
|
return String.valueOf(queryRsp.getStatus());
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
* {@inheritDoc}
|
119
|
*
|
120
|
* @see QueryResponseParser#getCurrentSize()
|
121
|
*/
|
122
|
@Override
|
123
|
public int getCurrentSize() {
|
124
|
return queryRsp.getResults().size();
|
125
|
}
|
126
|
|
127
|
/**
|
128
|
* Gets the query response.
|
129
|
*
|
130
|
* @return the query response
|
131
|
*/
|
132
|
public QueryResponse getQueryResponse() {
|
133
|
return queryRsp;
|
134
|
}
|
135
|
|
136
|
/**
|
137
|
* {@inheritDoc}
|
138
|
*
|
139
|
* @see QueryResponseParser#getResults()
|
140
|
*/
|
141
|
@Override
|
142
|
public List<String> getResults() {
|
143
|
return asRankedList(queryRsp.getResults());
|
144
|
}
|
145
|
|
146
|
/**
|
147
|
* {@inheritDoc}
|
148
|
*
|
149
|
* @see QueryResponseParser#getNumberOfBrowsingResults()
|
150
|
*/
|
151
|
@Override
|
152
|
public Long getNumberOfBrowsingResults() {
|
153
|
List<FacetField> ffList = queryRsp.getFacetFields();
|
154
|
Long maxCount = 0L;
|
155
|
|
156
|
if (ffList != null) {
|
157
|
for (FacetField ff : ffList) {
|
158
|
if (ff != null) {
|
159
|
Long countFacets = countFacets(ff.getValues());
|
160
|
if (countFacets > maxCount) {
|
161
|
maxCount = countFacets;
|
162
|
}
|
163
|
}
|
164
|
}
|
165
|
}
|
166
|
return maxCount;
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* {@inheritDoc}
|
171
|
*
|
172
|
* @see QueryResponseParser#getBrowsingResults()
|
173
|
*/
|
174
|
@Override
|
175
|
public List<BrowsingRow> getBrowsingResults() {
|
176
|
List<BrowsingRow> bresList = Lists.newArrayList();
|
177
|
List<GroupResult> facets = Lists.newArrayList();
|
178
|
|
179
|
final List<FacetField> ffList = queryRsp.getFacetFields();
|
180
|
|
181
|
Long numberOfBrowsingResults = getNumberOfBrowsingResults();
|
182
|
for (int i = 0; (ffList != null) && (i < numberOfBrowsingResults); i++) {
|
183
|
for (FacetField ff : ffList) {
|
184
|
|
185
|
String name = null;
|
186
|
if (aliases != null) {
|
187
|
name = aliases.inverse().get(ff.getName());
|
188
|
}
|
189
|
|
190
|
// fix #1456
|
191
|
if (name == null) {
|
192
|
name = ff.getName();
|
193
|
}
|
194
|
|
195
|
final Count facet = getFacet(ff, i);
|
196
|
|
197
|
if ((facet != null) && (facet.getCount() > 0)) {
|
198
|
|
199
|
final String value = facet.getName();
|
200
|
final int count = (int) facet.getCount();
|
201
|
|
202
|
if (returnEmptyFields || !value.isEmpty()) {
|
203
|
facets.add(new GroupResult(name, value, count));
|
204
|
}
|
205
|
}
|
206
|
}
|
207
|
|
208
|
if (facets.size() > 0) {
|
209
|
bresList.add(new BrowsingRow(Lists.newArrayList(facets)));
|
210
|
facets.clear();
|
211
|
}
|
212
|
}
|
213
|
if (log.isDebugEnabled()) {
|
214
|
log.debug("BrowsingResult size: " + bresList.size());
|
215
|
}
|
216
|
return bresList;
|
217
|
}
|
218
|
|
219
|
// /////////////// helpers
|
220
|
|
221
|
/**
|
222
|
* Gets the facet.
|
223
|
*
|
224
|
* @param ff
|
225
|
* the ff
|
226
|
* @param pos
|
227
|
* the pos
|
228
|
* @return the facet
|
229
|
*/
|
230
|
private Count getFacet(final FacetField ff, final int pos) {
|
231
|
|
232
|
if ((ff.getValues() == null) || (pos >= ff.getValues().size())) return null;
|
233
|
return ff.getValues().get(pos);
|
234
|
}
|
235
|
|
236
|
/**
|
237
|
* Given SolrDocumentList, return a List of Strings, representing it.
|
238
|
*
|
239
|
* @param documentList the document list
|
240
|
* @return the list< string>
|
241
|
*/
|
242
|
private List<String> asRankedList(final SolrDocumentList documentList) {
|
243
|
Function<SolrDocument, String> wrapper = includeRanking ? wrapperRank : wrapperNoRank;
|
244
|
|
245
|
if (queryRsp.getHighlighting() != null) return documentList
|
246
|
.stream()
|
247
|
.map(doc -> {
|
248
|
String score = getSingleField(doc, IndexFieldUtility.SCORE_FIELD);
|
249
|
|
250
|
String hl = getHighlighting(getSingleField(doc, IndexFieldUtility.INDEX_RECORD_ID));
|
251
|
String res = hl != null ? hl : getSingleField(doc, IndexFieldUtility.RESULT);
|
252
|
|
253
|
return includeRanking ? addRanking(res, score) : wrap(res);
|
254
|
}).map(highlightUtils)
|
255
|
.collect(Collectors.toList());
|
256
|
return documentList.stream().map(wrapper).collect(Collectors.toList());
|
257
|
}
|
258
|
|
259
|
/**
|
260
|
* Converts a String document to
|
261
|
*
|
262
|
* <record rank="score"> [document] </record>.
|
263
|
*
|
264
|
* @param doc
|
265
|
* the doc
|
266
|
* @param score
|
267
|
* the score
|
268
|
* @return the string
|
269
|
*/
|
270
|
private String addRanking(final String doc, final String score) {
|
271
|
return new String("<record rank=\"" + score + "\">" + doc + "</record>");
|
272
|
}
|
273
|
|
274
|
/**
|
275
|
* Wraps the given document as <record> [document] </record>.
|
276
|
*
|
277
|
* @param doc
|
278
|
* the doc
|
279
|
* @return the string
|
280
|
*/
|
281
|
private String wrap(final String doc) {
|
282
|
return new String("<record>" + doc + "</record>");
|
283
|
}
|
284
|
|
285
|
/**
|
286
|
* Gets the single field.
|
287
|
*
|
288
|
* @param doc
|
289
|
* the doc
|
290
|
* @param fieldName
|
291
|
* the field name
|
292
|
* @return the single field
|
293
|
*/
|
294
|
@SuppressWarnings("unchecked")
|
295
|
protected String getSingleField(final SolrDocument doc, final String fieldName) {
|
296
|
Object value = doc.getFieldValue(fieldName);
|
297
|
if (value instanceof Collection) return Iterables.getOnlyElement((Iterable<String>) value);
|
298
|
return String.valueOf(value);
|
299
|
}
|
300
|
|
301
|
private byte[] base64Decode(final String s) {
|
302
|
return org.apache.solr.common.util.Base64.base64ToByteArray(s);
|
303
|
}
|
304
|
|
305
|
private String unzip(final byte[] b) {
|
306
|
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(b)) {
|
307
|
try (GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream)) {
|
308
|
return new String(IOUtils.toByteArray(gzipInputStream));
|
309
|
}
|
310
|
} catch(IOException e) {
|
311
|
throw new RuntimeException("Failed to unzip content", e);
|
312
|
}
|
313
|
}
|
314
|
|
315
|
/**
|
316
|
* Gets the highlighting.
|
317
|
*
|
318
|
* @param docId
|
319
|
* the doc id
|
320
|
* @return the highlighting
|
321
|
*/
|
322
|
private String getHighlighting(final String docId) {
|
323
|
final Map<String, List<String>> highlight = queryRsp.getHighlighting().get(docId);
|
324
|
|
325
|
String result = new String();
|
326
|
if ((highlight != null) && (highlight.get(IndexFieldUtility.RESULT) != null)) {
|
327
|
for (String s : highlight.get(IndexFieldUtility.RESULT)) {
|
328
|
result = result.concat(s);
|
329
|
}
|
330
|
return result;
|
331
|
}
|
332
|
return null;
|
333
|
}
|
334
|
|
335
|
/**
|
336
|
* helper method.
|
337
|
*
|
338
|
* @param facets
|
339
|
* the list of facets to analyze
|
340
|
* @return the number of non-empty facets in the list whose count is greater than zero
|
341
|
*/
|
342
|
private Long countFacets(final List<Count> facets) {
|
343
|
|
344
|
if (facets == null) return 0L;
|
345
|
|
346
|
return (long) Iterables.size(Iterables.filter(facets, new Predicate<Count>() {
|
347
|
|
348
|
@Override
|
349
|
public boolean apply(final Count c) {
|
350
|
return (c != null) && (c.getName() != null) && !c.getName().isEmpty() && (c.getCount() > 0);
|
351
|
}
|
352
|
}));
|
353
|
}
|
354
|
|
355
|
@Override
|
356
|
public long getStart() {
|
357
|
// TODO Auto-generated method stub
|
358
|
return queryRsp.getResults().getStart();
|
359
|
}
|
360
|
|
361
|
}
|