In this tutorials, we will see how to delete documents from Solr using SolrJ.
Delete Documents from Solr:
In previous tutorials, we have seen how to add documents to Solr, now as part of this example, I am going to delete the documents from solr.
Technologies:
- Java 8
- Apache Solr 7.5.0
- Apache SolrJ 7.5.0
- Apache Http Core 4.4.6
- Apache Http Client 4.5.3
- Maven
Maven Dependency:
Add the below solrj dependency on your pom.xml, it will get all the necessary dependencies to connect with solr.
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>7.5.0</version>
</dependency>
Prerequisites:
Install/setup the Solr instance and run on your machine, add some document to it. I have my Solr installed on windows 10 and added some documents.
Delete Documents from Solr:
SolrJ gave two different ways (methods) to delete the documents from Solr.
- Deleting by Query and
- Deleting by id
Deleting by Query:
deleteByQuery(“column: data”) method takes a string as a parameter and executed the same on running solr, then the matching documents will be deleted.
Here I am going to delete a document where item_name is equal to Spring in action.
deleteByQuery(“item_name:Spring in action”)
package com.onlinetutorilspoint.solr;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.common.SolrInputDocument;
import java.io.IOException;
public class DeleteDocuments {
public static void main(String args[]) throws SolrServerException, IOException {
//Preparing the Solr client
String SOLR_URL = "http://localhost:8983/solr/sample-items";
SolrClient Solr = new HttpSolrClient.Builder(SOLR_URL).build();
//Preparing the Solr document
SolrInputDocument doc = new SolrInputDocument();
//Deleting the document where item-name equal Spring in action
Solr.deleteByQuery("item_name:Spring in action");
Solr.commit();
System.out.println("Documents deleted");
}
}
Deleting by multiple conditions:
deleteByQuery(“category:Mobiles AND item_name:iPhoneX”)
package com.onlinetutorilspoint.solr;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.common.SolrInputDocument;
import java.io.IOException;
public class DeleteDocuments {
public static void main(String args[]) throws SolrServerException, IOException {
//Preparing the Solr client
String SOLR_URL = "http://localhost:8983/solr/sample-items";
SolrClient Solr = new HttpSolrClient.Builder(SOLR_URL).build();
//Preparing the Solr document
SolrInputDocument doc = new SolrInputDocument();
//Deleting the documents from Solr
Solr.deleteByQuery("category:MobilesĀ ANDĀ item_name:iPhoneX");
Solr.commit();
System.out.println("Documents deleted");
}
}
Run:
>javac DeleteDocuments.java
>java DeleteDocuments
Documents deleted
After executing the above two queries Solr instance:
Delete All documents at a time:
deleteByQuery(“*”)
//Preparing the Solr document
SolrInputDocument doc = new SolrInputDocument();
//Deleting all documents in Solr
Solr.deleteByQuery("*");
Solr.commit();
Delete Documents by Id:
deleteById(“documentId”)
//Preparing the Solr document
SolrInputDocument doc = new SolrInputDocument();
//Deleting Solr Document by Id
Solr.deleteById("b8cd5a15-bb4b-4c06-ad96-2c1bd09177ed");
Solr.commit();
References:
Happy Learning š