世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。
问 :coerce有什么特点?
答 :
问 :coerce如何使用?
答 :
# coerce
PUT /coerce_test
{
"settings" : {
"index.mapping.coerce" : false
},
"mappings" : {
"properties" : {
"age_1" : {
"type" : "integer",
"coerce" : true
},
"age_2" : {
"type" : "integer"
}
}
}
}
# 索引
POST /coerce_test/_doc/1
{
"age_1" : "18",
"age_2" : 18
}
# 索引 age_2 没有强转而报错
POST /coerce_test/_doc/2
{
"age_1" : "18",
"age_2" : "18"
}
# 结果
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "failed to parse field [age_2] of type [integer] in document with id '2'. Preview of field's value: '18'"
}
],
"type" : "mapper_parsing_exception",
"reason" : "failed to parse field [age_2] of type [integer] in document with id '2'. Preview of field's value: '18'",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "Integer value passed as String"
}
},
"status" : 400
}
# 索引
POST /coerce_test/_doc/3
{
"age_1" : "18.03",
"age_2" : 18
}
# 搜索
# 搜索
GET /coerce_test/_search
{
"query" : {
"term" : {
"age_1" : {
"value" : "18"
}
}
}
}
# 结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "coerce_test",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"age_1" : "18",
"age_2" : 18
}
},
{
"_index" : "coerce_test",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"age_1" : "18.03",
"age_2" : 18
}
}
]
}
}