Search in sources :

Example 1 with StreamingResponseCallback

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;
}
Also used : StreamingResponseCallback(org.apache.solr.client.solrj.StreamingResponseCallback) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ConfigException(org.opensextant.ConfigException)

Example 2 with StreamingResponseCallback

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();
    }
}
Also used : ResultContext(org.apache.solr.response.ResultContext) SolrCore(org.apache.solr.core.SolrCore) BinaryResponseWriter(org.apache.solr.response.BinaryResponseWriter) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) StreamingResponseCallback(org.apache.solr.client.solrj.StreamingResponseCallback) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) SolrException(org.apache.solr.common.SolrException) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) InputStream(java.io.InputStream) NamedList(org.apache.solr.common.util.NamedList) IOException(java.io.IOException) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrRequestHandler(org.apache.solr.request.SolrRequestHandler)

Aggregations

StreamingResponseCallback (org.apache.solr.client.solrj.StreamingResponseCallback)2 SolrDocumentList (org.apache.solr.common.SolrDocumentList)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrException (org.apache.solr.common.SolrException)1 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)1 SolrParams (org.apache.solr.common.params.SolrParams)1 JavaBinCodec (org.apache.solr.common.util.JavaBinCodec)1 NamedList (org.apache.solr.common.util.NamedList)1 SolrCore (org.apache.solr.core.SolrCore)1 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)1 SolrRequestHandler (org.apache.solr.request.SolrRequestHandler)1 SolrRequestInfo (org.apache.solr.request.SolrRequestInfo)1 BinaryResponseWriter (org.apache.solr.response.BinaryResponseWriter)1 ResultContext (org.apache.solr.response.ResultContext)1 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)1