Search in sources :

Example 1 with SolrQuery

use of org.apache.gora.solr.query.SolrQuery in project gora by apache.

the class SolrStore method deleteByQuery.

@Override
public long deleteByQuery(Query<K, T> query) {
    UpdateResponse rsp;
    try {
        /*
        In this If block we check whether, user needs to delete full document or some fields in the document. We can't delete fields in a document by using solr deleteByQuery method.
        therefore what we have done here is setting the particular fields values into null.
       */
        if (query.getFields() != null && query.getFields().length < mapping.mapping.size() && !(Arrays.asList(query.getFields()).contains(mapping.getPrimaryKey()))) {
            Result<K, T> result = query.execute();
            Map<String, String> partialUpdateNull = new HashMap<>();
            partialUpdateNull.put("set", null);
            while (result.next()) {
                SolrInputDocument inputDoc = new SolrInputDocument();
                inputDoc.setField(mapping.getPrimaryKey(), result.getKey());
                for (String field : query.getFields()) {
                    inputDoc.setField(field, partialUpdateNull);
                }
                batch.add(inputDoc);
            }
            if (commitWithin == 0) {
                rsp = server.add(batch);
                server.commit(false, true, true);
                batch.clear();
                LOG.info(rsp.toString());
            } else {
                rsp = server.add(batch, commitWithin);
                batch.clear();
                LOG.info(rsp.toString());
            }
        } else {
            SolrQuery<K, T> solrQuery = (SolrQuery<K, T>) query;
            String q = solrQuery.toSolrQuery();
            rsp = server.deleteByQuery(q);
            server.commit();
            LOG.info(rsp.toString());
        }
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
    }
    return 0;
}
Also used : UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SolrQuery(org.apache.gora.solr.query.SolrQuery) SolrServerException(org.apache.solr.client.solrj.SolrServerException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 SolrQuery (org.apache.gora.solr.query.SolrQuery)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1 UpdateResponse (org.apache.solr.client.solrj.response.UpdateResponse)1 SolrInputDocument (org.apache.solr.common.SolrInputDocument)1