世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。
问:missing value和unmapped fields的配置有什么作用?
答:
问:missing和unmapped如何使用?
答:
# sort
# missing value and ignore unmapped fields
# unmapped type
DELETE /sort_test_6
# 映射
PUT /sort_test_6
{
"mappings": {
"properties": {
"name": {"type": "text"}
}
}
}
# 索引
POST /sort_test_6/_doc/1
{
"name": "hello"
}
# 搜索 - 没有age的map映射,报错
GET /sort_test_6/_search
{
"query": {
"match_all": {}
},
"sort": ["age"]
}
# 结果
{
"error" : {
"root_cause" : [
{
"type" : "query_shard_exception",
"reason" : "No mapping found for [age] in order to sort on",
"index_uuid" : "5UvUVsd7Q7uxocP1OGeuvw",
"index" : "sort_test_6"
}
],
"type" : "search_phase_execution_exception",
"reason" : "all shards failed",
"phase" : "query",
"grouped" : true,
"failed_shards" : [
{
"shard" : 0,
"index" : "sort_test_6",
"node" : "VK0GF1KyQGKmM_0KvgHHnw",
"reason" : {
"type" : "query_shard_exception",
"reason" : "No mapping found for [age] in order to sort on",
"index_uuid" : "5UvUVsd7Q7uxocP1OGeuvw",
"index" : "sort_test_6"
}
}
]
},
"status" : 400
}
# 搜索 - 没有age的map映射,默认long
GET /sort_test_6/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"unmapped_type": "long"
}
}
]
}
# 结果
{
"took" : 10,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "sort_test_6",
"_type" : "_doc",
"_id" : "1",
"_score" : null,
"_source" : {
"name" : "hello"
},
"sort" : [
9223372036854775807
]
}
]
}
}
# missing value
PUT /sort_test_7
{
"mappings": {
"properties": {
"age": {"type": "long"}
}
}
}
# 索引
PUT /sort_test_7/_doc/1
{
"age": 18
}
# 索引
PUT /sort_test_7/_doc/2
{
"age": 19
}
# 索引
PUT /sort_test_7/_doc/3
{
"name": "good"
}
# 搜索 - 如果文档没有字段值,missing可以指定
# 没有字段的文档在排序时是处在最上(_first)
# 或最下(_last)的位置
GET /sort_test_7/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order" : "desc",
"missing": "_first"
}
}
]
}
# 结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "sort_test_7",
"_type" : "_doc",
"_id" : "3",
"_score" : null,
"_source" : {
"name" : "good"
},
"sort" : [
9223372036854775807
]
},
{
"_index" : "sort_test_7",
"_type" : "_doc",
"_id" : "2",
"_score" : null,
"_source" : {
"age" : 19
},
"sort" : [
19
]
},
{
"_index" : "sort_test_7",
"_type" : "_doc",
"_id" : "1",
"_score" : null,
"_source" : {
"age" : 18
},
"sort" : [
18
]
}
]
}
}