Managing the index
Thanks to Compass::GPS the Searchable Plugin mirrors changes made through Hibernate/GORM to the index, and this means that you normally don't have to think about maintaining the index yourself.
But perhaps when an object is created/saved/updated/deleted with GORM/Hibernate is not when you want it to be indexed. Perhaps your application updates the DB with raw SQL or JDBC, which Compass::GPS isn't aware of, or there are other applications updating the DB. Maybe you don't like the mirroring feature and have disabled it (see Configuration).
In these cases you will need to maintain the index yourself, and Searchable Plugin provides a few methods to help with this. There are basically three methods - index, unindex and reindex - with two varieties, a singluar (eg, index) and plural (eg, indexAll). The plural varieties are available on both the SearchableService and as domain class static methods; the singular variety are also available as domain class instance methods.
- index - Indexes (adds to the search index) one or more objects by given instances or ids, or the instance itself
- indexAll - Perform a bulk index (adds to the search index) of all searchable class instances, or specific objects by given instances or ids
- unindex - Un-indexes (removes from the search index) one or more objects by given intances or ids, or the instance itself
- unindexAll - Perform a bulk un-index (removes from the search index) of all searchable class instances, or one or more specific objects by given intances or ids
- reindex - Re-indexes (ie, updates the search index) one or more objects by given instances or ids, or the instance itself
- reindexAll - Perform a bulk re-index (ie, updates the search index) for all searchable class instances, or one or more specific objects by given intances or ids
Here are a few quick examples, a complete reference follows:
// Re-index all searchable class instances: will take some time! searchableService.reindexAll() // Un-index a few Post instances by id Post.unindexAll(5l, 10l, 15l) // Update the index for a specific instance post.reindex() // Index some objects by class and id searchableService.indexAll(10l, 20l, 30l, [class: Post])
Managing the index method reference
index
Description
Indexes (adds to the search index) one or more objects by given instances or ids, or the instance itself
Parameters
- args - Either a single or list of object instances or instance ids. Not required if called as an domain class instance method
- options - A Map with a single 'class' entry like [class: Product]. Only required if called from SearchableService with instance ids for args
Examples
// Index the identified Post instance(s) searchableService.index(1l, [class: Post]) // or searchableService.index(1l, 2l, 3l, [class: Post]) // Index the given objects: they can be instances of different classes searchableService.index(postA) // or searchableService.index(postA, postB, commentA, userX) // Index the identified Post instance(s) Post.index(1l) // or Post.index(1l, 2l, 3l) // Index the Post instance(s) Post.index(postA) // or Post.index(postA, postB, postC) // Index the instance post.index()
indexAll
Description
Perform a bulk index (adds to the search index) of all searchable class instances, or specific objects by given instances or ids
Parameters
- args - Either a list of object instances or instance ids, or none to index ALL searchable class instances
- options - A Map with a single 'class' entry like [class: Product]. Only required if called from SearchableService with instance ids for args
Examples
// Index ALL searchable class instances: will take some time! searchableService.indexAll() // Index the identified Post instances searchableService.indexAll(1l, 2l, 3l, [class: Post]) // Index the given objects: they can be instances of different classes searchableService.indexAll(postA, postB, commentA, userX) // Index the identified Post instances Post.indexAll(1l, 2l, 3l) // Index the identified Post instances Post.indexAll(postA, postB, postB)
unindex
Description
Un-indexes (removes from the search index) one or more objects by given intances or ids, or the instance itself
Parameters
- args - Either a single or list of object instances or instance ids. Not required if called as an domain class instance method
- options - A Map with a single 'class' entry like [class: Product]. Only required if called from SearchableService with instance ids for args
Examples
// Un-index the identified Post instance(s) searchableService.unindex(1l, [class: Post]) // or searchableService.unindex(1l, 2l, 3l, [class: Post]) // Un-index the given objects: they can be instances of different classes searchableService.unindex(postA) // or searchableService.unindex(postA, postB, commentA, userX) // Un-index the identified Post instance(s) Post.unindex(1l) // or Post.unindex(1l, 2l, 3l) // Un-index the Post instance(s) Post.unindex(postA) // or Post.unindex(postA, postB, postC) // Un-index the instance post.unindex()
unindexAll
Description
Perform a bulk re-index (ie, updates the search index) for all searchable class instances, or one or more specific objects by given intances or ids
Parameters
- args - Either a list of object instances or instance ids, or none to index ALL searchable class instances, unless options is defined, in which case all instances of that class are un-indexed
- options - A Map with a single 'class' entry like [class: Product]. Can be combined with ids if if called from SearchableService or provided as the only argument to un-index all class instances
Examples
// Un-index ALL searchable class instances searchableService.unindexAll() // Un-index instances of Post searchableService.unindexAll(class: Post) // Un-index the identified Post instances searchableService.unindexAll(1l, 2l, 3l, [class: Post]) // Un-index the given objects: they can be instances of different classes searchableService.unindexAll(postA, postB, commentA, userX) // Un-index the identified Post instances Post.unindexAll(1l, 2l, 3l) // Un-index the identified Post instances Post.unindexAll(postA, postB, postB)
reindex
Description
Re-indexes (ie, updates the search index) one or more objects by given instances or ids, or the instance itself
Parameters
- args - Either a single or list of object instances or instance ids. Not required if called as an domain class instance method
- options - A Map with a single 'class' entry like [class: Product]. Only required if called from SearchableService with instance ids for args
Examples
// Re-index the identified Post instance(s) searchableService.reindex(1l, [class: Post]) // or searchableService.reindex(1l, 2l, 3l, [class: Post]) // Re-index the given objects: they can be instances of different classes searchableService.reindex(postA) // or searchableService.reindex(postA, postB, commentA, userX) // Re-index the identified Post instance(s) Post.reindex(1l) // or Post.reindex(1l, 2l, 3l) // Re-index the Post instance(s) Post.reindex(postA) // or Post.reindex(postA, postB, postC) // Re-index the instance post.reindex()
reindexAll
Description
Perform a bulk un-index (removes from the search index) of all searchable class instances, or one or more specific objects by given intances or ids
Parameters
- args - Either a single or list of object instances or instance ids. Not required if called as an domain class instance method
- options - A Map with a single 'class' entry like [class: Product]. Only required if called from SearchableService with instance ids for args
Examples
// Re-index ALL searchable class instances: will take some time! searchableService.unindexAll() // Re-index the identified Post instances searchableService.unindexAll(1l, 2l, 3l, [class: Post]) // Re-index the given objects: they can be instances of different classes searchableService.unindexAll(postA, postB, commentA, userX) // Re-index the identified Post instances Post.unindexAll(1l, 2l, 3l) // Re-index the identified Post instances Post.unindexAll(postA, postB, postB)

