首先你要下载Compass framework: http://www.opensymphony.com/compass/
你需要在你的class path 中添加3个jar: compass.jar,commons-logging.jar,lucene-core.jar.
在你的项目中创建下面的目录或文件(可以根据自己的定义来改动):
在你的项目中创建下面的目录或文件(可以根据自己的定义来改动):
compass.cfg.xml
- org
- compass
- sample
- example
alias.cmd.xml
CompassExample.java
Phrase.cpm.xml
Phrase.java
compass.cfg.xml:
xml 代码
xml version="1.0" encoding="UTF-8"?>
"-//Compass/Compass Core Configuration DTD 1.1//EN"
"http://www.opensymphony.com/compass/dtd/compass-core-configuration.dtd">
<compass-core-configuration>
<compass name="default">
<setting name="compass.engine.connection">targetsetting>
<meta-data resource="org/compass/sample/example/alias.cmd.xml" />
<mapping resource="org/compass/sample/example/Phrase.cpm.xml" />
compass>
compass-core-configuration>
指定的target 是一个存储目录, 放索引文件的.
alias.cmd.xml
:
xml 代码
xml version="1.0" encoding="UTF-8"?>
"-//Compass/Compass Core Meta Data DTD 1.1//EN"
"http://www.opensymphony.com/compass/dtd/compass-core-meta-data.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: (这个类必须有个无参数的构造和id属性)
java 代码
package org.compass.sample.example;
public class Phrase {
private Long id;
private String author;
private String text;
public Phrase() {};
/**
* @return author
*/
public String getAuthor() {
return author;
}
/**
* @param author 要设置的 author
*/
public void setAuthor(String author) {
this.author = author;
}
/**
* @return id
*/
public Long getId() {
return id;
}
/**
* @param id 要设置的 id
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return text
*/
public String getText() {
return text;
}
/**
* @param text 要设置的 text
*/
public void setText(String text) {
this.text = text;
}
public String toString() {
return text;
}
}
Phrase.cpm.xml:
xml 代码
xml version="1.0" encoding="UTF-8"?>
"-//Compass/Compass Core Mapping DTD 1.1//EN"
"http://www.opensymphony.com/compass/dtd/compass-core-mapping.dtd">
<compass-core-mapping package="org.compass.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.compass.sample.example;
import org.compass.core.Compass;
import org.compass.core.Compass;
import org.compass.core.CompassSession;
import org.compass.core.CompassTransaction;
import org.compass.core.config.CompassConfiguration;
public class CompassExample {
private Compass compass;
public static void main(String[] args) {
new CompassExample().process();
}
private void process() {
CompassConfiguration config = new CompassConfiguration().configure();
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);
System.out.println("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);
System.out.println("print() - Phrase by " + value.getAuthor() + ": " + value.getText());
}
}
以下是结果:
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"