Compass入门及其与Spring、iBatis的整合 spring mvc ibatis

开始之前

本文是Compass的入门指引,通过实例介绍了Compass与iBatis、Spring的整合,适合不了解Compass的读者,但要求读者了解Lucene、Spring和iBatis,写过一些简单的应用。

什么是Compass

Compass是一个Java搜索框架。它封装了Lucene,增加了一些Lucene不支持的特性(例如实时更新索引),支持各种数据(Java对象、xml、json)到索引的映射,支持各种数据源(JDBC,Hibernate, iBatis)。

与Spring、iBatis的整合

建索引

1、假设Spring + iBatis的框架已经搭建好。

2、配置Domain的OSEM

其实就是加几个Annotation而已。看到Index.ANALYZED、Store.YES这些东西,用过Lucene的应该大概都明白了吧。

3、建立LocalCompassBean,配置索引文件存放路径和进行映射的domain。

  <bean id="compass"><property name="compassSettings"><props><!-- 索引文件保存路径 --><prop key="compass.engine.connection">/home/index/compasstest</prop></props></property><property name="classMappings"> <!-- 进行映射的domain --><list><value>ren.domain.User</value><value>ren.domain.Book</value></list></property></bean>

4、建立SqlMapClientGpsDevice,配置iBatis的sqlMapClient和获取数据进行索引的SQL语句id。

  <bean id="ibatisGpsDevice"><property name="name" value="ibatis" /><property name="sqlMapClient"><ref bean="sqlMapClient" /> <!-- 引用项目中已经定义的ibatis的sqlMapClient --></property><property name="selectStatementsIds"> <!-- 对这些SQL查询的结果进行索引 --><list><value>user.getAllUsers</value><value>book.getAllBooks</value></list></property></bean>

5、建立CompassGps(SingleCompassGps或DualCompassGps),引用前面的compass和device。

  <bean id="compassGps"init-method="start" destroy-method="stop"><property name="compass" ref="compass" /><property name="gpsDevices"><list><ref local="ibatisGpsDevice"/></list></property></bean>


6、最后,直接调用CompassGps.index()方法建立索引。

@Component@Qualifier("indexBuilder")public class IndexBuilder {@Autowired@Qualifier("compassGps")private CompassGps compassGps;public void buildIndex() {compassGps.index(); // 一行代码搞定}}

查索引

1、建立CompassTemplate,引用LocalCompassBean。

  <bean id="compassTemplate"><property name="compass"><ref bean="compass" /></property></bean>

2、使用CompassTemplate.execute(CompassCallback<T>)进行查询。

@Component@Qualifier("indexSearcher")public class IndexSearcher {@Autowired@Qualifier("compassTemplate")private CompassTemplate compassTemplate;public List<User> searchUser(final String name, final String gender, final int age) {return compassTemplate.execute(new CompassCallback<List<User>>() {public List<User> doInCompass(CompassSession session) throws CompassException {CompassQueryBuilder builder = session.queryBuilder();String queryString = "";if (!StringUtils.isBlank(name)) {queryString += "and user.name:" + name;}if (!StringUtils.isBlank(gender)) {queryString += "and user.gender:" + gender;}if (age > 0) {queryString += "and user.age:" + age;}CompassQuery query = builder.queryString(queryString).toQuery();query.addSort("user.age", SortPropertyType.INT, SortDirection.REVERSE);CompassHits hits = query.hits();List<User> list = new ArrayList<User>();for (CompassHit hit : hits) {list.add((User)hit.data());}return list;}});}}

拼查询字符串这里写得比较累赘,小朋友不要学~

与Lucene的比较

1、Compass有比Lucene更易用的API(废话,封装了Lucene嘛),例如支持直接更新记录(因为resource类似数据库记录,含有主键)。像上面的建索引过程,如果用Lucene,肯定得写很多Java代码。

2、支持整合各种ORM框架和Spring,减少了代码量。例如上面例子中整合iBatis,直接几行配置就搞定了。

Compass入门及其与Spring、iBatis的整合 spring mvc ibatis
3、效率问题?感觉Lucene的API用起来老是不顺手,Compass这样封装虽然方便了,但有些担心会不会降低了性能,于是做了个简单的测试,分别索引4万条记录,结果是
Compass: 12203 ms.
Lucene: 9797 ms.

Compass比Lucene慢了大约25%,当然这个测试十分粗略,结果仅供参考。

经验总结

1、对多个表建索引后进行搜索,在添加排序条件时,如果不指定SortPropertyType,那么在没有指定converter的字段上排序时会抛Exception:
java.lang.RuntimeException: field "gender" does not appear to beindexed
但如果只对单个表建索引,不会有这个问题。应该是Compass的一个bug,不知道新版本有没有解决。

2、最好自己封装排序字段和分页。

3、总结,Compass比较适用于逻辑不太复杂的应用,会比Lucene少写很多代码。但如果需要一些较为特殊的需求,或者对效率要求比较高,还是用Lucene吧。

相关资源

大型网站的Lucene应用:http://www.luanxiang.org/blog/archives/605.html



  

爱华网本文地址 » http://www.aihuau.com/a/25101013/155031.html

更多阅读

特许经营与代理、经销的区别;代理和代销的区别 特许经销商

代理人是根据他人授权,按他人意志行事的人。授权的范围可能很小,只是完成两三样特定的工作,也可以通过普遍授权,使代理人拥有充分的行动权力。最根本的一点,代理人不是根据自己的利益行事,在第三方看来,代理人与授权人是没有区别的,代理人所

高中生英语学习信念及其与英语学习行为的关系

宋志燕/王耘/侯怀银【专题名称】中学外语教与学【专 题 号】G381【复印期号】2008年06期【原文出处】《山西师大学报:社科版》(临汾)2008年1期第143~146页【作者简介】宋志燕,山西长治学院社科系,046000;王耘,北京师范大学认知神经科学与

基督各宗教及其与犹太教、伊斯兰教的关系 伊斯兰教与犹太教

许多人总是问我同样的问题:天主教和基督教有什么不同,为什么都是十字架?看到天主教徒老说“圣母马利亚”,是不是他们信的是耶稣母亲,而你们基督教信耶稣?甚至有一个天主教徒知道了我信基督以后直接质问我:你为什么不信天主教?天主教比你们基

回族与伊斯兰教、穆斯林的关系转贴 回族和穆斯林的关系

转载自回族在线长期以来,在文化上一直处于劣势的回族大众几乎没有解本民族的知识的途径,因此无论一些舆论对我们民族做出怎样的诠释和宣传(如把回族等同与穆斯林),民间很少又人去真正的思考和质疑。零散分布在各地的回族大众,多数为了生

知己与情人、与老婆的区别 情人和知己的区别

(一)知己与情人的区别情人是任何人都可以拥有的,知己却是少部分人的专利。知己偏重于思想性方面的共鸣,情人则是偏重情感方面的互动,是填补内心世界及情感的空虚,含有利益关系,但是遇到其他因素,这种关系会不存在。知己是心有灵犀、知己能理

声明:《Compass入门及其与Spring、iBatis的整合 spring mvc ibatis》为网友狂风中拥抱你分享!如侵犯到您的合法权益请联系我们删除