Search in sources :

Example 11 with DeleteUpdateCommand

use of org.apache.solr.update.DeleteUpdateCommand in project lucene-solr by apache.

the class SolrWriter method deleteByQuery.

@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);
    }
}
Also used : DeleteUpdateCommand(org.apache.solr.update.DeleteUpdateCommand)

Example 12 with DeleteUpdateCommand

use of org.apache.solr.update.DeleteUpdateCommand in project lucene-solr by apache.

the class JavabinLoader method delete.

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);
        }
    }
}
Also used : Entry(java.util.Map.Entry) SolrParams(org.apache.solr.common.params.SolrParams) DeleteUpdateCommand(org.apache.solr.update.DeleteUpdateCommand) Map(java.util.Map)

Example 13 with DeleteUpdateCommand

use of org.apache.solr.update.DeleteUpdateCommand in project lucene-solr by apache.

the class XMLLoader method processDelete.

/**
   * @since solr 1.3
   */
void processDelete(SolrQueryRequest req, UpdateRequestProcessor processor, XMLStreamReader parser) throws XMLStreamException, IOException {
    // Parse the command
    DeleteUpdateCommand deleteCmd = new DeleteUpdateCommand(req);
    // First look for commitWithin parameter on the request, will be overwritten for individual <delete>'s
    SolrParams params = req.getParams();
    deleteCmd.commitWithin = params.getInt(UpdateParams.COMMIT_WITHIN, -1);
    for (int i = 0; i < parser.getAttributeCount(); i++) {
        String attrName = parser.getAttributeLocalName(i);
        String attrVal = parser.getAttributeValue(i);
        if ("fromPending".equals(attrName)) {
        // deprecated
        } else if ("fromCommitted".equals(attrName)) {
        // deprecated
        } else if (UpdateRequestHandler.COMMIT_WITHIN.equals(attrName)) {
            deleteCmd.commitWithin = Integer.parseInt(attrVal);
        } else {
            log.warn("XML element <delete> has invalid XML attr: " + attrName);
        }
    }
    StringBuilder text = new StringBuilder();
    while (true) {
        int event = parser.next();
        switch(event) {
            case XMLStreamConstants.START_ELEMENT:
                String mode = parser.getLocalName();
                if (!(ID.equals(mode) || "query".equals(mode))) {
                    String msg = "XML element <delete> has invalid XML child element: " + mode;
                    log.warn(msg);
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, msg);
                }
                text.setLength(0);
                if (ID.equals(mode)) {
                    for (int i = 0; i < parser.getAttributeCount(); i++) {
                        String attrName = parser.getAttributeLocalName(i);
                        String attrVal = parser.getAttributeValue(i);
                        if (UpdateRequestHandler.VERSION.equals(attrName)) {
                            deleteCmd.setVersion(Long.parseLong(attrVal));
                        }
                        if (ShardParams._ROUTE_.equals(attrName)) {
                            deleteCmd.setRoute(attrVal);
                        }
                    }
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                String currTag = parser.getLocalName();
                if (ID.equals(currTag)) {
                    deleteCmd.setId(text.toString());
                } else if ("query".equals(currTag)) {
                    deleteCmd.setQuery(text.toString());
                } else if ("delete".equals(currTag)) {
                    return;
                } else {
                    String msg = "XML element <delete> has invalid XML (closing) child element: " + currTag;
                    log.warn(msg);
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, msg);
                }
                processor.processDelete(deleteCmd);
                deleteCmd.clear();
                break;
            // Add everything to the text
            case XMLStreamConstants.SPACE:
            case XMLStreamConstants.CDATA:
            case XMLStreamConstants.CHARACTERS:
                text.append(parser.getText());
                break;
        }
    }
}
Also used : SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) DeleteUpdateCommand(org.apache.solr.update.DeleteUpdateCommand) SolrException(org.apache.solr.common.SolrException)

Example 14 with DeleteUpdateCommand

use of org.apache.solr.update.DeleteUpdateCommand in project lucene-solr by apache.

the class JsonLoaderTest method testDeleteSyntax.

// The delete syntax was both extended for simplification in 4.0
@Test
public void testDeleteSyntax() throws Exception {
    String str = "{'delete':10" + "\n ,'delete':'20'" + "\n ,'delete':['30','40']" + "\n ,'delete':{'id':50, '_version_':12345}" + "\n ,'delete':[{'id':60, '_version_':67890}, {'id':70, '_version_':77777}, {'query':'id:80', '_version_':88888}]" + "\n ,'delete':{'id':90, '_route_':'shard1', '_version_':88888}" + "\n}\n";
    str = str.replace('\'', '"');
    SolrQueryRequest req = req();
    SolrQueryResponse rsp = new SolrQueryResponse();
    BufferingRequestProcessor p = new BufferingRequestProcessor(null);
    JsonLoader loader = new JsonLoader();
    loader.load(req, rsp, new ContentStreamBase.StringStream(str), p);
    // DELETE COMMANDS
    assertEquals(9, p.deleteCommands.size());
    DeleteUpdateCommand delete = p.deleteCommands.get(0);
    assertEquals(delete.id, "10");
    assertEquals(delete.query, null);
    assertEquals(delete.commitWithin, -1);
    delete = p.deleteCommands.get(1);
    assertEquals(delete.id, "20");
    assertEquals(delete.query, null);
    assertEquals(delete.commitWithin, -1);
    delete = p.deleteCommands.get(2);
    assertEquals(delete.id, "30");
    assertEquals(delete.query, null);
    assertEquals(delete.commitWithin, -1);
    delete = p.deleteCommands.get(3);
    assertEquals(delete.id, "40");
    assertEquals(delete.query, null);
    assertEquals(delete.commitWithin, -1);
    delete = p.deleteCommands.get(4);
    assertEquals(delete.id, "50");
    assertEquals(delete.query, null);
    assertEquals(delete.getVersion(), 12345L);
    delete = p.deleteCommands.get(5);
    assertEquals(delete.id, "60");
    assertEquals(delete.query, null);
    assertEquals(delete.getVersion(), 67890L);
    delete = p.deleteCommands.get(6);
    assertEquals(delete.id, "70");
    assertEquals(delete.query, null);
    assertEquals(delete.getVersion(), 77777L);
    delete = p.deleteCommands.get(7);
    assertEquals(delete.id, null);
    assertEquals(delete.query, "id:80");
    assertEquals(delete.getVersion(), 88888L);
    delete = p.deleteCommands.get(8);
    assertEquals(delete.id, "90");
    assertEquals(delete.query, null);
    assertEquals(delete.getRoute(), "shard1");
    assertEquals(delete.getVersion(), 88888L);
    req.close();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) BufferingRequestProcessor(org.apache.solr.update.processor.BufferingRequestProcessor) JsonLoader(org.apache.solr.handler.loader.JsonLoader) DeleteUpdateCommand(org.apache.solr.update.DeleteUpdateCommand) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase) Test(org.junit.Test)

Example 15 with DeleteUpdateCommand

use of org.apache.solr.update.DeleteUpdateCommand in project lucene-solr by apache.

the class DistributedUpdateProcessor method waitForDependentUpdates.

/**
   * This method checks the update/transaction logs and index to find out if the update ("previous update") that the current update
   * depends on (in the case that this current update is an in-place update) has already been completed. If not,
   * this method will wait for the missing update until it has arrived. If it doesn't arrive within a timeout threshold,
   * then this actively fetches from the leader.
   * 
   * @return -1 if the current in-place should be dropped, or last found version if previous update has been indexed.
   */
private long waitForDependentUpdates(AddUpdateCommand cmd, long versionOnUpdate, boolean isReplayOrPeersync, VersionBucket bucket) throws IOException {
    long lastFoundVersion = 0;
    TimeOut waitTimeout = new TimeOut(5, TimeUnit.SECONDS);
    vinfo.lockForUpdate();
    try {
        synchronized (bucket) {
            Long lookedUpVersion = vinfo.lookupVersion(cmd.getIndexedId());
            lastFoundVersion = lookedUpVersion == null ? 0L : lookedUpVersion;
            if (Math.abs(lastFoundVersion) < cmd.prevVersion) {
                log.debug("Re-ordered inplace update. version={}, prevVersion={}, lastVersion={}, replayOrPeerSync={}, id={}", (cmd.getVersion() == 0 ? versionOnUpdate : cmd.getVersion()), cmd.prevVersion, lastFoundVersion, isReplayOrPeersync, cmd.getPrintableId());
            }
            while (Math.abs(lastFoundVersion) < cmd.prevVersion && !waitTimeout.hasTimedOut()) {
                try {
                    long timeLeft = waitTimeout.timeLeft(TimeUnit.MILLISECONDS);
                    if (timeLeft > 0) {
                        // wait(0) waits forever until notified, but we don't want that.
                        bucket.wait(timeLeft);
                    }
                } catch (InterruptedException ie) {
                    throw new RuntimeException(ie);
                }
                lookedUpVersion = vinfo.lookupVersion(cmd.getIndexedId());
                lastFoundVersion = lookedUpVersion == null ? 0L : lookedUpVersion;
            }
        }
    } finally {
        vinfo.unlockForUpdate();
    }
    if (Math.abs(lastFoundVersion) > cmd.prevVersion) {
        // we can drop the current update.
        if (log.isDebugEnabled()) {
            log.debug("Update was applied on version: {}, but last version I have is: {}" + ". Current update should be dropped. id={}", cmd.prevVersion, lastFoundVersion, cmd.getPrintableId());
        }
        return -1;
    } else if (Math.abs(lastFoundVersion) == cmd.prevVersion) {
        assert 0 < lastFoundVersion : "prevVersion " + cmd.prevVersion + " found but is a delete!";
        if (log.isDebugEnabled()) {
            log.debug("Dependent update found. id={}", cmd.getPrintableId());
        }
        return lastFoundVersion;
    }
    // We have waited enough, but dependent update didn't arrive. Its time to actively fetch it from leader
    log.info("Missing update, on which current in-place update depends on, hasn't arrived. id={}, looking for version={}, last found version={}", cmd.getPrintableId(), cmd.prevVersion, lastFoundVersion);
    UpdateCommand missingUpdate = fetchFullUpdateFromLeader(cmd, versionOnUpdate);
    if (missingUpdate instanceof DeleteUpdateCommand) {
        log.info("Tried to fetch document {} from the leader, but the leader says document has been deleted. " + "Deleting the document here and skipping this update: Last found version: {}, was looking for: {}", cmd.getPrintableId(), lastFoundVersion, cmd.prevVersion);
        versionDelete((DeleteUpdateCommand) missingUpdate);
        return -1;
    } else {
        assert missingUpdate instanceof AddUpdateCommand;
        log.debug("Fetched the document: {}", ((AddUpdateCommand) missingUpdate).getSolrInputDocument());
        versionAdd((AddUpdateCommand) missingUpdate);
        log.info("Added the fetched document, id=" + ((AddUpdateCommand) missingUpdate).getPrintableId() + ", version=" + missingUpdate.getVersion());
    }
    return missingUpdate.getVersion();
}
Also used : TimeOut(org.apache.solr.util.TimeOut) DeleteUpdateCommand(org.apache.solr.update.DeleteUpdateCommand) CommitUpdateCommand(org.apache.solr.update.CommitUpdateCommand) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) UpdateCommand(org.apache.solr.update.UpdateCommand) DeleteUpdateCommand(org.apache.solr.update.DeleteUpdateCommand) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand)

Aggregations

DeleteUpdateCommand (org.apache.solr.update.DeleteUpdateCommand)18 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)7 SolrInputDocument (org.apache.solr.common.SolrInputDocument)5 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)5 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)5 CommitUpdateCommand (org.apache.solr.update.CommitUpdateCommand)4 SolrException (org.apache.solr.common.SolrException)3 SolrInputField (org.apache.solr.common.SolrInputField)3 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)3 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)3 UpdateCommand (org.apache.solr.update.UpdateCommand)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 SolrParams (org.apache.solr.common.params.SolrParams)2 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)2 NamedList (org.apache.solr.common.util.NamedList)2 JsonLoader (org.apache.solr.handler.loader.JsonLoader)2 BufferingRequestProcessor (org.apache.solr.update.processor.BufferingRequestProcessor)2 UpdateRequestProcessor (org.apache.solr.update.processor.UpdateRequestProcessor)2 IntArrayList (com.carrotsearch.hppc.IntArrayList)1