首页 > 解决方案 > 如何提取具有 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]"
        }
    }

你能给我一些关于如何实现这一点的想法吗?欢迎任何想法或选择。
非常感谢。

标签: elasticsearchelasticsearch-painless

解决方案


好的,最后我发现,我们不能按嵌套对象计数查询。

因为我们必须访问_source计数嵌套对象的字段,
但目前 Elasticsearch 不支持访问_source字段,但出于性能原因更新查询除外。
因此只有一种方法是在客户端检索所有文档并过滤...


推荐阅读