转载自: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.如果涉及到服务器迁移,或者数据库的改动,修要从新为所有的书籍重建索引。我们只需要得到相关的全部实体,执行其创建方法就可以了。
分享到:
相关推荐
Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序。它提供了很多方面的功能,比如依赖注入、面向方面编程(AOP)、数据访问抽象及ASP.NET扩展等等。Spring.NET以Java版的Spring框架为...
spring
spring3.1官方所有的jar包 org.springframework.aop-3.1.RELEASE.jar org.springframework.asm-3.1.RELEASE.jar org.springframework.aspects-3.1.RELEASE.jar org.springframework.beans-3.1.RELEASE.jar org....
这篇文章将教你快速地上手使用 Spring 框架. 如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦! 首先, 我需要在你心里建立起 Spring...
Getting started with Spring Framework (4th Edition) is a hands-on guide to begin developing applications using Spring Framework 5. The examples (consisting of 88 sample projects) that accompany this ...
项目原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6 二、 项目目的: 整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + ...
Spring源代码解析(一):Spring中的事务处理 Spring源代码解析(二):ioc容器在Web容器中的启动 Spring源代码分析(三):Spring JDBC Spring源代码解析(四):Spring MVC Spring源代码解析(五):Spring AOP获取Proxy ...
spring3.0.0相关jar包 org.springframework.aop-3.0.0.RELEASE org.springframework.asm-3.0.0.RELEASE org.springframework.aspects-3.0.0.RELEASE org.springframework.beans-3.0.0.RELEASE org.springframework....
Spring boot 是 Spring 的一套快速配置脚手架,可以基于spring boot 快速开发单个微服务,Spring Cloud是一个基于Spring Boot实现的云应用开发工具;Spring boot专注于快速、方便集成的单个个体,Spring Cloud是关注...
spring 3.2.4 Realease 的所有jar包: spring-context-3.2.4.RELEASE.jar spring-core-3.2.4.RELEASE.jar spring-beans-3.2.4.RELEASE.jar spring-test-3.2.4.RELEASE.jar spring-web-3.2.4.RELEASE.jar spring-aop-...
Classes contained in spring-mock.jar: org.springframework.mock.jndi.ExpectedLookupTemplate.class org.springframework.mock.jndi.SimpleNamingContext.class org.springframework.mock.jndi....
开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...
spring4.3.1全套jar下载。 4.3.1/spring-aop-4.3.1.RELEASE.jar 4.3.1/spring-aspects-4.3.1.RELEASE.jar 4.3.1/spring-beans-4.3.1.RELEASE.jar 4.3.1/spring-context-4.3.1.RELEASE.jar 4.3.1/spring-core-4.3.1....
基于spring cloud 和vue全家桶的开源电商源码基于spring cloud 和vue全家桶的开源电商源码基于spring cloud 和vue全家桶的开源电商源码基于spring cloud 和vue全家桶的开源电商源码基于spring cloud 和vue全家桶的...
Spring技术内幕:深入解析Spring架构与设计原理(第2部分) 《Spring技术内幕:深入解析Spring架构与设计原理》是Spring领域的问鼎之作,由业界拥有10余年开发经验的资深Java专家亲自执笔!Java开发者社区和Spring...
Spring in Action CN.001<br>Spring in Action CN.002<br>Spring in Action CN.003<br>Spring in Action CN.004<br>Spring in Action CN.005<br>Spring in Action CN.006<br>Spring in Action CN.007<br>Spring in ...
org.springframework.aop-3.0.4.RELEASE.jar org.springframework.asm-3.0.4.RELEASE.jar org.springframework.aspects-3.0.4.RELEASE.jar org.springframework.beans-3.0.4.RELEASE.jar org.springframework....
一篇很好的springCloud学习的思维导读,详细的介绍了,springCloud的搭建步骤以及各组件的说明讲解 涵盖 Eureka服务注册与发现 Zookeeper服务注册与发现 Consul服务注册与发现 Ribbon负载均衡服务调用 OpenFeign...
spring boot , spring cloud alibaba, spring cloub 版本选型
本书由浅入深,循序渐进地介绍了Spring的体系结构和相关知识点,目的是帮助初学者快速掌握Spring,并能使用Spring进行应用程序的开发。本书最大的特色在于每章都是由浅入深,从一个简单的示例入手,让读者快速了解本...