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

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

Introduction

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

Prototype

Map deleteById

To view the source code for org.apache.solr.client.solrj.request UpdateRequest deleteById.

Click Source Link

Usage

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

License:Apache License

/**
 * Queue the deletion of a document in the Solr Core and check whether it is necessary to
 * commit.//  www  . j a  v  a  2  s.co m
 * 
 * @param docId
 */
public void queueDeletion(String docId) {
    serverLock.lock();
    try {
        UpdateRequest update = new UpdateRequest();
        update.setCommitWithin(10000);
        update.deleteById(docId);
        //update.setAction(ACTION.COMMIT, false, false);
        server.request(update);
    } catch (IOException e) {
        log.warn("I/O exception while removing SOLR document from index", e);
    } catch (SolrServerException e) {
        log.warn("server exception while removing SOLR document from index", e);
    } finally {
        serverLock.unlock();
    }

}

From source file:com.norconex.committer.solr.SolrCommitter.java

License:Apache License

@Override
protected void commitBatch(List<ICommitOperation> batch) {

    LOG.info("Sending " + batch.size() + " documents to Solr for update/deletion.");
    try {//  w w  w .  j av a 2  s .c om
        SolrServer server = solrServerFactory.createSolrServer(this);
        UpdateRequest request = new UpdateRequest();

        // Add to request any parameters provided
        for (String name : updateUrlParams.keySet()) {
            request.setParam(name, updateUrlParams.get(name));
        }

        // Add to request all operations in batch
        for (ICommitOperation op : batch) {
            if (op instanceof IAddOperation) {
                request.add(buildSolrDocument(((IAddOperation) op).getMetadata()));
            } else if (op instanceof IDeleteOperation) {
                request.deleteById(((IDeleteOperation) op).getReference());
            } else {
                throw new CommitterException("Unsupported operation:" + op);
            }
        }

        request.process(server);
        server.commit();
    } catch (Exception e) {
        throw new CommitterException("Cannot index document batch to Solr.", e);
    }
    LOG.info("Done sending documents to Solr for update/deletion.");
}

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  2s  .  c om
 *            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.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
            }/*from www .  ja va  2s.co  m*/
        }
        req.setCommitWithin(-1);
        log(req.process(server));
        deletes.clear();
    }
}

From source file:org.teiid.translator.solr.SolrUpdateExecution.java

License:Open Source License

private void performUpdate(Delete obj) throws TranslatorException {
    Table table = obj.getTable().getMetadataObject();
    KeyRecord pk = table.getPrimaryKey();
    final String id = getRecordName(pk.getColumns().get(0));

    if (obj.getParameterValues() != null) {
        throw new TranslatorException(SolrPlugin.Event.TEIID20008,
                SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20008));
    }//from   w ww .  j  a v  a 2  s .c om

    SolrQueryExecution query = new SolrQueryExecution(ef, obj, this.executionContext, this.metadata,
            this.connection);
    query.execute();

    final UpdateRequest request = new UpdateRequest();
    query.walkDocuments(new SolrDocumentCallback() {
        @Override
        public void walk(SolrDocument doc) {
            SolrUpdateExecution.this.updateCount++;
            request.deleteById(doc.getFieldValue(id).toString());
        }
    });

    UpdateResponse response = this.connection.update(request);
    if (response.getStatus() != 0) {
        throw new TranslatorException(SolrPlugin.Event.TEIID20005,
                SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20005, response.getStatus()));
    }
}

From source file:org.teiid.translator.solr.TestSolrUpdateExecution.java

License:Open Source License

@Test
public void testSimpleDelete() throws Exception {
    String query = "Delete from example where name = 'teiid'";

    SolrDocument doc = new SolrDocument();
    doc.addField("price", 1.10f);
    doc.addField("weight", 2.23f);
    doc.addField("popularity", 5);
    doc.addField("name", "teiid");
    doc.addField("nis", "any");

    SolrDocumentList list = new SolrDocumentList();
    list.add(doc);/*  ww  w.  ja  v  a2s  .c  om*/

    QueryResponse queryResponse = Mockito.mock(QueryResponse.class);
    Mockito.stub(queryResponse.getResults()).toReturn(list);

    QueryResponse queryResponse2 = Mockito.mock(QueryResponse.class);
    Mockito.stub(queryResponse2.getResults()).toReturn(new SolrDocumentList());

    UpdateRequest request = helpUpdate(query, queryResponse, queryResponse2);
    List<SolrInputDocument> docs = request.getDocuments();

    UpdateRequest expected = new UpdateRequest();
    expected.deleteById("teiid");

    assertEquals(expected.getDeleteById().toString(), request.getDeleteById().toString());
}