Last updated by maurice 8 years ago



Finds similar objects to the indicated searchable domain class instance.


domainObject.moreLikeThis(Map options)

DomainClass.moreLikeThis(Map options)
DomainClass.moreLikeThis(Serializable id)
DomainClass.moreLikeThis(Serializable id, Map options)
DomainClass.moreLikeThis(DomainClass domainObject)
DomainClass.moreLikeThis(DomainClass domainObject, Map options)

searchableService.moreLikeThis(Map options)
searchableService.moreLikeThis(Class domainClass, Serializable id)
searchableService.moreLikeThis(Class domainClass, Serializable id, Map options)
searchableService.moreLikeThis(DomainClass domainObject)
searchableService.moreLikeThis(DomainClass domainObject, Map options)


Issues a "more-like-this" query to the search engine and returns the result.

Note that you do not create the more-like-this query, it is created by the search engine for a given searchable class instance. You do need to provide or identify the class instance though.

Conceptually (apart from the difference in the query) this method is like search@, and you could think of it like a specialised "search" for more-like-some-object.

Unsurprisingly then they share code and this method has many options in common with @search itself, and also adds more of its own.

To use moreLikeThis you need to add termVector to the searchable domain class's "all" property mapping and rebuild the index.

See the example here.


  • @domainClass@ - The searchable domain class of the object to find more like
  • @id@ - The id of the searchable domain object to find more like
  • @domainObject@ - A searchable domain class instance to find more like
  • @options@ - A Map of options


Options affecting the more-like-this query

  • @sort@ - The field to sort results by (default is @'SCORE'@). More
  • @order@ or @direction@ - The sort order, only used with sort (default is @'auto'@). More
If you prefer you can identify the domain class instance with options alone:
  • @class@ - The searchable domain class to find more like. Not required for @DomainClass.moreLikeThis(...)@
  • @id@ - The searchable domain object identifier to find more like
These options tweak the more-like-this query:
  • @aliases@ - Exposes the Compass aliases option allowing you to narrow the search space by alias. A List of aliases, eg @['post', 'comment']@
  • @boost@ - Sets whether to boost terms in query based on "score" or not. true or @false@
  • @maxNumTokensParsed@ - The maximum number of tokens to parse in each example doc field that is not stored with TermVector support
  • @maxQueryTerms@ - The maximum number of query terms that will be included in any generated query.
  • @maxWordLen@ - The maximum word length above which words will be ignored. Set this to 0 for no maximum word length. The default is @0@.
  • @minResourceFreq@ - The frequency at which words will be ignored which do not occur in at least this many resources. Defaults to @5@
  • @minTermFreq@ - The frequency below which terms will be ignored in the source doc. Defaults to @2@.
  • @minWordLen@ - The minimum word length below which words will be ignored. Set this to 0 for no minimum word length. Default is @0@.
  • @properties@ - Limits the search to these class properties. A List of property names, eg @['post', 'title']@.
  • @stopWords@ - A List of words that are not considered when comparing items for similarity. This is used in addition to any stop-words your analyzer has
  • @subIndexes@ - A List of sub-indexes to search in, eg, @['bookmark']@

Options affecting the return value

  • @result@ - What should the method return? If defined, one of "searchResult"@, @"top"@, @"every" or @"count"@. Default is @"searchResult"@. (See Returns below)
  • @offset@ - The 0-based start result offset (default 0)
  • @max@ - The maximum number of results to return (default 10). Only used with @result: "searchResult"@
  • @reload@ - If @true@, reloads the objects from the database, attaching them to a Hibernate session, otherwise the objects are reconstructed from the index. Default is @false@
  • @withHighlighter@ - A Closure instance that is called for each search result hit to support highlighting. More
There are also some additional options for string queries.


By default (if result is undefined) or if result is @"searchResult"@, returns a "search result" object containing a subset of similar objects, with the following properties:

  • @results@ - A List of domain objects
  • @scores@ - A List of scores: one for for each entry in results
  • @total@ - The total number of hits
  • @offset@ - The 0-based hit object offset
  • @max@ - The maximum number of results _requested_. Note that this may be higher than results.size() if there were fewer hits than requested
If result is @"every"@, returns every similar object.

If result is @"top"@, returns the first similar domain object.

If result is @"count"@, returns the number of similar domain objects.

The order of the hits is either by relevance (the default) or a sort you define.


// Get more like the identified Artist
def searchResult = searchableService.moreLikeThis(Artist, 101l)

assert searchResult instanceof Map println "${} hits:" for (i in 0..<searchResult.results.size()) { println "${searchResult.offset + i + 1}: " + "${searchResult.results[i].toString()} " + "(score ${searchResult.scores[i]})" }

// Find similar items to a Product
def products = product.moreLikeThis(result: 'every')
println "We can also recommend ${products.size()} other items"

// Get a third page of more-like-this results for the 
// Catalogue instance defined by options (rather than
// formal parameters)
def searchResult = searchableService.moreLikThis(
    class: Catalogue, id: 312l, offset: 20, max: 10

// Get the most similar item to the given searchable class instance
def mostSimilar = searchableService.moreLikeThis(item, result: 'top')

// Get similar books to the instance identified with the
// the *id* option (rather than formal parameters),
// having the method return *all* hits, and
// saving highlights in an external List
def highlights = []
def bookHighlighter = { highlighter, index, sr ->
    highlights[index] = [
        title: highlighter.fragment("title"),
        summary: highlighter.fragment("summary")
def books = Book.moreLikeThis(
    id: 20l, result: 'every', withHighlighter: bookHighlighter