Last updated by maurice 5 years ago

countHits

Summary

Get the number of hits for a query.

Syntax

searchableService.countHits(String query)
searchableService.countHits(String queryString, Map options)
searchableService.countHits(Map options, String queryString) // same as previous
searchableService.countHits(Closure builder)
searchableService.countHits(Closure builder, Map options)
searchableService.countHits(Map options, Closure builder) // same as previous

DomainClass.countHits(String query)
DomainClass.countHits(String queryString, Map options)
DomainClass.countHits(Map options, String queryString) // same as previous
DomainClass.countHits(Closure builder)
DomainClass.countHits(Closure builder, Map options)
DomainClass.countHits(Map options, Closure builder) // same as previous

Description

Issues a query to the search engine and returns the number of hits.

This method is just like search@, except that it always return the hit count rather than relevant class instances. In fact you can use @search(..., result: 'count') and countHits(...) interchangably.

Options can be provided to modify the query.

Parameters

Options

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

Returns

The number of hits for the query

Examples

// count hits across all searchable class instances
def count = searchableService.countHits("samuri")

// count hits within Show instances,
// escaping reserved string query characters
def count = Show.countHits("CSI [Las Vegas]", escape: true)

// count hits across all searchable class instances
def count = searchableService.countHits("CSI [Miami]", [escape: true])

// count hits across all searchable class instances
// defining the query with a builder closure
def count = searchableService.countHits {
    term("format", "MP3")
    multiPhrase("title") {
        add("Wrecking")
        add("Ball")
    }
}

// count hits for Show instance with a query
// built with a closure 
def count = Show.countHits {
    term("keywords", "crime")
    term("keywords", "drama")
    queryString("ongoing love interest subplot", [defaultSearchProperty: "notes"])
}