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 例子.