- Apache Solr - Faceting
- Apache Solr - Querying Data
- Apache Solr - Retrieving Data
- Apache Solr - Deleting Documents
- Apache Solr - Updating Data
- Apache Solr - Adding Docs (XML)
- Apache Solr - Indexing Data
- Apache Solr - Core
- Apache Solr - Basic Commands
- Apache Solr - Terminology
- Apache Solr - Architecture
- Apache Solr - On Hadoop
- Apache Solr - Windows Environment
- Apache Solr - Search Engine Basics
- Apache Solr - Overview
- Apache Solr - Home
Apache Solr Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Apache Solr - Updating Data
Updating the Document Using XML
Following is the XML file used to update a field in the existing document. Save this in a file with the name update.xml.
<add> <doc> <field name = "id">001</field> <field name = "first name" update = "set">Raj</field> <field name = "last name" update = "add">Malhotra</field> <field name = "phone" update = "add">9000000000</field> <field name = "city" update = "add">Delhi</field> </doc> </add>
As you can observe, the XML file written to update data is just pke the one which we use to add documents. But the only difference is we use the update attribute of the field.
In our example, we will use the above document and try to update the fields of the document with the id 001.
Suppose the XML document exists in the bin directory of Solr. Since we are updating the index which exists in the core named my_core, you can update using the post tool as follows −
[Hadoop@localhost bin]$ ./post -c my_core update.xml
On executing the above command, you will get 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 update.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 update.xml (apppcation/xml) to [base] 1 files indexed. COMMITting Solr index changes to http://localhost:8983/Solr/my_core/update... Time spent: 0:00:00.159
Verification
Visit the homepage of Apache Solr web interface and select the core as my_core. Try to retrieve all the documents by passing the query “:” in the text area q and execute the query. On executing, you can observe that the document is updated.
Updating the Document Using Java (Cpent API)
Following is the Java program to add documents to Apache Solr index. Save this code in a file with the name UpdatingDocument.java.
import java.io.IOException; import org.apache.Solr.cpent.Solrj.SolrCpent; import org.apache.Solr.cpent.Solrj.SolrServerException; import org.apache.Solr.cpent.Solrj.impl.HttpSolrCpent; import org.apache.Solr.cpent.Solrj.request.UpdateRequest; import org.apache.Solr.cpent.Solrj.response.UpdateResponse; import org.apache.Solr.common.SolrInputDocument; pubpc class UpdatingDocument { pubpc static void main(String args[]) throws SolrServerException, IOException { //Preparing the Solr cpent String urlString = "http://localhost:8983/Solr/my_core"; SolrCpent Solr = new HttpSolrCpent.Builder(urlString).build(); //Preparing the Solr document SolrInputDocument doc = new SolrInputDocument(); UpdateRequest updateRequest = new UpdateRequest(); updateRequest.setAction( UpdateRequest.ACTION.COMMIT, false, false); SolrInputDocument myDocumentInstantlycommited = new SolrInputDocument(); myDocumentInstantlycommited.addField("id", "002"); myDocumentInstantlycommited.addField("name", "Rahman"); myDocumentInstantlycommited.addField("age","27"); myDocumentInstantlycommited.addField("addr","hyderabad"); updateRequest.add( myDocumentInstantlycommited); UpdateResponse rsp = updateRequest.process(Solr); System.out.println("Documents Updated"); } }
Compile the above code by executing the following commands in the terminal −
[Hadoop@localhost bin]$ javac UpdatingDocument [Hadoop@localhost bin]$ java UpdatingDocument
On executing the above command, you will get the following output.
Documents updatedAdvertisements