0

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.

Javvano
  • 969
  • 8
  • 16

1 Answers1

0

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 ...

Javvano
  • 969
  • 8
  • 16
  • 1
    You may also consider storing the count of nested objects explicitly in the ES document. In this case you will be able to use regular `range` query. – Nikolay Vasiliev Sep 13 '19 at 11:45
  • @NikolayVasiliev oh I get it ! , we will consider storing count, thank you for ur advice ! – Javvano Sep 14 '19 at 00:34