Last updated by maurice 5 years ago

Sorting

Search results are sorted by relevance by default, the most relevant being the first.

You can specify a custom sort when using either String queries or a query builder Closure using the sort and order@/@direction method options:

  • @sort@ - Either "SCORE" (ie, relevance) or the name of a mapped class property
  • @order@ or direction - One of @"auto"@, @"reverse"@, @"asc"@, @"desc"@.
Examples

// Get Property instances matching "Riverside Apartment" 
// sorted by price in ascending order
def searchResult = Property.search(
    "Riverside Apartment",
    sort: "price", order: "asc"
)

// Find the least relevant for a fuzzy term search
def leastRelevant = searchableService.search({
        fuzzy("word", "roam")
    },
    result: "top", sort: "SCORE", direction: "reverse"
)

The values "auto" and "reverse" are symbols for Compass API constants: when sorting by SCORE an order of "auto" means highest scoring first and "reverse" means lowest scoring first. When sorting by anything else, "auto" is the natural ascending order and "reverse" is the natural descending order.

The Searchable Plugin also gives you the opton to use "asc" or "desc" for the order or direction value.

Why are there two options for sort order - order and direction - and are they different? Well, they both control the sort order, and you can mix the option names and values, so they are actually just synonyms. But both are provided to satisfy those people more familiar with the GORM style parameters (_order_ with asc_/_desc_) and those more familiar with Compass and search engine queries (_direction with auto_/_reverse_). Choose whichever you prefer.

The following summaries the behaviour of these option combinations. Remember that order and direction are interchangable:

sortorder _or directionSorting behavoir
@"SCORE"@@"auto"@The results are ordered most-relevant (highest scoring) first. This is the default
@"SCORE"@@"reverse"@The results are ordered least-relevant (lowest scoring) first.
@"SCORE"@@"asc"@The results are ordered least-relevant (lowest scoring) first.
@"SCORE"@@"desc"@The results are ordered most-relevant (highest scoring) first. This is the same as SCORE + auto@, which is the default
@"someProperty"@@"asc"@The results are ordered in natural order for the @"someProperty" field value, eg, String ascending, Number ascending
@"someProperty"@@"desc"@The results are order in revserse natural order for the "someProperty" field value, eg, String descending, Number descending
@"someProperty"@@"auto"@The results are ordered in natural order for the "someProperty" field value, eg, String ascending, Number ascending
@"someProperty"@@"reverse"@The results are order in revserse natural order for the "someProperty" field value, eg, String descending, Number descending

It is also possible to add sorting using a query builder Closure, and in fact with that technique you can add multiple sorts, eg:

import org.compass.core.*

// Sort first by score (relevance), // then by most votes (when the score is equal) def hits = searchableService.searchEvery { queryString("reality tv") sort(CompassQuery.SortImplicitType.SCORE) sort("votes", CompassQuery.SortDirection.REVERSE) }

When using a query building Closure you can also combine sorts in the closure with the above sort/order options_. If you do this, the sort/order _options are added as the last sort in the chain and therefore are applied last.