Sort by count of nested objects matching two or more criterias

I need to sort by the count of nested objects matching two or more criterias.
ncomments is declared as type=nested in the mapping.

The query below throws this exception:

"reason" : "failed to run inline script [doc['ncomments'].value] using lang [groovy]",
 "type" : "illegal_argument_exception",
 "reason" : "No field found for [ncomments] in mapping with types []"

If there is a solution without scripting (or at least without grovvy scripting) i would prefer that.
I am using Elasticsearch 2.2

What i tried so far:

{
    "query":{
          "match_all":{}
    },
    "sort":[
        {
            "_script":{
                "script":{
                    "inline":"doc['ncomments'].values.length"
                },
                "type":"number",
                "order":"asc",
                "nested_path":"ncomments",
                "nested_filter":{
                    "bool":{
                        "must":[
                            {
                                "term":{
                                    "ncomments.c_status":"public"
                                }
                            },
                            {
                                "range":{
                                    "ncomments.date":{
                                        "lt":"2014-02-01",
                                        "format":"yyyy-MM-dd"
                                    }
                                }
                            }
                        ]
                    }
                }
            }
        }
    ]
}
1 Like