Javatpoint Logo
Javatpoint Logo

Deleting Document in Apache Solr

If we want to delete the document from the index of Apache Solr, we are required to initialize the ID's for the documents that we want to delete between the <delete> </delete> tags.

The above given code in XML can be used for the deletion of the documents with ID's 101 and 102. We just need to save this code with the name delete.xml.

In case, if we want to delete the documents from the index that belongs to the core named as my_core, then we can post the delete.xml file using the post tool, as given below.

When we compile and run the above command, we will receive the following output:

/ home/ Hadoop/ java/ bin/ home/ Hadoop/ Solr/ dist/ Solr-core
8.2.0.jar -Dauto = yes -Dc = my_core -Ddata = files 
org.apache.Solr.util.SimplePostTool delete.xml 
SimplePostTool version 5.0.0 
Posting files to [base] url http://localhost:8983/Solr/my_core/update... 
Entering auto mode. File endings considered are 
xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,
rtf,htm,html,txt,log 
POSTing file delete.xml (application/xml) to [base] 
1 file indexed. 
COMMITting Solr index changes to http://localhost:8983/Solr/my_core/update... 
Time spent: 0:00:00.179

Verifying the data

Go to the homepage of the Apache Solr web interface and choose the core as my_core. Try to get all the documents with passing the query ":".inside the text area q and run the query. When you run the query, you can notice that the specified document are deleted.

Deleting Document in Apache Solr

Deleting a Field

In some case, we have to delete documents based on fields other than ID. E.g. If we want to delete the documents where the city is New York. In that case, we have to specify the name and value of the filed within the <query></query> tag pair.

Save the above XML file as delete_field.xml and execute the delete operation on the core name my_core using the post tool of Solr.

When you execute the above given command, it gives you the following output.

/home/Hadoop/java/bin/java -classpath /home/Hadoop/Solr/dist/Solr-core
6.2.0.jar -Dauto = yes -Dc = my_core -Ddata = files 
org.apache.Solr.util.SimplePostTool delete_field.xml 
SimplePostTool version 5.0.0 
Posting files to [base] url http://localhost:8983/Solr/my_core/update... 
Entering auto mode. File endings considered are 
xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,
rtf,htm,html,txt,log 
POSTing file delete_field.xml (application/xml) to [base] 
1 files indexed. 
COMMITting Solr index changes to http://localhost:8983/Solr/my_core/update... 
Time spent: 0:00:00.084

Verifying the Document

Go to the homepage of the Apache Solr web interface and choose the core as my_core. Try to get all the documents by passing the query ":" in the text area q and execute the query. When you run the command, you can observe that the documents containing the specified filed value pair are deleted.

Deleting Document in Apache Solr

Deleting All the Document

As we have deleted a specific filed, we can delete all the docuemnts from an index, we just need to pass the symbol ":" between the tags <query></query>, as given below.

Save the above xml file as delete_all.xml and perform the delete operation on the core name my_core using the post tool of Solr

When you execute the command given above, it will return the following output.

/home/Hadoop/java/bin/java -classpath /home/Hadoop/Solr/dist/Solr-core
6.2.0.jar -Dauto = yes -Dc = my_core -Ddata = files 
org.apache.Solr.util.SimplePostTool deleteAll.xml 
SimplePostTool version 5.0.0 
Posting files to [base] url http://localhost:8983/Solr/my_core/update... 
Entering auto mode. File endings considered are 
xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,
htm,html,txt,log 
POSTing file deleteAll.xml (application/xml) to [base] 
1 files indexed. 
COMMITting Solr index changes to http://localhost:8983/Solr/my_core/update... 
Time spent: 0:00:00.138

Verifying the document

Redirect to the homepage of Apache Solr web interface and select my_core for the core configuration. Retrieve all the documents by passing the query ":" in the text area q and execute the query. After running, you can observe that the documents containing the pair of specified field value are deleted.

Deleting Document in Apache Solr

Deleting the documents using Java API

Below Is the Java program that can be used to add documents to Apache Solr index. Rewrite this code and save it in a file with the name UpdatingDocument.java.

Compile the code written above by executing the following commands in the terminal:

When we execute the above command then we will get the following result.

Documents deleted






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA