Last updated by maurice 5 years ago

suggestQuery

Summary

Suggest a new search query based on spelling

Syntax

searchableService.suggestQuery(String query)
searchableService.suggestQuery(String query, Map options)
searchableService.suggestQuery(Map options, String query) // same as previous

DomainClass.suggestQuery(String query)
DomainClass.suggestQuery(String query, Map options)
DomainClass.suggestQuery(Map options, String query) // same as previous

Description

Uses the spelling index to suggest an alternative query.

You need to add some sort of spellCheck mapping for domain classes/properties that you wish to include in spelling index, either at the class level or property level.

You can programmatically re-build the spelling index if you like. This might be useful for testing, but otherwise don't worry, it is periodically refreshed automatically by Compass.

You can also call search with a suggestQuery @option@, which returns search results for the original query, along with a suggested query.

The plugin provides a helper class to highlight the different terms in the suggested vs original query, a la Google, etc. More

Parameters

options

Options affecting the search query

  • @escape@ - Should special characters be escaped? Default is @false@. More

Options affecting the return value

  • @userFriendly@ - Should the suggested query look like a user-query? When false returns queries as they are re-written and suggested by the search engine. Default is @true@.
  • @emulateCapitalisation@ - When false returns a query as re-written and suggested by the search engine, which is usually all lowercase, when true tries to emulate the upper-casing of the original query. Default is @true@
  • @allowSame@ - Can the method return the same query as the given query@? This is possible if no better suggestions are found. If @false@, the method returns @null instead of allowing the same query. Default is @true@

Returns

A suggested query String or null

Examples

// Get a suggested query, using all available class instances
// for spelling suggestions
def suggestedQuery = searchableService.suggestQuery("grate briton")
println "Did you mean ${suggestedQuery}"

// Get a suggested query using only searchable text from "Player" 
// instances for spelling suggestions, allowing for bad
// characters and disabling the user-friendly options
def suggestedQuery = Player.suggestQuery(
    "kartoon", 
    escape: true, userFriendly: false, emulateCapitalisation: false
)
println "Did you mean ${suggestedQuery}"