一个简单的COMPASS应用
首先你要下载Compass framework:
Download Compass .
你需要在你的class path 中添加4个jar
compass -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.xml
code
指定的target/index 是一个存储目录放索引文件的
(这个类必须有个无参数的构造和id属性)
xml 代码
"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 代码
xml version ="1.0" ?> "-//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 代码
xml version ="1.0" ?> "-//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"