Solr 与 Hibernate Search - 选择哪个以及何时选择?

发布于 2024-11-08 14:21:57 字数 289 浏览 4 评论 0原文

我们正在构建一个电子商务应用程序。我们使用带有 Hibernate 和 Spring 框架的 JAVA 堆栈。与所有电子商务应用程序一样,我们需要在我们的应用程序中构建搜索功能。

因此,我们遇到了 Hibernate SearchApache Solr 。有人可以列出它们的优缺点,以便我们可以选择理想的企业搜索解决方案吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夜未央樱花落 2024-11-15 14:21:57

假设您正在使用 hibernate 作为 Web 应用程序的持久层,并具有基于注释的配置。然后,您可以使用用于注释的相同模型类(如我下面给出的模型类),使用 Solr 服务器特定注释在 Solr 服务器中设置它们的索引。

我将举例说明如何完成此操作。

以下类是一个没有 Solr 注释的客户模型类。

@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;
    }



}

现在让我们使用 Solr 注释来注释此类,以在 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;
    }



}

只需为要在 Solr 服务器中索引的文件添加 @Field 属性即可。

那么问题就是如何告诉solr来索引这个模型。可以按如下方式完成。

假设您要在数据库中持久保存一个名为 alex 的客户,那么我们将如下所示向 alex 添加数据

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

,并将此 alex 对象保存到数据库后,您需要告诉 solr 索引此数据对象。其操作如下。

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();
        }

这都是关于使用 Hibernate 技术的 solr 索引。它非常简单。我已经向您解释了如何使用它的基本概念。我从一个商业应用程序中得到了这个例子,我们使用上面的方法来实现搜索功能

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

絕版丫頭 2024-11-15 14:21:57

除了已经说过的之外,在集群环境中时:

Hibernate-search:

缺点:

  • 需要主/从组合,这并不总是可行的,特别是当您的构建/部署过程不可行时区分节点(所有节点的战争相同)。
  • 索引与运行 Hibernate 的应用程序托管在同一服务器/进程中,因此每个应用程序节点都有一个索引。这有时有点矫枉过正。
  • 它不是实时搜索,除非负载均衡器使用会话粘性。

优点:

  • 零到很少的配置。只需将 jar 放在类路径中即可。
  • Hibernate 和 Lucene 之间的桥梁非常简单。只需注释实体即可!

Solr/SolrCloud:

  • 它与应用程序本身是解耦的。
  • 不是实时搜索,就像休眠搜索一样。
  • 需要重新启动才能更改架构。
  • SolrCloud 并不是最容易配置的框架。
  • 没有直接的 Hibernate 桥。您必须编写自己的 Hibernate 侦听器并将它们绑定到 [插入|删除|更新] 后事件(或找到一个开源事件)

ElasticSearch

  • 服务器独立于应用程序,就像 solr 一样。
  • 它是迄今为止在集群/云中配置最简单的。
  • 它是实时
  • 也没有直接的 Hibernate 桥。 (GitHub 上的 es-hibernate-connector)

我个人更喜欢在云中运行时的 ElasticSearch。

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.

林空鹿饮溪 2024-11-15 14:21:57

Apache Solr 主要用于全文搜索:如果您想在一大堆文档中查找单词(例如单数和复数),其中每个文档的大小从一个段落到几页不等。如果您不将 Solr 用于文本搜索而仅用于 int 和 varchar 搜索,那么 Solr 可能并不比常规数据库更好。

此链接可能对您有用:

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

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

温暖的光 2024-11-15 14:21:57

还有另一种选择,即同时使用它们并将它们的优点结合在一起。
看看:结合Hibernate 搜索和 Solr
我把它们一起使用,效果很好。
Hibernate 搜索为我提供了所有实体注释和实体注释。分析和更改事务边界中的集合,而 Solr 为我提供了最好的搜索引擎,具有 1:m 方面、集群等强大功能...

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...

メ斷腸人バ 2024-11-15 14:21:57

听起来需要了解每种方案的优缺点。有大量可用文档。

如果您想听我的意见,我会说将 Hibernate Search 与 Hibernate 结合使用是有意义的。当 hibernate 执行数据库操作时并且仅在提交数据库事务时才会更新搜索索引。

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.

或十年 2024-11-15 14:21:57

Hibernate 搜索是 Hibernate 和 Lucene 之间的“桥梁”。换句话说,它使持久化的 Hibernate 实体可以在 Lucene 索引中自动搜索。

Solr 是一个构建在 Lucene 之上的框架(两个项目应该有一天会合并,但还有很长的路要走)。 另一篇 SO 文章 解释了 Solr 和 Lucene 之间的差异。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文