sztrzask
sztrzask

Reputation: 119

How do you find documents where inner_hits count is N?

How can I find documents where inner_hits count is exactly N?

I have a customer. My customer has a nested collection of invoices.

I want to find all customers that have exactly 2 unpaid invoices and I have trouble doing that. I think I can find those that have at least 2, using min_doc_count, but how to make it an exact value?

Upvotes: 1

Views: 661

Answers (1)

ibrahimozgon
ibrahimozgon

Reputation: 1187

I think you can do it by script.

{
    "filtered": {
        "filter": {
            "script": {
                "script": "if(_source.invoices.size<3) return false;{other conditions come here.}",
                "params": {
                    "param1": null,
                    "param2": null
                }
            }
        }
    }
}

You can find a very similar solution here: Elastic Search filter by count of a nested document

Upvotes: 1

Related Questions