Last updated by zhangyu_2100
3 years ago
search
Summary
Finds domain object hits for a query.Syntax
searchableService.search(String query) searchableService.search(String query, Map options) searchableService.search(Map options, String query) // same as previous searchableService.search(Closure builder) searchableService.search(Closure builder, Map options) searchableService.search(Map options, Closure builder) // same as previous
DomainClass.search(String query) DomainClass.search(String query, Map options) DomainClass.search(Map options, String query) // same as previous DomainClass.search(Closure builder) DomainClass.search(Closure builder, Map options) DomainClass.search(Map options, Closure builder) // same as previous
Description
Issues a query to the search engine and returns the result.Normally that result (return value) is a "search result" object, which contains a subset of hits and other data, but you can ask for a different result with the result option.The query can be specified as either a String or Closure parameter.Options can be provided to modify the query or result.Parameters
- query - A query String
- builder - A query-buiding Closure
- options - A Map of options
Options
Options affecting the search 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
Options for String queries
- escape - Should special characters be escaped? Default is false. More
- defaultProperty or defaultSearchProperty - The searchable property for un-prefixed terms. Default is "all". More
- properties - The names of the class properties in which to search. More
- defaultOperator - Either "and" or "or". Default is "and" unless set otherwise elsewhere. More
- analyzer - The name of a query analyzer. More
- parser or queryParser - The name of a query parser. More
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
- suggestQuery - Do you want a suggested query with that search result? If true or Map and result is undefined or "searchResult", adds a suggestedQuery property to the search result object. Use a Map value to define nested suggestQuery options. Use true to use the default suggestQuery options.
Returns
By default (if result is undefined) or if result is "searchResult", returns a "search result" object containing a subset of objects matching the query, with the following properties:- results - A List of matching 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
- suggestedQuery - An alternative query String, based on spelling suggestions. _Only if suggestQuery is non-false_
If you prefer, you can use
- searchTop(...) instead of search(result: 'top', ...)
- searchEvery(...) instead of search(result: 'every', ...)
- countHits(...) instead of search(result: 'count', ...)
Examples
// Get the first page of up to 20 domain objects
// matching the query 'Chelsea Florist'
def searchResult = searchableService.search(
"Chelsea Florist",
[offset: 0, max: 20]
)
assert searchResult instanceof Map
println "${searchResult.total} hits:"
for (i in 0..<searchResult.results.size()) {
println "${searchResult.offset + i + 1}: " +
"${searchResult.results[i].toString()} " +
"(score ${searchResult.scores[i]})"
}// Find the lowest priced product matching the query
// '(laser OR L.A.S.E.R.) beam'
def product = Product.search(
"(laser OR L.A.S.E.R.) beam",
[sort: 'price', order: 'asc', result: 'top']
)
assert product instanceof Product// Get all Articles that contain the terms
// 'police' OR 'doughnut', giving a higher score
// to items matching 'doughnut', and load from DB
def articles = Article.search(
"Police doughnut^2.0",
[reload: true, result: 'every']
)
assert articles.each { it instanceof Product }// Count the number of domain objects matching 'cow pie' def count = searchableService.countHits("cow pie") println "There are ${count} hits for query 'cow pie'"
// Find other objects like the identified Book
def searchResult = searchableService.moreLikeThis(
class: Book, id: 2l
)
assert searchResult instanceof Map
println "${searchResult?.results?.size()} similar items found"// Check the spelling in a query def suggestedQuery = Song.suggestQuery("living on a preyer") println "Did you mean ${suggestedQuery}?"
// Get a "search result" object for the given // closure-defined query across all // searchable class instances def searchResult = searchableService.search { fuzzy('name', 'lundon') }
// Get every Book relevant to the closure-defined query and // highlight the "title" and "summary" properties, // keeping the highlights in a separate List def highlights = [] def bookHighlighter = { highlighter, index, sr -> highlights[index] = [ title: highlighter.fragment("title"), summary: highlighter.fragment("summary") ] } def books = Book.search(result: 'every', withHighlighter: bookHighlighter) { gt('averageReview', 3) queryString('learning techniques') }
// With typical suggested query def searchResult = searchableService.search("lambda", suggestQuery: true) println "did you mean ${searchResult.suggestedQuery}?"
// With suggested query, non-default options def searchResult = Recipie.search("bacon", suggestQuery: [escape: true, userFriendly: false]) println "did you mean ${searchResult.suggestedQuery}?"