Posted on 2006-09-28 10:18
城市劣人 閱讀(1954)
評(píng)論(2) 編輯 收藏
已經(jīng)轉(zhuǎn)移到
好·色之徒--我的博客、我的生活compass compass是開源的java搜索引擎框架,建立在
Lucene搜索引擎的基礎(chǔ)之上的。是對(duì)Lucene搜索引擎在企業(yè)應(yīng)用(數(shù)據(jù)庫(kù)應(yīng)用)中的增強(qiáng)。 在
全文檢索(lucene)開發(fā)和
關(guān)于jforum2.1.6的檢索問(wèn)題(采用lucene實(shí)現(xiàn))兩篇文章中都是討論關(guān)于全文檢索的。這里想描述一下如何利用compass框架來(lái)實(shí)現(xiàn)這一功能。 在實(shí)際的項(xiàng)目中,一般都會(huì)涉及到數(shù)據(jù)庫(kù),如何保證數(shù)據(jù)庫(kù)的增刪改變實(shí)時(shí)的映射到索引文件中去,compass就是最佳的選擇。如果你采用了Hibernate等ORM方案,你只須在POJO中進(jìn)行annotation注釋,或者采用AOP的方式,自動(dòng)變更索引。如果你采用了JDBC,也可以在XML文件中加以配置,Compasss定時(shí)進(jìn)行更新。Compasss還支持事務(wù)處理。沒有Compasss的話,一般會(huì)采用比如深夜重建一次索引,或是采用一個(gè)線程類來(lái)定時(shí)建立或重建索引,在
關(guān)于jforum2.1.6的檢索問(wèn)題(采用lucene實(shí)現(xiàn))中就是這樣實(shí)現(xiàn)的,可以去參考一下。當(dāng)然從效率、實(shí)時(shí)性、安全性來(lái)說(shuō),Compasss是一個(gè)好的選擇,而且開發(fā)起來(lái)也比較方便。 Compasss和Spring很好的結(jié)合了起來(lái),在Compasss的包內(nèi)專門設(shè)置了Spring的相關(guān)操作,比如和Spring MVC的操作:org.compass.spring.web.mvc.CompassIndexController來(lái)建立索引,如果換成Struts來(lái)開發(fā)的話,需要作些調(diào)整,當(dāng)然動(dòng)作不會(huì)很大,模仿CompassIndexController就可以了。 這里給出一個(gè)小例子,以作參考,是采用了Hibernate-ORM方案實(shí)現(xiàn),同時(shí)采用了Spring、Struts:首先是POJO類: @Searchable(alias = "customer") public class Customer { @SearchableId private String guid; @SearchableProperty(name = "customerName") private String customerName; ...... } 然后是Compass的具體實(shí)現(xiàn),由于采用了Struts,所以改造相應(yīng)的建立索引和檢索的類。 /** * 仿照 {@link org.compass.spring.web.mvc.CompassIndexController} * 中的代碼,構(gòu)建了一個(gè)Service(建立索引),方便不使用Spring MVC的實(shí)現(xiàn) * * @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",則執(zhí)行構(gòu)建索引,并將構(gòu)建時(shí)間封裝入{@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; } } 然后建立一個(gè)檢索的類CompassSearchService,這里比較長(zhǎng),就不列出來(lái)了,可以去提供下載的地方一并下載 最后是有關(guān)Spring的配置,這個(gè)很重要,相關(guān)的服務(wù)都在這里進(jìn)行配置,這里就不列出了,可提供下載,具體的有一些注釋描述,需要Spring的相關(guān)知識(shí),可以自行閱讀,這里采用了springside的相關(guān)實(shí)現(xiàn),以在實(shí)際項(xiàng)目中使用。
可參見 已經(jīng)轉(zhuǎn)移到
好·色之徒--我的博客、我的生活