解决compass 数字范围搜索

11.5.2.Query String-Range Queries Extensions
Compass simplifies the usage of range queries when working with dates and numbers. When using numbers it
is preferred to store the number if a lexicography correct value(such as 00001, usually using the format
attribute).When using range queries, Compass allows to execute the following query:value:[1 T O3] and
internally Compass will automatically translate it to value:[0001 TO 0003].

When using dates, Compass allows to use several different formats for the same property.The format of the
Date object should be sortable in order to perform range queries. This means, for example, that the format
attribute should be: format="yyyy-MM-dd".This allows for range queries such as:date:[1980-01-01 TO
1985-01-01] to work. Compass also allows to use different formats for range queries. It can be configured
within the format configuration: format="yyyy-MM-dd||dd-MM-yyyy"(the first format is the one used to store
the String). And now the following range query can be executed: date:[01-01-1980 TO 01-01-1985].

Compass also allows for math like date formats using the now keyword.For example: "now+1year" will
translate to a date with a year from now. For more information please refer to the DateMathParser javadoc.

============================
例如 我搜索的范围为
1-100  101-200  。。。 1001-10000
建立索引的时候统一用 00001-00100  00101-00200  。。。  01001-10000格式
<property name="PersonCost">
    <meta-data index="un_tokenized" converter="personCostPropertyConverter" format="000000">PersonCost</meta-data>
</property>

format是建立索引的时候统一设置为00000格式,这个和DecimalFormat("00000").format()类似
这样的话,例如我的属性为 301,存入索引的时候存入的就是00301

同时在前台显示的时候,我们必须转换回来
我们取的属性为00301,但在前台我们要显示的属性为301,这里我们就必须通过转换器转换回来。
我写了个转换器personCostPropertyConverter是继承AbstractBasicConverter
重写doFromString方法就可以了

关于如何配置转换器和怎么写,具体可以参考 莫多泡泡的
http://jdkcn.com/entry/the-better-revolution-about-the-compass-lucene-highlight.html

============================
另外需要注意的是 搜索的时候 如果你的搜索条件是 1-100
请用DecimalFormat("00000").format()转换成 00001-00100

你可能感兴趣的:(html,xml,Lucene)