Searchable plugin – Lucene search on numeric values
While using searchable plugin for the first time, I wasn’t aware that lucene implements only lexicographic comparisons for searching on indexed values (even on numeric fields). Before I learned this, I kept on wondering why a Person of age 6 always appeared in search results when I look for People more than 20 years of age. The solution was to define a transient field ageIndexValue in domain class Person and implement its getter as:
String getAgeIndexValue() {
return org.apache.lucene.document.NumberTools.NumberTools.longToString(this.age)
}
Also, you will need to change the search query from
q=age:[20 TO *] to q=ageIndexValue:[0000000000000u TO *]
where, 0000000000000u == org.apache.lucene.document.NumberTools.longToString(20L)
~Aman Aggarwal
aman@intelligrape.com
Hi, Aman. You can also define a specific format for your field or a specific converter. Something like:
static searchable = {
(…)
age index: “not_analyzed”, format: “000000”
}
int age
q=age:[000020 TO *]
===
Take a look at:
http://www.grails.org/Searchable+Plugin+-+FAQ
http://www.grails.org/Searchable+Plugin+-+Converters
http://www.grails.org/Searchable+Plugin+-+Mapping+-+Searchable+Property