elasticsearch - 弹性搜索 | 如何搜索嵌套对象?
问题描述
我正在尝试创建一个搜索查询,该查询返回参与者同时为director
和的所有文档owner
。问题是这些数据分布在多个嵌套对象中。
请考虑以下两个文件:
{
name: 'Facebook Inc.'
participants: [
{
name: 'Mark Zuckerberg',
roles: [
{
type: 'founder',
},
{
type: 'owner',
},
{
type: 'director',
},
]
}
]
},
{
name: 'Alphabet Inc.'
participants: [
{
name: 'Larry Page',
roles: [
{
type: 'founder',
},
{
type: 'owner',
}
]
},
{
name: 'Sundar Pichai',
roles: [
{
type: 'director',
}
]
},
]
}
索引方式如下:
{
index: 'companies',
body: {
mappings: {
company: {
properties: {
name: {type: 'text'},
participants: {
type: 'nested',
properties: {
name: {type: 'text'},
roles: {
type: 'nested',
properties: {
type: {type: 'text'}
}
}
}
}
}
}
}
}
}
问题:如何创建一个搜索查询来返回 aparticipant
和director
的所有文档founder
?
预期的结果当然是 Facebook 的例子。
解决方案
这应该这样做:
{
"query" : {
"nested" : {
"path" : "participants",
"query" : {
"bool" : {
"must" : [
{
"nested" : {
"path" : "participants.roles",
"query" : {
"match" : { "participants.roles.type": "founder" }
}
}
},
{
"nested" : {
"path" : "participants.roles",
"query" : {
"match" : { "participants.roles.type": "director" }
}
}
}
]
}
}
}
}
}
请注意,布尔查询的位置很重要。将它放在第一个嵌套查询中表明它必须是具有两个角色的参与者。
推荐阅读
- google-data-studio - 如何正确计算 Google Data Studio 中的介质
- angular - 对角材料表中的可观察数据进行排序
- pandas - 如何从 for 循环的绘图结果创建一个图形
- c++ - 如何在 Visual Studio 上尽可能自动地通过 vcpkg 安装 wxWidgets?
- javascript - 如何确定 Javascript 中意外滚动事件的原因?
- javascript - 使用 LocalStorage API 持久化 Miragejs 数据库
- python - 如何将excel文件的名称作为参数传递给函数
- html - 我有选择框,如何在选择框的项目上添加徽标?
- python - 删除 matplotlib seaborn 点图中的垂直线
- javascript - 将 typescript 项目与托管在本地 Apache 服务器上的现有 PHP 应用程序集成和调试