码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate Search JPA + Lucene 简单应用实例

时间:2015-05-27 16:09:42      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

假设现在有这么一个应用场景。数据库里存放了大量文章,我们想要通过输入关键字,从数据库中检索出相关的文章。

1.建立Article Entity:

public class Article {
	private String id;            //ID
	private String title;         //标题
	private String content;       //内容
        private String isPublication; //发布状态
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getIsPublication() {
		return isPublication;
	}
	public void setIsPublication(String isPublication) {
		this.isPublication = isPublication;
	}
}


2.建立索引:

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.Search;

public class IndexArticle {
	
	@PersistenceContext
	protected EntityManager entityManager;
	
	public void indexArticle(){
//		......具体编码不细写了
//		List<Article> articles = articleService.findList(null, null, null, first, count);
//		for (Article article : articless) {
//			index(article);
//		}
	}
	
	public void index(Article article){
		//Hibernate Search JPA
		FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
		fullTextEntityManager.index(articles);
	}
}


3.检索关键字:

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.util.Version;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.FullTextQuery;
import org.hibernate.search.jpa.Search;
import org.wltea.analyzer.lucene.IKAnalyzer;

import com.dowik.dwshop.mall.entity.Article;

public class SearchArticle {
	
	@PersistenceContext
	protected EntityManager entityManager;

	public List<Article> searchResult(String keyword){
		
		try {
			
			//要搜索的关键词
			keyword = QueryParser.escape(keyword);
			
			//解析器,中文分词工具
			QueryParser titleParser = new QueryParser(Version.LUCENE_35, "title", new IKAnalyzer());
			titleParser.setDefaultOperator(QueryParser.AND_OPERATOR);
			Query titleQuery = titleParser.parse(keyword);
			
			//模糊搜索标题
			FuzzyQuery titleFuzzyQuery = new FuzzyQuery(new Term("title", keyword), 0.5F);
			
			//按词条搜索内容
			Query contentQuery = new TermQuery(new Term("content", keyword));
			
			//按词条搜索发布状态
			Query isPublicationQuery = new TermQuery(new Term("isPublication", "true"));
			
			//全文检索(标题搜索+内容搜索)
			BooleanQuery textQuery = new BooleanQuery();
			textQuery.add(titleQuery, Occur.SHOULD);
			textQuery.add(titleFuzzyQuery, Occur.SHOULD);
			textQuery.add(contentQuery, Occur.SHOULD);
			
			//发布状态+全文检索
			BooleanQuery query = new BooleanQuery();
			query.add(isPublicationQuery, Occur.MUST);
			query.add(textQuery, Occur.MUST);
			
			//Hibernate Search JPA
			FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
			FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query, Articles.class);
			
			List<Article> resultList = fullTextQuery.getResultList();
			
			return resultList;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return null;
	}
}


以上三个步骤构建了一个简单的 Hibernate Search JPA + Lucene 简单应用实例。

Hibernate Search JPA + Lucene 简单应用实例

标签:

原文地址:http://my.oschina.net/lock0818/blog/420574

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!