solr提供了内建的一些数据类型比如numbers, dates, geo location等类型;详情如下
每种数据类型都有一个Java类来管理。
这里主要讲讲以下几种类型
1. String fields
2. Date fields
3. Numeric fields
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.
name="tdate" class="solr.TrieDateField" omitNorms="true"
precisionStep="6" positionIncrementGap="0"/>
当你要用到日期的范围查询时,最好用这样的数据类型,而且在提交文档时日期的格式有特别要求,日期里有T,Z字符,比如有个timestamp字段设置为tdate类型了,那么提交时应该
...
"timestamp">2012-05-22T09:30:22Z
...
实际上这种日期格式是按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,起始时区)
如何设置时间的索引粒度呢?有时候我们并不需要查询精确到分秒级别,我们只需要查询到小时的范围即可,于是在提交文档的时候设置如下
<field name="timestamp">2016-05-22T09:30:22Z/HOURfield>
/HOUR就告诉solr建索引的粒度为小时,那么索引里的这个时间就等价于2016-05-22T09:00:00Z
此外,它还支持一些特殊的keyword,比如NOW,DAY,可以用timestamp:[NOW/DAY TO NOW/DAY+1DAY}来进行范围查询
name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>
"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.