Example usage for org.apache.solr.update AddUpdateCommand AddUpdateCommand

List of usage examples for org.apache.solr.update AddUpdateCommand AddUpdateCommand

Introduction

In this page you can find the example usage for org.apache.solr.update AddUpdateCommand AddUpdateCommand.

Prototype

public AddUpdateCommand(SolrQueryRequest req) 

Source Link

Usage

From source file:com.lucid.solr.sidecar.SidecarIndexReaderFactoryTest.java

License:Apache License

private void populate() throws Exception {
    // populate both indexes
    SolrCore source = cc.getCore("source");
    try {//from ww  w  . jav a2s.  c o  m
        for (int i = 99; i >= 0; i--) {
            AddUpdateCommand cmd = new AddUpdateCommand(makeReq(source));
            SolrInputDocument doc = new SolrInputDocument();
            doc.addField("id", "id" + i);
            doc.addField("side1_t", "foo bar side1 " + i);
            doc.addField("side2_t", "blah blah side2 " + i);
            cmd.solrDoc = doc;
            source.getUpdateHandler().addDoc(cmd);
        }
        source.getUpdateHandler().commit(new CommitUpdateCommand(makeReq(source), false));
    } finally {
        source.close();
    }
    SolrCore target = cc.getCore("target");
    try {
        for (int i = 0; i < 101; i++) {
            AddUpdateCommand cmd = new AddUpdateCommand(makeReq(target));
            SolrInputDocument doc = new SolrInputDocument();
            doc.addField("id", "id" + i);
            doc.addField("text", "document " + i);
            cmd.solrDoc = doc;
            target.getUpdateHandler().addDoc(cmd);
            if (i == 99) {
                target.getUpdateHandler().commit(new CommitUpdateCommand(makeReq(target), false));
            }
        }
        target.getUpdateHandler().commit(new CommitUpdateCommand(makeReq(target), false));
    } finally {
        target.close();
    }
}

From source file:com.lucid.solr.sidecar.SidecarIndexReaderFactoryTest.java

License:Apache License

@Test
public void testChanges() throws Exception {
    populate();/*w  ww  .  ja  va  2s. c o m*/
    // add some docs, overwriting some of the existing ones
    SolrCore target = cc.getCore("target");
    try {
        for (int i = 50; i < 150; i++) {
            AddUpdateCommand cmd = new AddUpdateCommand(makeReq(target));
            cmd.overwrite = true;
            SolrInputDocument doc = new SolrInputDocument();
            doc.addField("id", "id" + i);
            doc.addField("text", "new document " + i);
            cmd.solrDoc = doc;
            target.getUpdateHandler().addDoc(cmd);
        }
        target.getUpdateHandler().commit(new CommitUpdateCommand(makeReq(target), false));
    } finally {
        target.close();
    }
    target = cc.getCore("target");
    SolrIndexSearcher searcher = target.getSearcher().get();
    Query q = new MatchAllDocsQuery();
    try {
        // verify the stored parts
        TopDocs td = searcher.search(q, 151);
        assertNotNull(td);
        for (ScoreDoc sd : td.scoreDocs) {
            Document doc = searcher.doc(sd.doc);
            System.err.println(doc);
        }
    } finally {
        searcher.close();
        target.close();
    }
}

From source file:com.sindicetech.siren.solr.handler.JsonLoader.java

License:Open Source License

@Override
public void load(final SolrQueryRequest req, final SolrQueryResponse rsp, final ContentStream stream,
        final UpdateRequestProcessor processor) throws Exception {
    Reader reader = null;//from   ww w  .  j a  v  a 2 s.c  o  m
    try {
        reader = stream.getReader();
        // keep a copy of the body for the source entry
        // TODO: instead of reading the stream to make a copy, try to create a copy of the json
        // while parsing it in the JsonReader
        String body = IOUtils.toString(reader);

        FieldMappersHandler mappersHandler = new FieldMappersHandler(fieldMappers, req.getCore());
        DocumentBuilder docBuilder = new DocumentBuilder();

        // Add the source field entry
        FieldEntry source = new FieldEntry(SOURCE_FIELDNAME, body);
        docBuilder.add(mappersHandler.map(source));

        // Add the id field initialised with a UUID. It will be overwritten if an id field exist in the JSON document.
        FieldEntry id = new FieldEntry(IdFieldMapper.INPUT_FIELD,
                UUID.randomUUID().toString().toLowerCase(Locale.ROOT));
        docBuilder.add(mappersHandler.map(id));

        JsonParser parser = mapper.getJsonFactory().createJsonParser(new StringReader(body));
        JsonReader jreader = new JsonReader(parser);

        FieldEntry entry;
        while ((entry = jreader.next()) != null) {
            docBuilder.add(mappersHandler.map(entry));
        }

        // the index schema might have changed
        req.updateSchemaToLatest();

        // check that we have seen all the required field mappers
        Set<String> missingRequiredMappers = mappersHandler.getMissingRequiredMappers();
        if (!missingRequiredMappers.isEmpty()) {
            throw new SolrException(BAD_REQUEST,
                    "Document is missing the following required fields: " + missingRequiredMappers);
        }

        // Create and process the Add command
        AddUpdateCommand cmd = new AddUpdateCommand(req);
        cmd.solrDoc = docBuilder.getSolrInputDocument();
        processor.processAdd(cmd);
    } finally {
        IOUtils.closeQuietly(reader);
    }
}

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/*from w  w  w.  ja  v a2s .  c o m*/
 */
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.compaction.SolrUpdateService.java

License:Apache License

/**
 * Adds the given document to the solr index without commit.
 *
 * @param doc the document to add/*  w  w  w  .jav a2 s .c  o m*/
 * @throws IOException iff something goes wrong
 */
public void add(SolrInputDocument doc) throws IOException {
    AddUpdateCommand cmd = new AddUpdateCommand(req);
    cmd.solrDoc = doc;
    cmd.commitWithin = COMMIT_WITHIN;
    updateProcessor.processAdd(cmd);
}

From source file:de.qaware.chronix.solr.compaction.SolrUpdateService.java

License:Apache License

/**
 * Adds the given document to the solr index without commit.
 *
 * @param docs the documents to add/*from   ww w  .j a  va 2  s  .  c  om*/
 * @throws IOException iff something goes wrong
 */
public void add(Collection<SolrInputDocument> docs) throws IOException {
    for (SolrInputDocument doc : docs) {
        AddUpdateCommand cmd = new AddUpdateCommand(req);
        cmd.solrDoc = doc;
        cmd.commitWithin = COMMIT_WITHIN;
        updateProcessor.processAdd(cmd);
    }
}

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

License:Apache License

private void storeDocument(SolrInputDocument document, UpdateRequestProcessor processor, SolrQueryRequest req)
        throws IOException {
    LOGGER.debug("Adding Solr document...");
    AddUpdateCommand cmd = new AddUpdateCommand(req);
    cmd.solrDoc = document;/*from   w  w  w  .  j  av  a 2  s.  c o  m*/
    processor.processAdd(cmd);
    LOGGER.debug("Added Solr document");
}

From source file:org.alfresco.solr.AlfrescoCoreAdminTester.java

License:Open Source License

private NodeRef addNode(SolrCore core, AlfrescoSolrDataModel dataModel, int txid, int dbid, int aclid,
        QName type, QName[] aspects, Map<QName, PropertyValue> properties, Map<QName, String> content,
        String owner, ChildAssociationRef[] parentAssocs, NodeRef[] ancestors, String[] paths, NodeRef nodeRef,
        boolean commit) throws IOException {
    AddUpdateCommand addDocCmd = new AddUpdateCommand(solrQueryRequest);
    addDocCmd.overwrite = true;/*  www .ja  v  a 2  s . c  o m*/
    addDocCmd.solrDoc = createDocument(dataModel, new Long(txid), new Long(dbid), nodeRef, type, aspects,
            properties, content, new Long(aclid), paths, owner, parentAssocs, ancestors);
    core.getUpdateHandler().addDoc(addDocCmd);

    if (commit) {
        core.getUpdateHandler().commit(new CommitUpdateCommand(solrQueryRequest, false));
    }

    return nodeRef;
}

From source file:org.alfresco.solr.AlfrescoCoreAdminTester.java

License:Open Source License

private void addAcl(SolrCore core, AlfrescoSolrDataModel dataModel, int acltxid, int aclId, int maxReader,
        int totalReader) throws IOException {
    AddUpdateCommand aclTxCmd = new AddUpdateCommand(solrQueryRequest);
    aclTxCmd.overwrite = true;/*from  w w w .java 2  s  .c o  m*/
    SolrInputDocument aclTxSol = new SolrInputDocument();
    String aclTxId = AlfrescoSolrDataModel.getAclChangeSetDocumentId(new Long(acltxid));
    aclTxSol.addField(FIELD_SOLR4_ID, aclTxId);
    aclTxSol.addField(FIELD_VERSION, "0");
    aclTxSol.addField(FIELD_ACLTXID, acltxid);
    aclTxSol.addField(FIELD_INACLTXID, acltxid);
    aclTxSol.addField(FIELD_ACLTXCOMMITTIME, (new Date()).getTime());
    aclTxSol.addField(FIELD_DOC_TYPE, SolrInformationServer.DOC_TYPE_ACL_TX);
    aclTxCmd.solrDoc = aclTxSol;
    core.getUpdateHandler().addDoc(aclTxCmd);

    AddUpdateCommand aclCmd = new AddUpdateCommand(solrQueryRequest);
    aclCmd.overwrite = true;
    SolrInputDocument aclSol = new SolrInputDocument();
    String aclDocId = AlfrescoSolrDataModel.getAclDocumentId(AlfrescoSolrDataModel.DEFAULT_TENANT,
            new Long(aclId));
    aclSol.addField(FIELD_SOLR4_ID, aclDocId);
    aclSol.addField(FIELD_VERSION, "0");
    aclSol.addField(FIELD_ACLID, aclId);
    aclSol.addField(FIELD_INACLTXID, "" + acltxid);
    aclSol.addField(FIELD_READER, "GROUP_EVERYONE");
    aclSol.addField(FIELD_READER, "pig");
    for (int i = 0; i <= maxReader; i++) {
        aclSol.addField(FIELD_READER, "READER-" + (totalReader - i));
    }
    aclSol.addField(FIELD_DENIED, "something");
    aclSol.addField(FIELD_DOC_TYPE, SolrInformationServer.DOC_TYPE_ACL);
    aclCmd.solrDoc = aclSol;
    core.getUpdateHandler().addDoc(aclCmd);

}

From source file:org.alfresco.solr.AlfrescoCoreAdminTester.java

License:Open Source License

private void addStoreRoot(SolrCore core, AlfrescoSolrDataModel dataModel, NodeRef rootNodeRef, int txid,
        int dbid, int acltxid, int aclid) throws IOException {
    AddUpdateCommand addDocCmd = new AddUpdateCommand(this.solrQueryRequest);
    addDocCmd.overwrite = true;//from   w w w  . j  av  a2s. c  o  m
    addDocCmd.solrDoc = createDocument(dataModel, new Long(txid), new Long(dbid), rootNodeRef,
            ContentModel.TYPE_STOREROOT, new QName[] { ContentModel.ASPECT_ROOT }, null, null, new Long(aclid),
            new String[] { "/" }, "system", null, null);
    core.getUpdateHandler().addDoc(addDocCmd);
    addAcl(core, dataModel, acltxid, aclid, 0, 0);

    AddUpdateCommand txCmd = new AddUpdateCommand(this.solrQueryRequest);
    txCmd.overwrite = true;
    SolrInputDocument input = new SolrInputDocument();
    String id = AlfrescoSolrDataModel.getTransactionDocumentId(new Long(txid));
    input.addField(FIELD_SOLR4_ID, id);
    input.addField(FIELD_VERSION, "0");
    input.addField(FIELD_TXID, txid);
    input.addField(FIELD_INTXID, txid);
    input.addField(FIELD_TXCOMMITTIME, (new Date()).getTime());
    input.addField(FIELD_DOC_TYPE, SolrInformationServer.DOC_TYPE_TX);
    txCmd.solrDoc = input;
    core.getUpdateHandler().addDoc(txCmd);

    core.getUpdateHandler().commit(new CommitUpdateCommand(this.solrQueryRequest, false));
}