Last updated by maurice
4 years ago
suggestQuery
Summary
Suggest a new search query based on spellingSyntax
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 ofspellCheck 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 callThe plugin provides a helper class to highlight the different terms in the suggested vs original query, a la Google, etc. Moresearchwith asuggestQuery@option@, which returns search results for the original query, along with a suggested query.
Parameters
- @query@ - A query String
- @options@ - A
Mapof options
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
falsereturns queries as they are re-written and suggested by the search engine. Default is @true@. - @emulateCapitalisation@ - When
falsereturns a query as re-written and suggested by the search engine, which is usually all lowercase, whentruetries 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 @nullinstead of allowing the same query. Default is @true@
Returns
A suggested queryString or nullExamples
// 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}"