Search in sources :

Example 6 with UpdateRequestProcessor

use of org.apache.solr.update.processor.UpdateRequestProcessor in project lucene-solr by apache.

the class DefaultValueUpdateProcessorTest method processAdd.

/**
   * 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
   */
SolrInputDocument processAdd(final String chain, 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, new ModifiableSolrParams());
    try {
        SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
        AddUpdateCommand cmd = new AddUpdateCommand(req);
        cmd.solrDoc = docIn;
        UpdateRequestProcessor processor = pc.createProcessor(req, rsp);
        processor.processAdd(cmd);
        return cmd.solrDoc;
    } finally {
        SolrRequestInfo.clearRequestInfo();
        req.close();
    }
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrCore(org.apache.solr.core.SolrCore) UpdateRequestProcessorChain(org.apache.solr.update.processor.UpdateRequestProcessorChain) UpdateRequestProcessor(org.apache.solr.update.processor.UpdateRequestProcessor) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 7 with UpdateRequestProcessor

use of org.apache.solr.update.processor.UpdateRequestProcessor in project lucene-solr by apache.

the class ContentStreamHandlerBase method handleRequestBody.

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    SolrParams params = req.getParams();
    UpdateRequestProcessorChain processorChain = req.getCore().getUpdateProcessorChain(params);
    UpdateRequestProcessor processor = processorChain.createProcessor(req, rsp);
    try {
        ContentStreamLoader documentLoader = newLoader(req, processor);
        Iterable<ContentStream> streams = req.getContentStreams();
        if (streams == null) {
            if (!RequestHandlerUtils.handleCommit(req, processor, params, false) && !RequestHandlerUtils.handleRollback(req, processor, params, false)) {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "missing content stream");
            }
        } else {
            for (ContentStream stream : streams) {
                documentLoader.load(req, rsp, stream, processor);
            }
            // Perhaps commit from the parameters
            RequestHandlerUtils.handleCommit(req, processor, params, false);
            RequestHandlerUtils.handleRollback(req, processor, params, false);
        }
    } finally {
        // finish the request
        try {
            processor.finish();
        } finally {
            processor.close();
        }
    }
}
Also used : ContentStreamLoader(org.apache.solr.handler.loader.ContentStreamLoader) ContentStream(org.apache.solr.common.util.ContentStream) SolrParams(org.apache.solr.common.params.SolrParams) UpdateRequestProcessorChain(org.apache.solr.update.processor.UpdateRequestProcessorChain) UpdateRequestProcessor(org.apache.solr.update.processor.UpdateRequestProcessor) SolrException(org.apache.solr.common.SolrException)

Example 8 with UpdateRequestProcessor

use of org.apache.solr.update.processor.UpdateRequestProcessor in project lucene-solr by apache.

the class PeerSync method handleUpdates.

private boolean handleUpdates(ShardResponse srsp) {
    // we retrieved the last N updates from the replica
    List<Object> updates = (List<Object>) srsp.getSolrResponse().getResponse().get("updates");
    SyncShardRequest sreq = (SyncShardRequest) srsp.getShardRequest();
    if (updates.size() < sreq.totalRequestedUpdates) {
        log.error(msg() + " Requested " + sreq.totalRequestedUpdates + " updates from " + sreq.shards[0] + " but retrieved " + updates.size());
        return false;
    }
    // overwrite fingerprint we saved in 'handleVersions()'   
    Object fingerprint = srsp.getSolrResponse().getResponse().get("fingerprint");
    if (fingerprint != null) {
        sreq.fingerprint = IndexFingerprint.fromObject(fingerprint);
    }
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set(DISTRIB_UPDATE_PARAM, FROMLEADER.toString());
    // debugging
    params.set("peersync", true);
    SolrQueryRequest req = new LocalSolrQueryRequest(uhandler.core, params);
    SolrQueryResponse rsp = new SolrQueryResponse();
    UpdateRequestProcessorChain processorChain = req.getCore().getUpdateProcessingChain(null);
    UpdateRequestProcessor proc = processorChain.createProcessor(req, rsp);
    Collections.sort(updates, updateRecordComparator);
    Object o = null;
    long lastVersion = 0;
    try {
        // Apply oldest updates first
        for (Object obj : updates) {
            // should currently be a List<Oper,Ver,Doc/Id>
            o = obj;
            List<Object> entry = (List<Object>) o;
            if (debug) {
                log.debug(msg() + "raw update record " + o);
            }
            int oper = (Integer) entry.get(0) & UpdateLog.OPERATION_MASK;
            long version = (Long) entry.get(1);
            if (version == lastVersion && version != 0)
                continue;
            lastVersion = version;
            switch(oper) {
                case UpdateLog.ADD:
                    {
                        // byte[] idBytes = (byte[]) entry.get(2);
                        SolrInputDocument sdoc = (SolrInputDocument) entry.get(entry.size() - 1);
                        AddUpdateCommand cmd = new AddUpdateCommand(req);
                        // cmd.setIndexedId(new BytesRef(idBytes));
                        cmd.solrDoc = sdoc;
                        cmd.setVersion(version);
                        cmd.setFlags(UpdateCommand.PEER_SYNC | UpdateCommand.IGNORE_AUTOCOMMIT);
                        if (debug) {
                            log.debug(msg() + "add " + cmd + " id " + sdoc.getField(ID));
                        }
                        proc.processAdd(cmd);
                        break;
                    }
                case UpdateLog.DELETE:
                    {
                        byte[] idBytes = (byte[]) entry.get(2);
                        DeleteUpdateCommand cmd = new DeleteUpdateCommand(req);
                        cmd.setIndexedId(new BytesRef(idBytes));
                        cmd.setVersion(version);
                        cmd.setFlags(UpdateCommand.PEER_SYNC | UpdateCommand.IGNORE_AUTOCOMMIT);
                        if (debug) {
                            log.debug(msg() + "delete " + cmd + " " + new BytesRef(idBytes).utf8ToString());
                        }
                        proc.processDelete(cmd);
                        break;
                    }
                case UpdateLog.DELETE_BY_QUERY:
                    {
                        String query = (String) entry.get(2);
                        DeleteUpdateCommand cmd = new DeleteUpdateCommand(req);
                        cmd.query = query;
                        cmd.setVersion(version);
                        cmd.setFlags(UpdateCommand.PEER_SYNC | UpdateCommand.IGNORE_AUTOCOMMIT);
                        if (debug) {
                            log.debug(msg() + "deleteByQuery " + cmd);
                        }
                        proc.processDelete(cmd);
                        break;
                    }
                case UpdateLog.UPDATE_INPLACE:
                    {
                        AddUpdateCommand cmd = UpdateLog.convertTlogEntryToAddUpdateCommand(req, entry, oper, version);
                        cmd.setFlags(UpdateCommand.PEER_SYNC | UpdateCommand.IGNORE_AUTOCOMMIT);
                        if (debug) {
                            log.debug(msg() + "inplace update " + cmd + " prevVersion=" + cmd.prevVersion + ", doc=" + cmd.solrDoc);
                        }
                        proc.processAdd(cmd);
                        break;
                    }
                default:
                    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown Operation! " + oper);
            }
        }
    } catch (IOException e) {
        // TODO: should this be handled separately as a problem with us?
        // I guess it probably already will by causing replication to be kicked off.
        sreq.updateException = e;
        log.error(msg() + "Error applying updates from " + sreq.shards + " ,update=" + o, e);
        return false;
    } catch (Exception e) {
        sreq.updateException = e;
        log.error(msg() + "Error applying updates from " + sreq.shards + " ,update=" + o, e);
        return false;
    } finally {
        try {
            proc.finish();
        } catch (Exception e) {
            sreq.updateException = e;
            log.error(msg() + "Error applying updates from " + sreq.shards + " ,finish()", e);
            return false;
        } finally {
            IOUtils.closeQuietly(proc);
        }
    }
    return compareFingerprint(sreq);
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) UpdateRequestProcessorChain(org.apache.solr.update.processor.UpdateRequestProcessorChain) IOException(java.io.IOException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) NoHttpResponseException(org.apache.http.NoHttpResponseException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrException(org.apache.solr.common.SolrException) SocketException(java.net.SocketException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequestProcessor(org.apache.solr.update.processor.UpdateRequestProcessor) ArrayList(java.util.ArrayList) List(java.util.List) BytesRef(org.apache.lucene.util.BytesRef) SolrException(org.apache.solr.common.SolrException)

Example 9 with UpdateRequestProcessor

use of org.apache.solr.update.processor.UpdateRequestProcessor in project lucene-solr by apache.

the class BlobHandler method indexMap.

public static void indexMap(SolrQueryRequest req, SolrQueryResponse rsp, Map<String, Object> doc) throws IOException {
    SolrInputDocument solrDoc = new SolrInputDocument();
    for (Map.Entry<String, Object> e : doc.entrySet()) solrDoc.addField(e.getKey(), e.getValue());
    UpdateRequestProcessorChain processorChain = req.getCore().getUpdateProcessorChain(req.getParams());
    try (UpdateRequestProcessor processor = processorChain.createProcessor(req, rsp)) {
        AddUpdateCommand cmd = new AddUpdateCommand(req);
        cmd.solrDoc = solrDoc;
        log.info("Adding doc: " + doc);
        processor.processAdd(cmd);
        log.info("committing doc: " + doc);
        processor.processCommit(new CommitUpdateCommand(req, false));
        processor.finish();
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequestProcessorChain(org.apache.solr.update.processor.UpdateRequestProcessorChain) UpdateRequestProcessor(org.apache.solr.update.processor.UpdateRequestProcessor) CommitUpdateCommand(org.apache.solr.update.CommitUpdateCommand) Map(java.util.Map) Utils.makeMap(org.apache.solr.common.util.Utils.makeMap) Collections.singletonMap(java.util.Collections.singletonMap) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand)

Example 10 with UpdateRequestProcessor

use of org.apache.solr.update.processor.UpdateRequestProcessor in project lucene-solr by apache.

the class UIMAUpdateRequestProcessorTest method testMultiMap.

@Test
public void testMultiMap() {
    SolrCore core = h.getCore();
    UpdateRequestProcessorChain chained = core.getUpdateProcessingChain(UIMA_MULTI_MAP_CHAIN);
    assertNotNull(chained);
    UIMAUpdateRequestProcessorFactory factory = (UIMAUpdateRequestProcessorFactory) chained.getProcessors().get(0);
    assertNotNull(factory);
    UpdateRequestProcessor processor = factory.getInstance(req(), null, null);
    assertTrue(processor instanceof UIMAUpdateRequestProcessor);
    SolrUIMAConfiguration conf = ((UIMAUpdateRequestProcessor) processor).getConfiguration();
    Map<String, Map<String, MapField>> map = conf.getTypesFeaturesFieldsMapping();
    Map<String, MapField> subMap = map.get("a-type-which-can-have-multiple-features");
    assertEquals(2, subMap.size());
    assertEquals("1", subMap.get("A").getFieldName(null));
    assertEquals("2", subMap.get("B").getFieldName(null));
}
Also used : SolrCore(org.apache.solr.core.SolrCore) UpdateRequestProcessorChain(org.apache.solr.update.processor.UpdateRequestProcessorChain) UpdateRequestProcessor(org.apache.solr.update.processor.UpdateRequestProcessor) Map(java.util.Map) MapField(org.apache.solr.uima.processor.SolrUIMAConfiguration.MapField) Test(org.junit.Test)

Aggregations

UpdateRequestProcessor (org.apache.solr.update.processor.UpdateRequestProcessor)10 UpdateRequestProcessorChain (org.apache.solr.update.processor.UpdateRequestProcessorChain)10 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)5 SolrCore (org.apache.solr.core.SolrCore)4 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)4 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)4 Map (java.util.Map)3 SolrException (org.apache.solr.common.SolrException)3 SolrInputDocument (org.apache.solr.common.SolrInputDocument)3 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)3 SolrParams (org.apache.solr.common.params.SolrParams)3 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)3 ArrayList (java.util.ArrayList)2 ContentStream (org.apache.solr.common.util.ContentStream)2 SolrRequestInfo (org.apache.solr.request.SolrRequestInfo)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 SocketException (java.net.SocketException)1 Collections.singletonMap (java.util.Collections.singletonMap)1