es - elasticsearch mapping - field data type -3

151 阅读1分钟

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问 :numeric field types 都有哪些?各自表示的范围是多少?
答 :

1. 包含 : long integer short byte float double 
          half_float scaled_float unsigned_long
2. 范围 :  
        1. long           : -2^63 ~ 2^63-1
        2. integer        : -2^31 ~ 2^31-1
        3. short          : -2^15 ~ 2^15-1
        4. byte           : -2^7 ~ 2^7-1
        5. double         : 64位双精度浮点数
        6. float          : 32位单精度浮点数
        7. half_float     : 16位半精度浮点数
        8. scaled_float   : 加一个扩展因子,使浮点以整数的形式存储与显示 
        9. unsigned_long  : 0 ~ 2^64-1

问 :numeric field types 如何使用?
答 :

# numeric
# 配置项 :
#   1. coerce           : 字符串转数字,默认true
#   2. boost            : 默认1.0
#   3. doc_values       : 默认true
#   4. ignore_malformed : 忽略不规范的数字,默认false
#   5. index            : 默认true
#   6. null_value       : 默认null
#   7. store            : 默认false
#   8. meta             : 元数据

PUT /numeric_test
{
  "mappings" : {
    "properties" : {
      "my_integer" : {
        "type" : "integer"
      },
      "my_float" : {
        "type" : "float"
      },
      "my_double" : {
        "type" : "double"
      },
      "my_scaled_float" : {
        "type"          : "scaled_float",
        "scaling_factor": 100
      }
    }
  }
}

POST /numeric_test/_doc
{
  "my_integer"      : 10,
  "my_float"        : 2.35,
  "my_double"       : 7.48,
  "my_scaled_float" : 5.67
}

GET /numeric_test/_search

# 结果
{
  "took" : 57,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "numeric_test",
        "_type" : "_doc",
        "_id" : "ymt8zHcBnJvha9PMYoyX",
        "_score" : 1.0,
        "_source" : {
          "my_integer" : 10,
          "my_float" : 2.35,
          "my_double" : 7.48,
          "my_scaled_float" : 5.67
        }
      }
    ]
  }
}