MainOverviewWikiIssuesForumBuildFisheye

Chapter 3. Configuration

Compass must be configured to work with a specific applications domain model. There are a large number of configuration parameters available (with default settings), which controls how Compass works internally and with the underlying Search Engine. This section describes the configuration API.

In order to create a Compass instance, it first must be configured. CompassConfiguration can be used in order to configure a Compass instance, by having the ability to add different mapping definitions, configure Compass based on xml or JSON configuration files, and expose a programmatic configuration options.

3.1. Programmatic Configuration

A Compass instance can be programatically configured using CompassConfiguration. Two main configuration aspects are adding mapping definitions, and setting different settings.

CompassConfiguration provides several API's for adding mapping definitions (xml mapping files suffixed .cpm.xml or annotated classes), as well as Common Meta Data definition (xml mapping files suffixed .cmd.xml). The following table summarizes the most important API's:

Table 3.1. Configuration Mapping API

APIDescription
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.

Other than mapping file configuration API, Compass can be configured through the CompassSettings interface. CompassSettings is similar to Java Properties class and is accessible via the CompassConfiguration.getSettings() or the CopmassConfiguration.setSetting(String setting, String value) methods. Compass's many different settings are explained in Appendix A, Configuration Settings.

Compass setting can also be defined programmatically using the org.compass.core.config.CompassEnvironment and org.compass.core.lucene.LuceneEnvironment classes (hold programmatic manifestation of all the different settings names).

In terms of required settings, Compass only requires the compass.engine.connection (which maps to CompassEnvironment.CONNECTION) parameter defined.

Global Converters (classes that implement Compass Converter) can also be registered with the configuration to be used by the created compass instances. The converters are registered under a logical name, and can be referenced in the mapping definitions. The method to register a global converter is registerConverter.

Again, many words and so little code... . The following code example shows the minimal CompassConfiguration with programmatic control:

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

An important aspect of settings (properties like) configuration is the notion of group settings. Similar to the way log4j properties configuration file works, different aspects of Compass need to be configured in a grouped nature. If we take Compass converter configuration as an example, here is an example of a set of settings to configure a custom converter called test:

org.compass.converter.test.type=eg.TestConverter
org.compass.converter.test.param1=value1
org.compass.converter.test.param2=value2

Compass defined prefix for all converter configuration is org.compass.converter. The segment that comes afterwards (up until the next '.') is the converter (group) name, which is set to test. This will be the converter name that the converter will be registered under (and referenced by in different mapping definitions). Within the group, the following settings are defined: type, param1, and param2. type is one of the required settings for a custom Compass converter, and has the value of the fully qualified class name of the converter implementation. param1 and param2 are custom settings, that can be used by the custom converter (it should implement CompassConfigurable).

3.2. XML/JSON Configuration

All of Compass's operational configuration (apart from mapping definitions) can be defined in a single xml configuration file, with the default name compass.cfg.xml. You can define the environmental settings and mapping file locations within this file. The following table shows the different CompassConfiguration API's for locating the main configuration file:

Table 3.2. Compass Configuration API

APIDescription
configure()Loads a configuration file called compass.cfg.xml from the root of the class path.
configure(String)Loads a configuration file from the specified path

3.2.1. Schema Based Configuration

The preferred way to configure Compass (and the simplest way) is to use an Xml configuration file, which validates against a Schema. It allows for richer and more descriptive (and less erroneous) configuration of Compass. The schema is fully annotated, with each element and attribute documented within the schema. Note, that some additional information is explained in the Configuration Settings appendix, even if it does not apply in terms of the name of the setting to be used, it is advisable to read the appropriate section for more fuller explanation (such as converters, highlighters, analyzers, and so on).

Here are a few sample configuration files, just to get a feeling of the structure and nature of the configuration file. The first is a simple file based index with the OSEM definitions for the Author class.

<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>                

The next sample configures a jdbc based index, with a bigger buffer size for default file entries:

<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>
         <jdbc dialect="org.apache.lucene.store.jdbc.dialect.HSQLDialect">
             <dataSourceProvider>
                 <driverManager url="jdbc:hsqldb:mem:test" username="sa" password="" 
                                driverClass="org.hsqldb.jdbcDriver" />
             </dataSourceProvider>
             <fileEntries>
                 <fileEntry name="__default__">
                     <indexInput bufferSize="4096" />
                     <indexOutput bufferSize="4096" />
                 </fileEntry>
             </fileEntries>
         </jdbc>
     </connection>
   </compass>
</compass-core-config>

The next sample configures a jdbc based index, with a JTA transaction (note the managed="true" and commitBeforeCompletion="true"):

<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>
         <jdbc dialect="org.apache.lucene.store.jdbc.dialect.HSQLDialect" managed="true">
             <dataSourceProvider>
                 <driverManager url="jdbc:hsqldb:mem:test" username="sa" password="" 
                                driverClass="org.hsqldb.jdbcDriver" />
             </dataSourceProvider>
         </jdbc>
     </connection>
     <transaction factory="org.compass.core.transaction.JTASyncTransactionFactory" commitBeforeCompletion="true">
     </transaction>
   </compass>
</compass-core-config>

Here is another sample, that configures another analyzer, a specialized Converter, and changed the default date format for all Java Dates (date is an internal name that maps to Compass default date Converter).

<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>
   
      <converters>
          <converter name="date" type="org.compass.core.converter.basic.DateConverter">
              <setting name="format" value="yyyy-MM-dd" />
          </converter>
          <converter name="myConverter" type="test.Myconverter" />
      </converters>
   
      <searchEngine>
          <analyzer name="test" type="Snowball" snowballType="Lovins">
              <stopWords>
                  <stopWord value="test" />
              </stopWords>
          </analyzer>
      </searchEngine>
   </compass>
</compass-core-config>

The next configuration uses lucene transaction isolation, with a higher ram buffer size for faster indexing.

<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>

     <transaction isolation="lucene" />
     
     <settings>
       <setting name="compass.engine.ramBufferSize" value="64" />
     </settings>
  </compass>
</compass-core-config>

3.2.2. JSON Based Configuration

Compass can be configured using JSON based configuration. Basically, the JSON configuration breaks the settings into JSON elements. The Configuration Settings are explained in Appendix A, Configuration Settings. Here is an example:

   {
       compass : {
           engine : {
               connection : "test-index"
           },

           event : {
               preCreate : {
                   event1 : {
                       type : "test.MyEvent1"
                   },
                   event2 : {
                       type : "test.MyEvent2"
                   }
               }
           }
       }
   }

3.2.3. DTD Based Configuration

Compass can be configured using a DTD based xml configuration. The DTD configuration is less expressive than the schema based one, allowing to configure mappings and Compass settings. The Configuration Settings are explained in Appendix A, Configuration Settings.

And here is an example of the xml configuration file:

<!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.3. Obtaining a Compass reference

After CompassConfiguration has been set (either programmatic or using the XML configuration file), you can now build a Compass instance. Compass is intended to be shared among different application threads. The following simple code example shows how to obtain a Compass reference.

Compass compass = cfg.buildCompass();
      

Note: It is possible to have multiple Compass instances within the same application, each with a different configuration.

3.4. Rebuilding Compass

Compass allows to dynamically add and remove mappings using CompassConfigurartion that can be obtained from the Compass instance. Once all changes are done on the configuration object, Compass#rebuild operation. Here is an example:

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

The "old" Compass instance is closed after a graceful time (default to 60 seconds). The time can be controlled using the following setting: compass.rebuild.sleepBeforeClose.

3.5. Configuring Callback Events

Compass allows to configure events that will be fired once certain operations occur in using Compass, for example, save operation.

Configuring event listener can be done settings. For example, to configure a pre save event listener, the following setting should be used: compass.event.preSave.mylistener.type and its value can be the actual class name of the listener.

More information for each listener can be found in the javadoc under the org.compass.events package. An important note with regards to pre listener is the fact that they can filter out certain operations.