Example usage for org.apache.solr.client.solrj.request UpdateRequest deleteByQuery

List of usage examples for org.apache.solr.client.solrj.request UpdateRequest deleteByQuery

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.request UpdateRequest deleteByQuery.

Prototype

public UpdateRequest deleteByQuery(String q) 

Source Link

Usage

From source file:at.newmedialab.lmf.search.services.indexing.SolrCoreRuntime.java

License:Apache License

/**
 * Clear the index managed by this SolrCoreRuntime.
 *//* ww  w .j a  v  a  2  s. co  m*/
public void clear() {
    serverLock.lock();
    try {
        UpdateRequest request = new UpdateRequest();
        request.deleteByQuery("id:[* TO *]");
        request.setAction(ACTION.COMMIT, true, true);

        server.request(request);
    } catch (IOException e) {
        log.error("(" + config.getName() + ") could not clear search index: an I/O Exception occurred", e);
    } catch (SolrServerException e) {
        log.error(
                "(" + config.getName()
                        + ") could not clear search index: a SOLR Exception occurred (server not available?)",
                e);
    } catch (Exception e) {
        log.error("(" + config.getName()
                + ") index could not be cleared: a runtime Exception occurred (server sending invalid response?)",
                e);
    } finally {
        serverLock.unlock();
    }

}

From source file:com.thinkaurelius.titan.diskstorage.solr.Solr5Index.java

License:Apache License

@Override
public void clearStorage() throws BackendException {
    try {// w  w w  .  j av  a2  s.c o  m
        if (mode != Mode.CLOUD)
            throw new UnsupportedOperationException("Operation only supported for SolrCloud");
        logger.debug("Clearing storage from Solr: {}", solrClient);
        ZkStateReader zkStateReader = ((CloudSolrClient) solrClient).getZkStateReader();
        zkStateReader.updateClusterState(true);
        ClusterState clusterState = zkStateReader.getClusterState();
        for (String collection : clusterState.getCollections()) {
            logger.debug("Clearing collection [{}] in Solr", collection);
            UpdateRequest deleteAll = newUpdateRequest();
            deleteAll.deleteByQuery("*:*");
            solrClient.request(deleteAll, collection);
        }

    } catch (SolrServerException e) {
        logger.error("Unable to clear storage from index due to server error on Solr.", e);
        throw new PermanentBackendException(e);
    } catch (IOException e) {
        logger.error("Unable to clear storage from index due to low-level I/O error.", e);
        throw new PermanentBackendException(e);
    } catch (Exception e) {
        logger.error("Unable to clear storage from index due to general error.", e);
        throw new PermanentBackendException(e);
    }
}

From source file:org.apache.beam.sdk.io.solr.SolrIOTestUtils.java

License:Apache License

/** Clear given collection. */
static void clearCollection(String collection, AuthorizedSolrClient client) throws IOException {
    try {//from   w  ww  . j a va 2s  .  c  om
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.setAction(UpdateRequest.ACTION.COMMIT, true, true);
        updateRequest.deleteByQuery("*:*");
        client.process(collection, updateRequest);
    } catch (SolrServerException e) {
        throw new IOException(e);
    }
}

From source file:org.codelibs.elasticsearch.solr.solr.JavaBinUpdateRequestCodec.java

License:Apache License

/**
 * Reads a NamedList from the given InputStream, converts it into a
 * SolrInputDocument and passes it to the given StreamingUpdateHandler
 *
 * @param is/*  ww  w. j  a va  2 s. c o  m*/
 *            the InputStream from which to read
 * @param handler
 *            an instance of StreamingUpdateHandler to which
 *            SolrInputDocuments are streamed one by one
 *
 * @return the UpdateRequest
 *
 * @throws IOException
 *             in case of an exception while reading from the input stream
 *             or unmarshalling
 */
public UpdateRequest unmarshal(final InputStream is, final StreamingUpdateHandler handler) throws IOException {
    final UpdateRequest updateRequest = new UpdateRequest();
    List<Object> doclist; // mocksolrplugin: changed to Object
    List<String> delById;
    List<String> delByQ;
    final NamedList[] namedList = new NamedList[1];
    final JavaBinCodec codec = new JavaBinCodec() {

        // NOTE: this only works because this is an anonymous inner class
        // which will only ever be used on a single stream -- if this class
        // is ever refactored, this will not work.
        private boolean seenOuterMostDocIterator = false;

        @Override
        public NamedList readNamedList(final DataInputInputStream dis) throws IOException {
            final int sz = readSize(dis);
            final NamedList nl = new NamedList();
            if (namedList[0] == null) {
                namedList[0] = nl;
            }
            for (int i = 0; i < sz; i++) {
                final String name = (String) readVal(dis);
                final Object val = readVal(dis);
                nl.add(name, val);
            }
            return nl;
        }

        @Override
        public List readIterator(final DataInputInputStream fis) throws IOException {

            // default behavior for reading any regular Iterator in the
            // stream
            if (seenOuterMostDocIterator) {
                return super.readIterator(fis);
            }

            // special treatment for first outermost Iterator
            // (the list of documents)
            seenOuterMostDocIterator = true;
            return readOuterMostDocIterator(fis);
        }

        private List readOuterMostDocIterator(final DataInputInputStream fis) throws IOException {
            final NamedList params = (NamedList) namedList[0].getVal(0);
            updateRequest.setParams(new ModifiableSolrParams(SolrParams.toSolrParams(params)));
            if (handler == null) {
                return super.readIterator(fis);
            }
            while (true) {
                final Object o = readVal(fis);
                if (o == END_OBJ) {
                    break;
                }
                SolrInputDocument sdoc = null;
                if (o instanceof List) {
                    sdoc = JavaBinUpdateRequestCodec.this.listToSolrInputDocument((List<NamedList>) o);
                } else if (o instanceof NamedList) {
                    final UpdateRequest req = new UpdateRequest();
                    req.setParams(new ModifiableSolrParams(SolrParams.toSolrParams((NamedList) o)));
                    handler.update(null, req);
                } else {
                    sdoc = (SolrInputDocument) o;
                }
                handler.update(sdoc, updateRequest);
            }
            return Collections.EMPTY_LIST;
        }
    };

    codec.unmarshal(is);

    // NOTE: if the update request contains only delete commands the params
    // must be loaded now
    if (updateRequest.getParams() == null) {
        final NamedList params = (NamedList) namedList[0].get("params");
        if (params != null) {
            updateRequest.setParams(new ModifiableSolrParams(SolrParams.toSolrParams(params)));
        }
    }
    delById = (List<String>) namedList[0].get("delById");
    delByQ = (List<String>) namedList[0].get("delByQ");
    doclist = (List) namedList[0].get("docs");

    if (doclist != null && !doclist.isEmpty()) {
        final List<SolrInputDocument> solrInputDocs = new ArrayList<SolrInputDocument>();
        for (final Object o : doclist) {
            if (o instanceof List) {
                solrInputDocs.add(listToSolrInputDocument((List<NamedList>) o));
            } else {
                solrInputDocs.add((SolrInputDocument) o);
            }
        }
        updateRequest.add(solrInputDocs);
    }
    if (delById != null) {
        for (final String s : delById) {
            updateRequest.deleteById(s);
        }
    }
    if (delByQ != null) {
        for (final String s : delByQ) {
            updateRequest.deleteByQuery(s);
        }
    }
    return updateRequest;

}

From source file:org.entrystore.repository.util.SolrSupport.java

License:Apache License

public void clearSolrIndex(SolrServer solrServer) {
    UpdateRequest req = new UpdateRequest();
    req.setAction(AbstractUpdateRequest.ACTION.COMMIT, false, false);
    req.deleteByQuery("*:*");
    try {/*w ww. j  a  v a2  s.  c  om*/
        req.process(solrServer);
    } catch (SolrServerException sse) {
        log.error(sse.getMessage(), sse);
    } catch (IOException ioe) {
        log.error(ioe.getMessage(), ioe);
    }
}

From source file:org.entrystore.repository.util.SolrSupport.java

License:Apache License

public void removeEntry(Entry entry, SolrServer solrServer) {
    UpdateRequest req = new UpdateRequest();
    req.setAction(AbstractUpdateRequest.ACTION.COMMIT, false, false);
    String escapedURI = StringUtils.replace(entry.getEntryURI().toString(), ":", "\\:");
    req.deleteByQuery("uri:" + escapedURI);
    try {//from   ww  w  .j ava 2 s. com
        log.info("Removing document from Solr: " + entry.getEntryURI());
        UpdateResponse res = req.process(solrServer);
        if (res.getStatus() > 0) {
            log.error("Removal request was unsuccessful with status " + res.getStatus());
        }
    } catch (SolrServerException sse) {
        log.error(sse.getMessage(), sse);
    } catch (IOException ioe) {
        log.error(ioe.getMessage(), ioe);
    }
}

From source file:org.janusgraph.diskstorage.solr.SolrIndex.java

License:Apache License

@Override
public void clearStorage() throws BackendException {
    try {// w  w  w .j  a v  a  2s  .  c o m
        if (mode != Mode.CLOUD) {
            logger.error(
                    "Operation only supported for SolrCloud. Cores must be deleted manually through the Solr API when using HTTP mode.");
            return;
        }
        logger.debug("Clearing storage from Solr: {}", solrClient);
        final ZkStateReader zkStateReader = ((CloudSolrClient) solrClient).getZkStateReader();
        zkStateReader.forciblyRefreshAllClusterStateSlow();
        final ClusterState clusterState = zkStateReader.getClusterState();
        for (final String collection : clusterState.getCollectionsMap().keySet()) {
            logger.debug("Clearing collection [{}] in Solr", collection);
            // Collection is not dropped because it may have been created externally
            final UpdateRequest deleteAll = newUpdateRequest();
            deleteAll.deleteByQuery("*:*");
            solrClient.request(deleteAll, collection);
        }

    } catch (final SolrServerException e) {
        logger.error("Unable to clear storage from index due to server error on Solr.", e);
        throw new PermanentBackendException(e);
    } catch (final IOException e) {
        logger.error("Unable to clear storage from index due to low-level I/O error.", e);
        throw new PermanentBackendException(e);
    } catch (final Exception e) {
        logger.error("Unable to clear storage from index due to general error.", e);
        throw new PermanentBackendException(e);
    }
}

From source file:org.kitesdk.morphline.solr.SolrServerDocumentLoader.java

License:Apache License

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
            }/* w  w w. j a v  a2s  .co m*/
        }
        req.setCommitWithin(-1);
        log(req.process(server));
        deletes.clear();
    }
}

From source file:org.opencommercesearch.AbstractSearchServer.java

License:Apache License

@Override
public UpdateResponse deleteByQuery(String query, String collection, Locale locale)
        throws SearchServerException {
    UpdateRequest req = new UpdateRequest();
    req.deleteByQuery(query);
    req.setParam("collection", getCollectionName(collection, locale));

    try {//  w  w w .  j a va  2s .  c om
        return req.process(getSolrServer(collection, locale));
    } catch (SolrServerException ex) {
        throw create(UPDATE_EXCEPTION, ex);
    } catch (IOException ex) {
        throw create(UPDATE_EXCEPTION, ex);
    }
}