Unsigned long 字段类型
unsigned_long 字段类型是一种数值字段类型,表示无符号 64 位整数,最小值为 0,最大值为 264 − 1。在以下示例中,counter 被映射为 unsigned_long 字段:
PUT testindex
{
"mappings" : {
"properties" : {
"counter" : {
"type" : "unsigned_long"
}
}
}
}
索引
要索引包含 unsigned_long 值的文档,请使用以下请求:
PUT testindex/_doc/1
{
"counter" : 10223372036854775807
}
或者,您也可以使用 Bulk API如下:
POST _bulk
{ "index": { "_index": "testindex", "_id": "1" } }
{ "counter": 10223372036854775807 }
如果 unsigned_long 类型的字段将 store 参数设置为 true(即该字段是存储字段),它将以字符串形式存储和返回。unsigned_long 值不支持小数部分,因此如果提供了小数部分,将被截断。
查询
unsigned_long 字段支持大多数其他数值类型支持的查询。例如,您可以在 unsigned_long 字段上使用词项查询:
POST _search
{
"query": {
"term": {
"counter": {
"value": 10223372036854775807
}
}
}
}
您也可以使用范围查询:
POST _search
{
"query": {
"range": {
"counter": {
"gte": 10223372036854775807
}
}
}
}
排序
您可以将 sort 值与 unsigned_long 字段一起使用来对搜索结果进行排序,例如:
POST _search
{
"sort" : [
{
"counter" : {
"order" : "asc"
}
}
],
"query": {
"range": {
"counter": {
"gte": 10223372036854775807
}
}
}
}
unsigned_long 字段不能用作索引排序字段(在 sort.field 索引设置中)。
聚合
与其他数值字段类似,unsigned_long 字段支持聚合。对于 terms 和 multi_terms 聚合,unsigned_long 值按原样使用,但对于其他聚合类型,值会转换为 double 类型(可能会丢失精度)。以下是 terms 聚合的示例:
POST _search
{
"query": {
"match_all": {}
},
"aggs": {
"counters": {
"terms": {
"field": "counter"
}
}
}
}
脚本
在脚本中,unsigned_long 字段作为 BigInteger 类的实例返回:
POST _search
{
"query": {
"bool": {
"filter": {
"script": {
"script": "BigInteger amount = doc['counter'].value; return amount.compareTo(BigInteger.ZERO) > 0;"
}
}
}
}
}
限制
请注意 unsigned_long 字段类型的以下限制:
当在不同数值类型之间执行聚合且其中一种类型为
unsigned_long时,值将转换为double类型并使用double算术运算,很可能导致精度损失。unsigned_long字段不能用作索引排序字段(在sort.field索引设置中)。此限制也适用于在多个索引上执行搜索,并且结果按在至少一个索引中具有unsigned_long类型但在其他索引中具有不同数值类型的字段进行排序的情况。