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

The following examples show how to use org.apache.solr.client.solrj.request.UpdateRequest#deleteById() . 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: DeleteStream.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Overrides implementation to extract the <code>"id"</code> and <code>"_version_"</code> 
 * (if included) from each document and use that information to construct a "Delete By Id" request.  
 * Any other fields (ie: Tuple values) are ignored.
 */
@Override
protected void uploadBatchToCollection(List<SolrInputDocument> documentBatch) throws IOException {
  if (documentBatch.size() == 0) {
    return;
  }

  try {
    // convert each doc into a deleteById request...
    final UpdateRequest req = new UpdateRequest();
    for (SolrInputDocument doc : documentBatch) {
      final String id = doc.getFieldValue(ID_TUPLE_KEY).toString();
      final Long version = getVersion(doc);
      req.deleteById(id, version);
    }
    req.process(getCloudSolrClient(), getCollectionName());
  } catch (SolrServerException | NumberFormatException| IOException e) {
    log.warn("Unable to delete documents from collection due to unexpected error.", e);
    String className = e.getClass().getName();
    String message = e.getMessage();
    throw new IOException(String.format(Locale.ROOT,"Unexpected error when deleting documents from collection %s- %s:%s", getCollectionName(), className, message));
  }
}
 
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: TestStressInPlaceUpdates.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
protected long deleteDocAndGetVersion(String id, ModifiableSolrParams params, boolean deleteByQuery) throws Exception {
  params.add("versions", "true");
 
  UpdateRequest ureq = new UpdateRequest();
  ureq.setParams(params);
  if (deleteByQuery) {
    ureq.deleteByQuery("id:"+id);
  } else {
    ureq.deleteById(id);
  }
  UpdateResponse resp;
  // send updates to leader, to avoid SOLR-8733
  resp = ureq.process(leaderClient);
  
  String key = deleteByQuery? "deleteByQuery": "deletes";
  long returnedVersion = Long.parseLong(((NamedList) resp.getResponse().get(key)).getVal(0).toString());
  assertTrue("Due to SOLR-8733, sometimes returned version is 0. Let us assert that we have successfully"
      + " worked around that problem here.", returnedVersion < 0);
  return returnedVersion;
}
 
Example 4
Source File: SolrClientInterceptorTest.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteById() throws Throwable {
    UpdateRequest request = new UpdateRequest();
    arguments = new Object[] {
        request.deleteById("12"),
        null,
        collection
    };
    interceptor.beforeMethod(enhancedInstance, method, arguments, argumentType, null);
    interceptor.afterMethod(enhancedInstance, method, arguments, argumentType, getResponse());

    List<TraceSegment> segments = segmentStorage.getTraceSegments();
    List<AbstractTracingSpan> spans = SegmentHelper.getSpans(segments.get(0));

    Assert.assertEquals(segments.size(), 1);
    Assert.assertEquals(spans.size(), 1);

    AbstractTracingSpan span = spans.get(0);
    spanDeleteAssert(span, "solrJ/collection/update/DELETE_BY_IDS", "[12]");
}
 
Example 5
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 6
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 7
Source File: BaseDistributedSearchTestCase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected UpdateResponse del(SolrClient client, SolrParams params, Object... ids) throws IOException, SolrServerException {
  UpdateRequest ureq = new UpdateRequest();
  ureq.setParams(new ModifiableSolrParams(params));
  for (Object id: ids) {
    ureq.deleteById(id.toString());
  }
  return ureq.process(client);
}
 
Example 8
Source File: TestInPlaceUpdatesDistrib.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
UpdateRequest simulatedDeleteRequest(int id, long version) throws SolrServerException, IOException {
  String baseUrl = getBaseUrl(""+id);

  UpdateRequest ur = new UpdateRequest();
  if (random().nextBoolean() || onlyLeaderIndexes) {
    ur.deleteById(""+id);
  } else {
    ur.deleteByQuery("id:"+id);
  }
  ur.setParam("_version_", ""+version);
  ur.setParam("update.distrib", "FROMLEADER");
  ur.setParam("distrib.from", baseUrl);
  return ur;
}
 
Example 9
Source File: TestDistribDocBasedVersion.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void vdelete(String id, long version, String... params) throws Exception {
  UpdateRequest req = new UpdateRequest();
  req.deleteById(id);
  req.setParam("del_version", Long.toString(version));
  for (int i=0; i<params.length; i+=2) {
    req.setParam( params[i], params[i+1]);
  }
  solrClient.request(req);
  // req.process(cloudClient);
}
 
Example 10
Source File: ReplicationFactorTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected void doDBIdWithRetry(int expectedRf, int retries, String msg, int docsToAdd) throws Exception {
  Set<Integer> docIds = getSomeIds(docsToAdd);
  addDocs(docIds, expectedRf, retries);
  UpdateRequest req = new UpdateRequest();
  req.deleteById(StringUtils.join(docIds, ","));
  boolean minRfExplicit = maybeAddMinRfExplicitly(expectedRf, req);
  doDelete(req, msg, expectedRf, retries, minRfExplicit);
}
 
Example 11
Source File: CdcrVersionReplicationTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void vdelete(String id, long version, String... params) throws Exception {
  UpdateRequest req = new UpdateRequest();
  req.deleteById(id);
  req.setParam(vfield, Long.toString(version));

  for (int i = 0; i < params.length; i += 2) {
    req.setParam(params[i], params[i + 1]);
  }
  solrServer.request(req);
}
 
Example 12
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 13
Source File: CdcrReplicator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private UpdateRequest processUpdate(Object o, UpdateRequest req) {

    // should currently be a List<Oper,Ver,Doc/Id>
    @SuppressWarnings({"rawtypes"})
    List entry = (List) o;

    int operationAndFlags = (Integer) entry.get(0);
    int oper = operationAndFlags & UpdateLog.OPERATION_MASK;
    long version = (Long) entry.get(1);

    // record the operation in the benchmark timer
    state.getBenchmarkTimer().incrementCounter(oper);

    switch (oper) {
      case UpdateLog.ADD: {
        // the version is already attached to the document
        SolrInputDocument sdoc = (SolrInputDocument) entry.get(entry.size() - 1);
        req.add(sdoc);
        return req;
      }
      case UpdateLog.DELETE: {
        byte[] idBytes = (byte[]) entry.get(2);
        req.deleteById(new String(idBytes, Charset.forName("UTF-8")));
        req.setParam(VERSION_FIELD, Long.toString(version));
        return req;
      }

      case UpdateLog.DELETE_BY_QUERY: {
        String query = (String) entry.get(2);
        req.deleteByQuery(query);
        req.setParam(VERSION_FIELD, Long.toString(version));
        return req;
      }

      case UpdateLog.COMMIT: {
        return null;
      }

      default:
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown Operation! " + oper);
    }
  }
 
Example 14
Source File: TestInPlaceUpdatesDistrib.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
UpdateRequest regularDeleteRequest(int id) throws SolrServerException, IOException {
  UpdateRequest ur = new UpdateRequest();
  ur.deleteById(""+id);
  return ur;
}
 
Example 15
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 16
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);
}