Hibernate Search 注解实例

pojo 对象 News


import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;

@Entity
@Table(name="t_news")
@Indexed
public class News {

 @Id
 @GeneratedValue(strategy=GenerationType.AUTO)
 @DocumentId
 private int id;
 @Field(store=Store.YES)
 private String title;
 @Field
 private String context;
 
 private Date date;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public String getContext() {
  return context;
 }

 public void setContext(String context) {
  this.context = context;
 }

 public Date getDate() {
  return date;
 }

 public void setDate(Date date) {
  this.date = date;
 }
 
}

 

hibernate 配置文件

<?xml version="1.0" encoding="ISO-8859-1"?>
<!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.connection.driver_class">
  com.mysql.jdbc.Driver
 </property>
 <property name="hibernate.connection.url">
  jdbc:mysql://localhost/test
 </property>
 <property name="hibernate.connection.username">root</property>
 <property name="hibernate.dialect">
  org.hibernate.dialect.MySQLDialect
 </property>
 <property name="hibernate.show_sql">true</property>
 <property name="hibernate.search.default.directory_provider">
  org.hibernate.search.store.FSDirectoryProvider
 </property>
 <property name="hibernate.search.default.indexBase">
  D:/temp/index
 </property>
 <mapping class="com.search.entity.News" />
</session-factory>
</hibernate-configuration>

 

测试类 NewsTest


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Query;
import org.hibernate.Session;
import org.hibernate.search.FullTextQuery;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;

import com.search.dao.NewsDao;
import com.search.entity.News;
import com.search.utils.HibernateUtiles;

public class NewsTest{

 public void addNews(News news) {
  // TODO Auto-generated method stub
 }

 public void createIndex(){
  Session session=HibernateUtiles.getSession();
     FullTextSession fts=Search.createFullTextSession(session);
     fts.getTransaction().begin();
     List<News> newsList = session.createQuery("from News").list();
     for (News news : newsList) {
   fts.index(news);
  }
     fts.getTransaction().commit();
 }
 
 public List<News> search(String key) {
  // TODO Auto-generated method stub
  List<News> result =new ArrayList<News>();
  Map<String, String> map=new HashMap<String, String>();
  map.put("title", "我");
  map.put("context", "我");
  String[] str={"title","context"};
  try {
   QueryParser parser = new MultiFieldQueryParser(str, new StandardAnalyzer(),map);
   Query luceneQuery = parser.parse("context:我");
      FullTextSession s = Search.createFullTextSession(HibernateUtiles.getSession());
      FullTextQuery query = s.createFullTextQuery(luceneQuery, News.class);
      result = query.list();
      for (News news : result) {
   System.out.println("id:"+news.getId());
   }
     
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return result;
 }

 public static void main(String[] args) {
  new NewsTest().search("");
 }
 
}
这个一个最简单的 hibernate search 例子.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值