Search code examples
elasticsearchelasticsearch-painless

How to extract documents that has 2 or more nested object?


I am trying to extract documents that has 2 or more nested objects on Elasticsearch 6.1.2.
our index has such a simple mapping.

    {
        "organizations": {
            "type": "nested",
            "properties": {
            "id": {
                "type": "text"
            }
            "name": {
                "type": "text"
            }
        }
    }

and we would like to extract documents that has 2 or more organizations.
like this

    {
        "organizations": [
            {
                "id" : "1",
                "name" : "company A"
            },
            {
                "id" : "2",
                "name" : "company B"
            }
        ]
    }

some articles says painless script query useful for this case,
so I expected could achieve that with below query

    {
        "query": {
            "nested": {
                "path": "organizations",
                "query": {
                    "bool": {
                        "must": {
                            "script": {
                                "script": {
                                    "inline": "doc['organizations'].length > 1",
                                    "lang": "painless"
                                }
                            }
                        }
                    }
                }
            }
        }
    }

but Elasticsearch said

    {
        "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "No field found for [organizations] in mapping with types [top]"
        }
    }

Can you please give me some ideas of how to achive this?any idea or options welcome.
Thanks a lot.


Solution

  • ok finally I found out , we can't query by nested objects count.

    cause we have to access _source field for count nested objects
    but currentlly Elasticsearch doesn't support access _source field except update query for performance reason.
    hence only one way is retrieve all documents and filter at client ...