solr之字段数据类型

solr提供了内建的一些数据类型好比numbers, dates, geo location等类型;详情以下

每种数据类型都有一个Java类来管理。
这里主要讲讲如下几种类型
1. String fields
2. Date fields
3. Numeric fieldsjava

1. String fields

<fieldType name="string" class="solr.StrField"
sortMissingLast="true" omitNorms="true"/>
  • 1
  • 2
  • 1
  • 2

它用在文本数据上,Solr provides the string field type for fields that contain structured values that shouldn’t be altered in any way.ide

2. Date fields

<fieldType name="tdate" class="solr.TrieDateField" omitNorms="true"
precisionStep="6" positionIncrementGap="0"/>
  • 1
  • 2
  • 1
  • 2

当你要用到日期的范围查询时,最好用这样的数据类型,并且在提交文档时日期的格式有特别要求,日期里有T,Z字符,好比有个timestamp字段设置为tdate类型了,那么提交时应该spa

<add>
<doc>
...
<field name="timestamp">2012-05-22T09:30:22Z</field>
...
</doc>
</add>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

实际上这种日期格式是按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>
  • 1
  • 1

/HOUR就告诉solr建索引的粒度为小时,那么索引里的这个时间就等价于2016-05-22T09:00:00Z
此外,它还支持一些特殊的keyword,好比NOW,DAY,能够用timestamp:[NOW/DAY TO NOW/DAY+1DAY}来进行范围查询orm

3. Numeric fields

<fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>
  • 1
  • 1
<field name="favorites_count" type="int" indexed="true" stored="true" />
  • 1
  • 1

这种类型的字段通常很差用来进行检索,可是它能够用来排序,若是你对string的字段排序它是按字典的顺序来排的,并非按数字的大小来排的。排序

4. 一些高级用法

上面的介绍中能够看到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

相关文章
相关标签/搜索