elasticsearch - 是否可以从 Elasticsearch 中的文档中获取整个嵌套对象?
问题描述
想象一下,我有一个这样的文件:
{
"_index": "bank-accounts",
"_type": "_doc",
"_id": "1",
"_version": 4,
"_seq_no": 3,
"_primary_term": 1,
"found": true,
"_source": {
"id": 1,
"balance": 140,
"transactions": [
{
"id": "42f52474-a49b-4707-86e4-e983efb4ab31",
"type": "Deposit",
"amount": 100
},
{
"id": "3f8396a3-d747-4a4c-8926-cdcedea6b5c3",
"type": "Deposit",
"amount": 50
},
{
"id": "5693585d-6356-4d1a-8d7b-cac5d0dab39f",
"type": "Withdraw",
"amount": 10
}
],
"accountCreatedAt": 1614029062764
}
}
我确实想只返回transactions
查询中的数组。
我将如何在 Elasticsearch 中执行此操作?这甚至可能吗?我已经使用 实现了结果fields[ "transactions.*" ]
,但它返回单独数组中的每个字段:
{
...
"hits": [
{
"_index": "bank-accounts",
"_type": "_doc",
"_id": "1",
"_score": 1,
"fields": {
"transactions.id": [
"42f52474-a49b-4707-86e4-e983efb4ab31",
"3f8396a3-d747-4a4c-8926-cdcedea6b5c3",
"5693585d-6356-4d1a-8d7b-cac5d0dab39f"
],
"transactions.amount": [
100,
50,
10
],
"transactions.type": [
"Deposit",
"Deposit",
"Withdraw"
],
...
}
}
]
}
}
我的意思是,我很可能会使用它,但我想要一些更简单的东西来处理。我希望得到这样的东西:
*我必须在搜索中使用文档 ID
{
...
"hits": [
{
"_index": "bank-accounts",
"_type": "_doc",
"_id": "1",
"_score": 3,
"transactions": [
{
"id": "42f52474-a49b-4707-86e4-e983efb4ab31",
"type": "Deposit",
"amount": 100
},
{
"id": "3f8396a3-d747-4a4c-8926-cdcedea6b5c3",
"type": "Deposit",
"amount": 50
},
{
"id": "5693585d-6356-4d1a-8d7b-cac5d0dab39f",
"type": "Withdraw",
"amount": 10
},
....
]
}
]
}
}
这有可能实现吗?
解决方案
如果您只想返回transactions
数组(因为您没有提到任何需要搜索的查询条件),您可以使用源过滤来实现。
添加一个工作示例
索引映射:
{
"mappings": {
"properties": {
"transactions": {
"type": "nested"
}
}
}
}
指数数据:
{
"id": 1,
"balance": 140,
"transactions": [
{
"id": "42f52474-a49b-4707-86e4-e983efb4ab31",
"type": "Deposit",
"amount": 100
},
{
"id": "3f8396a3-d747-4a4c-8926-cdcedea6b5c3",
"type": "Deposit",
"amount": 50
},
{
"id": "5693585d-6356-4d1a-8d7b-cac5d0dab39f",
"type": "Withdraw",
"amount": 10
}
],
"accountCreatedAt": 1614029062764
}
搜索查询:
{
"_source": [
"transactions.*"
]
}
搜索结果:
"hits": [
{
"_index": "66324257",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"transactions": [
{
"amount": 100,
"id": "42f52474-a49b-4707-86e4-e983efb4ab31",
"type": "Deposit"
},
{
"amount": 50,
"id": "3f8396a3-d747-4a4c-8926-cdcedea6b5c3",
"type": "Deposit"
},
{
"amount": 10,
"id": "5693585d-6356-4d1a-8d7b-cac5d0dab39f",
"type": "Withdraw"
}
]
}
}
]
推荐阅读
- mysql - 有什么方法可以在不手动进行的情况下加密我的数据?
- html - 在python中将api数据转换为HTML
- java - 如何在没有内容类型标头的情况下从请求访问请求正文
- ipv6 - 没有IP地址的IPFS在线节点
- python - 如何在 Linux 上的 tkinter.Canvas.create_window() 小部件中为 ttk.Frame 启用 MouseWheel 滚动?
- embedded - STM32 USB 中间件:HID 输出报告事务错误
- javascript - 如何在 Boostrap 4 的 Dropmenu 上放置动画
- python - 根据同一数据框的行中的值在数据框中创建一个新列
- php - PHP 中不显示引号和撇号
- node.js - mongodb 连接错误导致 kubernetes pod 挂起并重新启动 pod 修复了问题