解決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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章