Example usage for org.apache.solr.client.solrj.request UpdateRequest setParam

List of usage examples for org.apache.solr.client.solrj.request UpdateRequest setParam

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.request UpdateRequest setParam.

Prototype

public void setParam(String param, String value) 

Source Link

Usage

From source file:com.ifactory.press.db.solr.processor.UpdateDocValuesTest.java

License:Apache License

@Test
/** The update service throws an error when key field is provided with no value fields */
public void testMissingValue() throws Exception {
    insertTestDocuments(1);/*  www . j  a v a2 s.  co  m*/
    UpdateRequest req = new UpdateRequest();
    SolrInputDocument doc = new SolrInputDocument();
    req.add(doc);
    req.setPath(UPDATE_DOCVALUES);
    req.setParam(UpdateDocValuesProcessor.UPDATEDV_KEY_FIELD, "id");
    req.setParam(UpdateDocValuesProcessor.UPDATEDV_VALUE_FIELD, "x");
    // doc with no value for key
    try {
        solr.request(req);
        assertFalse("expected exception not thrown", true);
    } catch (SolrException e) {
        assertTrue(e.getMessage(), e.getMessage().contains("no value for updatedv.key.field"));
    }

    doc.addField("id", "id0");
    req.setParam(UpdateDocValuesProcessor.UPDATEDV_VALUE_FIELD, null);
    // no UpdateDocValuesProcessor.UPDATEDV_VALUE_FIELD
    try {
        solr.request(req);
        assertFalse("expected exception not thrown", true);
    } catch (SolrException e) {
        assertTrue(e.getMessage().contains("missing parameter updatedv.value.field"));
    }
}

From source file:com.ifactory.press.db.solr.processor.UpdateDocValuesTest.java

License:Apache License

private void insertTestDocuments(int n, int nkids, boolean preserveDV) throws Exception {
    UpdateRequest req = new UpdateRequest();
    req.setPath(UPDATE_DOCVALUES);/*from   w w w .  j  a  va  2s.c  o  m*/
    if (preserveDV) {
        req.setParam(UpdateDocValuesProcessor.UPDATEDV_VALUE_FIELD, WEIGHT_DV);
    }
    for (int i = 1; i <= n; i++) {
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField(URI, uri(i));
        doc.addField(TEXT_FIELD, "This is document " + i);
        // NOTE: must provide a value for at least one document in order to create the field:
        // it's not enough to just put it in the solr schema
        if (!preserveDV) {
            doc.addField(WEIGHT_DV, 0);
        }
        for (int j = 1; j <= nkids; j++) {
            SolrInputDocument kid = new SolrInputDocument();
            kid.addField(URI, uriChild(i, j));
            kid.addField(TEXT_FIELD, "This is child document " + i + "/" + j);
            doc.addChildDocument(kid);
        }
        req.add(doc);
    }
    solr.request(req);
    solr.commit(false, true, true);
}

From source file:com.ifactory.press.db.solr.processor.UpdateDocValuesTest.java

License:Apache License

private UpdateRequest updateDocValues() {
    UpdateRequest req = new UpdateRequest();
    req.setParam(UpdateDocValuesProcessor.UPDATEDV_KEY_FIELD, URI);
    req.setParam(UpdateDocValuesProcessor.UPDATEDV_VALUE_FIELD, WEIGHT_DV);
    req.setPath(UPDATE_DOCVALUES);//from www  . j av  a  2  s.c  o m
    return req;
}

From source file:com.norconex.committer.solr.SolrCommitter.java

License:Apache License

@Override
protected void commitBatch(List<ICommitOperation> batch) {

    LOG.info("Sending " + batch.size() + " documents to Solr for update/deletion.");
    try {//from  w  w w .java 2 s .c o  m
        SolrServer server = solrServerFactory.createSolrServer(this);
        UpdateRequest request = new UpdateRequest();

        // Add to request any parameters provided
        for (String name : updateUrlParams.keySet()) {
            request.setParam(name, updateUrlParams.get(name));
        }

        // Add to request all operations in batch
        for (ICommitOperation op : batch) {
            if (op instanceof IAddOperation) {
                request.add(buildSolrDocument(((IAddOperation) op).getMetadata()));
            } else if (op instanceof IDeleteOperation) {
                request.deleteById(((IDeleteOperation) op).getReference());
            } else {
                throw new CommitterException("Unsupported operation:" + op);
            }
        }

        request.process(server);
        server.commit();
    } catch (Exception e) {
        throw new CommitterException("Cannot index document batch to Solr.", e);
    }
    LOG.info("Done sending documents to Solr for update/deletion.");
}

From source file:com.shaie.solr.AutoAddReplicaTest.java

License:Apache License

private void indexDocumentAndWaitForSync(String docId, String collectionName) {
    final SolrInputDocument doc = new SolrInputDocument();
    doc.setField("id", docId);
    final UpdateRequest docUpdate = new UpdateRequest();
    docUpdate.add(doc);/*from   w w w  .  ja  v a2 s.  com*/
    docUpdate.setParam(UpdateParams.COLLECTION, collectionName);
    docUpdate.setWaitSearcher(true);
    docUpdate.setParam(UpdateParams.SOFT_COMMIT, Boolean.TRUE.toString());
    try {
        final UpdateResponse response = docUpdate.process(solrClient);
        assertThat(response.getStatus()).isEqualTo(0);
        SolrCloudUtils.waitForReplicasToSync(collectionName, solrClient, WAIT_TIMEOUT_SECONDS);
    } catch (SolrServerException | IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.shaie.solr.MiniSolrCloudClusterTest.java

License:Apache License

private void indexDocumentAndWaitForSync(String docId) {
    final SolrInputDocument doc = new SolrInputDocument();
    doc.setField("id", docId);
    final UpdateRequest docUpdate = new UpdateRequest();
    docUpdate.add(doc);/*w  ww  . ja  va  2  s. c o  m*/
    docUpdate.setParam(UpdateParams.COLLECTION, COLLECTION_NAME);
    docUpdate.setWaitSearcher(true);
    docUpdate.setParam(UpdateParams.SOFT_COMMIT, Boolean.TRUE.toString());
    try {
        final UpdateResponse response = docUpdate.process(solrClient);
        assertThat(response.getStatus()).isEqualTo(0);
        SolrCloudUtils.waitForReplicasToSync(COLLECTION_NAME, solrClient, WAIT_TIMEOUT_SECONDS);
    } catch (SolrServerException | IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:de.bitocean.mm.importer.GMailLoader.java

License:Apache License

private static void importMailsFromFolder(Folder f, String label)
        throws MessagingException, LoginException, IOException, SolrServerException {

    System.out.println("**** Import an email folder *** \n" + f.getFullName());

    f.open(Folder.READ_ONLY);//from   ww w.  ja v a2s. c om

    Message[] m2 = f.getMessages();

    for (Message m : m2) {

        System.out.println("[[Mail Label:" + f.getFullName() + "]]        \n# Subject: > " + m.getSubject());

        UUID uid = UUID.randomUUID();
        System.out.println("# UUID:    > " + uid.toString());

        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", uid.toString());
        document.addField("tag", m.getSubject());

        String cont = handleContent(document, m);

        document.addField("answer", cont);

        document.addField("question", m.getSubject());

        document.addField("type", "raw.email");
        document.addField("context", label);

        String author = "MAILIMPORT";
        Address[] from = m.getFrom();
        if (from != null) {
            author = "";
            for (Address a : from) {
                author = author.concat(", " + a.toString());
            }
        }
        document.addField("author", author);

        Date d = m.getReceivedDate();

        DateFormat dfmt1 = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat dfmt2 = new SimpleDateFormat("hh:mm:ss");
        document.addField("timeasked", dfmt1.format(d) + "T" + dfmt2.format(d) + "Z");

        UpdateRequest add = new UpdateRequest();
        add.add(document);
        add.setParam("collection", collection);
        add.process(solr);

    }

    UpdateRequest commit = new UpdateRequest();
    commit.setAction(UpdateRequest.ACTION.COMMIT, true, true);
    commit.setParam("collection", collection);
    commit.process(solr);

}

From source file:io.confluent.connect.solr.sink.CloudSolrInputDocumentHandler.java

License:Apache License

@Override
protected void beforeFlush(UpdateRequest updateRequest) {
    updateRequest.setParam("collection", this.collection);
}

From source file:jp.aegif.nemaki.tracker.CoreTracker.java

License:Open Source License

/**
 * Build an update request to Solr without file
 *
 * @param content//from ww  w.j a  va 2 s.c o  m
 * @return
 */
// TODO Unify that of Registration class
private AbstractUpdateRequest buildUpdateRequest(Map<String, Object> map) {
    logger.info("Start buildUpdateRequest");
    UpdateRequest up = new UpdateRequest();
    SolrInputDocument sid = new SolrInputDocument();

    // Set SolrDocument parameters
    Iterator<String> iterator = map.keySet().iterator();
    while (iterator.hasNext()) {
        String key = iterator.next();
        sid.addField(key, map.get(key));
    }

    // Set UpdateRequest
    up.add(sid);
    // Ignored(for schema.xml, ignoring some SolrCell meta fields)
    up.setParam(UNKNOWN_FIELD_PREFIX, "ignored_");

    // Set Solr action parameter
    up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
    logger.info(up.toString());
    return up;
}

From source file:nl.minbzk.dwr.zoeken.enricher.uploader.SolrResultUploader.java

License:Open Source License

/**
 * {@inheritDoc}//from  w  w  w.  j a v a  2s . c  om
 */
@Override
public void upload(final EnricherJob job, final GeneratorResult result) throws Exception {
    try {
        SolrServer server = getInstance(job.getDatabaseName(), job.getGeneratorTypes());

        // Create a new update request

        UpdateRequest request = createRequest();

        request.add(((MultiGeneratorResult<SolrInputDocument>) result).getDocuments());

        // Set a collection in case of a (resolvable) composite database name

        if (StringUtils.hasText(job.getDatabaseNameComposition())) {
            String compositeDatabaseName = determineAlternateDatabaseName(job.getDatabaseName(),
                    job.getDatabaseNameComposition(), job.getDatabaseNamePrerequisitesExpression(),
                    ((MultiGeneratorResult<SolrInputDocument>) result).getDocuments());

            if (compositeDatabaseName != null) {
                if (logger.isDebugEnabled())
                    logger.debug(String.format("Composite database name resolved to collection '%s'",
                            compositeDatabaseName));

                request.setParam("collection", compositeDatabaseName);
            } else {
                if (logger.isDebugEnabled())
                    logger.debug(String.format(
                            "Composite database name could not be (completely) resolved - will use default collection '%s'",
                            job.getDatabaseName()));
            }
        }

        // Now perform the request

        UpdateResponse response = request.process(server);

        if (response.getStatus() != 0)
            logger.error("Failed to add documents to Solr, status = " + response.getStatus());
        else
            logger.info(String.format("Successfully added document to Solr, using request URL %s",
                    response.getRequestUrl()));

        // Perform direct notification now that the document has been uploaded

        notifier.process(result);
    } catch (SolrServerException e) {
        logger.error("Failed to add documents to Solr", e);
    }
}