Solr vs Hibernate Search - 选择何时以及何时选择?

Luc*_*ari 28 solr hibernate hibernate-search

我们正在构建电子商务应用程序.我们正在使用JAVA堆栈与Hibernate和Spring Framework.与所有电子商务应用程序一样,我们需要在我们的网站中构建搜索功能.

所以,我们遇到了Hibernate SearchApache Solr.有人可以列出两者的优缺点,以便我们可以为企业搜索选择理想的解决方案吗?

KIt*_*tis 17

假设您正在使用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;
    }



}
Run Code Online (Sandbox Code Playgroud)

现在,我们使用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;
    }



}
Run Code Online (Sandbox Code Playgroud)

只需为要在Solr服务器中编入索引的字段添加@Field属性.

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

假设您要在数据库中保留一个名为alex的客户,那么我们将按如下方式向alex添加数据

Customer alex = new Customer();
alex.setCustomerName("Alex Rod");
alex.setCustomerAddress("101 washington st, DC");
Run Code Online (Sandbox Code Playgroud)

并且,在将此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();
        }
Run Code Online (Sandbox Code Playgroud)

这就是使用Hibernate技术进行solr索引的全部内容.它非常直接.我向你解释了如何使用它的基本思路.我从商业应用程序中得到了这个例子,我们使用上面的方法来实现搜索功能


Fra*_*ein 15

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

Hibernate的搜索:

缺点:

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

优点:

  • 零到小配置.只需将jar放入类路径中即可.
  • Hibernate和Lucene之间的桥梁非常直接.只是注释实体和voilá!

Solr的/ SolrCloud:

  • 它与自己的应用程序分离.
  • 不是实时搜索,就像hibernate-search一样.
  • 需要重新启动才能更改架构.
  • SolrCloud并不是最容易配置的框架.
  • 没有直接的Hibernate桥.你必须编写自己的Hibernate监听器代码并将它们绑定到post- [insert | delete | update]事件(或找到一个开源的)

ElasticSearch

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

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


Bob*_*ait 7

Apache Solr主要用于全文搜索:如果要在大量文档中查找单词(例如单数和复数),其中每个文档的大小从一个段落到几个页面.如果您不将它用于文本搜索但仅用于int和varchar搜索,则Solr可能不会比常规数据库更好.

此链接可能对您有用:

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

  • 我完全不同意这一点.在Solr中,您可以索引任何所需的索引,以适合您的应用程序的任何方式配置索引.它只是Lucene包裹在一个光滑的界面,所以它具有Lucene的所有力量.开箱即用的Solr具有令人难以置信的可配置性和强大而快速的特性,但是,如果您需要更多,那么编写插件来制作自定义内容并不困难.现在,考虑到SolrCloud,Solr不会比常规数据库快几倍 - 使用基于Web的查询语言,过滤器,缓存,...... (4认同)

Avn*_*evy 5

还有另一种选择是将它们一起使用并将它们的优点结合在一起.
看看:结合Hibernate Search和Solr的强大功能
我一起使用它们并且工作正常.
Hibernate搜索为我提供了所有实体注释和分析以及在事务边界中更改集合,而Solr为我提供了具有1:m facet,cluster等强大功能的最佳搜索引擎...

  • 看看https://github.com/avner-levy/hibernate_search_solr_integration.这是一个非常小的例子,但它可以帮助你理解.我会在接下来的几天内改进它. (4认同)