| « | November 2025 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | | | |
| 公告 |
| 本博客在此声明所有文章均为转摘,只做资料收集使用。并无其他商业用途。 |
| Blog信息 |
|
blog名称: 日志总数:210 评论数量:205 留言数量:-19 访问次数:927876 建立时间:2007年5月10日 |

| |
|
[Compass]Compass VS Lucene 文章收藏, 网上资源, 电脑与网络
李小白 发表于 2007/5/10 15:15:19 |
| 已加入Opensymphony的Compass 是对Lucene搜索引擎在企业应用(数据库应用)中的增强。 Lucene本身的API已经非常简单,看看IBM DW上的Beef up Web search apps with Lucene已经大概了解,那Compass还能做什么样的增强呢?
1.在我的项目里,最实际的增强就是Data Mirror功能。
DataMirror 会把数据库的增删改变化实时映射到索引文件中。如果你采用Hibernate等ORM方案,你只须在POJO中进行annotation注释, Compass就会与Hibernate的event机制结合,或者使用AOP的方式,自动在数据库增删改时变更索引;如果你只是采用JDBC,也可以在 XML文件配置Table Mapping或ResultSet Mapping,指定version列,Compasss定时进行索引更新。而且,Compass还支持事务,在查询数据库遍历结果集的过程中如果出现异常,会在Index Segments 文件一级进行事务控制。
如果没有Compass,我们一般会在每天深夜重建一次索引。相比Compass的做法, 一来反应迟缓,平均延时半天; 二来效率没有Compass高。如果采用完全重建索引,效率就不用说了。如果进行增量索引,就要增加一个字段,在数据更新时进行特殊的处理,删除时也不能直接删除数据,要等lucene删完索引它才能删除,这样Lucene对应用就非常不透明了。 三来不支持事务,如果建立索引过程中出现异常,索引文件的状态是不可控的。
2.Compass对查询的API也作了一定简化,可以考虑使用。
详见参考文档 10.5 Searching, 简单直接用session find,如
CompassHits hits = session.find( " name:jack " );
加入排序,改分词Analyzer,用QueryBuilder()
500)this.width=500'>500)this.width=500" align=top border=0> CompassHits hits = session.createQueryBuilder()500)this.width=500'>500)this.width=500" align=top border=0> .queryString( " +name:jack +familyName:london " )500)this.width=500'>500)this.width=500" align=top border=0> .setAnalyzer( " an1 " ) // use a different analyzer 500)this.width=500'>500)this.width=500" align=top border=0> .toQuery()500)this.width=500'>500)this.width=500" align=top border=0> .addSort( " familyName " , CompassQuery.SortPropertyType.STRING)500)this.width=500'>500)this.width=500" align=top border=0> .hits();500)this.width=500'>500)this.width=500" align=top border=0>
3. 一段Pragmatic的Compass 搜索程序是这样写的:
1.用annotation将pojo映射为searchable。(详细请看参考文档,如果没有JDK5,可以参考XML式的配置)
public class Product { @SearchableId private Integer id; private String name; @SearchableProperty(name = " name " ) public String getName() { return this .name; }}
2.用Compass提供的Spring2 Schema 来配置Compass与Hibernate,Spring的结合。 SchemaBase的配置是Spring 2.0的新特征,相比原来的配置代码要少一些。
3.编写搜索结果显示页,将Hits,Command,Page三个变量显示出来。 Compass的代码重用已经到了Controller一级,只要给Controller 配上compass bean和结果显示的jsp就可以了。Controller提供足够的配置参数,使它完全可以被配置重用,这是个值得SpringSide学习的地方。 即使你的web应用不是采用Spring MVC,如果没有大规模改写的需求,也可以直接使用,让Struts与Spring MVC并存。
4.Controller默认的查询需要扩展时
Contrller默认的查询是在所有fileld里查询关键字,如果需要限定field,加入排序,加入and ,exclude,模糊查询等就不适用了。高级搜索页一般会提供比较多的过滤条件输入框让用户输入,然后在Controller对这些输入条件进行重新组合。
组合的方式既可以按Lucene的Query语法进行拼SQL式的组合,最后得到"+name:jack -familyName:london" 这样的句子。
也可以用类似Hibernate Criteria API的方式,如:
CompassHits hits = session.createQueryBuilder().bool() .addMust( queryBuilder.term( " name " , " jack " ) ) .addMustNot( queryBuilder.term( " familyName " , " london " ) ) .toQuery() .hits();
因此,如果你的Lucene应用是面向数据库的,就不妨用一下Compass。Compass另一个让我学习的地方是它充分考虑用户客户化的需要,enough thing can be configure ,从而连Controller也可以重用的做法。 |
|
|