Chapter 7. Resource Mapping

7.1. Introduction

Compass::Core provides OSEM technology for use with an applications Object domain model. Compass::Core also provides Resource Mapping technology for resources other than Objects (that do not benefit from OSEM). The benefits of using Resources can be summarised as:

  • Your application does not have a domain model (therefore cannot use OSEM), but you still want to use the functionality of Compass.

  • Your application already works with Lucene, but you want to add Compass::Core additional features (i.e. transactions, fast updates). Working with Resources makes your migration easy.

  • You execute a query and want to update all the meta-data (Resource Property) with a certain value. You use OSEM in your application, but you do not wish to iterate through the results, performing run-time object type checking and casting to the appropriate object type before method call. You can simply use the Resource interface and treat all the results in the same abstracted way.

7.2. Mapping Declaration

In order to work directly with a Resource, Compass needs to know the alias and the primary properties (i.e. primary keys in data-base systems) associated with the Resource. The primary properties are also known as id properties. This information is declared in Resource Mapping XML documents, so that Compass knows how to manage the Resource internally.

Here is an example of a Resource Mapping XML document:

<?xml version="1.0"?>
<!DOCTYPE compass-core-mapping PUBLIC
    "-//Compass/Compass Core Mapping DTD 1.0//EN"
    "http://www.opensymphony.com/compass/dtd/compass-core-mapping.dtd">

<compass-core-mapping>

  <resource alias="a">
    <resource-id name="id" />
  </resource>

  <resource alias="b">
    <resource-id name="id1" />
    <resource-id name="id2" />
  </resource>

  <resource alias="c">
    <resource-id name="id" />
    <resource-property name="value1" />
    <resource-property name="value2" store="yes" index="tokenized" />
    <resource-property name="value3" store="compress" index="tokenized" />
    <resource-property name="value4" store="yes" index="un_tokenized" />
    <resource-property name="value5" store="yes" index="no" />
  </resource>
</compass-core-mapping>

Now that the Resource Mapping has be declared, you can create the Resource in the application. In the following code example the Resource is created with an alias and id property matching the Resource Mapping declaration.

Resource r = session.createResource("a");
Property id = session.createProperty("id", "1",
    Property.Store.YES, Property.Index.UN_TOKENIZED);
r.addProperty(id);
r.addProperty(session.createProperty("mvalue", "property test",
    Property.Store.YES, Property.Index.TOKENIZED));

session.save(r);

The Resource Mapping file example above defines mappings for two resources (each identified with a different alias). Each resource has a set of resource ids that are associated with it. The value for the resource-id tag is the name of the Property that is associated with the primary property for the Resource.

The third mapping (alias "c"), defined resource-property mappings as well as resource-id mappings. The resource-property mapping works with the Resource#addProperty(String name, Object value) operation. It provides definitions for the resource properties that are added (index, store, and so on), and they are then looked up when using the mentioned add method. Using the resource-property mapping, helps clean up the code when constructing a Resource, since all the Property characteristics are defined in the mapping definition, as well as auto conversion from different objects, and the ability to define new ones. Note that the resource-property definition will only work with the mentioned addProperty method, and no other addProperty method.

All XML mappings should declare the doctype shown. The actual DTD may be found at the URL above or in the compass-core-x.x.x.jar. Compass will always look for the DTD in the classpath first.

There are no compass-core-mapping attributes that are applicable when working with resource mappings.

7.2.1. resource

You may declare a resource mapping using the resource element:

<resource
      alias="aliasName"
      sub-index="sub index name"
      analyzer="name of the analyzer"
      all="true|false"
      all-term-vector="no|yes|positions|offsets|positios_offsets"
      all-metadata="name of all metadata"
/>
    resource-id*,
    (resource-analyzer?),
    (resource-property)*

Table 7.1. 

AttributeDescription
aliasThe name of the alias that represents the Resource.
sub-index (optional, defaults to the alias valueThe name of the sub-index that the alias will map to.
analyzer (optional, defaults to the default analyzer)The name of the analyzer that will be used to analyze TOKENIZED properties. Defaults to the default analyzer which is one of the internal analyzers that comes with Compass. Note, that when using the resource-analyzer mapping (a child mapping of resource mapping) (for a resource property value that controls the analyzer), the analyzer attribute will have no effects.
all (optional, defaults to true)Specifies if when persisting the Resource, Compass::Core will create the all property (which represents all the properties/meta-data values).
all-term-vector (optional, defaults to configuration setting compass.property.all.termVector)The term vector value of the all property.
all-metadata (optional, defaults to the setting)The name of the all meta-data (property).

7.2.2. resource-id

Mapped Resource's must declare at least one resource-id. The resource-id element defines the Property that identifies the Resource for the specified alias.

<resource-id
      name="idName"
/>

Table 7.2. 

AttributeDescription
nameThe name of the Property (known also as the name of the meta-data) that is the id of the Resource.

7.2.3. resource-property

Declaring and using the resource-property element.

<resource-property
      name="property name"
      store="yes|no|compress"
      index="tokenized|un_tokenized|no"
      boost="boost value for the property"
      analyzer="name of the analyzer"
      reverse="no|reader|string"
      exclude-from-all="[parent's exclude-from-all]|false|true"
      converter="fully qualified converter class name"
      converter-param="parameter for the converter"
>
   (converter-param)*,
</resource-property>

Table 7.3. 

AttributeDescription
nameThe name of the Property (known also as the name of the meta-data).
store (optional, defaults to yes)If the value of the resource property is going to be stored in the index.
index (optional, defaults to tokenized)If the value of the resource property is going to be indexed (searchable). If it does, than controls if the value is going to be broken down and analysed (tokenized), or is going to be used as is (un_tokenized).
boost (optional, defaults to 1.0f)Controls the boost level for the resource property.
analyzer (optional, defaults to the resource mapping analyzer decision scheme)The name of the analyzer that will be used to analyze TOKENIZED resource property mappings defined for the given property. Defaults to the resource mapping analyzer decision scheme based on the analyzer set, or the resource-analyzer mapping.
exclude-from-all (optional, default to false)Excludes the property from participating in the "all" meta-data.
override (optional, defaults to true)If there is another definition with the same mapping name, if it will be overridden or added as additional mapping. Mainly used to override definitions made in extended mappings.
reverse (optional, defaults to no)The meta-data will have it's value reversed. Can have the values of no - no reverse will happen, string - the reverse will happen and the value stored will be a reversed string, and reader - a special reader will wrap the string and reverse it. The reader option is more perform ant, but the store and index settings will be discarded.
converter (optional)The fully qualified class of a custom converter.
converter-param (optional)A single parameter for the converter (none required for the default one).

Defines the charectaristics of a Resource Property identified by the name mapping. The definition only applies when using the Resource#addProperty(String name, Object value) operation, and the operation can only be used with the resourcde-property mapping.

Note that other Resource Property can be added that are not defined in the resource mapping using the createProperty operation.

7.2.4. resource-analyzer

Declaring an analyzer controller property using the resource-analyzer element.

<resource-analyzer
      name="property name"
      null-analyzer="analyzer name if value is null"
      converter="fully qualified converter class name"
      converter-param="parameter for the converter"
>
</resource-analyzer>

Table 7.4. 

AttributeDescription
nameThe name of the Property (known also as the name of the meta-data).
null-analyzer (optional, defaults to error in case of a null value)The name of the analyzer that will be used if the property has the null value.
converter (optional)The fully qualified class of a custom converter.
converter-param (optional)A single parameter for the converter (none required for the default one).

The analyzer resource property mapping, controls the analyzer that will be used when indexing the Resource. If the mapping is defined, it will override the resource mapping analyzer attribute setting.

If, for example, Compass is configured to have two additional analyzers, called an1 (and have settings in the form of compass.engine.analyzer.an1.*), and another called an2. The values that the resource property can hold are: default (which is an internal Compass analyzer, that can be configured as well), an1 and an2. If the analyzer will have a null value, and it is applicable with the application, than a null-analyzer can be configured that will be used in that case. If the resource property has a value, but there is not matching analyzer, an exception will be thrown.