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

The following examples show how to use org.apache.solr.client.solrj.request.UpdateRequest#deleteByQuery() . 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: SolrClientInterceptorTest.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteByQuery() throws Throwable {
    UpdateRequest request = new UpdateRequest();
    arguments = new Object[] {
        request.deleteByQuery("id:[2 TO 5]"),
        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_QUERY", "[id:[2 TO 5]]");
}
 
Example 2
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 3
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 4
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 5
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 6
Source File: ReplicationFactorTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected void doDBQWithRetry(int expectedRf, int retries, String msg, int docsToAdd) throws Exception {
  Set<Integer> docIds = getSomeIds(docsToAdd);
  addDocs(docIds, expectedRf, retries);
  UpdateRequest req = new UpdateRequest();
  req.deleteByQuery("id:(" + StringUtils.join(docIds, " OR ") + ")");
  boolean minRfExplicit = maybeAddMinRfExplicitly(expectedRf, req);
  doDelete(req, msg, expectedRf, retries, minRfExplicit);
}
 
Example 7
Source File: SolrIOTestUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Clear given collection. */
static void clearCollection(String collection, AuthorizedSolrClient client) throws IOException {
  try {
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.setAction(UpdateRequest.ACTION.COMMIT, true, true);
    updateRequest.deleteByQuery("*:*");
    client.process(collection, updateRequest);
  } catch (SolrServerException e) {
    throw new IOException(e);
  }
}
 
Example 8
Source File: TestInPlaceUpdatesDistrib.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
UpdateRequest simulatedDeleteRequest(String query, long version) throws SolrServerException, IOException {
  String baseUrl = getBaseUrl((HttpSolrClient)LEADER);

  UpdateRequest ur = new UpdateRequest();
  ur.deleteByQuery(query);
  ur.setParam("_version_", ""+version);
  ur.setParam("update.distrib", "FROMLEADER");
  ur.setParam("distrib.from", baseUrl + DEFAULT_COLLECTION + "/");
  return ur;
}
 
Example 9
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 10
Source File: SolrCmdDistributorTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void getRfFromResponseShouldNotCloseTheInputStream() {
  UpdateRequest dbqReq = new UpdateRequest();
  dbqReq.deleteByQuery("*:*");
  SolrCmdDistributor.Req req = new SolrCmdDistributor.Req(null, new StdNode(null, "collection1", "shard1", 1), dbqReq, true);
  AtomicBoolean isClosed = new AtomicBoolean(false);
  ByteArrayInputStream is = new ByteArrayInputStream(new byte[100]) {
    @Override
    public void close() throws IOException {
      isClosed.set(true);
      super.close();
    }
  };
  req.trackRequestResult(null, is, true);
  assertFalse("Underlying stream should not be closed!", isClosed.get());
}
 
Example 11
Source File: SolrCmdDistributorTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void testReqShouldRetryDBQ() {
  Error err = getError(new SocketException()); 
  UpdateRequest dbqReq = new UpdateRequest();
  dbqReq.deleteByQuery("*:*");
  SolrCmdDistributor.Req req = new SolrCmdDistributor.Req(null, new StdNode(null, "collection1", "shard1", 1), dbqReq, true);
  assertFalse(req.shouldRetry(err));
}
 
Example 12
Source File: BaseDistributedSearchTestCase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected UpdateResponse delQ(SolrClient client, SolrParams params, String... queries) throws IOException, SolrServerException {
  UpdateRequest ureq = new UpdateRequest();
  ureq.setParams(new ModifiableSolrParams(params));
  for (String q: queries) {
    ureq.deleteByQuery(q);
  }
  return ureq.process(client);
}
 
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 regularDeleteByQueryRequest(String q) throws SolrServerException, IOException {
  UpdateRequest ur = new UpdateRequest();
  ur.deleteByQuery(q);
  return ur;
}
 
Example 15
Source File: CrossCollectionJoinQueryTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static void clearCollection(String collection) throws IOException, SolrServerException {
  UpdateRequest update = new UpdateRequest();
  update.deleteByQuery("*:*");
  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: ShardRoutingTest.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 18
Source File: TestSolrProperties.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testProperties() throws Exception {

  UpdateRequest up = new UpdateRequest();
  up.setAction(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("core0", "yup stopfra stopfrb stopena stopenb");

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

  SolrTestCaseJ4.ignoreException("unknown field");

  // You can't add it to core1
  expectThrows(Exception.class, () -> up.process(getSolrCore1()));

  // Add to core1
  doc.setField("id", "BBB");
  doc.setField("core1", "yup stopfra stopfrb stopena stopenb");
  doc.removeField("core0");
  up.add(doc);
  up.process(getSolrCore1());

  // You can't add it to core1
  SolrTestCaseJ4.ignoreException("core0");
  expectThrows(Exception.class, () -> up.process(getSolrCore0()));
  SolrTestCaseJ4.resetExceptionIgnores();

  // 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());

  // Now test Changing the default core
  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());

  // Now test reloading it should have a newer open time
  String name = "core0";
  SolrClient coreadmin = getSolrAdmin();
  CoreAdminResponse mcr = CoreAdminRequest.getStatus(name, coreadmin);
  long before = mcr.getStartTime(name).getTime();
  CoreAdminRequest.reloadCore(name, coreadmin);

  mcr = CoreAdminRequest.getStatus(name, coreadmin);
  long after = mcr.getStartTime(name).getTime();
  assertTrue("should have more recent time: " + after + "," + before, after > before);

}
 
Example 19
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 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);
}