我们正在构建一个电子商务应用程序。我们正在使用带有 Hibernate 和 Spring Framework 的 JAVA 堆栈。与所有电子商务应用程序一样,我们需要在我们的应用程序中构建搜索功能。
因此,我们遇到了Hibernate Search和Apache Solr。有人可以列出两者的优缺点,以便我们为企业搜索选择理想的解决方案吗?
我们正在构建一个电子商务应用程序。我们正在使用带有 Hibernate 和 Spring Framework 的 JAVA 堆栈。与所有电子商务应用程序一样,我们需要在我们的应用程序中构建搜索功能。
因此,我们遇到了Hibernate Search和Apache Solr。有人可以列出两者的优缺点,以便我们为企业搜索选择理想的解决方案吗?
假设您正在使用基于注释的配置的 web 应用程序的持久层使用 hibernate。然后,您可以使用用于注释的相同模型类(如下面给出的模型类)使用 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 服务器中索引客户详细信息。
@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 索引。它非常简单。我已经向您解释了如何使用它的基本概念。我从一个商业应用程序中得到了这个例子,我们使用上述方法来实现搜索功能
除了已经说过的,在集群环境中:
休眠搜索:
缺点:
优点:
Solr/SolrCloud:
弹性搜索
在云中运行时,我个人更喜欢 ElasticSearch。
Apache Solr 主要用于全文搜索:如果您想在一大堆文档中查找单词(例如单数和复数),其中每个文档的大小从一个段落到几页不等。如果您不将 Solr 用于文本搜索而仅用于 int 和 varchar 搜索,则 Solr 可能不会比常规数据库好。
此链接可能对您有用:
http://engineering.twitter.com/2011/04/twitter-search-is-now-3x-faster_1656.html
还有另一种选择,将它们一起使用并将它们的优点组合在一起。
看看:结合 Hibernate Search 和 Solr 的强大功能,
我将它们一起使用,效果很好。
Hibernate 搜索为我提供了事务边界中的所有实体注释和分析以及更改集合,而 Solr 为我提供了具有强大功能的最佳搜索引擎,例如 1:m 刻面、集群等......
听起来您需要阅读每一个的优缺点。有大量可用的文档。
如果您想要我的意见,我会说将 Hibernate Search 与 Hibernate 一起使用是有意义的。搜索索引的更新发生在 hibernate 执行数据库操作并且仅在提交数据库事务时。