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


«November 2025»
1
2345678
9101112131415
16171819202122
23242526272829
30


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

我的分类(专题)

日志更新

最新评论

留言板

链接

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




[Java Open Source]一个简单的COMPASS应用
软件技术,  电脑与网络

lhwork 发表于 2006/6/29 14:07:33

首先你要下载Compass framework: Download Compass. 你需要在你的class path 中添加4个jarcompass-0.8.1/modules/core/compass-core-0.8.1.jar, compass-0.8.1/modules/core/lib/commons-logging-1.0.4.jar, compass-0.8.1/modules/core/lib/log4j-1.2.8.jar, compass-0.8.1/modules/core/lib/lucene-core-1.9-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 是一个存储目录放索引文件的<!DOCTYPE compass-core-configuration PUBLIC"-//Compass/Compass Core Configuration DTD 1.0//EN""http://static.compassframework.org/dtd/compass-core-configuration-1.0.dtd"><compass-core-configuration><compass><setting name="compass.engine.connection">target/index</setting><meta-data resource="org/compassframework/sample/example/alias.cmd.xml" /></compass></compass-core-configuration> alias.cmd.xml:<?xml version="1.0"?><!DOCTYPE compass-core-meta-data PUBLIC "-//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 Data</description><uri>http://compass/example</uri><alias id="phrase" displayName="Phrase"><description>Phrase alias</description><uri>http://compass/example/phrase</uri><name>phrase</name></alias><meta-data id="author" displayName="Author"><description>Author alias</description><uri>http://compass/example/author</uri><name>author</name></meta-data><meta-data id="text" displayName="Text"><description>Text alias</description><uri>http://compass/example/text</uri><name>text</name></meta-data></meta-data-group></compass-core-meta-data>Phrase.java (这个类必须有个无参数的构造和id属性)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 version="1.0"?><!DOCTYPE compass-core-mapping PUBLIC "-//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.javapackage org.compassframework.sample.example;import org.apache.log4j.Logger;import org.compassframework.core.Compass;import org.compassframework.core.CompassHits;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 Compass compass;  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();     CompassHits 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(CompassHits 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"


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



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



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

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