遗失在古战场 2014-05-03
集成Hibernate Search做全文检索 原文来自 http://blog.csdn.net/zhengwei223/article/details/11952763
版本及依赖:
<property name="hibernate.search.default.directory_provider"> org.hibernate.search.store.impl.FSDirectoryProvider </property> <property name="hibernate.search.default.indexBase"> e:\luceneLinde </property>
一个是存储的实现,一个是存储的路径
2、给实体类上注解
import javax.persistence.*; import org.hibernate.annotations.GenericGenerator; import org.hibernate.search.annotations.DocumentId; import org.hibernate.search.annotations.Field; import org.hibernate.search.annotations.Indexed; import org.hibernate.search.annotations.IndexedEmbedded; import org.hibernate.search.annotations.Store; @Entity @Table(name = "PAGEINFO") @Indexed(index="PageInfo")/*标记该表可索引,参数index指定存放索引信息的文件名,路径在主配置文件中指定*/
@Analyzer(impl=SmartChineseAnalyzer.class)//分词器 public class Pageinfo implements java.io.Serializable { private static final long serialVersionUID = 5454155825314635342L; // columns START //省略1000字 // columns END @Id @GeneratedValue(generator = "custom-id") @GenericGenerator(name = "custom-id", strategy = "uuid") @Column(name = "ID", unique = true, nullable = false, insertable = true, updatable = true, length = 32) @DocumentId /*以字段id作为文档id*/ public java.lang.String getId() { return this.id; } @Column(name = "TITLE", unique = false, nullable = true, insertable = true, updatable = true, length = 255) @Field(store=Store.NO) /*可索引,但不存储*/ public java.lang.String getTitle() { return this.title; } @Column(name = "CONTENT", unique = false, nullable = true, insertable = true, updatable = true) @Field(store=Store.NO) public java.lang.String getContent() { return this.content; } @Column(name = "SOURCE", unique = false, nullable = true, insertable = true, updatable = true) @Field(store=Store.NO) public java.lang.String getSource() { return this.source; } @Column(name = "SUMMARY", unique = false, nullable = true, insertable = true, updatable = true) @Field(store=Store.NO) public java.lang.String getSummary() { return this.summary; } @ManyToOne(cascade = {}, fetch = FetchType.LAZY) @JoinColumns({ @JoinColumn(name = "SITE_ID", nullable = false, insertable = false, updatable = false) }) @IndexedEmbedded(prefix="site_",depth=1) /*关联检索,如field为site_name实则是按关联表的那么属性检索*/ public GrabageSiteconfig getGrabageSiteconfig() { return grabageSiteconfig; } }
省略了大量东西,如域成员,set方法等,一般以id作为doc的id,其他的你想对哪些字段做全文检索,就使用@Field标记,至于其store属性和lucene中的含义一致,不赘述。
3、使用API
package othertest; import java.util.Iterator; import java.util.List; import javacommon.gather.bean.Pageinfo; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.Query; import org.apache.lucene.util.Version; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.search.FullTextQuery; import org.hibernate.search.FullTextSession; import org.hibernate.search.Search; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class SearchTest { private static SessionFactory sf; @BeforeClass public static void init() { sf = HibernateConfigTest.sf;//弄一个SessionFactory,不多说 } @Before //执行索引 public void index(){ Session session = sf.openSession(); FullTextSession fullTextSession = Search.getFullTextSession(session); //查出结果 List<Pageinfo> pageinfos = session.createCriteria(Pageinfo.class).list(); session.beginTransaction(); //依次建立索引 for (Iterator iterator = pageinfos.iterator(); iterator.hasNext();) { Pageinfo pageinfo = (Pageinfo) iterator.next(); fullTextSession.index(pageinfo); } session.getTransaction().commit(); session.close(); System.out.println("index over......"); } @Test public void searchTest() { Session session = sf.openSession(); FullTextSession fullTextSession = Search.getFullTextSession(session); //在字段content中检索 QueryParser queryParser = new QueryParser(Version.LUCENE_36, "content", new SmartChineseAnalyzer(Version.LUCENE_36)); Query luceneqQuery=null; try { //检索含有“大风”的信息 luceneqQuery = queryParser.parse("大风"); } catch (ParseException e) { e.printStackTrace(); } //执行检索,得到结果集 FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(luceneqQuery, Pageinfo.class); List<Pageinfo> pageinfos = fullTextQuery.list(); //查看结果做验证 for (Iterator iterator = pageinfos.iterator(); iterator.hasNext();) { Pageinfo pageinfo = (Pageinfo) iterator.next(); System.out.println(pageinfo.getContent()); } } }