Last updated by maurice 5 years ago

Class Mapping

Summary

Defines the class mapping

Syntax

static searchable = {
    option value
    option value
}

static searchable = {
    mapping options
}

static searchable = {
    mapping {
        option value
        option value
    }
}

static searchable = {
    mapping options, {
        option value
        option value
    }
}

Description

A number of mapping options affect the class itself or provide defaults for property mappings.

The syntaxes are interchangeable. Use whichever you prefer.

One of the last three syntaxes (introduced by the @mapping@ keyword) may be necessary if you need to use an option that clashes with one of your domain class property names.

Parameters

Options

  • @all@ - Controls the behaviour of the automatically created "all" field in the index for instances of this class. If not defined uses built-in defaults. More
  • @analyzer@ - The name of a configured Analyzer used to analyze the searchable data of instances. Overridable on a per property basis. Default is @"default"@, a built-in Analyzer.
  • @alias@ - A name for documents of this class type in the index. Default is the simple class name, eg, the class org.boom.User would have an alias of "User" by default. It's essentially a piece of constant text added for every object of this class type, and allows you to query by class, eg, @"alias:User maurice"@
  • @boost@ - A decimal value which increases or decreases the ranking of hits for this class if positive or negative respectively. Default is @1.0@
  • @constant@ - Allows you to define constant searchable data for every instance of the class. Requires a Map value, which defines the constant@'s @name and value or values and other options. More
  • @converter@ - The name of configured Converter used to convert this class to/from searchable text. A Compass default is used if not defined.
  • @root@ - Defines whether class instances are kept in their own index. When false instances should be searchable components of another class, otherwise they are not saved in any index and cannot be searched for. Default is @true@.
  • @spellCheck@ - Whether to include the instance searchable data is added to the spell check index. Either "@include"@ or "exclude" to include or exclude from the spell-check index respectively. Default is @"exclude"@.
  • @subIndex@ - The name of the Lucene index for this class. By default, each searchable class (or hierarchy) gets its own complete Lucene index, with the same name as @alias@. (Compass calls these "sub-indexes".) This name is then used as the index's on-disk directory name or as the table-table name, for example, depending on your index storage strategy. This option allows you to override the default behaviour. For example you could use the same sub-index for multiple different classes.
  • @supportUnmarshall@ - Whether you need to get the data in the index back out again as domain classes. If false class instances are "marshalled" to the index, but they cannot be "unmarshalled" back into classes (ie the reverse). May be useful if you want to easily index your data but are accessing the index directly with Compass/Lucene yourself. Default is @true@.

Examples

// Define class mapping options the simple way
static searchable = {
    alias "foobar"
    subIndex "fb"
    constant name: "type", value: "some foobar"
    constant name: "noise", values: ["squawk", "shriek"]
}

// Define class mapping options with the "mapping" keyword;
// some options are defined within a Map option to "mapping"
// and others are given in a nested Closure
static searchable = {
    mapping alias: "foobar", subIndex: "fb", {
        constant name: "type", value: "some foobar"
        constant name: "noise", values: ["squawk", "shriek"]
    }
}

// Define class mapping options within the nested "mapping" Closure
static searchable = {
    mapping {
        boost 2.0
        spellCheck "include"
    }
}