Searchable plugin – Lucene search on numeric values

14 / Jul / 2010 by Aman Aggarwal 1 comments

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

http://www.IntelliGrape.com/

FOUND THIS USEFUL? SHARE IT

comments (1 “Searchable plugin – Lucene search on numeric values”)

Leave a Reply

Your email address will not be published. Required fields are marked *