Luc*_*ari 28 solr hibernate hibernate-search
我们正在构建电子商务应用程序.我们正在使用JAVA堆栈与Hibernate和Spring Framework.与所有电子商务应用程序一样,我们需要在我们的网站中构建搜索功能.
所以,我们遇到了Hibernate Search和Apache 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的搜索:
缺点:
优点:
Solr的/ SolrCloud:
ElasticSearch
我个人在云中运行时更喜欢ElasticSearch.
Apache 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 facet,cluster等强大功能的最佳搜索引擎...
归档时间: |
|
查看次数: |
13606 次 |
最近记录: |