elasticsearch - 如何提取具有 2 个或更多嵌套对象的文档?
问题描述
我正在尝试在 Elasticsearch 6.1.2 上提取具有 2 个或更多嵌套对象的文档。
我们的索引有这样一个简单的映射。
{
"organizations": {
"type": "nested",
"properties": {
"id": {
"type": "text"
}
"name": {
"type": "text"
}
}
}
我们想提取具有 2 个或更多组织的文档。
像这样
{
"organizations": [
{
"id" : "1",
"name" : "company A"
},
{
"id" : "2",
"name" : "company B"
}
]
}
有些文章说无痛脚本查询对这种情况很有用,
所以我希望可以通过以下查询来实现
{
"query": {
"nested": {
"path": "organizations",
"query": {
"bool": {
"must": {
"script": {
"script": {
"inline": "doc['organizations'].length > 1",
"lang": "painless"
}
}
}
}
}
}
}
}
但 Elasticsearch 说
{
"caused_by": {
"type": "illegal_argument_exception",
"reason": "No field found for [organizations] in mapping with types [top]"
}
}
你能给我一些关于如何实现这一点的想法吗?欢迎任何想法或选择。
非常感谢。
解决方案
好的,最后我发现,我们不能按嵌套对象计数查询。
因为我们必须访问_source
计数嵌套对象的字段,
但目前 Elasticsearch 不支持访问_source
字段,但出于性能原因更新查询除外。
因此只有一种方法是在客户端检索所有文档并过滤...
推荐阅读
- sql - 使用 HAVING COUNT(column name) 未按预期工作
- javascript - 使用html混合模式过滤自定义自动完成?
- python-3.x - 在熊猫时间序列中用 groupby 拟合一条线并得到斜率
- python - 如何使用 Pandas 对单个列进行分组并在 excel 中打印相同的列
- python - 在python中查找并删除以特定数字开头和结尾的字符串
- sql-server - SQL 2019 上的 CU6 到 CU 11 升级使对表格函数的查询非常慢
- sql-server - 从 SQL Express 实例创建到 SQL Azure 数据库的链接服务器时出错“此版本的 SQL Server 不支持 Windows 登录。”
- python - 我不知道如何用一个 txt 文件来 blit 一个列表(blit 在两个 for 循环中),下面我已经尝试过了,但它不起作用
- mysql - 在 WHERE 中添加 OR
- discord.py - 我想在我的 discord.py 机器人启用时向特定频道发送消息