---
title: "Compass学习文档"
author: "fuyangchang"
date: "2007-06-15T13:49:00Z"
tags: []
slug: "compass"
draft: false
lastmod: "2007-06-15T13:49:00Z"
id: 104078
url: "https://csdn.iteye.com/blog/104078"
---

Compass学习文档(1)  
作者：javafish  
Compass是第一个实现java搜索引擎的开源框架,它是基于 Lucene之上的，提供更简单的搜索引擎API，事务支持，对象到搜索引擎映射（Annotations & XML），Xml到搜索引擎映射，可以和Hibernate,Spring集成，功能非常的强大。如果用Hibernate,Spring做的项目需要搜 索引擎的话，Compass是个非常好的选择。个人认为：如果说Lucene是搜索引擎中的JDBC，那么Compass就是Hibernate。而 Compass和Hibernate不仅仅是比喻的像，它们的类和用法，和作用也非常的像。Compass是用面向对象编程方法来实现搜索。如果会 Hibernate的话学Compass是非常容易的。  
大家可以到[http://www.opensymphony.com/compass/download.action](http://www.opensymphony.com/compass/download.action)去下载最新的版本。2006年8月28日Compass 1.1 M1 发布，用的是Lucene2.0，现在讲的就是这个版本。  
虽 然有人说Hibernate3.1以后版本自带了对Lucene的支持，Compass好像没用，但是只要看一下Hibernate源代码可以看出 Hibernate对Lucene的支持很简陋，且不支持事务并且Hibernate3.2用的还是Lucene版本是Lucene1.4.3，所以说当 做大的项目的时候Compass还是非常有必要的。  
还是先看个HelloWorld程序：（这里的测试功能的例子都是用的JUnit测试用例，为方便阅读异常全部抛出）  
一共4个文件Article.java， compass.cfg.xml，Article.cpm.xml，JUnit插入测试。  
1.Article.java：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package org.li.compass;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.Date;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)public class Article  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) private long id;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) private String title;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) private String content;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) private Date publishDate;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public String getContent()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  return content;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public void setContent(String content)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  this.content = content;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public Date getPublishDate()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  return publishDate;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public void setPublishDate(Date publishDate)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  this.publishDate = publishDate;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public String getTitle()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  return title;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public void setTitle(String title)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  this.title = title;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public long getId()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  return id;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public void setId(long id)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  this.id = id;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

上面就是一个Article的POJO类上面有文章题目，内容，发布时间，这里的id的compass必须的（有点和数据库一样了）。  
2.compass.cfg.xml

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!DOCTYPE compass-core-configuration PUBLIC  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    "-//Compass/Compass Core Configuration DTD 1.0//EN"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    "http://www.opensymphony.com/compass/dtd/compass-core-configuration.dtd"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;compass-core-configuration&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;compass&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;setting name="compass.engine.connection"&gt;target/test&lt;/setting&gt;&lt;!-- 这里配置的是索引的相对路径 --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;mapping resource="org/li/compass/Article.cpm.xml"/&gt;&lt;!-- 这里是每个对象的映射文件 --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;/compass&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/compass-core-configuration&gt;

这是Compass的配置文件上面标明了索引目录和映射文件。映射好文件了就看看这个映射的什么吧  
3\. Article.cpm.xml

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!DOCTYPE compass-core-mapping PUBLIC  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    "-//Compass/Compass Core Mapping DTD 1.0//EN"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    "http://www.opensymphony.com/compass/dtd/compass-core-mapping.dtd"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;compass-core-mapping package="org.li.compass"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;class name="Article" alias="article"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;!-- 这是必须有的 --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;id name="id"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;!-- 你可以通过这个配置来在底层给这个对象加一个Field("type","java") --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;constant&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;meta-data&gt;type&lt;/meta-data&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;meta-data-value&gt;java&lt;/meta-data-value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;/constant&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;!-- 配置一下属性 --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;property name="title"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;meta-data&gt;titleIndex&lt;/meta-data&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;property name="content"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;meta-data&gt;contentIndex&lt;/meta-data&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;property name="publishDate"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;meta-data&gt;publishDateIndex&lt;/meta-data&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;/class&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/compass-core-mapping&gt;

这个配置应该可以看的懂吧可能会有人问meta-data是干什么用的？？  
它的值是作为底层Field的名字，如果没有配置的话Compass用$article/…来代替。而它的作用是配置对象的属性的储存形式，Compass就是读取它来new Field()的:  
&lt;!ELEMENT meta-data (  
    #PCDATA  
)&gt;  
    &lt;!ATTLIST meta-data store (no|yes|compress) "yes"&gt;  
    &lt;!ATTLIST meta-data index (no|tokenized|un\_tokenized) "tokenized"&gt;  
    &lt;!ATTLIST meta-data term-vector (no|yes|positions|offsets|positions\_offsets) "no"&gt;  
    &lt;!ATTLIST meta-data reverse (no|reader|string) "no"&gt;  
    &lt;!ATTLIST meta-data analyzer CDATA #IMPLIED&gt;  
    &lt;!ATTLIST meta-data exclude-from-all (true|false) #IMPLIED&gt;  
    &lt;!ATTLIST meta-data converter CDATA #IMPLIED&gt;  
    &lt;!ATTLIST meta-data format CDATA #IMPLIED&gt;  
    &lt;!ATTLIST meta-data boost CDATA #IMPLIED&gt;  
4.JUnit插入测试

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)public void InsertCompass()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)CompassConfiguration comConfig = new CompassConfiguration().configure().addClass(Article.class);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  Compass com = comConfig.buildCompass();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  CompassSession comSession = com.openSession();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  CompassTransaction comTx = comSession.beginTransaction();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  Article article = new Article();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  article.setContent("Compass是第一个基于lucene的java开源框架");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  article.setTitle("学习Compass");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  article.setPublishDate(new Date());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  comSession.save(article);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  comTx.commit();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) }

当大家看到这里的时候就看出来Compass和Hibernate太像了。如果会Hibernate的话，学Compass将是非常容易的。  
看看效果如何：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)public void LoadCompass()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  CompassConfiguration comConfig = new CompassConfiguration().configure().addClass(Article.class);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  Compass com = comConfig.buildCompass();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  CompassSession comSession = com.openSession();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  CompassTransaction comTx = comSession.beginTransaction();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  Article article = (Article)comSession.load(Article.class, 0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  System.out.println(article.getTitle());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  System.out.println(article.getContent());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  System.out.println(article.getPublishDate());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) }

结果:  
学习Compass  
Compass是第一个基于lucene的java开源框架  
Sat Sep 23 00:51:35 CST 2006

注意：和其它的框架一样，Compass也是需要一定的配置的，而Compass的数据配置可分为Annotations，DTD验证的XML和Schema验证的XML。这里用的是dtd验证的XML配置。  
我们可以用Lucene做一下检测嘛  
JUnit测试用例：  
public void TestArticle() throws ParseException, IOException  
 {  
  QueryParser queryParser = new QueryParser("titleIndex",new StandardAnalyzer());  
  Query query = queryParser.parse("compass");  
  IndexSearcher indexSearcher = new IndexSearcher("target/test/index/article");  
  Hits hits = indexSearcher.search(query);  
  for(int i=0;i&lt;hits.length();i++)  
  {  
   Document doc = hits.doc(i);  
   System.out.println(doc.get("contentIndex"));//大家可以在这里设置个断点看一下doc里各个Field是什么。就会学到很多的东西  
  }  
}