{"id":1274,"date":"2010-07-14T23:45:47","date_gmt":"2010-07-14T18:15:47","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1274"},"modified":"2016-12-19T15:28:30","modified_gmt":"2016-12-19T09:58:30","slug":"searchable-plugin-lucene-search-on-numeric-values","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/searchable-plugin-lucene-search-on-numeric-values\/","title":{"rendered":"Searchable plugin &#8211; Lucene search on numeric values"},"content":{"rendered":"<p>While using searchable plugin for the first time, I wasn&#8217;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:<\/p>\n<pre lang=\"groovy\">String getAgeIndexValue() {\r\n    return org.apache.lucene.document.NumberTools.NumberTools.longToString(this.age)\r\n}<\/pre>\n<p>Also, you will need to change the search query from<\/p>\n<pre lang=\"groovy\">q=age:[20 TO *] to q=ageIndexValue:[0000000000000u TO *]<\/pre>\n<pre lang=\"groovy\">where, 0000000000000u == org.apache.lucene.document.NumberTools.longToString(20L)<\/pre>\n<p>~Aman Aggarwal<br \/>\n<a href=\"mailto:aman@intelligrape.com\">aman@intelligrape.com<\/a><\/p>\n<p><a href=\"http:\/\/www.tothenew.com\/blog\/\">http:\/\/www.IntelliGrape.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>While using searchable plugin for the first time, I wasn&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":282,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[7],"tags":[4840,331,328,329,333,330,327,332,326],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1274"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/282"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1274"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1274\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}