世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。
问 :nested field type有什么特点?
答 :
1. 为数组元素建立嵌套索引
2. 使用nested搜索
3. 使用nested聚合和reverse_nested聚合进行分析
4. 使用nested sorting排序
5. 使用netsted inner hits实现检索和高亮
6. 消耗大
问 :nested field type防止mappings explosion的settings有哪些?
答 :
1. index.mapping.nested_fields.limit :
每个索引nested类型的field的最大数量,默认50
2. index.mapping.nested_objects.limit :
每个文档可以包含的nested对象的最大数量,默认10000
问 :nested field type如何使用?
答 :
# nested
# 配置项 :
# 1. dynamic
# 2. properties
# 3. include_in_parent
# 4. include_in_root
PUT /netsted_test
{
"mappings" : {
"properties" : {
"user1" : {
"type" : "nested"
}, "user2" : {
"type" : "flattened"
}
}
}
}
POST /netsted_test/_doc
{
"user1" : [
{"first" : "hello", "last" : "good"},
{"first" : "me", "last" : "kik"}
],
"user2" : [
{"first" : "hello", "last" : "good"},
{"first" : "me", "last" : "kik"}
]
}
# 不使用netsted,搜索结果为空
GET /netsted_test/_search
{
"query" : {
"bool" : {
"must" : [
{"match" : {"user1.first" : "hello"}},
{"match" : {"user1.last" : "good"}}
]
}
}
}
# 结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
# flattened匹配数组内的任何元素节点,也可以交叉匹配
GET /netsted_test/_search
{
"query" : {
"bool" : {
"must" : [
{"match" : {"user2.first" : "hello"}},
{"match" : {"user2.last" : "kik"}}
]
}
}
}
# 结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.83003354,
"hits" : [
{
"_index" : "netsted_test",
"_type" : "_doc",
"_id" : "42vD9XcBnJvha9PMe4w_",
"_score" : 0.83003354,
"_source" : {
"user1" : [
{
"first" : "hello",
"last" : "good"
},
{
"first" : "me",
"last" : "kik"
}
],
"user2" : [
{
"first" : "hello",
"last" : "good"
},
{
"first" : "me",
"last" : "kik"
}
]
}
}
]
}
}
# nested只针对数组内某个元素匹配
GET /netsted_test/_search
{
"query" : {
"nested" : {
"path" : "user1",
"query" : {
"bool" : {
"must" : [
{"match" : {"user1.first" : "hello"}},
{"match" : {"user1.last" : "good"}}
]
}
},
"inner_hits" : {
"highlight" : {
"fields" : {"user1.first": {}}
}
}
}
}
}
# 结果
{
"took" : 62,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.3862942,
"hits" : [
{
"_index" : "netsted_test",
"_type" : "_doc",
"_id" : "42vD9XcBnJvha9PMe4w_",
"_score" : 1.3862942,
"_source" : {
"user1" : [
{
"first" : "hello",
"last" : "good"
},
{
"first" : "me",
"last" : "kik"
}
],
"user2" : [
{
"first" : "hello",
"last" : "good"
},
{
"first" : "me",
"last" : "kik"
}
]
},
"inner_hits" : {
"user1" : {
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.3862942,
"hits" : [
{
"_index" : "netsted_test",
"_type" : "_doc",
"_id" : "42vD9XcBnJvha9PMe4w_",
"_nested" : {
"field" : "user1",
"offset" : 0
},
"_score" : 1.3862942,
"_source" : {
"last" : "good",
"first" : "hello"
},
"highlight" : {
"user1.first" : [
"<em>hello</em>"
]
}
}
]
}
}
}
}
]
}
}