集成Hibernate Search做全文检索 原文来自 http://blog.csdn.net/zhengwei223/article/details/11952763
版本及依赖:
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-search-orm</artifactId>
- <version>4.2.0.Final</version>
- </dependency>
- <dependency>
- <span style="white-space:pre"> </span><groupId>org.apache.lucene</groupId>
- <span style="white-space:pre"> </span><artifactId>lucene-smartcn</artifactId>
- <span style="white-space:pre"> </span><version>3.6.2</version>
- <span style="white-space:pre"> </span></dependency>
1、修改hibernate主配置文件,增加:
- <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());
- }
- }
- }
相关推荐
它是hibernate对著名的全文检索系统Lucene的一个集成方案,作用在于对数据表中某些内容庞大的字段(如声明为text的字段)建立全文索引,这样通过hibernate search就可以对这些字段进行全文检索后获得相应的POJO,...
它是hibernate对著名的全文检索系统Lucene的一个集成方案,作用在于对数据表中某些内容庞大的字段(如声明为text的字段)建立全文索引,这样通过hibernate search就可以对这些字段进行全文检索后获得相应的POJO,...
它是hibernate对著名的全文检索系统Lucene的一个集成方案,作用在于对数据表中某些内容庞大的字段(如声明为text的字段)建立全文索引,这样通过hibernate search就可以对这些字段进行全文检索后获得相应的POJO,...
compile("org.grails.plugins:hibernate-search:2.3.0") compile("org.grails.plugins:hibernate5:6.1.8") compile("org.grails.plugins:cache") compile("org.hibernate:hibernate-core:5.2.10.Final") compile(...
Hibernate Search,JDG和JMS:演示JBoss EAP将Hibernate Search与Infinispan目录和JMS后端集成在一起作者:马特·罗布森(Matt Robson) 技术:JBoss EAP,JBoss数据网格,Hibernate Search,HornetQ 产品:JBoss ...
为轻量化的异步性集成了EJB Timer Service和Quartz,为工作流集成了jBPM,为业务规则集成了JBoss规则,为电子邮件集成了Meldware Mail,为完整的文本搜索集成了Hibernate Search和Lucene,为消息集成了JMS,以及为...
实时搜索通过 db 中的链接使用 ajax 进行搜索 - spring mvc、hibernate 模型 + dao 更新 - git bash update2 - git bash update3 - git eclipse 集成update4 - git bash update5 - Eclipse
Apache Commons:一个流行的Java类库,提供了许多实用的工具和组件,如Commons Lang(用于处理核心...它提供了许多用于处理数据库访问的工具和组件,如Hibernate Search(提供了搜索引擎功能)、Hibernate Validator
本章节会将带领学员了解全文搜索的概念,并熟悉如何用ElasticSearch来实现全文搜索。 第8章 架构设计与分层 本章节讲解了系统的整体架构设计思路,包括如何来组织项目结构。让学员理解系统的数据流程。 第9章 ...
本章节会将带领学员了解全文搜索的概念,并熟悉如何用ElasticSearch来实现全文搜索。 第8章 架构设计与分层 本章节讲解了系统的整体架构设计思路,包括如何来组织项目结构。让学员理解系统的数据流程。 第9章 ...
做数据检索,支持中文分词和结果关键字高亮 自己实现了一个Markdown编辑器, 附带菜单,书写方便, 还支持拖拽图片上传 待完成的功能 如果有你折腾过的,欢迎提pr,先谢过了:-) 权限修改后实现热更新,不用重新登录就...
做数据检索,支持中文分词和结果关键字高亮 自己实现了一个Markdown编辑器, 附带菜单,书写方便, 还支持拖拽图片上传 待完成的功能 如果有你折腾过的,欢迎提pr,先谢过了:-) 权限修改后实现热更新,不用重新登录就...
Seam - 语境相关的组件[满江红20071230]............................................................................................................................ 1 Java EE 框架...........................
支持信息分析、相似性检索技术,支持关键字检索、全文检索、组合检索等,并使关键字在搜索结果中进行高亮显示。文章发布后即生成相应的文件索件,避免过多查询数据库,引起服务器堵塞。 Lerx 网站内容管理系统截图 ...
AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...
AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...
AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...
AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...
AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...
AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器...