use of org.apache.solr.client.solrj.StreamingResponseCallback in project Xponents by OpenSextant.
the class SolrMatcherSupport method tagTextCallSolrTagger.
/**
* Solr call: tag input buffer, returning all candiate reference data that
* matched during tagging.
*
* @param buffer text to tag
* @param docid id for text, only for tracking purposes
* @param refDataMap
* - a map of reference data in solr, It will store caller's
* domain objects. e.g., rec.id => domain(rec)
* @return solr response
* @throws ExtractionException tagger error
*/
protected QueryResponse tagTextCallSolrTagger(String buffer, String docid, final Map<Integer, Object> refDataMap) throws ExtractionException {
SolrTaggerRequest tagRequest = new SolrTaggerRequest(getMatcherParameters(), buffer);
tagRequest.setPath(requestHandler);
// Stream the response to avoid serialization and to save memory by
// only keeping one SolrDocument materialized at a time
tagRequest.setStreamingResponseCallback(new StreamingResponseCallback() {
@Override
public void streamDocListInfo(long numFound, long start, Float maxScore) {
}
// Future optimization: it would be nice if Solr could give us the
// doc id without giving us a SolrDocument, allowing us to
// conditionally get it. It would save disk IO & speed, at the
// expense of putting ids into memory.
@Override
public void streamSolrDocument(final SolrDocument solrDoc) {
Integer id = (Integer) solrDoc.getFirstValue("id");
// create a domain object for the given tag;
// this callback handler caches such domain obj in simple k/v
// map.
Object domainObj = createTag(solrDoc);
if (domainObj != null) {
refDataMap.put(id, domainObj);
}
}
});
QueryResponse response;
try {
response = tagRequest.process(solr.getInternalSolrServer());
} catch (Exception err) {
throw new ExtractionException("Failed to tag document=" + docid, err);
}
// see https://issues.apache.org/jira/browse/SOLR-5154
SolrDocumentList docList = response.getResults();
if (docList != null) {
// log.debug("Not streaming docs from Solr (not supported)");
StreamingResponseCallback callback = tagRequest.getStreamingResponseCallback();
callback.streamDocListInfo(docList.getNumFound(), docList.getStart(), docList.getMaxScore());
for (SolrDocument solrDoc : docList) {
/**
* This appears to be an empty list; what is this explicit
* callback loop for?
*/
callback.streamSolrDocument(solrDoc);
}
}
return response;
}
use of org.apache.solr.client.solrj.StreamingResponseCallback in project lucene-solr by apache.
the class EmbeddedSolrServer method request.
// TODO-- this implementation sends the response to XML and then parses it.
// It *should* be able to convert the response directly into a named list.
@Override
public NamedList<Object> request(SolrRequest request, String coreName) throws SolrServerException, IOException {
String path = request.getPath();
if (path == null || !path.startsWith("/")) {
path = "/select";
}
SolrRequestHandler handler = coreContainer.getRequestHandler(path);
if (handler != null) {
try {
SolrQueryRequest req = _parser.buildRequestFrom(null, request.getParams(), request.getContentStreams());
SolrQueryResponse resp = new SolrQueryResponse();
handler.handleRequest(req, resp);
checkForExceptions(resp);
return BinaryResponseWriter.getParsedResponse(req, resp);
} catch (IOException | SolrException iox) {
throw iox;
} catch (Exception ex) {
throw new SolrServerException(ex);
}
}
if (coreName == null)
coreName = this.coreName;
// Check for cores action
SolrQueryRequest req = null;
try (SolrCore core = coreContainer.getCore(coreName)) {
if (core == null) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "No such core: " + coreName);
}
SolrParams params = request.getParams();
if (params == null) {
params = new ModifiableSolrParams();
}
// Extract the handler from the path or params
handler = core.getRequestHandler(path);
if (handler == null) {
if ("/select".equals(path) || "/select/".equalsIgnoreCase(path)) {
String qt = params.get(CommonParams.QT);
handler = core.getRequestHandler(qt);
if (handler == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unknown handler: " + qt);
}
}
}
if (handler == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unknown handler: " + path);
}
req = _parser.buildRequestFrom(core, params, request.getContentStreams());
req.getContext().put(PATH, path);
req.getContext().put("httpMethod", request.getMethod().name());
SolrQueryResponse rsp = new SolrQueryResponse();
SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
core.execute(handler, req, rsp);
checkForExceptions(rsp);
// Check if this should stream results
if (request.getStreamingResponseCallback() != null) {
try {
final StreamingResponseCallback callback = request.getStreamingResponseCallback();
BinaryResponseWriter.Resolver resolver = new BinaryResponseWriter.Resolver(req, rsp.getReturnFields()) {
@Override
public void writeResults(ResultContext ctx, JavaBinCodec codec) throws IOException {
// write an empty list...
SolrDocumentList docs = new SolrDocumentList();
docs.setNumFound(ctx.getDocList().matches());
docs.setStart(ctx.getDocList().offset());
docs.setMaxScore(ctx.getDocList().maxScore());
codec.writeSolrDocumentList(docs);
// This will transform
writeResultsBody(ctx, codec);
}
};
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
createJavaBinCodec(callback, resolver).setWritableDocFields(resolver).marshal(rsp.getValues(), out);
try (InputStream in = out.toInputStream()) {
return (NamedList<Object>) new JavaBinCodec(resolver).unmarshal(in);
}
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
// Now write it out
NamedList<Object> normalized = BinaryResponseWriter.getParsedResponse(req, rsp);
return normalized;
} catch (IOException | SolrException iox) {
throw iox;
} catch (Exception ex) {
throw new SolrServerException(ex);
} finally {
if (req != null)
req.close();
SolrRequestInfo.clearRequestInfo();
}
}
Aggregations