Apache Solr Tutorial
Apache Solr Useful Resources
Selected Reading
- 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 - Retrieving Data
Apache Solr - Retrieving Data
In this chapter, we will discuss how to retrieve data using Java Cpent API. Suppose we have a .csv document named sample.csv with the following content.
001,9848022337,Hyderabad,Rajiv,Reddy 002,9848022338,Kolkata,Siddarth,Battacharya 003,9848022339,Delhi,Rajesh,Khanna
You can index this data under the core named sample_Solr using the post command.
[Hadoop@localhost bin]$ ./post -c Solr_sample sample.csv
Following is the Java program to add documents to Apache Solr index. Save this code in a file with named RetrievingData.java.
import java.io.IOException; import org.apache.Solr.cpent.Solrj.SolrCpent; import org.apache.Solr.cpent.Solrj.SolrQuery; import org.apache.Solr.cpent.Solrj.SolrServerException; import org.apache.Solr.cpent.Solrj.impl.HttpSolrCpent; import org.apache.Solr.cpent.Solrj.response.QueryResponse; import org.apache.Solr.common.SolrDocumentList; pubpc class RetrievingData { 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 Solr query SolrQuery query = new SolrQuery(); query.setQuery("*:*"); //Adding the field to be retrieved query.addField("*"); //Executing the query QueryResponse queryResponse = Solr.query(query); //Storing the results of the query SolrDocumentList docs = queryResponse.getResults(); System.out.println(docs); System.out.println(docs.get(0)); System.out.println(docs.get(1)); System.out.println(docs.get(2)); //Saving the operations Solr.commit(); } }
Compile the above code by executing the following commands in the terminal −
[Hadoop@localhost bin]$ javac RetrievingData [Hadoop@localhost bin]$ java RetrievingData
On executing the above command, you will get the following output.
{numFound = 3,start = 0,docs = [SolrDocument{id=001, phone = [9848022337], city = [Hyderabad], first_name = [Rajiv], last_name = [Reddy], _version_ = 1547262806014820352}, SolrDocument{id = 002, phone = [9848022338], city = [Kolkata], first_name = [Siddarth], last_name = [Battacharya], _version_ = 1547262806026354688}, SolrDocument{id = 003, phone = [9848022339], city = [Delhi], first_name = [Rajesh], last_name = [Khanna], _version_ = 1547262806029500416}]} SolrDocument{id = 001, phone = [9848022337], city = [Hyderabad], first_name = [Rajiv], last_name = [Reddy], _version_ = 1547262806014820352} SolrDocument{id = 002, phone = [9848022338], city = [Kolkata], first_name = [Siddarth], last_name = [Battacharya], _version_ = 1547262806026354688} SolrDocument{id = 003, phone = [9848022339], city = [Delhi], first_name = [Rajesh], last_name = [Khanna], _version_ = 1547262806029500416}Advertisements