Search in sources :

Example 46 with AddUpdateCommand

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

the class SolrWriter method upload.

@Override
public boolean upload(SolrInputDocument d) {
    try {
        AddUpdateCommand command = new AddUpdateCommand(req);
        command.solrDoc = d;
        command.commitWithin = commitWithin;
        processor.processAdd(command);
    } catch (Exception e) {
        log.warn("Error creating document : " + d, e);
        return false;
    }
    return true;
}
Also used : AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) SolrException(org.apache.solr.common.SolrException)

Example 47 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 48 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 49 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)

Example 50 with AddUpdateCommand

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

the class BinaryUpdateRequestHandlerTest method testRequestParams.

@Test
public void testRequestParams() throws Exception {
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", "1");
    UpdateRequest ureq = new UpdateRequest();
    ureq.add(doc);
    ureq.setParam(UpdateParams.COMMIT_WITHIN, "100");
    ureq.setParam(UpdateParams.OVERWRITE, Boolean.toString(false));
    BinaryRequestWriter brw = new BinaryRequestWriter();
    BufferingRequestProcessor p = new BufferingRequestProcessor(null);
    SolrQueryResponse rsp = new SolrQueryResponse();
    UpdateRequestHandler handler = new UpdateRequestHandler();
    handler.init(new NamedList());
    SolrQueryRequest req = req();
    ContentStreamLoader csl = handler.newLoader(req, p);
    csl.load(req, rsp, brw.getContentStream(ureq), p);
    AddUpdateCommand add = p.addCommands.get(0);
    System.out.println(add.solrDoc);
    assertEquals(false, add.overwrite);
    assertEquals(100, add.commitWithin);
    req.close();
}
Also used : ContentStreamLoader(org.apache.solr.handler.loader.ContentStreamLoader) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) BufferingRequestProcessor(org.apache.solr.update.processor.BufferingRequestProcessor) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) NamedList(org.apache.solr.common.util.NamedList) BinaryRequestWriter(org.apache.solr.client.solrj.impl.BinaryRequestWriter) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) 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