Home » Apache Solr Adding Document

Apache Solr Adding Document

by Online Tutorials Library

Apache Solr – Adding Document

In the Apache Solr Core, we saw the way to add data into Solr, which is in the file format of JSON and .CSV. This page will illustrate how to add data into Apache Solr index using XML file format.

Sample File

Let’s the following data to be added to the Solr index using the XML file format.

Student ID First Name Phone City
001 Olivia +148022337 California
002 Emma +148022338 Hawaii
003 Sophia +148022339 Florida
004 Emily +148022330 Texas
005 Harper +148022336 Kansas
006 Scarlett +148022335 Kentucky

Adding Documents Using XML

For the addition of the above data into the Solr index, we should prepare an XML document, as given below. Save the following document in a file with the name sample.xml.

As we can see, the XML file created to add data to index file contains the given three important tags named as, <add> text </add>, <doc> text </doc>, and < field > text < /field >.

  • < add > – The add tag is the root tag for addition of the documents for indexing. It acquires one or more documents which will be added to the index.
  • < doc > – The data we added should be wrapped under the <doc> text here </doc> tags. The given document contains the data in the form of fields.
  • < field > – This tag holds the name and value of the fields of the data we added.

After creating the document, we can add the created document to the index with the means discussed in the indexing.

Suppose the XML file exists in the Solr’s bin directory and it is to be indexed in the my_core file, then we can add it to index fo Solr using the post tool as given below –

When we run the above given command, we will get the below result.

/home/Hadoop/java/bin/java -classpath /home/Hadoop/Solr/dist/Solr-  Core8.2.0.jar -Dauto = yes -Dc = my_core -Ddata = files   org.apache.Solr.util.SimplePostTool sample.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 sample.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.201  

Verification

Go to the homepage of Apache Solr web UI and choose the my_core file. Try to get all the documents bypassing the query “:” inside the text area q and run the query. After executing, we can observe that the desired data is added to the Solr index.

Apache Solr - Adding Document


You may also like