1. 创建索引
/**
* 模拟数据库中取出一条记录后创建索引库
*/
@Test
public void createIndex() {
Article article = new Article();
article.setId(2l);
article.setTitle("compass对lucene搜索的包装试验");
article.setContent("本例对compass包装后的lucene的功能进行测试,从而试验常用compass的方法");
article.setPostTime(new Date());
CompassSession ss = null;
ss = CompassUtils.getCompass().openSession();//开启compass会话
CompassTransaction tx = null;
try {
tx = ss.beginTransaction();//开启事务
ss.create(article);//创建索引的compass核心代码
tx.commit();//提交事务
} catch (CompassException e) {
if (tx != null) {// 注意一定要判断
tx.rollback();// 回滚事务
}
} finally {
if (ss!=null) {
ss.close();
}
}
}
2. 搜索
/**
* 检索索引库后输出匹配的总记录数及所有文档内容
*/
@Test
public void search(){
CompassSession ss = null; //compass会话
ss = CompassUtils.getCompass().openSession();//
CompassTransaction tx = null;//compass事务
List<Article> articles=new ArrayList<Article>();//匹配结果集
try {
CompassHits hits=ss.find("compass");//compass检索核心语句
int count =hits.length();//匹配的总记录数
System.out.println("匹配的总记录数为"+count);
for (int i = 0; i < count; i++) {
Article art=(Article) hits.data(i);//取出真实的文档内容
articles.add(art);//每一条记录添加到集合中
}
for (Article art : articles) {
System.out.println("id--->"+art.getId());
System.out.println("title--->"+art.getTitle());
System.out.println("content--->"+art.getContent());
System.out.println("postTime--->"+DateTools.dateToString(art.getPostTime(), Resolution.SECOND));//转换日期为精确到DAY/SECOND的字符串
}
} catch (CompassException e) {
e.printStackTrace();
} finally {
if (ss!=null) {
ss.close();
}
}
}
}
//DateTools日期工具类可以实现日期到字符串的相互转换(可精确到某天/时/秒)
/**
* 模拟数据库中取出一条记录后创建索引库
*/
@Test
public void createIndex() {
Article article = new Article();
article.setId(2l);
article.setTitle("compass对lucene搜索的包装试验");
article.setContent("本例对compass包装后的lucene的功能进行测试,从而试验常用compass的方法");
article.setPostTime(new Date());
CompassSession ss = null;
ss = CompassUtils.getCompass().openSession();//开启compass会话
CompassTransaction tx = null;
try {
tx = ss.beginTransaction();//开启事务
ss.create(article);//创建索引的compass核心代码
tx.commit();//提交事务
} catch (CompassException e) {
if (tx != null) {// 注意一定要判断
tx.rollback();// 回滚事务
}
} finally {
if (ss!=null) {
ss.close();
}
}
}
2. 搜索
/**
* 检索索引库后输出匹配的总记录数及所有文档内容
*/
@Test
public void search(){
CompassSession ss = null; //compass会话
ss = CompassUtils.getCompass().openSession();//
CompassTransaction tx = null;//compass事务
List<Article> articles=new ArrayList<Article>();//匹配结果集
try {
CompassHits hits=ss.find("compass");//compass检索核心语句
int count =hits.length();//匹配的总记录数
System.out.println("匹配的总记录数为"+count);
for (int i = 0; i < count; i++) {
Article art=(Article) hits.data(i);//取出真实的文档内容
articles.add(art);//每一条记录添加到集合中
}
for (Article art : articles) {
System.out.println("id--->"+art.getId());
System.out.println("title--->"+art.getTitle());
System.out.println("content--->"+art.getContent());
System.out.println("postTime--->"+DateTools.dateToString(art.getPostTime(), Resolution.SECOND));//转换日期为精确到DAY/SECOND的字符串
}
} catch (CompassException e) {
e.printStackTrace();
} finally {
if (ss!=null) {
ss.close();
}
}
}
}
//DateTools日期工具类可以实现日期到字符串的相互转换(可精确到某天/时/秒)