Solr vs Hibernate Search - Which to choose and When?
Asked Answered
A

6

28

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?

Allies answered 20/5, 2011 at 7:32 Comment(4)
As it is not an answer but a suggestion: you might also want to consider ElasticSearch, it is very similar to Solr, and handles scaling. elasticsearch.org. engineering.socialcast.com/2011/05/…Listlessness
every architect/dev will always respond... it depends! There is many questions. how big will your ecommerce app be? ie no. of users? mostly read? mostly write? how extensive the search capability?Biltong
@Steve It is a more read application. I think any e-commerce app will be more read than write. Anyways, I want a generic one so that it will be helpful to the future seekers :)Allies
@Listlessness looks nice will look into it :)Allies
S
7

Apache Solr is mainly used for full text search: if you want to find words (singular and plurals for example) in a big set of documents where the size of each doc is from one paragraph to a few pages. Solr may not be better than a regular database if you don't use it for text search but only for int and varchar search.

This link might be useful to you:

http://engineering.twitter.com/2011/04/twitter-search-is-now-3x-faster_1656.html

Stonewort answered 1/6, 2011 at 18:53 Comment(4)
It helped me in my research :) I will still wait to accept it as an answer coz I am looking for a more complete oneAllies
I totally disagree with this. In Solr you can index anything you want, configure your index in whatever way makes sense for your application. It is just Lucene wrapped in a slick interface so it has all the power of Lucene. The out of box Solr is incredibly configurable and powerful and fast but, if you need more, it isn't hard to write plugins to do custom stuff. And now, given SolrCloud, there is no chance that Solr wouldn't do many times faster than a regular database - with a web-based query language, filters, caching, ...Troublous
I am not saying it cannot be done with Solr, I am saying that if the scenario is a standard relational scenario, a standard SQL DB may be more appropriate (join, etc)Stonewort
@2014 is this still relevantTownship
P
17

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

Postboy answered 31/5, 2011 at 8:21 Comment(1)
Thanks for the wonderful implementation. But my main question is for the Pros and Cons and hence would like an answer for thatAllies
O
15

In addition to what has been said, when in a clustered environment:

Hibernate-search:

Cons:

  • Requires a master/slave combination which isn't always feasible, specially when your build/deployment process doesn't distinguish among the nodes (same war for all nodes).
  • The indexes are hosted in the same server/process as the application running Hibernate, so you have one index per application node. This is sometimes overkill.
  • It isn't real-time search, unless the load balancer uses session stickiness.

Pros:

  • Zero to little configuration. Just drop the jar in the classpath.
  • The bridge between Hibernate and Lucene is very straight forward. Just annotate the Entities and voilá!

Solr/SolrCloud:

  • It is decoupled of the application it self.
  • Not real-time search, just as hibernate-search.
  • Requires restart to change the schema.
  • SolrCloud isn't exactly the easiest framework to configure.
  • No straight forward Hibernate bridge. You have to code your own Hibernate listener and bind them to post-[insert|delete|update] events (or find an open source one)

ElasticSearch

  • Servers are independent of the application, just like solr.
  • It is by far the easiest to configure in a cluster/cloud.
  • It is real-time
  • No straight forward Hibernate bridge, as well. (es-hibernate-connector on GitHub)

Personally I prefer ElasticSearch when running in the cloud.

Oxbow answered 15/8, 2012 at 4:7 Comment(0)
S
7

Apache Solr is mainly used for full text search: if you want to find words (singular and plurals for example) in a big set of documents where the size of each doc is from one paragraph to a few pages. Solr may not be better than a regular database if you don't use it for text search but only for int and varchar search.

This link might be useful to you:

http://engineering.twitter.com/2011/04/twitter-search-is-now-3x-faster_1656.html

Stonewort answered 1/6, 2011 at 18:53 Comment(4)
It helped me in my research :) I will still wait to accept it as an answer coz I am looking for a more complete oneAllies
I totally disagree with this. In Solr you can index anything you want, configure your index in whatever way makes sense for your application. It is just Lucene wrapped in a slick interface so it has all the power of Lucene. The out of box Solr is incredibly configurable and powerful and fast but, if you need more, it isn't hard to write plugins to do custom stuff. And now, given SolrCloud, there is no chance that Solr wouldn't do many times faster than a regular database - with a web-based query language, filters, caching, ...Troublous
I am not saying it cannot be done with Solr, I am saying that if the scenario is a standard relational scenario, a standard SQL DB may be more appropriate (join, etc)Stonewort
@2014 is this still relevantTownship
J
6

There is another alternative which is using them both together and combining their pros together.
Have a look at: Combining the power of Hibernate Search and Solr
I'm using them together and it works fine.
Hibernate search provides me all the entities annotations & analysis and changes collection in transaction boundaries while Solr provides me the best search engine with great features as 1:m facets, clusters, etc...

Josphinejoss answered 5/11, 2012 at 14:33 Comment(5)
do you have an example implementation of this? The link does not provide enough to get me going in the right direcitonLargess
I can try and build some example. Still you will need to query with the solrj client. Is it relevant for you?Josphinejoss
Yes. Most of my searching will be done straight through the Solr HTTP interface. I am looking to use Hibernate Search to easily get documents in the index and utilize the entity annotations and FieldBridgesLargess
Have a look at github.com/avner-levy/hibernate_search_solr_integration. It is a very minimal example but it should help you get the idea. I'll improve it in the next few days.Josphinejoss
great thanks! the BackendQueueProcessor implementation is exactly what I needed to see!Largess
C
1

It sound like you need to read up on the pros and cons of each of these. There is extensive documentation available.

If you wanted my opinion I would say that it makes sense to use Hibernate Search with Hibernate. The updating of search indexes occurs when hibernate performs database operations and only when a database transaction is committed.

Chloro answered 20/5, 2011 at 16:48 Comment(1)
Not individual Pros and Cons. I want to comparing them both.Allies
E
1

Hibernate search is a "bridge" between Hibernate and Lucene. In other words, it makes persisted Hibernate entities automagically searchable in Lucene index.

Solr is a framework built on top of Lucene (both projects are supposed to be merged one day, but it's a long way to go). Differences between Solr and Lucene are explained in another SO post.

Evaevacuant answered 1/6, 2011 at 22:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.