无符号长整型字段类型
2.8 版本引入
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
}
或者,您可以使用批量 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
类型并使用双精度算术,这很可能导致精度损失。 -
unsigned_long
字段不能用作索引排序字段(在sort.field
索引设置中)。当对多个索引执行搜索且结果按至少一个索引中为unsigned_long
类型但在其他索引中为不同数值类型或多种类型的字段排序时,此限制也适用。