Tuesday, December 11, 2012

Solr Search integration with ADF Faces - Part 1

Solr is the popular, blazing fast open source enterprise search platform from the Apache Lucene project. Its major features include powerful full-text search, hit highlighting, faceted search, dynamic clustering, database integration, rich document (e.g., Word, PDF) handling, and geospatial search.

Here why I'm trying to integrate Solr search with ADF Faces is, take a scenario where we need to build an EBook application having large data set. Each book may consists some 200 pages, If user wants to search some content inside the book or in cross books. It's very difficult to search in which page of the book the content exits and more over it will be overhead on DB server. By using Solr we can avoid this and do content indexing and do full-text also.

In Part 1, will learn on how to do content indexing. In my next post - Part 2, I will show how full-text search can be integrated with ADF application. Follow the article to install Solr server n windows, I have installed the Apache Solr 4.0 version, after the complete installation of jdk, tomcat, solr we can see the screen as follows, click on the collection1 schema and notice Num Docs will be 0.


Next download the SolrJ version 4.0, Solrj is a java client to access solr. It offers a java interface to add, update, and query the solr index.

Model Diagram: Download the sql script.


Next is to add the fields that will be indexed in Solr server, open the C:\solr\collection1\conf\schema.xml and alert the schema file by adding the below fields under <fields> tag. Before adding below fields shutdown the tomcat server.


Notice few fields will be already there, so add only missing fields then start the tomcat server and try to access the Solr admin. If the page doesn't load properly then schema file has some issue.

Create Fusion Web Application with entities based on category and product tables. Create a session bean and java client, add the following jar in "Project Properties-> Libraries and ClassPath":
  • apache-solr-solrj-4.0.0
  • commons-codec-1.3
  • commons-httpclient-3.1
  • commons-io-2.1
  • jcl-over-slf4j-1.6.4
  • slf4j-api-1.6.4
  • slf4j-jdk14-1.6.4
  • solr-solrj-1.4.0
All the above jar will be present in downloaded "apache-solr-4.0.0\dist\solrj-lib" directory, if you can't find all jar. You can download from the link.

Open the Java Client, add the below code.
private static void printProduct(Product product) throws MalformedURLException, SolrServerException, IOException {
        //Ip address is hard corded, where the Solr server is installed
        SolrServer server = new CommonsHttpSolrServer("http://10.177.252.178:8080/solr");
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", product.getId());
        doc.addField("title", product.getTitle());
        doc.addField("category", product.getCategoryRef().getName());
        doc.addField("productby", product.getProductBy());
        doc.addField("price", product.getPrice());
        doc.addField("description", product.getDescription());
        String features = product.getFeatures().replaceAll(";", " ");
        doc.addField("features", features);
        System.out.println("Content Indexing Started for Id " + product.getId());
        server.add(doc);
        server.commit();
        System.out.println("Content Indexing Completed for Id " + product.getId());
    }

Run the java client, all the records will get indexed into solr server. Now we can see on Solr admin homepage for collection1 schema 40 doc are added.

3 comments:

  1. hi ,


    I was not able to get how did u execute this java file. Could you please explain more on this.
    As i even have same requirement and tried to acces in the same way you implemented but not able to understand how you are able to add it to the java client .

    Thank you,
    SashankP

    ReplyDelete
  2. Hi Sashank,

    I have just created the sample Java Client in EJB Project. First deploy the SessionEjb and then you can run the java client.

    Please provide me your email id, I will share the above sample project.

    ReplyDelete
  3. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in APACHE SOLR
    , kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on APACHE SOLR . We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us:
    Name : Arunkumar U
    Email : arun@maxmunus.com
    Skype id: training_maxmunus
    Contact No.-+91-9738507310
    Company Website –http://www.maxmunus.com



    ReplyDelete