Posted on 2006-09-28 10:18
城市劣人 閱讀(1954)
評論(2) 編輯 收藏
已經轉移到
好·色之徒--我的博客、我的生活compass compass是開源的java搜索引擎框架,建立在
Lucene搜索引擎的基礎之上的。是對Lucene搜索引擎在企業應用(數據庫應用)中的增強。 在
全文檢索(lucene)開發和
關于jforum2.1.6的檢索問題(采用lucene實現)兩篇文章中都是討論關于全文檢索的。這里想描述一下如何利用compass框架來實現這一功能。 在實際的項目中,一般都會涉及到數據庫,如何保證數據庫的增刪改變實時的映射到索引文件中去,compass就是最佳的選擇。如果你采用了Hibernate等ORM方案,你只須在POJO中進行annotation注釋,或者采用AOP的方式,自動變更索引。如果你采用了JDBC,也可以在XML文件中加以配置,Compasss定時進行更新。Compasss還支持事務處理。沒有Compasss的話,一般會采用比如深夜重建一次索引,或是采用一個線程類來定時建立或重建索引,在
關于jforum2.1.6的檢索問題(采用lucene實現)中就是這樣實現的,可以去參考一下。當然從效率、實時性、安全性來說,Compasss是一個好的選擇,而且開發起來也比較方便。 Compasss和Spring很好的結合了起來,在Compasss的包內專門設置了Spring的相關操作,比如和Spring MVC的操作:org.compass.spring.web.mvc.CompassIndexController來建立索引,如果換成Struts來開發的話,需要作些調整,當然動作不會很大,模仿CompassIndexController就可以了。 這里給出一個小例子,以作參考,是采用了Hibernate-ORM方案實現,同時采用了Spring、Struts:首先是POJO類: @Searchable(alias = "customer") public class Customer { @SearchableId private String guid; @SearchableProperty(name = "customerName") private String customerName; ...... } 然后是Compass的具體實現,由于采用了Struts,所以改造相應的建立索引和檢索的類。 /** * 仿照 {@link org.compass.spring.web.mvc.CompassIndexController} * 中的代碼,構建了一個Service(建立索引),方便不使用Spring MVC的實現 * * @author Schweigen * @see org.compass.spring.web.mvc.CompassIndexController * @see org.compass.spring.web.mvc.AbstractCompassGpsCommandController */ public class CompassIndexService implements InitializingBean { private CompassGps compassGps; public void afterPropertiesSet() throws Exception { if (compassGps == null) { throw new IllegalArgumentException("Must set the compassGpd property"); } } /** * 建立索引的接口 * * @param command 里面doIndex 為"true",則執行構建索引,并將構建時間封裝入{@link org.compass.spring.web.mvc.CompassIndexResults} * 中
* 否則,不做任何操作,返回null * @return * @see org.compass.spring.web.mvc.CompassIndexResults */ public CompassIndexResults index(CompassIndexCommand command) { if (StringUtils.isBlank(command.getDoIndex()) || !command.getDoIndex().equalsIgnoreCase("true")) { return null; } long time = System.currentTimeMillis(); // 建立索引 getCompassGps().index(); time = System.currentTimeMillis() - time; CompassIndexResults indexResults = new CompassIndexResults(time); return indexResults; } public CompassGps getCompassGps() { return compassGps; } public void setCompassGps(CompassGps compassGps) { this.compassGps = compassGps; } } 然后建立一個檢索的類CompassSearchService,這里比較長,就不列出來了,可以去提供下載的地方一并下載 最后是有關Spring的配置,這個很重要,相關的服務都在這里進行配置,這里就不列出了,可提供下載,具體的有一些注釋描述,需要Spring的相關知識,可以自行閱讀,這里采用了springside的相關實現,以在實際項目中使用。
可參見 已經轉移到
好·色之徒--我的博客、我的生活