org.apache.solr.update.DeleteUpdateCommand Java Examples

The following examples show how to use org.apache.solr.update.DeleteUpdateCommand. 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: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void deleteByQuery(String query) throws IOException
{
    UpdateRequestProcessor processor = null;
    try (SolrQueryRequest request = newSolrQueryRequest())
    {
        processor = this.core.getUpdateProcessingChain(null).createProcessor(request, newSolrQueryResponse());
        DeleteUpdateCommand delDocCmd = new DeleteUpdateCommand(request);
        delDocCmd.setQuery(query);
        processor.processDelete(delDocCmd);
    }
    finally
    {
        if (processor != null)
        {
            processor.finish();
        }
    }
}
 
Example #2
Source File: UpdateProcessorTestBase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected void processDeleteById(final String chain, String id) throws IOException {
  SolrCore core = h.getCore();
  UpdateRequestProcessorChain pc = core.getUpdateProcessingChain(chain);
  assertNotNull("No Chain named: " + chain, pc);

  SolrQueryResponse rsp = new SolrQueryResponse();

  SolrQueryRequest req = new LocalSolrQueryRequest(core, new ModifiableSolrParams());

  DeleteUpdateCommand cmd = new DeleteUpdateCommand(req);
  cmd.setId(id);
  UpdateRequestProcessor processor = pc.createProcessor(req, rsp);
  try {
    processor.processDelete(cmd);
  } finally {
    req.close();
  }
}
 
Example #3
Source File: DistributedUpdateProcessor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected void versionDeleteByQuery(DeleteUpdateCommand cmd) throws IOException {
  // Find the version
  long versionOnUpdate = findVersionOnUpdate(cmd);

  boolean isReplayOrPeersync = (cmd.getFlags() & (UpdateCommand.REPLAY | UpdateCommand.PEER_SYNC)) != 0;
  boolean leaderLogic = isLeader && !isReplayOrPeersync;

  if (!leaderLogic && versionOnUpdate == 0) {
    throw new SolrException(ErrorCode.BAD_REQUEST, "missing _version_ on update from leader");
  }

  vinfo.blockUpdates();
  try {

    doLocalDeleteByQuery(cmd, versionOnUpdate, isReplayOrPeersync);

    // since we don't know which documents were deleted, the easiest thing to do is to invalidate
    // all real-time caches (i.e. UpdateLog) which involves also getting a new version of the IndexReader
    // (so cache misses will see up-to-date data)

  } finally {
    vinfo.unblockUpdates();
  }
}
 
Example #4
Source File: DistributedUpdateProcessor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * should be called by implementing class after setting up replicas
 * @param cmd delete command
 * @param replicas list of Nodes replicas to pass to {@link DistributedUpdateProcessor#doDistribDeleteByQuery(DeleteUpdateCommand, List, DocCollection)}
 * @param coll the collection in zookeeper {@link org.apache.solr.common.cloud.DocCollection},
 *             passed to {@link DistributedUpdateProcessor#doDistribDeleteByQuery(DeleteUpdateCommand, List, DocCollection)}
 */
protected void doDeleteByQuery(DeleteUpdateCommand cmd, List<SolrCmdDistributor.Node> replicas, DocCollection coll) throws IOException {
  if (vinfo == null) {
    super.processDelete(cmd);
    return;
  }

  // at this point, there is an update we need to try and apply.
  // we may or may not be the leader.

  versionDeleteByQuery(cmd);

  doDistribDeleteByQuery(cmd, replicas, coll);


  if (returnVersions && rsp != null) {
    if (deleteByQueryResponse == null) {
      deleteByQueryResponse = new NamedList<>(1);
      rsp.add("deleteByQuery", deleteByQueryResponse);
    }
    deleteByQueryResponse.add(cmd.getQuery(), cmd.getVersion());
  }
}
 
Example #5
Source File: DistributedUpdateProcessorTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testVersionDelete() throws IOException {
  SolrQueryRequest req = new LocalSolrQueryRequest(h.getCore(), new ModifiableSolrParams());

  int threads = 5;
  Function<DistributedUpdateProcessor,Boolean> versionDeleteFunc = (DistributedUpdateProcessor process) -> {
    try {
      DeleteUpdateCommand cmd = new DeleteUpdateCommand(req);
      cmd.id = "1";
      return process.versionDelete(cmd);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  };

  int succeeded = runCommands(threads, 1000, req, versionDeleteFunc);
  // only one should succeed
  assertThat(succeeded, is(1));

  succeeded = runCommands(threads, -1, req, versionDeleteFunc);
  // all should succeed
  assertThat(succeeded, is(threads));
}
 
Example #6
Source File: CdcrUpdateProcessor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean versionDelete(DeleteUpdateCommand cmd) throws IOException {
  /*
  temporarily set the PEER_SYNC flag so that DistributedUpdateProcessor.deleteAdd doesn't execute leader logic
  but the else part of that if. That way version remains preserved.

  we cannot set the flag for the whole processDelete method because DistributedUpdateProcessor.setupRequest() would set
  isLeader to false which wouldn't work
   */
  if (cmd.getReq().getParams().get(CDCR_UPDATE) != null) {
    cmd.setFlags(cmd.getFlags() | UpdateCommand.PEER_SYNC); // we need super.versionAdd() to set leaderLogic to false
  }

  boolean result = super.versionDelete(cmd);

  // unset the flag to avoid unintended consequences down the chain
  if (cmd.getReq().getParams().get(CDCR_UPDATE) != null) {
    cmd.setFlags(cmd.getFlags() & ~UpdateCommand.PEER_SYNC);
  }

  return result;
}
 
Example #7
Source File: CdcrUpdateProcessor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
protected void versionDeleteByQuery(DeleteUpdateCommand cmd) throws IOException {
  /*
  temporarily set the PEER_SYNC flag so that DistributedUpdateProcessor.versionDeleteByQuery doesn't execute leader logic
  That way version remains preserved.

   */
  if (cmd.getReq().getParams().get(CDCR_UPDATE) != null) {
    cmd.setFlags(cmd.getFlags() | UpdateCommand.PEER_SYNC); // we need super.versionDeleteByQuery() to set leaderLogic to false
  }

  super.versionDeleteByQuery(cmd);

  // unset the flag to avoid unintended consequences down the chain
  if (cmd.getReq().getParams().get(CDCR_UPDATE) != null) {
    cmd.setFlags(cmd.getFlags() & ~UpdateCommand.PEER_SYNC);
  }
}
 
Example #8
Source File: JavabinLoader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void delete(SolrQueryRequest req, UpdateRequest update, UpdateRequestProcessor processor) throws IOException {
  SolrParams params = update.getParams();
  DeleteUpdateCommand delcmd = new DeleteUpdateCommand(req);
  if(params != null) {
    delcmd.commitWithin = params.getInt(UpdateParams.COMMIT_WITHIN, -1);
  }
  
  if(update.getDeleteByIdMap() != null) {
    Set<Entry<String,Map<String,Object>>> entries = update.getDeleteByIdMap().entrySet();
    for (Entry<String,Map<String,Object>> e : entries) {
      delcmd.id = e.getKey();
      Map<String,Object> map = e.getValue();
      if (map != null) {
        Long version = (Long) map.get("ver");
        if (version != null) {
          delcmd.setVersion(version);
        }
      }
      if (map != null) {
        String route = (String) map.get(ShardParams._ROUTE_);
        if (route != null) {
          delcmd.setRoute(route);
        }
      }
      processor.processDelete(delcmd);
      delcmd.clear();
    }
  }
  
  if(update.getDeleteQuery() != null) {
    for (String s : update.getDeleteQuery()) {
      delcmd.query = s;
      processor.processDelete(delcmd);
    }
  }
}
 
Example #9
Source File: DistributedZkUpdateProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void processDelete(DeleteUpdateCommand cmd) throws IOException {
  clusterState = zkController.getClusterState();

  if (isReadOnly()) {
    throw new SolrException(ErrorCode.FORBIDDEN, "Collection " + collection + " is read-only.");
  }

  super.processDelete(cmd);
}
 
Example #10
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void deleteErrorNode(UpdateRequestProcessor processor, SolrQueryRequest request, Node node) throws IOException
{
    String errorDocId = PREFIX_ERROR + node.getId();
    if (getDocListSize(FIELD_SOLR4_ID + ":" + errorDocId) > 0)
    {
        DeleteUpdateCommand delErrorDocCmd = new DeleteUpdateCommand(request);
        delErrorDocCmd.setId(errorDocId);
        processor.processDelete(delErrorDocCmd);
    }
}
 
Example #11
Source File: DistributedZkUpdateProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
void setupRequest(UpdateCommand cmd) {
  updateCommand = cmd;
  zkCheck();
  if (cmd instanceof AddUpdateCommand) {
    AddUpdateCommand acmd = (AddUpdateCommand)cmd;
    nodes = setupRequest(acmd.getRootIdUsingRouteParam(), acmd.getSolrInputDocument());
  } else if (cmd instanceof DeleteUpdateCommand) {
    DeleteUpdateCommand dcmd = (DeleteUpdateCommand)cmd;
    nodes = setupRequest(dcmd.getId(), null);
  }
}
 
Example #12
Source File: DocBasedVersionConstraintsProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private String[] getDeleteParamValuesFromRequest(DeleteUpdateCommand cmd) {
  SolrParams params = cmd.getReq().getParams();
  String[] returnArr = new String[deleteVersionParamNames.length];
  for (int i = 0; i < deleteVersionParamNames.length; i++) {
    String deleteVersionParamName = deleteVersionParamNames[i];
    String deleteParamValue = params.get(deleteVersionParamName);
    returnArr[i] = deleteParamValue;
  }
  return returnArr;
}
 
Example #13
Source File: TolerantUpdateProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void processDelete(DeleteUpdateCommand cmd) throws IOException {
  
  try {

    super.processDelete(cmd);

  } catch (Throwable t) {
    firstErrTracker.caught(t);

    ToleratedUpdateError err = new ToleratedUpdateError(cmd.isDeleteById() ? CmdType.DELID : CmdType.DELQ,
                                                        cmd.isDeleteById() ? cmd.id : cmd.query,
                                                        t.getMessage());
    knownErrors.add(err);

    // NOTE: we're not using this to dedup before adding to knownErrors.
    // if we're lucky enough to get an immediate local failure (ie: we're a leader, or some other processor
    // failed) then recording the multiple failures is a good thing -- helps us with an accurate fail
    // fast if we exceed maxErrors
    if (CmdType.DELQ.equals(err.getType())) {
      knownDBQErrors.add(err);
    }

    if (knownErrors.size() > maxErrors) {
      firstErrTracker.throwFirst();
    }
  }
}
 
Example #14
Source File: JsonLoader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void handleSingleDelete(int ev) throws IOException {
  if (ev == JSONParser.OBJECT_START) {
    handleDeleteMap(ev);
  } else {
    DeleteUpdateCommand cmd = new DeleteUpdateCommand(req);
    cmd.commitWithin = commitWithin;
    String id = getString(ev);
    cmd.setId(id);
    processor.processDelete(cmd);
  }
}
 
Example #15
Source File: DistributedZkUpdateProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected void doDeleteById(DeleteUpdateCommand cmd) throws IOException {
  setupRequest(cmd);

  // check if client has requested minimum replication factor information. will set replicationTracker to null if
  // we aren't the leader or subShardLeader
  checkReplicationTracker(cmd);

  super.doDeleteById(cmd);
}
 
Example #16
Source File: XmlUpdateRequestHandlerTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void expectDelete(String id, String query, int commitWithin, long version, String route) {
  DeleteUpdateCommand cmd = new DeleteUpdateCommand(null);
  cmd.id = id;
  cmd.query = query;
  cmd.commitWithin = commitWithin;
  if (version!=0)
    cmd.setVersion(version);
  if (route!=null)
    cmd.setRoute(route);
  deleteCommands.add(cmd);
}
 
Example #17
Source File: XmlUpdateRequestHandlerTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void processDelete(DeleteUpdateCommand cmd) throws IOException {
  DeleteUpdateCommand expected = deleteCommands.poll();
  assertNotNull("Unexpected delete command: [" + cmd + "]", expected);
  assertTrue("Expected [" + expected + "] but found [" + cmd + "]",
      Objects.equals(expected.id, cmd.id) &&
      Objects.equals(expected.query, cmd.query) &&
      expected.commitWithin==cmd.commitWithin && 
      Objects.equals(expected.getRoute(), cmd.getRoute()));
}
 
Example #18
Source File: SolrUpdateService.java    From chronix.server with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes documents identified by the given documents.
 *
 * @param docs the documents
 * @throws IOException iff something goes wrong
 */
public void delete(Collection<Document> docs) throws IOException {
    DeleteUpdateCommand cmd = new DeleteUpdateCommand(req);
    cmd.commitWithin = COMMIT_WITHIN;
    cmd.setFlags(DeleteUpdateCommand.BUFFERING);
    cmd.setQuery("{!terms f=" + ID + "}" + docs.stream().map(it -> it.get(ID)).collect(joining(",")));
    updateProcessor.processDelete(cmd);
}
 
Example #19
Source File: LocalGraph.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
@Override
public void performDelete(final Triple triple) {
	final DeleteUpdateCommand deleteCommand = new DeleteUpdateCommand(request);
	deleteCommand.query = deleteQuery(triple);
	try {
		updateProcessor.processDelete(deleteCommand);
	} catch (final Exception exception) {
		LOGGER.error(MessageCatalog._00113_NWS_FAILURE, exception);
		throw new DeleteDeniedException(exception.getMessage(), triple);
	}		
}
 
Example #20
Source File: UpdateIndexAuthorizationProcessor.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
@Override
public void processDelete(DeleteUpdateCommand cmd) throws IOException {
  String operation = cmd.name();
  if (cmd.isDeleteById()) {
    operation += "ById";
  } else {
    operation += "ByQuery";
  }
  authorizeCollectionAction(operation);
  super.processDelete(cmd);
}
 
Example #21
Source File: UpdateIndexAuthorizationProcessorTest.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
private void verifyAuthorized(String collection, String user) throws Exception {
  SolrQueryRequestBase req = new SolrQueryRequestBase(core, new MapSolrParams(new HashMap())) {};
  getProcessor(collection, user).processAdd(new AddUpdateCommand(req));
  getProcessor(collection, user).processDelete(new DeleteUpdateCommand(req));
  DeleteUpdateCommand deleteByQueryCommand = new DeleteUpdateCommand(req);
  deleteByQueryCommand.setQuery("*:*");
  getProcessor(collection, user).processDelete(deleteByQueryCommand);
  getProcessor(collection, user).processMergeIndexes(new MergeIndexesCommand(null, req));
  getProcessor(collection, user).processCommit(new CommitUpdateCommand(req, false));
  getProcessor(collection, user).processRollback(new RollbackUpdateCommand(req));
  getProcessor(collection, user).finish();
}
 
Example #22
Source File: DistributedUpdateProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void doLocalDeleteByQuery(DeleteUpdateCommand cmd, long versionOnUpdate, boolean isReplayOrPeersync) throws IOException {
  if (versionsStored) {
    final boolean leaderLogic = isLeader & !isReplayOrPeersync;
    if (leaderLogic) {
      long version = vinfo.getNewClock();
      cmd.setVersion(-version);
      // TODO update versions in all buckets

      doLocalDelete(cmd);

    } else {
      cmd.setVersion(-versionOnUpdate);

      if (ulog.getState() != UpdateLog.State.ACTIVE && isReplayOrPeersync == false) {
        // we're not in an active state, and this update isn't from a replay, so buffer it.
        cmd.setFlags(cmd.getFlags() | UpdateCommand.BUFFERING);
        ulog.deleteByQuery(cmd);
        return;
      }

      if (!isSubShardLeader && replicaType == Replica.Type.TLOG && (cmd.getFlags() & UpdateCommand.REPLAY) == 0) {
        // TLOG replica not leader, don't write the DBQ to IW
        cmd.setFlags(cmd.getFlags() | UpdateCommand.IGNORE_INDEXWRITER);
      }
      doLocalDelete(cmd);
    }
  }
}
 
Example #23
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void deleteNode(UpdateRequestProcessor processor, SolrQueryRequest request, long dbid) throws IOException
{
    if (getDocListSize(FIELD_DBID + ":" + dbid) > 0)
    {
        DeleteUpdateCommand delDocCmd = new DeleteUpdateCommand(request);
        delDocCmd.setQuery(FIELD_DBID + ":" + dbid);
        processor.processDelete(delDocCmd);
    }
}
 
Example #24
Source File: SolrWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void deleteDoc(Object id) {
  try {
    log.info("Deleting document: {}", id);
    DeleteUpdateCommand delCmd = new DeleteUpdateCommand(req);
    delCmd.setId(id.toString());
    processor.processDelete(delCmd);
  } catch (IOException e) {
    log.error("Exception while deleteing: {}", id, e);
  }
}
 
Example #25
Source File: SolrWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void deleteByQuery(String query) {
  try {
    log.info("Deleting documents from Solr with query: {}", query);
    DeleteUpdateCommand delCmd = new DeleteUpdateCommand(req);
    delCmd.query = query;
    processor.processDelete(delCmd);
  } catch (IOException e) {
    log.error("Exception while deleting by query: {}", query, e);
  }
}
 
Example #26
Source File: SolrWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void doDeleteAll() {
  try {
    DeleteUpdateCommand deleteCommand = new DeleteUpdateCommand(req);
    deleteCommand.query = "*:*";
    processor.processDelete(deleteCommand);
  } catch (IOException e) {
    throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
            "Exception in full dump while deleting all documents.", e);
  }
}
 
Example #27
Source File: DistributedUpdateProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void processDelete(DeleteUpdateCommand cmd) throws IOException {
  
  assert TestInjection.injectFailUpdateRequests();

  updateCommand = cmd;

  if (!cmd.isDeleteById()) {
    doDeleteByQuery(cmd);
  } else {
    doDeleteById(cmd);
  }
}
 
Example #28
Source File: RoutedAliasUpdateProcessor.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void processDelete(DeleteUpdateCommand cmd) throws IOException {
  final List<SolrCmdDistributor.Node> nodes = lookupShardLeadersOfCollections();
  cmdDistrib.distribDelete(cmd, nodes, new ModifiableSolrParams(outParamsToLeader));
}
 
Example #29
Source File: DistributedUpdateProcessor.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
protected boolean versionDelete(DeleteUpdateCommand cmd) throws IOException {

    BytesRef idBytes = cmd.getIndexedId();

    if (vinfo == null || idBytes == null) {
      super.processDelete(cmd);
      return false;
    }

    // This is only the hash for the bucket, and must be based only on the uniqueKey (i.e. do not use a pluggable hash
    // here)
    int bucketHash = bucketHash(idBytes);

    // at this point, there is an update we need to try and apply.
    // we may or may not be the leader.

    // Find the version
    long versionOnUpdate = cmd.getVersion();
    if (versionOnUpdate == 0) {
      String versionOnUpdateS = req.getParams().get(CommonParams.VERSION_FIELD);
      versionOnUpdate = versionOnUpdateS == null ? 0 : Long.parseLong(versionOnUpdateS);
    }
    long signedVersionOnUpdate = versionOnUpdate;
    versionOnUpdate = Math.abs(versionOnUpdate); // normalize to positive version

    boolean isReplayOrPeersync = (cmd.getFlags() & (UpdateCommand.REPLAY | UpdateCommand.PEER_SYNC)) != 0;
    boolean leaderLogic = isLeader && !isReplayOrPeersync;
    boolean forwardedFromCollection = cmd.getReq().getParams().get(DISTRIB_FROM_COLLECTION) != null;

    if (!leaderLogic && versionOnUpdate == 0) {
      throw new SolrException(ErrorCode.BAD_REQUEST, "missing _version_ on update from leader");
    }

    VersionBucket bucket = vinfo.bucket(bucketHash);

    vinfo.lockForUpdate();
    try {
      long finalVersionOnUpdate = versionOnUpdate;
      return bucket.runWithLock(vinfo.getVersionBucketLockTimeoutMs(), () -> doVersionDelete(cmd, finalVersionOnUpdate, signedVersionOnUpdate, isReplayOrPeersync, leaderLogic,
          forwardedFromCollection, bucket));
    } finally {
      vinfo.unlockForUpdate();
    }
  }
 
Example #30
Source File: BufferingRequestProcessor.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void processDelete(DeleteUpdateCommand cmd) throws IOException {
  deleteCommands.add( cmd );
}