本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7593729
建立时间:2006年5月29日




[OpenSymphony]一个简单的COMPASS应用
软件技术

lhwork 发表于 2007/1/25 9:38:41

首先你要下载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 代码 <!--CTYPE compass-core-configuration PUBLIC"-//Compass/Compass Core Configuration DTD 1.0//EN"    </sp-->       "http://static.compassframework.org/dtd/compass-core-configuration-1.0.dtd">              <      compass-core-configuration>     <compass>      <setting name="compass.engine.connection">target/indexsetting>      <meta-data resource="org/compassframework/sample/example/alias.cmd.xml" />      compass>      compass-core-configuration>       alias.cmd.xml: xml 代码 <!--sp-->      xml version="1.0"?>     <!--CTYPE compass-core-meta-data PUBLIC    </sp--> "-//Compass/Compass Core Meta Data DTD 1.0//EN"     "http://static.compassframework.org/dtd/compass-core-meta-data-1.0.dtd">     <compass-core-meta-data>     <meta-data-group id="example" displayName="Example Meta Data">      <description>Example Meta Datadescription>      <uri>http://compass/exampleuri>         <alias id="phrase" displayName="Phrase">      <description>Phrase aliasdescription>      <uri>http://compass/example/phraseuri>      <name>phrasename>      alias>      <meta-data id="author" displayName="Author">      <description>Author aliasdescription>      <uri>http://compass/example/authoruri>      <name>authorname>      meta-data>      <meta-data id="text" displayName="Text">      <description>Text aliasdescription>      <uri>http://compass/example/texturi>      <name>textname>      meta-data>      meta-data-group>      compass-core-meta-data>   Phrase.java java 代码 package org.compassframework.sample.example;       public class Phrase {        private String author;        private String text;        private Long id;        public Phrase() {        }        public String getAuthor() {         return author;     }        public void setAuthor(String author) {         this.author = author;     }        public String getText() {         return text;     }        public void setText(String text) {         this.text = text;     }        public Long getId() {         return id;     }        public void setId(Long id) {         this.id = id;     }        public String toString() {            return text;     }       }     Phrase.cpm.xml   xml 代码 <!--sp-->xml version="1.0"?>     <!--CTYPE compass-core-mapping PUBLIC    </sp--> "-//Compass/Compass Core Mapping DTD 1.0//EN"     "http://static.compassframework.org/dtd/compass-core-mapping-1.0.dtd">     <compass-core-mapping package="org.compassframework.sample.example">     <class name="Phrase" alias="${example.phrase}">      <id name="id" />      <property name="author">      <meta-data>${example.author}meta-data>      property>      <property name="text">      <meta-data>${example.text}meta-data>      property>      class>      compass-core-mapping>   CompassExample.java java 代码 package org.compassframework.sample.example;       import org.apache.log4j.Logger;       import org.compassframework.core.Compass;    import org.compassframework.core.Compass;    import org.compassframework.core.CompassSession;    import org.compassframework.core.CompassTransaction;    import org.compassframework.core.config.CompassConfiguration;       public class CompassExample {        private static final Logger logger = Logger.getLogger(CompassExample.class);          private compasscompass;          public static void main(String[] args){         new CompassExample().process();     }          private void process(){         CompassConfiguration config = new CompassConfiguration();         config.configure("org/compassframework/sample/example/compass.cfg.xml");         config.addClass(Phrase.class);         compass = config.buildCompass();         compass.getSearchEngineIndexManager().deleteIndex();         compass.getSearchEngineIndexManager().createIndex();               createIndex();         search("mule AND crazy");         search("Marisol OR Ramon");     }          private void createIndex(){      CompassSession session = compass.openSession();         CompassTransaction tx = session.beginTransaction();         Phrase phrase = new Phrase();         phrase.setAuthor("Joe");         phrase.setText("I don't think it's nice you laughing. " +                 "You see my mule don't like people laughing. " +                 "He gets the crazy idea you're laughing at him. " +                 "Now if you apologize like I know you're going to, " +                 "I might convince him that you really didn't mean it...");         phrase.setId(new Long(1));         session.save(phrase);         tx.commit();         session.close();     }          private void search(String query){         CompassSession session = compass.openSession();         CompassTransaction tx = session.beginTransaction();         Compass hits = session.find(query);         if (logger.isDebugEnabled()) {            logger.debug("search() - Found " + hits.getLength()+" hits for \""+query+"\"");         }         for (int i = 0; i < hits.getLength(); i++) {            print(hits, i);         }         hits.close();         tx.commit();         session.close();         compass.close();     }          private void print(Compass hits, int hitNumber) {         Phrase value = (Phrase)hits.data(hitNumber);         if (logger.isDebugEnabled()) {             logger.debug("print() - Phrase by " + value.getAuthor() + ": "                + value.getText());         }     }       }         为了保证能打印出我们的测试,需要加入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"


阅读全文(3067) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.328 second(s), page refreshed 144763867 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号