solr之字段数据类型
2016-12-28 14:58
225 查看
solr提供了内建的一些数据类型比如numbers, dates, geo location等类型;详情如下
![](http://img.blog.csdn.net/20160630163953442)
每种数据类型都有一个Java类来管理。
这里主要讲讲以下几种类型
1. String fields
2. Date fields
3. Numeric fields
1
2
1
2
它用在文本数据上,Solr provides the string field type for fields that contain structured values that shouldn’t be altered in any way.
1
2
1
2
当你要用到日期的范围查询时,最好用这样的数据类型,而且在提交文档时日期的格式有特别要求,日期里有T,Z字符,比如有个timestamp字段设置为tdate类型了,那么提交时应该
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,起始时区)
如何设置时间的索引粒度呢?有时候我们并不需要查询精确到分秒级别,我们只需要查询到小时的范围即可,于是在提交文档的时候设置如下
1
1
/HOUR就告诉solr建索引的粒度为小时,那么索引里的这个时间就等价于2016-05-22T09:00:00Z
此外,它还支持一些特殊的keyword,比如NOW,DAY,可以用timestamp:[NOW/DAY TO NOW/DAY+1DAY}来进行范围查询
1
1
1
1
这种类型的字段一般不好用来进行检索,但是它可以用来排序,如果你对string的字段排序它是按字典的顺序来排的,并不是按数字的大小来排的。
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.
每种数据类型都有一个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 | 用在字符短语,区分短语之间的间隔距离 |
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.
相关文章推荐
- Solr4:数据导入(dataimport)时,不符合Solr日期类型要求的字段的处理
- solr之字段数据类型
- JDBC数据类型与数据库字段对应表――mysql篇
- Sql Server关于字段中的数据类型
- 用Sum函数对DateTime数据类型字段进行求和统计
- [转帖]修复identity 类型字段数据的跳跃
- Access 可用的字段数据类型及用法
- 使用系统表获得MS SQL Server表或视图的字段列表及其数据类型
- MsSql 数据表中把字段类型为text的字段内容导出为文本文件
- 怎样得到数据库中某一个表的某一个字段在设计时候的数据类型
- 去除指定表中数据类型是VARCHAR,CHAR,NVARCHAR的字段值中的不可见字符
- 查询某一个表的字段和数据类型
- JDBC数据类型与数据库字段对应表――SQL Server篇
- ADO.NET中取得数据库字段的数据类型
- 更改Delphi 中ClientDataSet中字段的数据类型
- 浅析J2EE应用中的时间值字段的数据类型(转4)
- 如何更改数据表主键字段的数据类型
- 得到数据库中所有用户表的字段、数据类型
- 浅析J2EE应用中的时间值字段的数据类型(转1)
- Oracle 表字段数据类型