假设我有以下文档:
{
id: 1,
name: "xyz",
users: [
{
name: 'abc',
surname: 'def'
},
{
name: 'xyz',
surname: 'wef'
},
{
name: 'defg',
surname: 'pqr'
}
]
}
我只想获取与搜索响应中的所有顶级字段匹配的嵌套对象。
我的意思是,如果我搜索/过滤名为“abc”的用户,我想要以下响应
{
id: 1,
name: "xyz",
users: [
{
name: 'abc',
surname: 'def'
}
]
}
我怎样才能做到这一点?
参考 :从elasticsearch中的数组中选择匹配的对象 https://stackoverflow.com/questions/31719968/select-matching-objects-from-array-in-elasticsearch
如果您同意拥有除嵌套字段之外的所有根字段,然后仅在嵌套字段中匹配内部匹配,那么我们可以通过指定稍微复杂一些的源过滤参数来重复使用之前的答案:
{
"_source": {
"includes": [ "*" ],
"excludes": [ "users" ]
},
"query": {
"nested": {
"path": "users",
"inner_hits": { <---- this is where the magic happens
"_source": [
"name", "surname"
]
},
"query": {
"bool": {
"must": [
{
"term": {
"users.name": "abc"
}
}
]
}
}
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)