Monday, August 14, 2017

Apache Solr

Apache Solr is server provided by Apache Community for having better and quick serach of document at the server side.Solr enables you to easily create search engines which searches websites, databases and files.
To understand the concept of Apache Solr lets try to install it.
Step 1:- Download Solr server from below given location
http://lucene.apache.org/solr/mirrors-solr-latest-redir.html
Step 2:- Extract the solr-6.6.0.zip file
Step 3:- Start the server using following command i.e. here we are keeping our project name as siddhuapachesolrproject
C:\solr-6.6.0\solr-6.6.0\bin\solr -e siddhuapachesolrproject
Step 4:- Now lets try to see how our Newly created Apache Solr server helps us in searing the documents. For this lets try to index one the file on the server.
If we see the C:\solr-6.6.0\solr-6.6.0\example\exampledocs folder we will find there are many *.xml, *.pdf, *.cvs files are kept for testing and demo purpose. We will index one of the *.xml into our Apache solr server.
This can be done using post.jar which is provided by Apache Solr server inside exampledocs folder. Execute following command
C:\solr-6.6.0\solr-6.6.0\example\exampledocs>java -Dc=techproducts -jar post.jar sd500.xml
Now we can confirm if our server is able to find this file using following command
http://localhost:8983/solr/siddhuapachesolrproject/select?q=sd500&wt=json

Now lets try to use java code to perform indexing and finding the text from the Apache Solr
Use following java code to upload or indexing the file on Apache 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 SiddhuUploadFile {
public static void main(String[] args) throws IOException, SolrServerException {
SolrClient client = new HttpSolrClient.Builder("http://localhost:8983/solr/siddhuapachesolrproject").build();
for(int i=0;i<1000 i="" span="">
SolrInputDocument doc = new SolrInputDocument();
doc.addField("cat", "book");
doc.addField("id", "book-" + i);
doc.addField("name", "The Legend of the Hobbit part " + i);
client.add(doc);
if(i%100==0) client.commit(); // periodically flush
}
client.commit();
}
}

And use below code to find the perticular file from Apache Solr
import java.io.IOException;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
public class SiddhuSearchFile {
public static void main(String[] args) throws IOException, SolrServerException {
SolrClient client = new HttpSolrClient.Builder("http://localhost:8983/solr/techproducts").build();
SolrQuery query = new SolrQuery();
query.setQuery("Canon PowerShot SD500");
query.addFilterQuery("cat:electronics","manu_id_s:canon");
query.setFields("id","price","cat","store");
query.setStart(0);
query.set("defType", "edismax");
QueryResponse response = client.query(query);
SolrDocumentList results = response.getResults();
for (int i = 0; i < results.size(); ++i) {
System.out.println(results.get(i));
}
}
}

Project

You can find all the required jar  inside solr-6.6.0.zip folder which we had downloaded to setup the server.
C:\solr-6.6.0\solr-6.6.0\dist\solrj-lib
Note:- To Shutdown the server use following command
bin/solr stop
- You can learn different Query Syntex of Lucene for finding and searching the file on Apache Solr server.

No comments: