COMPASS应用(转)

一个简单的COMPASS应用

首先你要下载Compass framework: Download Compass.
你需要在你的class path 中添加4个jarcompass-x/modules/core/compass-core-x.jar, compass/modules/core/lib/commons-logging-x.jar, compass-x/modules/core/lib/log4j-x.jar, compass-x/modules/core/lib/lucene-core-x-rc1-dev.jar.
在你的项目中创建下面的目录(可以根据自己的定义来改动):

log4j.properties
- org
   - compassframework
     - sample
       - example
           alias.cmd.xml
           compass.cfg.xml
           CompassExample.java
           Phrase.cpm.xml
           Phrase.java
下面说一下几个重要的配置文件

 compass.cfg.xmlcode

指定的target/index 是一个存储目录放索引文件的

(这个类必须有个无参数的构造和id属性)

xml 代码
  1. <!--CTYPE compass-core-configuration PUBLIC"-//Compass/Compass Core Configuration DTD 1.0//EN"    </sp-->
  2.   
  3.   
  4. "http://static.compassframework.org/dtd/compass-core-configuration-1.0.dtd" >     
  5.   
  6.   
  7.   
  8. <   
  9.   
  10. compass-core-configuration >     
  11. < compass >   
  12.   
  13. < setting   name = "compass.engine.connection" > target/index setting >   
  14.   
  15. < meta-data   resource = "org/compassframework/sample/example/alias.cmd.xml"   />   
  16.   
  17. compass >   
  18.   
  19. compass-core-configuration >   
  20.   

 alias.cmd.xml:

xml 代码
  1. <!--sp-->  
  2.   
  3. xml   version = "1.0" ?>     
  4. <!--CTYPE compass-core-meta-data PUBLIC    </sp-->
  5. "-//Compass/Compass Core Meta Data DTD 1.0//EN"    
  6. "http://static.compassframework.org/dtd/compass-core-meta-data-1.0.dtd" >     
  7. < compass-core-meta-data >     
  8. < meta-data-group   id = "example"   displayName = "Example Meta Data" >   
  9.   
  10. < description > Example Meta Data description >   
  11.   
  12. < uri > http://compass/example uri >   
  13.   
  14.   
  15. < alias   id = "phrase"   displayName = "Phrase" >   
  16.   
  17. < description > Phrase alias description >   
  18.   
  19. < uri > http://compass/example/phrase uri >   
  20.   
  21. < name > phrase name >   
  22.   
  23. alias >   
  24.   
  25. < meta-data   id = "author"   displayName = "Author" >   
  26.   
  27. < description > Author alias description >   
  28.   
  29. < uri > http://compass/example/author uri >   
  30.   
  31. < name > author name >   
  32.   
  33. meta-data >   
  34.   
  35. < meta-data   id = "text"   displayName = "Text" >   
  36.   
  37. < description > Text alias description >   
  38.   
  39. < uri > http://compass/example/text uri >   
  40.   
  41. < name > text name >   
  42.   
  43. meta-data >   
  44.   
  45. meta-data-group >   
  46.   
  47. compass-core-meta-data >   

Phrase.java

java 代码
  1. package  org.compassframework.sample.example;   
  2.   
  3. public   class  Phrase {   
  4.   
  5.   private  String author;   
  6.   
  7.   private  String text;   
  8.   
  9.   private  Long id;   
  10.   
  11.   public  Phrase() {   
  12.   
  13.  }   
  14.   
  15.   public  String getAuthor() {   
  16.       return  author;   
  17.  }   
  18.   
  19.   public   void  setAuthor(String author) {   
  20.       this .author = author;   
  21.  }   
  22.   
  23.   public  String getText() {   
  24.       return  text;   
  25.  }   
  26.   
  27.   public   void  setText(String text) {   
  28.       this .text = text;   
  29.  }   
  30.   
  31.   public  Long getId() {   
  32.       return  id;   
  33.  }   
  34.   
  35.   public   void  setId(Long id) {   
  36.       this .id = id;   
  37.  }   
  38.   
  39.   public  String toString() {   
  40.   
  41.       return  text;   
  42.  }   
  43.   
  44. }    
Phrase.cpm.xml

 

xml 代码
  1. <!--sp-->xml version="1.0"?>    
  2. <!--CTYPE compass-core-mapping PUBLIC    </sp-->
  3. "-//Compass/Compass Core Mapping DTD 1.0//EN"    
  4. "http://static.compassframework.org/dtd/compass-core-mapping-1.0.dtd" >     
  5. < compass-core-mapping   package = "org.compassframework.sample.example" >     
  6. < class   name = "Phrase"   alias = "${example.phrase}" >   
  7.   
  8. < id   name = "id"   />   
  9.   
  10. < property   name = "author" >   
  11.   
  12. < meta-data > ${example.author} meta-data >   
  13.   
  14. property >   
  15.   
  16. < property   name = "text" >   
  17.   
  18. < meta-data > ${example.text} meta-data >   
  19.   
  20. property >   
  21.   
  22. class >   
  23.   
  24. compass-core-mapping >   
CompassExample.java

java 代码
  1. package  org.compassframework.sample.example;   
  2.   
  3. import  org.apache.log4j.Logger;   
  4.   
  5. import  org.compassframework.core.Compass;   
  6. import  org.compassframework.core.Compass;   
  7. import  org.compassframework.core.CompassSession;   
  8. import  org.compassframework.core.CompassTransaction;   
  9. import  org.compassframework.core.config.CompassConfiguration;   
  10.   
  11. public   class  CompassExample {   
  12.   
  13.   private   static   final  Logger logger = Logger.getLogger(CompassExample. class );   
  14.     
  15.   private  compasscompass;   
  16.     
  17.   public   static   void  main(String[] args){   
  18.       new  CompassExample().process();   
  19.  }   
  20.     
  21.   private   void  process(){   
  22.      CompassConfiguration config =  new  CompassConfiguration();   
  23.      config.configure( "org/compassframework/sample/example/compass.cfg.xml" );   
  24.      config.addClass(Phrase. class );   
  25.      compass = config.buildCompass();   
  26.      compass.getSearchEngineIndexManager().deleteIndex();   
  27.      compass.getSearchEngineIndexManager().createIndex();   
  28.      
  29.      createIndex();   
  30.      search( "mule AND crazy" );   
  31.      search( "Marisol OR Ramon" );   
  32.  }   
  33.     
  34.   private   void  createIndex(){   
  35.   CompassSession session = compass.openSession();   
  36.      CompassTransaction tx = session.beginTransaction();   
  37.      Phrase phrase =  new  Phrase();   
  38.      phrase.setAuthor( "Joe" );   
  39.      phrase.setText( "I don't think it's nice you laughing. "  +    
  40.              "You see my mule don't like people laughing. "  +    
  41.              "He gets the crazy idea you're laughing at him. "  +    
  42.              "Now if you apologize like I know you're going to, "  +    
  43.              "I might convince him that you really didn't mean it..." );   
  44.      phrase.setId( new  Long( 1 ));   
  45.      session.save(phrase);   
  46.      tx.commit();   
  47.      session.close();   
  48.  }   
  49.     
  50.   private   void  search(String query){   
  51.      CompassSession session = compass.openSession();   
  52.      CompassTransaction tx = session.beginTransaction();   
  53.      Compass hits = session.find(query);   
  54.       if  (logger.isDebugEnabled()) {   
  55.         logger.debug( "search() - Found "  + hits.getLength()+ " hits for \"" +query+ "\"" );   
  56.      }   
  57.       for  ( int  i =  0 ; i < hits.getLength(); i++) {   
  58.         print(hits, i);   
  59.      }   
  60.      hits.close();   
  61.      tx.commit();   
  62.      session.close();   
  63.      compass.close();   
  64.  }   
  65.     
  66.   private   void  print(Compass hits,  int  hitNumber) {   
  67.      Phrase value = (Phrase)hits.data(hitNumber);   
  68.       if  (logger.isDebugEnabled()) {   
  69.          logger.debug( "print() - Phrase by "  + value.getAuthor() +  ": "   
  70.              + value.getText());   
  71.      }   
  72.  }   
  73.   
  74. }   
  75.   

 

为了保证能打印出我们的测试,需要加入log4j.properties
log4j.rootLogger=ERROR, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p %c - %m%n
log4j.logger.org.compassframework.sample.example=DEBUG


下面是打印出来的测试结果:
search() - Found 1 hits for "mule AND crazy"
print() - Phrase by Joe: I don't think it's nice you laughing...
search() - Found 0 hits for "Marisol OR Ramon"

posted on 2007-01-29 18:12 leoli 阅读(493) 评论(0)  编辑  收藏 所属分类: Frame


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

统计

常用链接

留言簿(6)

随笔分类

随笔档案(17)

文章分类(86)

收藏夹(3)

flex blog

good site

java blog

my friend

tools

抓虾

搜索

最新评论

阅读排行榜

评论排行榜