天天看點

解決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