现在的位置: 首页 > 综合 > 正文

hibernate -search

2018年05月01日 ⁄ 综合 ⁄ 共 4111字 ⁄ 字号 评论关闭

Hibernate-search的步骤

1先用hibernate生成了相应的pojo类,和dao层中的对应的类 (在生成的时候最好用hibernate的映射方式生成,不用copy方式,因为反正在后面要删除,便于删除)

2至关重要的一步:偷梁换柱--> 先是工程的右键--build Path --->configure build path --> libraries --> 删掉刚才添加的hibernate的一些文映射,然后斩草除根,在lib目录下去删掉刚才删掉的映射的jar

3hibernate-search的开发包拷贝到lib目录下面,一定不要忘记把paoding的字典文件dic考到工程的src目录下面

4对要全文搜索的类进行加索引

·在类上面加 

@indexedindex="路径"//指定存放索引的相对位置,相对hibernate.search.defualt.indexBase

@analyer=(impl=PaodingAanalyer.class)//指定中文分词器

·在主键字段上面加  

@documentid //主键

·在要索引的字段上面加

@field(store=Store.yes,index=Index.tokenStream) //(是否存储,是否进行分词)

·对于高亮显示,还需要在pojo相应的类中增加字段来存放高亮显示的内容

==========片断代码:

//这里是指定索引的位置,这里是相对位置,相对于hibernate.search.defualt.indexBase配置的路径

@Indexed(index="articles")

//指定使用的分词器

@Analyzer(impl=PaodingAnalyzer.class)

public class Articles implements java.io.Serializable {

@DocumentId //主键配置DocumentId

private Integer aid;

//要索引的字段上面需要配置@Field(store=Store.YES,index= Index.TOKENIZED)---(是否存储 ; 是否索引)

@Field(store=Store.YES,index = Index.TOKENIZED)

private String atitle;

@Field(store=Store.YES,index = Index.TOKENIZED)

private String acontent;

//这两个字段是为高亮显示准备的

//在数据库中没有相应的字段,是为了存储被高亮显示,格式了的结果

private String titleString;

private String contentString;

5BaseHibernateDao(只是针对hibernate,如果以后用spring的话,可以不在这里面写,)中要写一个得到FullTextSession的方法

//得到hibernate-search需要的全文搜索的Session----FullSession

public FullTextSession getFullTextSession(){

//要先得到session

return Search.getFullTextSession(getSession());

}

6针对你要全文搜索的DAO中写方法,来搜索你需要的内容

//先得到中文分词的分词器

private Analyzer analyzer = new PaodingAnalyzer();

public List<Articles> queryArticles(String keyWord) throws ParseException, IOException, InvalidTokenOffsetsException{

//得到fullTextSession

FullTextSession fullTextSession = getFullTextSession();

//创建两个查询转换器,一个是标题的,一个是内容的--(版本,要查询的字段,分词器)

         QueryParser titleParser = new QueryParser(Version.LUCENE_30,"atitle",analyzer);

QueryParser contentParser = new QueryParser(Version.LUCENE_30,"acontent",analyzer);

//注意这里用的query是lucen下面的,把关键词转换为查询语句

org.apache.lucene.search.Query titleQuery = titleParser.parse(keyWord); 

org.apache.lucene.search.Query contentQuery = contentParser.parse(keyWord);

//创建一个布尔查询,来结合上面的两个查询

BooleanQuery bQuery = new BooleanQuery();

bQuery.add(titleQuery,Occur.MUST);

bQuery.add(contentQuery,Occur.SHOULD);

//这里的query是hibernate的query

Query query = fullTextSession.createFullTextQuery(bQuery, Articles.class);//要指定是针对哪个类的全文搜索

List<Articles> articleList = query.list(); //搜索出来的结果

//====================

//高亮显示部分,其实就是对搜索出来的结果加个红色字体的标签,并且设置一下得到结果的字数

//简单的标签格式化,对搜索的关键字标记

SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<font color=red>""</font>");

//片段,设置要显示的字数

SimpleFragmenter fragmenterTitle = new SimpleFragmenter(10);

SimpleFragmenter fragmenterContent = new SimpleFragmenter(200);

//高亮显示 (用来标记搜索词的html标签,分数查询)

Highlighter highlighter = new Highlighter(formatter,new QueryScorer(titleQuery));//按照分数的高低显示

Highlighter highlighter2 = new Highlighter(formatter,new QueryScorer(contentQuery));

//设置显示的个数

highlighter.setTextFragmenter(fragmenterTitle);

highlighter2.setTextFragmenter(fragmenterContent);

//==============

//把高亮显示的结果设置到pojo类中专门为高亮显示增加的字段中去

for (Articles articles : articleList) {

//得到分数最高的片段(分词器,字段名,要搜索的字段内容)

String title = highlighter.getBestFragment(analyzer"atitle", articles.getAtitle());

String content = highlighter2.getBestFragment(analyzer"acontent", articles.getAcontent());

if(title!=null){

articles.setTitleString(title);//设置pojo类中专门为了高亮显示的字段的值

}

if(content!=null){

articles.setContentString(content);

}

}

return articleList;

}

7如果在servlet中调用不到hibernate-search的方法的话,但是在main函数中可以的话,那就要在hibernate.cfg.xml中配置一句话:

·hibernate.search.default.indexBase

·d:/index(可以不同)

8 对于你要索引的表中已经存了数据的话,那么一定要创建索引,否则会找不到记录,对于没数据的表可以不创建。

Session session = HibernateSessionFactory.getSession();

//得到FullSession

FullTextSession textSession = Search.getFullTextSession(session);

Transaction transaction = null;

Query query = textSession.createQuery("from Articles");//用FullTextSession来创建Query

transaction = textSession.beginTransaction();//开启事务

try {

List<Articles> list = query.list();

for (Articles articles : list) {

textSession.index(articles); //建立索引

}

transaction.commit();

catch (Exception e) {

e.printStackTrace();

transaction.rollback();

}

抱歉!评论已关闭.