我有一个包含嵌套对象列表的文档。我想创建一个查询以返回包含某些嵌套元素的文档,但也将嵌套元素的结果列表限制为满足条件的文档。
例如:
我想获取所有包含嵌套元素“ user.first”:“ Alice” 的文档,而且还希望将“用户”中返回的结果限制为仅包含first的对象:爱丽丝
PUT /posts?pretty
PUT posts
{
"mappings": {
"doc": {
"properties": {
"user": {
"type": "nested"
}
}
}
}
}
PUT posts/doc/1
{
"group" : "fans",
"user" : [
{
"first" : "John",
"last" : "Smith"
},
{
"first" : "Alice",
"last" : "White"
}
]
}
PUT posts/doc/2
{
"group" : "fans",
"user" : [
{
"first" : "John",
"last" : "Smith"
},
{
"first" : "Alice",
"last" : "Black"
},
{
"first" : "Alice",
"last" : "Red"
}
]
}
然后我可以使用inner_hits来获取匹配的用户,但是我想在_source.user中检索此列表,而不是在inner_hits下(不要返回不包含 Alice 的_source.user ):
GET posts/_search
{
"query": {
"nested": {
"path": "user",
"query": {
"bool": {
"must": [
{ "match": { "user.first": "Alice" }}
]
}
},
"inner_hits": { }
}
}
}
返回此:
#hit 1:
{.....
},
#hit 2:
{
"_index": "posts",
"_type": "doc",
"_id": "2",
"_source": {
"group": "fans",
"user": [
{
"first" : "John",
"last" : "Smith"
},
{
"first" : "Alice",
"last" : "Black"
},
{
"first" : "Alice",
"last" : "Red"
}
]
},
"inner_hits": {
"user": {
"hits": {
"total": 2,
"hits": [
{
"_nested": {
"field": "user",
"offset": 2
},
"_source": {
"first": "Alice",
"last": "Black"
}
},
{
"_nested": {
"field": "user",
"offset": 1
},
"_source": {
"first": "Alice",
"last": "Red"
}
}
]
}
}
}