您的位置:首页 > 其它

solr之字段数据类型

2016-12-28 14:58 225 查看
solr提供了内建的一些数据类型比如numbers, dates, geo location等类型;详情如下



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

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.

2. Date fields

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


1

2

1

2

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

<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,起始时区)

如何设置时间的索引粒度呢?有时候我们并不需要查询精确到分秒级别,我们只需要查询到小时的范围即可,于是在提交文档的时候设置如下

<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}来进行范围查询

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: