Revision 49005
Added by Claudio Atzori about 6 years ago
modules/dnet-index-solr-client/trunk/src/main/java/eu/dnetlib/functionality/index/query/SolrResponseParser.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.functionality.index.query; |
2 | 2 |
|
3 |
import static eu.dnetlib.miscutils.collections.MappedCollection.listMap;
|
|
4 |
|
|
3 |
import java.io.ByteArrayInputStream;
|
|
4 |
import java.io.IOException; |
|
5 | 5 |
import java.util.Collection; |
6 | 6 |
import java.util.List; |
7 | 7 |
import java.util.Map; |
8 |
import java.util.zip.GZIPInputStream; |
|
8 | 9 |
|
10 |
import com.google.common.base.Predicate; |
|
11 |
import com.google.common.collect.BiMap; |
|
12 |
import com.google.common.collect.Iterables; |
|
13 |
import com.google.common.collect.Lists; |
|
14 |
import eu.dnetlib.data.provision.index.rmi.BrowsingRow; |
|
15 |
import eu.dnetlib.data.provision.index.rmi.GroupResult; |
|
16 |
import eu.dnetlib.functionality.index.utils.IndexFieldUtility; |
|
17 |
import eu.dnetlib.miscutils.functional.UnaryFunction; |
|
18 |
import org.apache.commons.io.IOUtils; |
|
9 | 19 |
import org.apache.commons.logging.Log; |
10 | 20 |
import org.apache.commons.logging.LogFactory; |
11 | 21 |
import org.apache.solr.client.solrj.response.FacetField; |
... | ... | |
14 | 24 |
import org.apache.solr.common.SolrDocument; |
15 | 25 |
import org.apache.solr.common.SolrDocumentList; |
16 | 26 |
|
17 |
import com.google.common.base.Predicate; |
|
18 |
import com.google.common.collect.BiMap; |
|
19 |
import com.google.common.collect.Iterables; |
|
20 |
import com.google.common.collect.Lists; |
|
27 |
import static eu.dnetlib.miscutils.collections.MappedCollection.listMap; |
|
21 | 28 |
|
22 |
import eu.dnetlib.data.provision.index.rmi.BrowsingRow; |
|
23 |
import eu.dnetlib.data.provision.index.rmi.GroupResult; |
|
24 |
import eu.dnetlib.functionality.index.utils.IndexFieldUtility; |
|
25 |
import eu.dnetlib.miscutils.functional.UnaryFunction; |
|
26 |
|
|
27 | 29 |
/** |
28 | 30 |
* The Class SolrResponseParser. |
29 | 31 |
*/ |
... | ... | |
40 | 42 |
private QueryResponse queryRsp = null; |
41 | 43 |
|
42 | 44 |
/** The wrapper rank. */ |
43 |
protected final UnaryFunction<String, SolrDocument> wrapperRank = new UnaryFunction<String, SolrDocument>() { |
|
45 |
protected final UnaryFunction<String, SolrDocument> wrapperRank = |
|
46 |
doc -> addRanking( |
|
47 |
unzip(base64Decode(getSingleField(doc, IndexFieldUtility.RESULT))), |
|
48 |
getSingleField(doc, IndexFieldUtility.SCORE_FIELD)); |
|
44 | 49 |
|
45 |
@Override |
|
46 |
public String evaluate(final SolrDocument doc) { |
|
47 |
return addRanking(getSingleField(doc, IndexFieldUtility.RESULT), getSingleField(doc, IndexFieldUtility.SCORE_FIELD)); |
|
48 |
} |
|
49 |
}; |
|
50 |
|
|
51 | 50 |
/** The wrapper no rank. */ |
52 |
protected final UnaryFunction<String, SolrDocument> wrapperNoRank = new UnaryFunction<String, SolrDocument>() { |
|
51 |
protected final UnaryFunction<String, SolrDocument> wrapperNoRank = |
|
52 |
doc -> wrap(unzip(base64Decode(getSingleField(doc, IndexFieldUtility.RESULT)))); |
|
53 | 53 |
|
54 |
@Override |
|
55 |
public String evaluate(final SolrDocument doc) { |
|
56 |
return wrap(getSingleField(doc, IndexFieldUtility.RESULT)); |
|
57 |
} |
|
58 |
}; |
|
59 |
|
|
60 | 54 |
/** |
61 | 55 |
* The Constructor. |
62 | 56 |
* |
... | ... | |
248 | 242 |
|
249 | 243 |
UnaryFunction<String, SolrDocument> wrapper = includeRanking ? wrapperRank : wrapperNoRank; |
250 | 244 |
|
251 |
if (queryRsp.getHighlighting() != null) return listMap(listMap(documentList, new UnaryFunction<String, SolrDocument>() {
|
|
245 |
if (queryRsp.getHighlighting() != null) return listMap(listMap(documentList, doc -> {
|
|
252 | 246 |
|
253 |
@Override |
|
254 |
public String evaluate(final SolrDocument doc) { |
|
247 |
String score = getSingleField(doc, IndexFieldUtility.SCORE_FIELD); |
|
255 | 248 |
|
256 |
String score = getSingleField(doc, IndexFieldUtility.SCORE_FIELD); |
|
249 |
String hl = getHighlighting(getSingleField(doc, IndexFieldUtility.INDEX_RECORD_ID)); |
|
250 |
String res = hl != null ? hl : getSingleField(doc, IndexFieldUtility.RESULT); |
|
257 | 251 |
|
258 |
String hl = getHighlighting(getSingleField(doc, IndexFieldUtility.INDEX_RECORD_ID)); |
|
259 |
String res = hl != null ? hl : getSingleField(doc, IndexFieldUtility.RESULT); |
|
260 |
|
|
261 |
return includeRanking ? addRanking(res, score) : wrap(res); |
|
262 |
} |
|
252 |
return includeRanking ? addRanking(res, score) : wrap(res); |
|
263 | 253 |
}), highlightUtils); |
264 | 254 |
|
265 | 255 |
return listMap(documentList, wrapper); |
... | ... | |
307 | 297 |
return String.valueOf(value); |
308 | 298 |
} |
309 | 299 |
|
300 |
private byte[] base64Decode(final String s) { |
|
301 |
return org.apache.solr.common.util.Base64.base64ToByteArray(s); |
|
302 |
} |
|
303 |
|
|
304 |
private String unzip(final byte[] b) { |
|
305 |
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(b)) { |
|
306 |
try (GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream)) { |
|
307 |
return new String(IOUtils.toByteArray(gzipInputStream)); |
|
308 |
} |
|
309 |
} catch(IOException e) { |
|
310 |
throw new RuntimeException("Failed to unzip content", e); |
|
311 |
} |
|
312 |
} |
|
313 |
|
|
310 | 314 |
/** |
311 | 315 |
* Gets the highlighting. |
312 | 316 |
* |
Also available in: Unified diff
base64 decode and gunzip field __result from the index responses