solr提供了內建的一些數據類型好比numbers, dates, geo location等類型;詳情以下
每種數據類型都有一個Java類來管理。
這裏主要講講如下幾種類型
1. String fields
2. Date fields
3. Numeric fieldsjava
<fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
它用在文本數據上,Solr provides the string field type for fields that contain structured values that shouldn’t be altered in any way.ide
<fieldType name="tdate" class="solr.TrieDateField" omitNorms="true" precisionStep="6" positionIncrementGap="0"/>
當你要用到日期的範圍查詢時,最好用這樣的數據類型,並且在提交文檔時日期的格式有特別要求,日期裏有T,Z字符,好比有個timestamp字段設置爲tdate類型了,那麼提交時應該spa
<add> <doc> ... <field name="timestamp">2012-05-22T09:30:22Z</field> ... </doc> </add>
實際上這種日期格式是按ISO-8601 Date/Time格式來組織的,也就是yyyy-MMddTHH:
mm:ssZ。那麼對於2016-05-22T09:30:22Z則有
yyyy = 2016
MM = 05
dd = 22
HH = 09 (24-hr clock)
mm = 30
ss = 22
Z = UTC Timezone (Z is for Zulu,起始時區).net
如何設置時間的索引粒度呢?有時候咱們並不須要查詢精確到分秒級別,咱們只須要查詢到小時的範圍便可,因而在提交文檔的時候設置以下code
<field name="timestamp">2016-05-22T09:30:22Z/HOUR</field>
/HOUR就告訴solr建索引的粒度爲小時,那麼索引裏的這個時間就等價於2016-05-22T09:00:00Z
此外,它還支持一些特殊的keyword,好比NOW,DAY,能夠用timestamp:[NOW/DAY TO NOW/DAY+1DAY}來進行範圍查詢orm
<fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>
<field name="favorites_count" type="int" indexed="true" stored="true" />
這種類型的字段通常很差用來進行檢索,可是它能夠用來排序,若是你對string的字段排序它是按字典的順序來排的,並非按數字的大小來排的。排序
上面的介紹中能夠看到positionIncrementGap這樣配置屬性,這些屬性是一些高級用法,相似還有索引
屬性 | 當設置爲true時 |
---|---|
sortMissingFirst | 當排序時,檢索結果裏會列那些這個字段沒有值的記錄放在最前面 |
sortMissingLast | 當排序時,檢索結果裏會列那些這個字段沒有值的記錄放在最後面 |
precisionStep | 用在number類型的字段,表示精度 |
positionIncrementGap | 用在字符短語,區分短語之間的間隔距離 |
precisionStep 和positionIncrementGap主要是爲了提升範圍查詢的速度,原理比較複雜,stackoverflow上有個回答:
The precisionStep is a count, after how many bits of the indexed value a new term starts. The original value is always indexed in full precision. Precision step of 4 for a 32 bit value(integer) means terms with these bit counts: All 32, left 28, left 24, left 20, left 16, left 12, left 8, left 4 bits of the value (total 8 terms/value). A precision step of 26 would index 2 terms: all 32 bits and one single term with the remaining 6 bits from the left.ci