Example usage for org.apache.solr.update.processor UpdateRequestProcessorChain createProcessor

List of usage examples for org.apache.solr.update.processor UpdateRequestProcessorChain createProcessor

Introduction

In this page you can find the example usage for org.apache.solr.update.processor UpdateRequestProcessorChain createProcessor.

Prototype

public UpdateRequestProcessor createProcessor(SolrQueryRequest req, SolrQueryResponse rsp) 

Source Link

Document

Uses the factories in this chain to creates a new UpdateRequestProcessor instance specific for this request.

Usage

From source file:com.sindicetech.siren.solr.UpdateProcessorTestBase.java

License:Open Source License

/**
 * Runs a document through the specified chain, and returns the final
 * document used when the chain is completed (NOTE: some chains may
 * modify the document in place/*  ww  w .  j ava2 s. com*/
 */
protected SolrInputDocument processAdd(final String chain, final SolrParams requestParams,
        final SolrInputDocument docIn) throws IOException {

    SolrCore core = h.getCore();
    UpdateRequestProcessorChain pc = core.getUpdateProcessingChain(chain);
    assertNotNull("No Chain named: " + chain, pc);

    SolrQueryResponse rsp = new SolrQueryResponse();

    SolrQueryRequest req = new LocalSolrQueryRequest(core, requestParams);
    try {
        AddUpdateCommand cmd = new AddUpdateCommand(req);
        cmd.solrDoc = docIn;

        UpdateRequestProcessor processor = pc.createProcessor(req, rsp);
        processor.processAdd(cmd);

        return cmd.solrDoc;
    } finally {
        req.close();
    }
}

From source file:de.qaware.chronix.solr.ingestion.AbstractIngestionHandler.java

License:Apache License

@Override
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    formatResponseAsJson(req);/*from w  w  w .j  a  v a2s  . c  o m*/

    if (req.getContentStreams() == null) {
        LOGGER.warn("no content stream");
        rsp.add("error", "No content stream");
        return;
    }

    InputStream stream = req.getContentStreams().iterator().next().getStream();

    MetricTimeSeriesConverter converter = new MetricTimeSeriesConverter();

    UpdateRequestProcessorChain processorChain = req.getCore().getUpdateProcessorChain(req.getParams());
    UpdateRequestProcessor processor = processorChain.createProcessor(req, rsp);
    try {
        for (MetricTimeSeries series : formatParser.parse(stream)) {
            SolrInputDocument document = new SolrInputDocument();
            converter.to(series).getFields().forEach(document::addField);
            storeDocument(document, processor, req);
        }

        LOGGER.debug("Committing transaction...");
        processor.processCommit(new CommitUpdateCommand(req, false));
        LOGGER.debug("Committed transaction");
    } finally {
        processor.finish();
    }
}

From source file:de.qaware.chronix.solr.retention.ChronixRetentionHandler.java

License:Apache License

/**
 * Gets the processor build from the processor update chain(UpdateParams.UPDATE_CHAIN)
 *
 * @param req - the solr query request information
 * @param rsp - the solr query response information
 * @return the update request processor//from  w ww .java2 s  .c  o  m
 */
private UpdateRequestProcessor getProcessor(SolrQueryRequest req, SolrQueryResponse rsp) {
    UpdateRequestProcessorChain processorChain = req.getCore()
            .getUpdateProcessingChain(req.getParams().get(UpdateParams.UPDATE_CHAIN));
    return processorChain.createProcessor(req, rsp);
}

From source file:lux.solr.SolrDocWriter.java

License:Mozilla Public License

private void writeToCloud(SolrInputDocument solrDoc, String uri) {
    ArrayList<String> urls = xqueryComponent.getShardURLs(true);
    LoggerFactory.getLogger(getClass()).debug("writing " + uri + " to cloud at " + urls);
    SolrQueryResponse rsp = new SolrQueryResponse();
    SolrQueryRequest req = UpdateDocCommand.makeSolrRequest(core);
    ((ModifiableSolrParams) req.getParams()).add(ShardParams.SHARDS, urls.toArray(new String[urls.size()]));
    UpdateRequest updateReq = new UpdateRequest();
    updateReq.add(solrDoc);/*from   w  w  w . j  a v  a  2s . c  o m*/
    UpdateDocCommand cmd = new UpdateDocCommand(req, solrDoc, null, uri);
    UpdateRequestProcessorChain updateChain = xqueryComponent.getCore()
            .getUpdateProcessingChain("lux-update-chain");
    try {
        UpdateRequestProcessor processor = updateChain.createProcessor(req, rsp);
        processor.processAdd(cmd);
        processor.finish();
    } catch (IOException e) {
        throw new LuxException(e);
    }
}

From source file:lux.solr.SolrDocWriter.java

License:Mozilla Public License

private void deleteCloud(DeleteUpdateCommand cmd) throws IOException {
    UpdateRequestProcessorChain updateChain = xqueryComponent.getCore()
            .getUpdateProcessingChain("lux-update-chain");
    SolrQueryResponse rsp = new SolrQueryResponse();
    SolrQueryRequest req = UpdateDocCommand.makeSolrRequest(core);
    UpdateRequestProcessor processor = updateChain.createProcessor(req, rsp);
    processor.processDelete(cmd);/* w  ww .  j a v a 2  s . c om*/
    processor.finish();
}

From source file:lux.solr.XQueryComponent.java

License:Mozilla Public License

protected void doCommit() {
    boolean isCloud = shards != null && shards.length > 1;
    SolrQueryRequest req = new SolrQueryRequestBase(core, new ModifiableSolrParams()) {
    };/*from  w  ww  . jav a  2s  . co  m*/
    CommitUpdateCommand cmd = new CommitUpdateCommand(req, false);
    cmd.softCommit = true;
    // cmd.expungeDeletes = false;
    // cmd.waitFlush = true;
    // cmd.waitSearcher = true;
    LoggerFactory.getLogger(getClass()).debug("commit {}", shards);
    try {
        if (isCloud) {
            SolrQueryResponse rsp = new SolrQueryResponse();
            // ((ModifiableSolrParams)req.getParams()).add(ShardParams.SHARDS, getShardURLs(false));
            UpdateRequestProcessorChain updateChain = core.getUpdateProcessingChain("lux-update-chain");
            updateChain.createProcessor(req, rsp).processCommit(cmd);
        } else {
            // commit locally
            core.getUpdateHandler().commit(cmd);
        }
    } catch (IOException e) {
        throw new LuxException(e);
    }
}

From source file:org.codeexample.jeffery.solr.ThreadedUpdateRequestHandler.java

License:Apache License

private ExecutorService importStreamsMultiThreaded(final SolrQueryRequest req, final SolrQueryResponse rsp,
        List<ContentStream> streams) throws InterruptedException, IOException {
    ExecutorService executor = null;
    SolrParams params = req.getParams();

    final UpdateRequestProcessorChain processorChain = req.getCore()
            .getUpdateProcessingChain(params.get(UpdateParams.UPDATE_CHAIN));

    UpdateRequestProcessor processor = processorChain.createProcessor(req, rsp);
    try {//from   www.  java 2 s.  com
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();

        resultMap.put("start_time", new Date());
        List<Map<String, Object>> details = new ArrayList<Map<String, Object>>();

        try {

            int threads = determineThreadsNumber(params, streams.size());
            ThreadFactory threadFactory = new ThreadFactory() {
                public Thread newThread(Runnable r) {
                    return new Thread(r, "threadedReqeustHandler-" + new Date());
                }
            };
            executor = Executors.newFixedThreadPool(threads, threadFactory);
            String contentType = params.get(CommonParams.STREAM_CONTENTTYPE);

            Iterator<ContentStream> iterator = streams.iterator();
            while (iterator.hasNext()) {
                ContentStream stream = iterator.next();
                iterator.remove();
                if (stream instanceof ContentStreamBase) {
                    ((ContentStreamBase) stream).setContentType(contentType);

                }
                submitTask(req, rsp, processorChain, executor, stream, details);
            }

            executor.shutdown();

            boolean terminated = executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
            if (!terminated) {
                throw new RuntimeException("Request takes too much time");
            }
            // Perhaps commit from the parameters
            RequestHandlerUtils.handleCommit(req, processor, params, false);
            RequestHandlerUtils.handleRollback(req, processor, params, false);
        } finally {
            resultMap.put("end_time", new Date());

            // check whether there is error in details
            for (Map<String, Object> map : details) {
                Exception ex = (Exception) map.get("exception");
                if (ex != null) {
                    rsp.setException(ex);
                    if (ex instanceof SolrException) {
                        rsp.add("status", ((SolrException) ex).code());
                    } else {
                        rsp.add("status", SolrException.ErrorCode.BAD_REQUEST);
                    }
                    break;
                }
            }
        }
        resultMap.put("details", details);
        rsp.add("result", resultMap);
        return executor;
    } finally {
        if (executor != null && !executor.isShutdown()) {
            executor.shutdownNow();
        }
        // finish the request
        processor.finish();
    }
}

From source file:org.codeexample.jeffery.solr.ThreadedUpdateRequestHandler.java

License:Apache License

private void submitTask(final SolrQueryRequest req, final SolrQueryResponse rsp,
        final UpdateRequestProcessorChain processorChain, ExecutorService executor, final ContentStream stream,
        final List<Map<String, Object>> rspResult) {
    Thread thread = new Thread() {
        public void run() {
            Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("start_time", new Date().toString());

            if (stream instanceof ContentStreamBase.FileStream) {
                map.put("Import File: ", ((ContentStreamBase.FileStream) stream).getName());
            }//w w w  .java  2 s .  com
            try {
                UpdateRequestProcessor processor = null;
                try {
                    processor = processorChain.createProcessor(req, rsp);

                    ContentStreamLoader documentLoader = newLoader(req, processor);

                    documentLoader.load(req, rsp, stream, processor);
                    System.err.println(rsp);

                } finally {
                    if (processor != null) {
                        // finish the request
                        processor.finish();
                    }
                }
            } catch (Exception e) {
                rsp.setException(e);
            } finally {
                map.put("end_time", new Date().toString());
                if (rsp.getException() != null) {
                    map.put("exception", rsp.getException());
                }
                rspResult.add(map);
            }

        };
    };

    executor.execute(thread);
}