`
crazycat03
  • 浏览: 176500 次
  • 性别: Icon_minigender_2
  • 来自: 呼和浩特
社区版块
存档分类
最新评论

Compass---CompassConfiguration

阅读更多

CompassConfiguration配置

 

为了创建Compass实例,首先要配置CompassConfiguration

有两种配置方法

1 程序配置

一个Compass实例能使用CompassConfiguration类程序化配置,两个主要的配置方面是添加映射定义和设置不同的参数。CompassConfiguration提供了很多接口去添加映射定义(xml映射文件,后缀.cmp.xml,或注释类),还有通用元数据定义(后缀为.cmd.xml的xml配置文件)。

 

 

addFile(String) Loads the mapping file (cpm or cmd) according to the  specified file path string.
addFile(File) Loads the mapping file (cpm or cmd) according to the specified file object reference.
addClass(Class) Loads the mapping file (cpm) according to the specified  class. test.Author.class will map to
test/Author.cpm.xml within the class path. Can also add annotated classes if using Compass annotations support.
addURL(URL) Loads the mapping file (cpm or cmd) according to the specified URL.
addResource(String) Loads the mapping file (cpm or cmd) according to the specified resource path from the class path.
addInputStream(InputStream) Loads the mapping file (cpm or cmd) according to the specified input stream.
addDirectory(String) Loads all the files named *.cpm.xml or *.cmd.xml from within the specified directory.
addJar(File) Loads all the files named *.cpm.xml or *.cmd.xml from within the specified Jar file.
addMapping(ResourceMapping) Programmatically add resource mapping (domain model that represents different mappings such as XSEM, OSEM,
and RSEM).
addScan(String basePackage, String pattern)Scans for all the mappings that exist wihtin the base
backage recursively. An optioal ant style pattern can be provided as well. The mappings detected are all the xml
based mappings. Annotation based mappings will be detected automatically if either ASM or Javassist exists
within the classpath.
addMappingResolver(MappingResolver) Uses a class that implements the MappingResolver to get an
InputStream for xml mapping definitions.

 下面列出一个最小化的程序控制的例子:

 

CompassConfiguration conf = new CompassConfiguration()
.setSetting(CompassEnvironment.CONNECTION, "my/index/dir")
.addResource(DublinCore.cmd.xml)
.addClass(Author.class);

 2 XML/JSON配置

所有Compass的操作配置都可以定义在一个xml配置文件里,以compass.cfg.xml为默认名称。在这个文件里你可以定义环境的配置和映射文件所在位置。

configure() Loads a configuration file called compass.cfg.xml from the root of the class path.默认从类路径下寻找compass.cfg.xml
configure(String) Loads a configuration file from the specified path 从指定位置下寻找

(1) 基于schema配置

<compass-core-config xmlns="http://www.compass-project.org/schema/core-config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.compass-project.org/schema/core-config
http://www.compass-project.org/schema/compass-core-config-2.2.xsd">
<compass name="default">
<connection>
<file path="target/test-index"/>
</connection>
<mappings>
<class name="test.Author" />
</mappings>
</compass>
</compass-core-config>

 还有比较复杂的基于jdbc,以及事务,分析器(such as converters, highlighters, analyzers, and so on)的配置

(2)基于json的配置

{
compass : {
engine : {
connection : "test-index"
},
event : {
preCreate : {
event1 : {
type : "test.MyEvent1"
},
event2 : {
type : "test.MyEvent2"
}
}
}
}
}

 (3) 基于dtd配置

<!DOCTYPE compass-core-configuration PUBLIC
"-//Compass/Compass Core Configuration DTD 2.2//EN"
"http://www.compass-project.org/dtd/compass-core-configuration-2.2.dtd">
<compass-core-configuration>
<compass>
<setting name="compass.engine.connection">my/index/dir</setting>
<meta-data resource="vocabulary/DublinCore.cmd.xml" />
<mapping resource="test/Author.cpm.xml" />
</compass>
</compass-core-configuration>

 

3  Compass获取

 CompassConfiguration配置之后,你就可以创建一个Compass实例了,Compass将会在不同的应用线程之间共享。

下面的简单代码说明如何获取compass实例

Compass compass = cfg.buildCompass();

 

注: 在同一个应用中可能会有多个Compass 实例,每一个都有不同的配置。

 

 

4 重建Compass

Compass允许使用从Compass取得的CompassConfiguration实例动态添加或删除映射,一旦所以改变在配置对象上完成,可以调用Compass的rebuild()方法

Compass compass = cfg.buildCompass();
// ...
compass.getConfig().removeMappingByClass(A.class);
compass.getConifg().addClass(B.class);
compass.rebuild();

 旧的Compass实例在短时间内将关闭。关闭时间可以用配置参数compass.rebuild.sleepBeforeClose控制。

 

5 配置回调事件

Compass允许配置事件(events),当在Compass使用指定的操作时触发的事件,比如保存操作。

配置事件监听器可以由设置(settings)完成。比如说,配置一个保存前事件监听器,下面的配置参数将会用到,

compass.event.preSave.mylistener.type ,它的值可以是监听器的真实类名

 

 

 

分享到:
评论

相关推荐

    compass_使用详解.pdf compass_教程 compass_试用案例

    Compass compass = new CompassConfiguration().configure().buildCompass(); ``` - **打开 Compass Session**: ```java CompassSession session = compass.openSession(); ``` - **执行事务**: ```java ...

    Compass原理深入学习笔记

    - CompassConfiguration用于加载配置和映射文件,创建Compass实例。 - CompassSession查询数据时返回CompassHits,提供分数、资源和映射对象。 - CompassTemplate简化索引操作,自动处理Session和Transaction。 ...

    Compass_入门指南

    Compass 的核心 API 借鉴了 Hibernate 的术语,包括 CompassConfiguration、Compass、CompassSession 和 CompassTransaction 四个核心接口。其中,CompassConfiguration 用于配置 Compass 的参数、配置文件和映射...

    compass入门指南

    - **CompassConfiguration**:类似于HibernateConfiguration,负责Compass的参数设定、配置文件加载以及映射定义。它是创建Compass接口的基础。 - **Compass**:类似于HibernateSessionFactory,用于创建线程安全的...

    compass学习笔记

    &lt;br&gt;Compass也是采用CompassConfiguration(装载配置和映射文件)进行创建的。创建Compass时将会链接已经存在的索引或者创建一个新的索引。当Compass创建完后,就可以用compass得到compassSession。...

    COMPASS介绍

    1. **CompassConfiguration**:类似于HibernateConfiguration,用于配置COMPASS的各项参数、配置文件以及映射定义。 2. **Compass**:类似于HibernateSessionFactory,用于创建线程安全的CompassSession实例,并提供...

    COMPASS+spring构建自己的搜索引擎.pdf

    - **CompassConfiguration**: 类似于Hibernate的`Configuration`,用于设置参数、配置文件和映射定义,创建`Compass`实例。 - **Compass**: 相当于Hibernate的`SessionFactory`,提供单线程使用的实例以打开`...

    compass 笔记

    - **CompassConfiguration** 用于配置 Compass 的行为,包括加载配置文件和映射文件。 - **CompassConfigurationFactory** 提供了一个简便的方式来创建 `CompassConfiguration` 实例。 #### 2. CompassTemplate 和 ...

Global site tag (gtag.js) - Google Analytics