Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Deleting documents in solr collection

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 574
    Comment on it

    Deleting documents:

    Suppose, you have collection with the name of demo and you have indexed your file in the server in this collection but you want to delete all the documents or some of them, then it is possible by two ways:

    Deletion by SolrJ:

    It is used with java and you can use the following code if you want to delete all the documents in the collection:

    import java.io.IOException;
    import org.apache.solr.client.solrj.SolrClient;
    import org.apache.solr.client.solrj.SolrServerException;
    import org.apache.solr.client.solrj.impl.HttpSolrClient;
    public class Delete {
    public static void main(String args[]) {
    String url= "http://localhost:8983/solr/demo";
    SolrClient solr = new HttpSolrClient(url);
    try {
    solr.deleteByQuery("*:*"); // for deleting all the documents
    solr.commit();
    } catch (SolrServerException e) {
    
    e.printStackTrace();
    } catch (IOException e) {
    
    e.printStackTrace();
    }
    }
    }

    This code is used to delete documents with the help of query . We can also change the query if you don't want to delete all the documents. "demo" is the name of the collection

    e.g.

    solr.deleteByQuery("name:John");

    This can be used to delete documents with the name as "John"

    If you want to delete by Id , you can use the following code:

    import java.io.IOException;
    import org.apache.solr.client.solrj.SolrClient;
    import org.apache.solr.client.solrj.SolrServerException;
    import org.apache.solr.client.solrj.impl.HttpSolrClient;
    public class Delete {
    public static void main(String args[]) {
    String url= "http://localhost:8983/solr/demo";
    SolrClient solr = new HttpSolrClient(url);
    try {
    solr.deleteByID("011"); // for deleting document with the id : 011
    solr.commit();
    } catch (SolrServerException e) {
    
    e.printStackTrace();
    } catch (IOException e) {
    
    e.printStackTrace();
    }
    }
    }

    This will only delete the document which has the id:"011". 

    Deletion by url:

    http://localhost:8983/solr/demo/update?stream.body=<delete><query>*:*</query></delete>&commit=true

    Just copy paste the following url in the browser to delete all the documents from solr. 

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: