I have Grails project with domain that has multiple fields, among them I have field 'price'. I added searchable plugin to the project and it works fine, via general search:
def searchResults = searchableService.search(params.q, params)
Now I need to add price search via range. Example: price between $100 and $200. I tried following but it doesn't work:
def searchResults = searchableService.search({
queryString(params.q)
lt("price", params.pmax?.trim().toBigDecimal())
},params)
How do I implement range search? Does it matter what kind of data type it is: Integer, BigDecimal, Long?
Thank you
I did a search by range like this:
def searchResults = searchableService.search(params){
must(queryString(params.q) {
ge('price', params.pmax as BigDecimal)
le('price', params.pmin as BigDecimal)
})
}
[EDITED]
My previous solution doesn't work perfectly. The right way to do search with numbers with searchable, you need first add this in your domain class:
searchable {
price index: "not_analyzed", format : "0000000000"
}
And then to do the search:
def searchResults = yourDomainClass.search("price:[" + (params.pmin ? params.pmin.trim().padLeft(10, "0") : "*" )+ " TO " + (params.pmax ? params.pmax.trim().padLeft(10, "0") : "*" )+ "]"
All that is need because when your data is indexed, is also textified.
http://brettscott.wordpress.com/2011/11/19/lucene-number-range-search-integers-floats/