近期接手一个新的项目,就是搭建分布式搜索。经过调研决定用开源的hibernate search。拍下来以后,网上找资料,最后实在是搞不定了,原因网上的东西太杂,版本不一样,方法不一样,没有针对入门级的文章。无奈去官网看文档,终于搞通了。下面把具体的东西发上来,以供大家参考,以备自己留用。
一、首先引入jar包,maven配置如下:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search</artifactId>
<version>3.4.0.Final</version>
</dependency>
二、看hibernate的配置,如下:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/hise
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.search.default.directory_provider">
org.hibernate.search.store.FSDirectoryProvider
</property>
<property name="hibernate.search.default.indexBase">e:/index</property>
<mapping resource="po/weibo.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
三、定义一个pojo,如下:
package cn.damai.hibernate.dto;
import java.io.Serializable;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.hibernate.search.annotations.Analyzer;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;
@Indexed(index = "weiboIndex")
public class Weibo implements Serializable {
/**
*
*/
private static final long serialVersionUID = -8675404851597089664L;
@DocumentId
private Long id;
@Field(name = "text", store = Store.YES, index = Index.TOKENIZED, analyzer = @Analyzer(impl = StandardAnalyzer.class))
private String text;
@Field(name = "ct", store = Store.YES, index = Index.UN_TOKENIZED)
private Long createTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Long getCreateTime() {
return createTime;
}
public void setCreateTime(Long createTime) {
this.createTime = createTime;
}
}
四、hibernate我用的配置文件,没有选择注解,原因是注解太麻烦了,真的太麻烦了。所以我们还有一个weibo.hbm.xml的对象的配置文件,如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.damai.hibernate.dto.Weibo" table="weibo">
<id name="id" type="java.lang.Long">
<generator class="native" />
</id>
<property name="createTime" type="java.lang.Long">
</property>
<property name="text" type="java.lang.String">
</property>
</class>
</hibernate-mapping>
五、下面就改上测试代码了,先看代码,再做解释:
package cn.damai.hibernate.test;
import java.util.Date;
import java.util.List;
import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.util.Version;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import cn.damai.hibernate.dto.Department;
import cn.damai.hibernate.dto.Employee;
import cn.damai.hibernate.dto.Weibo;
public class Test extends BaseTest {
private static SessionFactory sf = null;
private static Session session = null;
private static Transaction tx = null;
@SuppressWarnings("deprecation")
@Before
public void setUp() throws Exception {
try {
sf = new AnnotationConfiguration().configure("hibernate-cfg.xml")
.buildSessionFactory();
session = sf.openSession();
tx = session.beginTransaction();
tx.begin();
} catch (Exception e) {
e.printStackTrace();
}
}
@After
public void tearDown() throws Exception {
tx.commit();
session.close();
}
/**
* 添加数据的时候默认做了索引
*/
public void testAddWeibo() {
for (int i = 0; i < 100; i++) {
Weibo weibo = new Weibo();
weibo.setCreateTime((long) 11111);
weibo.setText("zhang " + i);
session.save(weibo);
}
}
public void testCreateWeiboIndex() throws Exception {
FullTextSession fullTextSession = Search.getFullTextSession(session);
fullTextSession.createIndexer().startAndWait();
}
@SuppressWarnings({ "deprecation", "unused", "unchecked" })
public void testGetAllWeibo() {
FullTextSession s = Search.getFullTextSession(session);
List<Weibo> list = null;
try {
list = s.find("from weibo");
} catch (Exception e) {
e.printStackTrace();
}
}
public void testGetWeiboIndex2() {
FullTextSession fullTextSession = Search.getFullTextSession(session);
Transaction tx = fullTextSession.beginTransaction();
QueryBuilder qb = fullTextSession.getSearchFactory()
.buildQueryBuilder().forEntity(Weibo.class).get();
try {
org.apache.lucene.search.Query query = qb.keyword().onField("text")
.matching("zhang").createQuery();
org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(
query, Weibo.class);
List result = hibQuery.list();
tx.commit();
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
上面的几个方法,有俩个是通过的,一个是添加数据到weibo表里。hibernate search 本身集成了lucene,默认情况下你添加数据到库同时会做索引。最后的一个查询也是没问题的。
按我的方法走,跑起来是没问题的。还有很多功能没有探索,等这个项目做完了,有空分享上来。