好·色之徒

          全文檢索compass

          Posted on 2006-09-28 10:18 城市劣人 閱讀(1959) 評論(2)  編輯  收藏
          已經(jīng)轉(zhuǎn)移到 好·色之徒--我的博客、我的生活

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

          Feedback

          # re: 全文檢索compass  回復  更多評論   

          2006-10-17 15:34 by 小白
          發(fā)個整合spring的來看看 還有compassDaoSupport

          # re: 全文檢索compass  回復  更多評論   

          2006-10-18 09:15 by 城市劣人
          不好意思,并沒有整合spring,只是學習compass中整合spring的部分,compassDaoSupport也是compass中提供的

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           

          posts - 39, comments - 91, trackbacks - 0, articles - 1

          Copyright © 城市劣人

          好·色之徒
          主站蜘蛛池模板: 宁国市| 道真| 陆河县| 凤凰县| 新丰县| 新宾| 贺兰县| 台东市| 措勤县| 灯塔市| 石渠县| 江永县| 江源县| 襄垣县| 兰考县| 高雄县| 福鼎市| 乐都县| 象山县| 额敏县| 永泰县| 彭州市| 晋州市| 池州市| 偃师市| 安远县| 旬阳县| 宣化县| 北安市| 肥东县| 太康县| 陆河县| 奇台县| 酒泉市| 林周县| 乐业县| 友谊县| 彭阳县| 全南县| 石家庄市| 都兰县|