Last updated by maurice 8 years ago

Compass annotations

In order to use Compass annotations just define them in your classes, eg:

import org.compass.annotations.*

@Searchable(alias = 'user') class User { static hasMany = [friends: User]

@SearchableId Long id

@SearchableProperty String name

@SearchableReference(refAlias = 'user') Set friends }

Normally with Compass you need a master XML config file - typically compass.cfg.xml - in which you declare these mapped classes. With the Searchable Plugin you can choose whether you want a compass.cfg.xml@; if it is not found, it detects annotated domain classes automatically.

Note that when using annotations, Compass (well, Java really) needs actual properties for the annotations to annotate, so you might find you have to add things like the @id property of the class and any Collection properties that are normally created dynamically by Grails.

Additionally, relationships in Grails are lazy by default, meaning that direct property access does not always work as you might expect (you might be accessing the property of a lazy proxy rather than the associated domain class) and getter/setter methods should be used instead. You may find you need to need to add an accessor = 'property' to your annotations like so:

@SearchableId(accessor = 'property')
Long id

which tells Compass to use getter/setter access rather than field access. Or you can just annotate the getter instead of the field.

Refer to the Compass docs from here for more.