Last updated by Shailen 5 years ago

Not All Properties

Using only and except

You can limit the properties that are made searchable like:

class Post {
    static searchable = [only: ['category', 'title']] 
    // …


class Post {
    static searchable = [except: 'createdAt']
    // …

The value of except or only can be a String or List<String> as shown above.

The String can contain shell-like-wildcards:

  • * means any number of characters
  • ? means any single character

except and only wildcard examples

// map, eg, 'addressLine1', 'addressLine2', 'addressPostcode', etc…
static searchable = [only: 'address*']

// do not map, eg, 'screenX' and 'screenY' and 'version'
static searchable = [except: ['screen?', 'version']]

However read this caveat and alternative approach.

Using except or only with class property mapping

You can combine class property mappings with only and except:

class Post {
    static searchable = {
        except = ["version", "createdAt"]   // version and createdAt will not be mapped to the index
        category index: 'not_analyzed', excludeFromAll: true
        title boost: 2.0
        comments component: true
    static hasMany = [comments: Comment]

User author String title, post, category Date createdAt }

Excluding certain fields from being searched

Note that using only or except means that the index will only contain those properties as indicated, so when an object is returned from the index, some properties may be null, even if they have non-null values in the database.

You can do better than this by still mapping those properties to the index, but having them not actually indexed for search purposes. To do this add a mapping for the property with an index: 'no' option.

This method may become default behavior for properties excluded from mapping with only and except in future.