Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solr vs Hibernate Search - Which to choose and When?

We are building an ecommerce application. We are using JAVA stack with Hibernate and Spring Framework. As with all ecommerce application, we need to build search capability into ours.

So, we came across Hibernate Search and Apache Solr . Can someone list out the pros and cons of both of them so that we can select the ideal solution for Enterprise Search?

like image 447
Lucky Murari Avatar asked May 20 '11 07:05

Lucky Murari


People also ask

What is Solr good for?

Solr offers powerful features such as distributed full-text search, faceting, near real-time indexing, high availability, NoSQL features, integrations with big data tools such as Hadoop, and the ability to handle rich-text documents such as Word and PDF.

Is Solr a search engine?

What is Solr? Solr is a leading open source search engine from the Apache Software Foundation's Lucene project. Thanks to its flexibility, scalability, and cost-effectiveness, Solr is widely used by large and small enterprises.

Is Solr Java based?

Apache SolrJ is a Java-based client for Solr that provides interfaces for the main features of search like indexing, querying, and deleting documents.

What is hibernate search engine?

Hibernate Search integrates Apache Lucene, a high-performance and extensible full-text search-engine library written in Java. This combines the power of Lucene with the simplicity of Hibernate and JPA.


1 Answers

Say you are using hibernate for the persistent layer of your web application with annotation based configuration. Then, you can use same model classes(like the one i given below) used for annotation to set them index in the Solr server using Solr server specific annotation.

i will give you example where this is done.

Following class is a Customer Model Class without Solr annotations.

@Entity @Table(name="Customer") public class Customer {      private int customerId;     private String customerName;     private String customerAddress;       @Id          public int getCustomerId() {         return customerId;     }     public void setCustomerId(int customerId) {         this.customerId = customerId;     }     public String getCustomerName() {         return customerName;     }     public void setCustomerName(String customerName) {         this.customerName = customerName;     }      public String getCustomerAddress() {         return customerAddress;     }     public void setCustomerAddress(String customerAddress) {         this.customerAddress = customerAddress;     }    } 

Now lets annotate this class with Solr annotations to index Customer details in Solr Server.

@Entity @Table(name="Customer") public class Customer {     @Field     private int customerId;     @Field     private String customerName;     @Field     private String customerAddress;       @Id          public int getCustomerId() {         return customerId;     }     public void setCustomerId(int customerId) {         this.customerId = customerId;     }     public String getCustomerName() {         return customerName;     }     public void setCustomerName(String customerName) {         this.customerName = customerName;     }      public String getCustomerAddress() {         return customerAddress;     }     public void setCustomerAddress(String customerAddress) {         this.customerAddress = customerAddress;     }    } 

Just put @Field attribute for filed that you want indexed in Solr server.

Then the problem is how to tell solr to index this model. it can be done as follows.

Say you are going to persist a customer called alex in the database, then we will add data to the alex as follows

Customer alex = new Customer(); alex.setCustomerName("Alex Rod"); alex.setCustomerAddress("101 washington st, DC"); 

and, after saving this alex object to database, you need to tell solr to index this data object. it is done as follows.

session.save(alex);          session.getTransaction().commit();           String url = "http://localhost:8983/solr";         SolrServer server = null;         try {             server = new CommonsHttpSolrServer(url);             server.addBean(alex);             server.commit();         } catch (MalformedURLException e) {             // TODO Auto-generated catch block             e.printStackTrace();         } 

This is all about solr indexing with the use of Hibernate Technology. it is pretty straight forward.i have explained you the basic idea of how to use it. i got this example from a commercial application where we used above method to implement search functionality

like image 53
KItis Avatar answered Sep 19 '22 01:09

KItis