Searchable plugin – Lucene search on numeric values « Intelligrape Groovy & Grails Blogs

Searchable plugin – Lucene search on numeric values

Posted by Aman Aggarwal

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/

  • Share/Bookmark
This entry was posted on July 14th, 2010 at 11:45 pm and is filed under Grails . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Searchable plugin – Lucene search on numeric values”

  1. Daniel Henrique says:

    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

Leave a Reply