Search in sources :

Example 66 with AddUpdateCommand

use of org.apache.solr.update.AddUpdateCommand 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 67 with AddUpdateCommand

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

the class XMLLoader method processUpdate.

/**
   * @since solr 1.2
   */
void processUpdate(SolrQueryRequest req, UpdateRequestProcessor processor, XMLStreamReader parser) throws XMLStreamException, IOException, FactoryConfigurationError {
    AddUpdateCommand addCmd = null;
    SolrParams params = req.getParams();
    while (true) {
        int event = parser.next();
        switch(event) {
            case XMLStreamConstants.END_DOCUMENT:
                parser.close();
                return;
            case XMLStreamConstants.START_ELEMENT:
                String currTag = parser.getLocalName();
                if (currTag.equals(UpdateRequestHandler.ADD)) {
                    log.trace("SolrCore.update(add)");
                    addCmd = new AddUpdateCommand(req);
                    // First look for commitWithin parameter on the request, will be overwritten for individual <add>'s
                    addCmd.commitWithin = params.getInt(UpdateParams.COMMIT_WITHIN, -1);
                    addCmd.overwrite = params.getBool(UpdateParams.OVERWRITE, true);
                    for (int i = 0; i < parser.getAttributeCount(); i++) {
                        String attrName = parser.getAttributeLocalName(i);
                        String attrVal = parser.getAttributeValue(i);
                        if (UpdateRequestHandler.OVERWRITE.equals(attrName)) {
                            addCmd.overwrite = StrUtils.parseBoolean(attrVal);
                        } else if (UpdateRequestHandler.COMMIT_WITHIN.equals(attrName)) {
                            addCmd.commitWithin = Integer.parseInt(attrVal);
                        } else {
                            log.warn("XML element <add> has invalid XML attr: " + attrName);
                        }
                    }
                } else if ("doc".equals(currTag)) {
                    if (addCmd != null) {
                        log.trace("adding doc...");
                        addCmd.clear();
                        addCmd.solrDoc = readDoc(parser);
                        processor.processAdd(addCmd);
                    } else {
                        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unexpected <doc> tag without an <add> tag surrounding it.");
                    }
                } else if (UpdateRequestHandler.COMMIT.equals(currTag) || UpdateRequestHandler.OPTIMIZE.equals(currTag)) {
                    log.trace("parsing " + currTag);
                    CommitUpdateCommand cmd = new CommitUpdateCommand(req, UpdateRequestHandler.OPTIMIZE.equals(currTag));
                    ModifiableSolrParams mp = new ModifiableSolrParams();
                    for (int i = 0; i < parser.getAttributeCount(); i++) {
                        String attrName = parser.getAttributeLocalName(i);
                        String attrVal = parser.getAttributeValue(i);
                        mp.set(attrName, attrVal);
                    }
                    RequestHandlerUtils.validateCommitParams(mp);
                    // default to the normal request params for commit options
                    SolrParams p = SolrParams.wrapDefaults(mp, req.getParams());
                    RequestHandlerUtils.updateCommit(cmd, p);
                    processor.processCommit(cmd);
                } else // end commit
                if (UpdateRequestHandler.ROLLBACK.equals(currTag)) {
                    log.trace("parsing rollback");
                    RollbackUpdateCommand cmd = new RollbackUpdateCommand(req);
                    processor.processRollback(cmd);
                } else // end rollback
                if (UpdateRequestHandler.DELETE.equals(currTag)) {
                    log.trace("parsing delete");
                    processDelete(req, processor, parser);
                }
                // end delete
                break;
        }
    }
}
Also used : RollbackUpdateCommand(org.apache.solr.update.RollbackUpdateCommand) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) CommitUpdateCommand(org.apache.solr.update.CommitUpdateCommand) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) SolrException(org.apache.solr.common.SolrException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 68 with AddUpdateCommand

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

the class ExtractingRequestHandlerTest method testCommitWithin.

@Test
public void testCommitWithin() throws Exception {
    ExtractingRequestHandler handler = (ExtractingRequestHandler) h.getCore().getRequestHandler("/update/extract");
    assertTrue("handler is null and it shouldn't be", handler != null);
    SolrQueryRequest req = req("literal.id", "one", ExtractingParams.RESOURCE_NAME, "extraction/version_control.txt", "commitWithin", "200");
    SolrQueryResponse rsp = new SolrQueryResponse();
    BufferingRequestProcessor p = new BufferingRequestProcessor(null);
    ExtractingDocumentLoader loader = (ExtractingDocumentLoader) handler.newLoader(req, p);
    loader.load(req, rsp, new ContentStreamBase.FileStream(getFile("extraction/version_control.txt")), p);
    AddUpdateCommand add = p.addCommands.get(0);
    assertEquals(200, add.commitWithin);
    req.close();
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) BufferingRequestProcessor(org.apache.solr.update.processor.BufferingRequestProcessor) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase) Test(org.junit.Test)

Aggregations

AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)68 SolrInputDocument (org.apache.solr.common.SolrInputDocument)41 Test (org.junit.Test)34 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)31 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)26 BufferingRequestProcessor (org.apache.solr.update.processor.BufferingRequestProcessor)19 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)17 SolrInputField (org.apache.solr.common.SolrInputField)14 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)12 JsonLoader (org.apache.solr.handler.loader.JsonLoader)11 ArrayList (java.util.ArrayList)10 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)10 SkipExistingDocumentsUpdateProcessor (org.apache.solr.update.processor.SkipExistingDocumentsProcessorFactory.SkipExistingDocumentsUpdateProcessor)8 SolrException (org.apache.solr.common.SolrException)7 DeleteUpdateCommand (org.apache.solr.update.DeleteUpdateCommand)6 CommitUpdateCommand (org.apache.solr.update.CommitUpdateCommand)5 SolrCore (org.apache.solr.core.SolrCore)4 SolrRequestInfo (org.apache.solr.request.SolrRequestInfo)4 UpdateRequestProcessor (org.apache.solr.update.processor.UpdateRequestProcessor)4 UpdateRequestProcessorChain (org.apache.solr.update.processor.UpdateRequestProcessorChain)4