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

The following examples show how to use org.apache.solr.client.solrj.request.UpdateRequest#setCommitWithin() . 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: Operations.java    From kafka-connect-solr with Apache License 2.0 6 votes vote down vote up
UpdateRequest addOperation(boolean delete) {
  UpdateRequest result;
  if (delete) {
    result = (this.lastDelete = new UpdateRequest());
  } else {
    result = (this.lastUpdate = new UpdateRequest());
  }
  this.operations.add(result);
  this.lastOperationIsDelete = delete;
  if (this.config.commitWithin > 0) {
    result.setCommitWithin(this.config.commitWithin);
  }
  if (this.config.useBasicAuthentication) {
    result.setBasicAuthCredentials(
        this.config.username,
        this.config.password
    );
  }
  return result;
}
 
Example 2
Source File: SolrCmdDistributor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void distribDelete(DeleteUpdateCommand cmd, List<Node> nodes, ModifiableSolrParams params, boolean sync,
                          RollupRequestReplicationTracker rollupTracker,
                          LeaderRequestReplicationTracker leaderTracker) throws IOException {
  
  if (!cmd.isDeleteById()) {
    blockAndDoRetries(); // For DBQ, flush all writes before submitting
  }
  
  for (Node node : nodes) {
    UpdateRequest uReq = new UpdateRequest();
    uReq.setParams(params);
    uReq.setCommitWithin(cmd.commitWithin);
    if (cmd.isDeleteById()) {
      uReq.deleteById(cmd.getId(), cmd.getRoute(), cmd.getVersion());
    } else {
      uReq.deleteByQuery(cmd.query);
    }
    submit(new Req(cmd, node, uReq, sync, rollupTracker, leaderTracker), false);
  }
}
 
Example 3
Source File: DistribJoinFromCollectionTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected static String indexDoc(String collection, int id, String joinField, String matchField, String getField) throws Exception {
  UpdateRequest up = new UpdateRequest();
  up.setCommitWithin(50);
  up.setParam("collection", collection);
  SolrInputDocument doc = new SolrInputDocument();
  String docId = "" + id;
  doc.addField("id", docId);
  doc.addField("join_s", joinField);
  if (matchField != null)
    doc.addField("match_s", matchField);
  if (getField != null)
    doc.addField("get_s", getField);
  up.add(doc);
  cluster.getSolrClient().request(up);
  return docId;
}
 
Example 4
Source File: SolrServerDocumentLoader.java    From kite with Apache License 2.0 6 votes vote down vote up
private void sendDeletes(List deletes) throws SolrServerException, IOException {
  if (deletes.size() > 0) {
    UpdateRequest req = new UpdateRequest();
    for (Object delete : deletes) {
      if (delete instanceof String) {
        req.deleteById((String)delete); // add the delete to the req list
      } else {
        String query = ((QueryStringHolder)delete).getQuery();
        req.deleteByQuery(query); // add the delete to the req list
      }
    }
    req.setCommitWithin(-1);
    log(req.process(server));
    deletes.clear();
  }
}
 
Example 5
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 6
Source File: SolrConnection.java    From storm-crawler with Apache License 2.0 5 votes vote down vote up
public static UpdateRequest getRequest(Map stormConf, String boltType) {
    int commitWithin = ConfUtils.getInt(stormConf, "solr." + boltType
            + ".commit.within", -1);

    UpdateRequest request = new UpdateRequest();

    if (commitWithin != -1) {
        request.setCommitWithin(commitWithin);
    }

    return request;
}
 
Example 7
Source File: MCRSolrAbstractIndexHandler.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
protected UpdateRequest getUpdateRequest(String path) {
    UpdateRequest req = path != null ? new UpdateRequest(path) : new UpdateRequest();
    req.setCommitWithin(getCommitWithin());
    return req;
}
 
Example 8
Source File: SolrExampleTestsBase.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * query the example
 */
@Test
public void testCommitWithinOnAdd() throws Exception {
  // make sure it is empty...
  SolrClient client = getSolrClient();
  client.deleteByQuery("*:*");// delete everything!
  client.commit();
  QueryResponse rsp = client.query(new SolrQuery("*:*"));
  Assert.assertEquals(0, rsp.getResults().getNumFound());
  
  // Now try a timed commit...
  SolrInputDocument doc3 = new SolrInputDocument();
  doc3.addField("id", "id3");
  doc3.addField("name", "doc3");
  doc3.addField("price", 10);
  UpdateRequest up = new UpdateRequest();
  up.add(doc3);
  up.setCommitWithin(500); // a smaller commitWithin caused failures on the
                           // following assert
  up.process(client);
  
  rsp = client.query(new SolrQuery("*:*"));
  Assert.assertEquals(0, rsp.getResults().getNumFound());
  
  // TODO: not a great way to test this - timing is easily out
  // of whack due to parallel tests and various computer specs/load
  Thread.sleep(1000); // wait 1 sec
  
  // now check that it comes out...
  rsp = client.query(new SolrQuery("id:id3"));
  
  int cnt = 0;
  while (rsp.getResults().getNumFound() == 0) {
    // wait and try again for slower/busier machines
    // and/or parallel test effects.
    
    if (cnt++ == 10) {
      break;
    }
    
    Thread.sleep(2000); // wait 2 seconds...
    
    rsp = client.query(new SolrQuery("id:id3"));
  }
  
  Assert.assertEquals(1, rsp.getResults().getNumFound());
  
  // Now test the new convenience parameter on the add() for commitWithin
  SolrInputDocument doc4 = new SolrInputDocument();
  doc4.addField("id", "id4");
  doc4.addField("name", "doc4");
  doc4.addField("price", 10);
  client.add(doc4, 500);
  
  Thread.sleep(1000); // wait 1 sec
  
  // now check that it comes out...
  rsp = client.query(new SolrQuery("id:id4"));
  
  cnt = 0;
  while (rsp.getResults().getNumFound() == 0) {
    // wait and try again for slower/busier machines
    // and/or parallel test effects.
    
    if (cnt++ == 10) {
      break;
    }
    
    Thread.sleep(2000); // wait 2 seconds...
    
    rsp = client.query(new SolrQuery("id:id3"));
  }
  
  Assert.assertEquals(1, rsp.getResults().getNumFound());
}
 
Example 9
Source File: SolrExampleTestsBase.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testCommitWithinOnDelete() throws Exception {
  // make sure it is empty...
  SolrClient client = getSolrClient();
  client.deleteByQuery("*:*");// delete everything!
  client.commit();
  QueryResponse rsp = client.query(new SolrQuery("*:*"));
  Assert.assertEquals(0, rsp.getResults().getNumFound());
  
  // Now add one document...
  SolrInputDocument doc3 = new SolrInputDocument();
  doc3.addField("id", "id3");
  doc3.addField("name", "doc3");
  doc3.addField("price", 10);
  client.add(doc3);
  client.commit();
  
  // now check that it comes out...
  rsp = client.query(new SolrQuery("id:id3"));
  Assert.assertEquals(1, rsp.getResults().getNumFound());
  
  // now test commitWithin on a delete
  UpdateRequest up = new UpdateRequest();
  up.setCommitWithin(1000);
  up.deleteById("id3");
  up.process(client);
  
  // the document should still be there
  rsp = client.query(new SolrQuery("id:id3"));
  Assert.assertEquals(1, rsp.getResults().getNumFound());
  
  // check if the doc has been deleted every 250 ms for 30 seconds
  TimeOut timeout = new TimeOut(30, TimeUnit.SECONDS, TimeSource.NANO_TIME);
  do {
    Thread.sleep(250); // wait 250 ms
    
    rsp = client.query(new SolrQuery("id:id3"));
    if (rsp.getResults().getNumFound() == 0) {
      return;
    }
  } while (! timeout.hasTimedOut());
  
  Assert.fail("commitWithin failed to commit");
}
 
Example 10
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 11
Source File: SolrClient.java    From lucene-solr with Apache License 2.0 3 votes vote down vote up
/**
 * Adds a single document specifying max time before it becomes committed
 *
 * @param collection the Solr collection to add the document to
 * @param doc  the input document
 * @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, SolrInputDocument doc, int commitWithinMs) throws SolrServerException, IOException {
  UpdateRequest req = new UpdateRequest();
  req.add(doc);
  req.setCommitWithin(commitWithinMs);
  return req.process(this, collection);
}
 
Example 12
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 13
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 14
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);
}