Spring

转载自:http://3seefans.iteye.com/blog/438438
关键字: compass
Compass是基于lucene的一个全文搜索框架,我们可以把她看成是封装了lucene的ORM框架。

compass好比hibernate封装了jdbc一样封装了lucene。它可以自动完成实体对象到lucene提供的Document对象的转换,同时Compass隐藏了lucene复杂的多线程模型,避免了索引的同步问题。

类似hibernate,compass提供了Compass,CompassSession,CompassTransaction等对象,其中Compass类似hibernate的SessionFactory,负责拿到CompassSession,而CompassSession提供了create()和delete()方法用于创建和删除索引,find()方法用于实现全文搜索,同时CompassTransaction对其提供事务支持。

compass在项目中的使用:

1.实现对象到Document的映射,利用java 5注释,很容易做到。在对象中已get开头的属性中添加Compass注释@SearchableProperty。

@SearchableProperty包括四个属性:index,sotre,converter,boost。

index表示是否索引改属性。

sotre是否存储该属性。

converter是如何实现属性和string之间的转换。注意,lucene的属性都是string,如果非String类型,则需要手动指定一个转换器。

boost则是指定给属性在索引中的重要性。

注意,对于用于标志实体的唯一标志属性要用@SearchableId标志。

2.定义和编写相关的searchService接口和相关的实现类,定义相关的创建,删除,更新和搜索等方法。

这里可以看下livebookstore中的searcheService的实现:


public class SearchServiceImpl implements SearchService {

private final Log log = LogFactory.getLog(getClass());

private String directory;
private Compass compass;

/**
* 设置索引的存储目录
* @spring.property value="/WEB-INF/compass"
*/
public void setIndexDirectory(Resource resouce) {
try {
File dir = resouce.getFile();
if (!dir.isDirectory()) {
if (!dir.mkdirs()) {
throw new ExceptionInInitializerError("Could not create directory for compass: " + dir.getPath());
}
}
directory = dir.getPath();
log.info("Set compass directory: " + directory);
} catch (IOException e) {
throw new ExceptionInInitializerError(e);
}
}

/**
* 初始化Copass

*/
public void init() {
DateConverter dateConverter = new DateConverter();
dateConverter.setFormat("yyyy-MM-dd");
compass = new CompassAnnotationsConfiguration().setConnection(directory).addClass(Book.class)
.registerConverter("date", dateConverter).buildCompass();
}

/**
* Destroy compass.
*/
public void destroy() {
compass.close();
}

public void index(Book book) {
log.info("Index book...");
CompassSession session = null;
CompassTransaction tx = null;
try {
session = compass.openSession();
tx = session.beginTransaction();
session.create(book);
tx.commit();
log.info("Done.");
} catch (RuntimeException e) {
tx.rollback();
throw e;
} finally {
if (session != null)
session.close();
}
}

public void unindex(Book book) {
log.info("Unindex book...");
CompassSession session = null;
CompassTransaction tx = null;
try {
session = compass.openSession();
tx = session.beginTransaction();
session.delete(book);
tx.commit();
log.info("Done.");
} catch (RuntimeException e) {
tx.rollback();
throw e;
} finally {
if (session != null)
session.close();
}
}

public void reindex(Book book) {
log.info("Reindex book...");
CompassSession session = null;
CompassTransaction tx = null;
try {
session = compass.openSession();
tx = session.beginTransaction();
session.delete(book);
session.create(book);
tx.commit();
log.info("Done.");
} catch (RuntimeException e) {
tx.rollback();
throw e;
} finally {
if (session != null)
session.close();
}
}

@SuppressWarnings("unchecked")
public List<Book> search(String q, Page page) {
if (q == null)
return Collections.EMPTY_LIST;
q = q.trim();
if ("".equals(q))
return Collections.EMPTY_LIST;
CompassSession session = null;
CompassTransaction tx = null;
try {
session = compass.openSession();
tx = session.beginTransaction();
CompassHits hits = session.find(q);
int count = hits.length();
page.setTotalCount(count);
if (count == 0) {
tx.commit();
return Collections.EMPTY_LIST;
}
// fetch hits[start, end):
int start = page.getFirstResult();
int end = start + page.getPageSize();
if (end > count)
end = count;
List<Book> results = new ArrayList<Book>(end - start);
for (int i = start; i < end; i++) {
results.add((Book) hits.data(i));
}
tx.commit();
return results;
} catch (RuntimeException e) {
tx.rollback();
throw e;
} finally {
if (session != null)
session.close();
}
}

}
从上面可以看出,实现一个Compass 是很容易的。

当对象的创建,删除和更新会影响到索引的变化。这里我们就需要在对象的创建,删除和更新的方法中相应的更新索引,也就是调用service的相应创建,删除和更新方法既可以。

3.如果涉及到服务器迁移,或者数据库的改动,修要从新为所有的书籍重建索引。我们只需要得到相关的全部实体,执行其创建方法就可以了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值