Java Code Examples for org.apache.solr.client.solrj.request.UpdateRequest#process()

The following examples show how to use org.apache.solr.client.solrj.request.UpdateRequest#process() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SolrWriter.java    From anthelion with Apache License 2.0 6 votes vote down vote up
public void close() throws IOException {
  try {
    if (!inputDocs.isEmpty()) {
      LOG.info("Indexing " + Integer.toString(inputDocs.size()) + " documents");
      if (numDeletes > 0) {
        LOG.info("Deleting " + Integer.toString(numDeletes) + " documents");
      }
      UpdateRequest req = new UpdateRequest();
      req.add(inputDocs);
      req.setParams(params);
      req.process(solr);
      inputDocs.clear();
    }
    // solr.commit();
  } catch (final SolrServerException e) {
    throw makeIOException(e);
  }
}
 
Example 2
Source File: SolrIndexWriter.java    From nutch-htmlunit with Apache License 2.0 6 votes vote down vote up
public void close() throws IOException {
    try {
        if (!inputDocs.isEmpty()) {
            LOG.info("Indexing " + Integer.toString(inputDocs.size())
                    + " documents");
            if (numDeletes > 0) {
                LOG.info("Deleting " + Integer.toString(numDeletes)
                        + " documents");
            }
            UpdateRequest req = new UpdateRequest();
            req.add(inputDocs);
            req.setParams(params);
            req.process(solr);
            inputDocs.clear();
        }
    } catch (final SolrServerException e) {
        throw makeIOException(e);
    }
}
 
Example 3
Source File: CloudHttp2SolrClientTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testParallelUpdateQTime() throws Exception {
  CollectionAdminRequest.createCollection(COLLECTION, "conf", 2, 1).process(cluster.getSolrClient());
  cluster.waitForActiveCollection(COLLECTION, 2, 2);
  UpdateRequest req = new UpdateRequest();
  for (int i=0; i<10; i++)  {
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", String.valueOf(TestUtil.nextInt(random(), 1000, 1100)));
    req.add(doc);
  }
  UpdateResponse response = req.process(getRandomClient(), COLLECTION);
  // See SOLR-6547, we just need to ensure that no exception is thrown here
  assertTrue(response.getQTime() >= 0);
}
 
Example 4
Source File: MergeIndexesExampleTestBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testMergeIndexesByDirName() throws Exception {
  UpdateRequest up = setupCores();

  // Now get the index directory of core1 and merge with core0
  CoreAdminRequest.mergeIndexes("core0", new String[] {getIndexDirCore1()}, new String[0], getSolrAdmin());

  // Now commit the merged index
  up.clear(); // just do commit
  up.process(getSolrCore0());

  assertEquals(1,
      getSolrCore0().query(new SolrQuery("id:AAA")).getResults().size());
  assertEquals(1,
      getSolrCore0().query(new SolrQuery("id:BBB")).getResults().size());
}
 
Example 5
Source File: TestReplicationHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private UpdateResponse emptyUpdate(SolrClient client, String... params)
  throws SolrServerException, IOException {

  UpdateRequest req = new UpdateRequest();
  req.setParams(params(params));
  return req.process(client);
}
 
Example 6
Source File: AbstractFullDistribZkTestBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected void index_specific(SolrClient client, Object... fields)
    throws Exception {
  SolrInputDocument doc = new SolrInputDocument();
  for (int i = 0; i < fields.length; i += 2) {
    doc.addField((String) (fields[i]), fields[i + 1]);
  }

  UpdateRequest ureq = new UpdateRequest();
  ureq.add(doc);
  // ureq.setParam("update.chain", DISTRIB_UPDATE_CHAIN);
  ureq.process(client);

  // add to control second in case adding to shards fails
  controlClient.add(doc);
}
 
Example 7
Source File: SolrAbstractSink.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public void write(Record<T> record) {
    UpdateRequest updateRequest = new UpdateRequest();
    if (solrSinkConfig.getSolrCommitWithinMs() > 0) {
        updateRequest.setCommitWithin(solrSinkConfig.getSolrCommitWithinMs());
    }
    if (enableBasicAuth) {
        updateRequest.setBasicAuthCredentials(
            solrSinkConfig.getUsername(),
            solrSinkConfig.getPassword()
        );
    }

    SolrInputDocument document = convert(record);
    updateRequest.add(document);

    try {
        UpdateResponse updateResponse = updateRequest.process(client, solrSinkConfig.getSolrCollection());
        if (updateResponse.getStatus() == 0) {
            record.ack();
        } else {
            record.fail();
        }
    } catch (SolrServerException | IOException e) {
        record.fail();
        log.warn("Solr update document exception ", e);
    }
}
 
Example 8
Source File: MCRSolrIndexer.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Convenient method to delete a derivate and all its files at once.
 *
 * @param id the derivate id
 * @return the solr response
 */
public static UpdateResponse deleteDerivate(SolrClient solrClient, String id) {
    if (id == null) {
        return null;
    }
    UpdateResponse updateResponse = null;
    long start = System.currentTimeMillis();
    try {
        LOGGER.debug("Deleting derivate \"{}\" from solr", id);
        UpdateRequest req = new UpdateRequest();
        StringBuilder deleteQuery = new StringBuilder();
        deleteQuery.append("id:").append(id).append(" ");
        deleteQuery.append("derivateID:").append(id);
        if (MCRSolrUtils.useNestedDocuments()) {
            deleteQuery.append(" ").append("_root_:").append(id);
        }
        req.deleteByQuery(deleteQuery.toString());
        updateResponse = req.process(solrClient);
        solrClient.commit();
    } catch (Exception e) {
        LOGGER.error("Error deleting document from solr", e);
    }
    long end = System.currentTimeMillis();
    MCRSolrIndexStatistic operations = MCRSolrIndexStatisticCollector.OPERATIONS;
    operations.addDocument(1);
    operations.addTime(end - start);
    return updateResponse;
}
 
Example 9
Source File: MCRSolrIndexer.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Deletes a list of documents by unique ID. Also removes any nested document of that ID.
 *
 * @param solrIDs
 *            the list of solr document IDs to delete
 */
public static UpdateResponse deleteById(SolrClient client, String... solrIDs) {
    if (solrIDs == null || solrIDs.length == 0) {
        return null;
    }

    UpdateResponse updateResponse = null;
    long start = System.currentTimeMillis();
    try {
        LOGGER.debug("Deleting \"{}\" from solr", Arrays.asList(solrIDs));
        UpdateRequest req = new UpdateRequest();
        //delete all documents rooted at this id
        if (MCRSolrUtils.useNestedDocuments()) {
            StringBuilder deleteQuery = new StringBuilder("_root_:(");
            for (String solrID : solrIDs) {
                deleteQuery.append('"');
                deleteQuery.append(MCRSolrUtils.escapeSearchValue(solrID));
                deleteQuery.append("\" ");
            }
            deleteQuery.setCharAt(deleteQuery.length() - 1, ')');
            req.deleteByQuery(deleteQuery.toString());
        }
        //for document without nested
        req.deleteById(Arrays.asList(solrIDs));
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Delete request: {}", req.getXML());
        }
        updateResponse = req.process(client);
        client.commit();
    } catch (Exception e) {
        LOGGER.error("Error deleting document from solr", e);
    }
    long end = System.currentTimeMillis();
    MCRSolrIndexStatistic operations = MCRSolrIndexStatisticCollector.OPERATIONS;
    operations.addDocument(1);
    operations.addTime(end - start);
    return updateResponse;

}
 
Example 10
Source File: MCRSolrInputDocumentsHandler.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void index() throws IOException, SolrServerException {
    if (documents == null || documents.isEmpty()) {
        LOGGER.warn("No input documents to index.");
        return;
    }
    int totalCount = documents.size();
    LOGGER.info("Handling {} documents", totalCount);
    SolrClient solrClient = getSolrClient();
    if (solrClient instanceof ConcurrentUpdateSolrClient) {
        LOGGER.info("Detected ConcurrentUpdateSolrClient. Split up batch update.");
        splitDocuments();
        //for statistics:
        documents.clear();
        return;
    }
    UpdateResponse updateResponse;
    try {
        UpdateRequest updateRequest = getUpdateRequest(MCRSolrConstants.SOLR_UPDATE_PATH);
        updateRequest.add(documents);
        updateResponse = updateRequest.process(getSolrClient());
    } catch (Throwable e) {
        LOGGER.warn("Error while indexing document collection. Split and retry.");
        splitDocuments();
        return;
    }
    if (updateResponse.getStatus() != 0) {
        LOGGER.error("Error while indexing document collection. Split and retry: {}", updateResponse.getResponse());
        splitDocuments();
    } else {
        LOGGER.info("Sending {} documents was successful in {} ms.", totalCount, updateResponse.getElapsedTime());
    }
}
 
Example 11
Source File: AbstractAlfrescoDistributedIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected static UpdateResponse add(SolrClient client, SolrParams params, SolrInputDocument... sdocs)
        throws IOException, SolrServerException
{
    UpdateRequest ureq = new UpdateRequest();
    ureq.setParams(new ModifiableSolrParams(params));
    for (SolrInputDocument sdoc : sdocs)
    {
        ureq.add(sdoc);
    }
    return ureq.process(client);
}
 
Example 12
Source File: CdcrBootstrapTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // 6-Sep-2018
@Test
@AwaitsFix(bugUrl = "https://issues.apache.org/jira/browse/SOLR-12028")
public void testBootstrapWithContinousIndexingOnSourceCluster() throws Exception {
  // start the target first so that we know its zkhost
  MiniSolrCloudCluster target = new MiniSolrCloudCluster(1, createTempDir("cdcr-target"), buildJettyConfig("/solr"));
  try {
    if (log.isInfoEnabled()) {
      log.info("Target zkHost = {}", target.getZkServer().getZkAddress());
    }
    System.setProperty("cdcr.target.zkHost", target.getZkServer().getZkAddress());

    MiniSolrCloudCluster source = new MiniSolrCloudCluster(1, createTempDir("cdcr-source"), buildJettyConfig("/solr"));
    try {
      source.uploadConfigSet(configset("cdcr-source"), "cdcr-source");

      CollectionAdminRequest.createCollection("cdcr-source", "cdcr-source", 1, 1)
          .withProperty("solr.directoryFactory", "solr.StandardDirectoryFactory")
          .process(source.getSolrClient());
      source.waitForActiveCollection("cdcr-source", 1, 1);
      CloudSolrClient sourceSolrClient = source.getSolrClient();
      int docs = (TEST_NIGHTLY ? 100 : 10);
      int numDocs = indexDocs(sourceSolrClient, "cdcr-source", docs);

      QueryResponse response = sourceSolrClient.query(new SolrQuery("*:*"));
      assertEquals("", numDocs, response.getResults().getNumFound());

      // setup the target cluster
      target.uploadConfigSet(configset("cdcr-target"), "cdcr-target");
      CollectionAdminRequest.createCollection("cdcr-target", "cdcr-target", 1, 1)
          .process(target.getSolrClient());
      target.waitForActiveCollection("cdcr-target", 1, 1);
      CloudSolrClient targetSolrClient = target.getSolrClient();
      targetSolrClient.setDefaultCollection("cdcr-target");
      Thread.sleep(1000);

      CdcrTestsUtil.cdcrStart(targetSolrClient);
      CdcrTestsUtil.cdcrStart(sourceSolrClient);
      int c = 0;
      for (int k = 0; k < docs; k++) {
        UpdateRequest req = new UpdateRequest();
        for (; c < (k + 1) * 100; c++, numDocs++) {
          SolrInputDocument doc = new SolrInputDocument();
          doc.addField("id", "source_" + numDocs);
          doc.addField("xyz", numDocs);
          req.add(doc);
        }
        req.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
        log.info("Adding {} docs with commit=true, numDocs={}", docs, numDocs);
        req.process(sourceSolrClient);
      }

      response = sourceSolrClient.query(new SolrQuery("*:*"));
      assertEquals("", numDocs, response.getResults().getNumFound());

      response = CdcrTestsUtil.getCdcrQueue(sourceSolrClient);
      if (log.isInfoEnabled()) {
        log.info("Cdcr queue response: {}", response.getResponse());
      }
      long foundDocs = CdcrTestsUtil.waitForClusterToSync(numDocs, targetSolrClient);
      assertEquals("Document mismatch on target after sync", numDocs, foundDocs);

    } finally {
      source.shutdown();
    }
  } finally {
    target.shutdown();
  }
}
 
Example 13
Source File: TestDistribDocBasedVersion.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
void doDBQ(String q, String... reqParams) throws Exception {
  UpdateRequest req = new UpdateRequest();
  req.deleteByQuery(q);
  req.setParams(params(reqParams));
  req.process(cloudClient);
}
 
Example 14
Source File: CdcrVersionReplicationTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
void doDeleteByQuery(String q, String... reqParams) throws Exception {
  UpdateRequest req = new UpdateRequest();
  req.deleteByQuery(q);
  req.setParams(params(reqParams));
  req.process(solrServer);
}
 
Example 15
Source File: CrossCollectionJoinQueryTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static void indexDocs(String collection, Collection<SolrInputDocument> docs) throws IOException, SolrServerException {
  UpdateRequest update = new UpdateRequest();
  update.add(docs);
  update.process(cluster.getSolrClient(), collection);
}
 
Example 16
Source File: MergeIndexesExampleTestBase.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private UpdateRequest setupCores() throws SolrServerException, IOException {
  UpdateRequest up = new UpdateRequest();
  up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
  up.deleteByQuery("*:*");
  up.process(getSolrCore0());
  up.process(getSolrCore1());
  up.clear();

  // Add something to each core
  SolrInputDocument doc = new SolrInputDocument();
  doc.setField("id", "AAA");
  doc.setField("name", "core0");

  // Add to core0
  up.add(doc);
  up.process(getSolrCore0());

  // Add to core1
  doc.setField("id", "BBB");
  doc.setField("name", "core1");
  up.add(doc);
  up.process(getSolrCore1());

  // Now Make sure AAA is in 0 and BBB in 1
  SolrQuery q = new SolrQuery();
  QueryRequest r = new QueryRequest(q);
  q.setQuery("id:AAA");
  assertEquals(1, r.process(getSolrCore0()).getResults().size());
  assertEquals(0, r.process(getSolrCore1()).getResults().size());

  assertEquals(1,
      getSolrCore0().query(new SolrQuery("id:AAA")).getResults().size());
  assertEquals(0,
      getSolrCore0().query(new SolrQuery("id:BBB")).getResults().size());

  assertEquals(0,
      getSolrCore1().query(new SolrQuery("id:AAA")).getResults().size());
  assertEquals(1,
      getSolrCore1().query(new SolrQuery("id:BBB")).getResults().size());

  return up;
}
 
Example 17
Source File: SolrClient.java    From lucene-solr with Apache License 2.0 3 votes vote down vote up
/**
 * Adds a collection of documents, specifying max time before they become committed
 *
 * @param collection the Solr collection to add documents to
 * @param docs  the collection of documents
 * @param commitWithinMs  max time (in ms) before a commit will happen
 *
 * @return an {@link org.apache.solr.client.solrj.response.UpdateResponse} from the server
 *
 * @throws IOException         if there is a communication error with the server
 * @throws SolrServerException if there is an error on the server
 *
 * @since Solr 5.1
 */
public UpdateResponse add(String collection, Collection<SolrInputDocument> docs, int commitWithinMs) throws SolrServerException, IOException {
  UpdateRequest req = new UpdateRequest();
  req.add(docs);
  req.setCommitWithin(commitWithinMs);
  return req.process(this, collection);
}
 
Example 18
Source File: SolrClient.java    From lucene-solr with Apache License 2.0 3 votes vote down vote up
/**
 * Deletes a single document by unique ID, specifying max time before commit.
 * Doesn't work for child/nested docs.
 *
 * @param collection the Solr collection to delete the document from
 * @param id  the ID of the document to delete
 * @param commitWithinMs  max time (in ms) before a commit will happen
 *
 * @return an {@link org.apache.solr.client.solrj.response.UpdateResponse} containing the response
 *         from the server
 *
 * @throws IOException If there is a low-level I/O error.
 * @throws SolrServerException if there is an error on the server
 *
 * @since 5.1
 */
public UpdateResponse deleteById(String collection, String id, int commitWithinMs) throws SolrServerException, IOException {
  UpdateRequest req = new UpdateRequest();
  req.deleteById(id);
  req.setCommitWithin(commitWithinMs);
  return req.process(this, collection);
}
 
Example 19
Source File: SolrClient.java    From lucene-solr with Apache License 2.0 3 votes vote down vote up
/**
 * Deletes a list of documents by unique ID, specifying max time before commit.
 * Doesn't work for child/nested docs.
 *
 * @param collection the Solr collection to delete the documents from
 * @param ids  the list of document IDs to delete; must be non-null and contain elements
 * @param commitWithinMs  max time (in ms) before a commit will happen
 *
 * @return an {@link org.apache.solr.client.solrj.response.UpdateResponse} containing the response
 *         from the server
 *
 * @throws IOException If there is a low-level I/O error.
 * @throws SolrServerException if there is an error on the server
 *
 * @since 5.1
 */
public UpdateResponse deleteById(String collection, List<String> ids, int commitWithinMs) throws SolrServerException, IOException {
  if (ids == null) throw new IllegalArgumentException("'ids' parameter must be non-null");
  if (ids.isEmpty()) throw new IllegalArgumentException("'ids' parameter must not be empty; should contain IDs to delete");

  UpdateRequest req = new UpdateRequest();
  req.deleteById(ids);
  req.setCommitWithin(commitWithinMs);
  return req.process(this, collection);
}
 
Example 20
Source File: SolrClient.java    From lucene-solr with Apache License 2.0 3 votes vote down vote up
/**
 * Deletes documents from the index based on a query, specifying max time before commit
 *
 * @param collection the Solr collection to delete the documents from
 * @param query  the query expressing what documents to delete
 * @param commitWithinMs  max time (in ms) before a commit will happen
 *
 * @return an {@link org.apache.solr.client.solrj.response.UpdateResponse} containing the response
 *         from the server
 *
 * @throws IOException If there is a low-level I/O error.
 * @throws SolrServerException if there is an error on the server
 *
 * @since 5.1
 */
public UpdateResponse deleteByQuery(String collection, String query, int commitWithinMs) throws SolrServerException, IOException {
  UpdateRequest req = new UpdateRequest();
  req.deleteByQuery(query);
  req.setCommitWithin(commitWithinMs);
  return req.process(this, collection);
}