'Using parent data in a nested aggregation in ElasticSearch

I have an aggregation that uses comments nested within a task, the purpose of this aggregation is to count comments that other users who are not responsible for the task have left on the task. Tried using painless for this, but I can't get the document's parent data. How can I get it to do my calculation?

This is my mapping:

curl -XPUT 'localhost:9200/tasks?pretty' -H 'Content-Type: application/json' -d '
{
    "settings": {
        "number_of_shards": 1
    },
    "mappings": {
        "task": {
            "properties": {
                "owner_id": {
                    "type": "long",
                    "index": true
                },
                "title": {
                    "type": "text",
                    "index": true
                },
                "comments": {
                    "type": "object",
                    "properties": {
                        "content": {
                            "type": "nested",
                            "properties": {
                                "comment_id": {
                                    "type": "long",
                                    "index": true
                                },
                                "message": {
                                    "type": "text",
                                    "index": true
                                },
                                "user_id": {
                                    "type": "long",
                                    "index": true
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}'

This is my query:

{
    "size": 0,
    "_source": false,
    "stored_fields": "_none_",
    "aggregations": {
        "comments": {
            "nested": {
                "path": "comments.content"
            },
            "aggs": {
                "operators": {
                    "terms": {
                        "field": "comments.content.user_id",
                        "exclude": [
                            0
                        ]
                    },
                    "aggs": {
                        "comment_task_someone_else": {
                            "sum": {
                                "script": {
                                    "source": "(doc['comments.content.user_id'].value !== doc['owner_id'].value)?1:0"
                                }
                            }
                        },
                        "comment_own_task": {
                            "sum": {
                                "script": {
                                    "source": "(doc['comments.content.user_id'].value === doc['owner_id'].value)?1:0"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

I tried adding just the doc['owner_id'].value and it always comes to zero.

How can I get this data to count? Or is it possible to totally change the way I do the aggregation to get what I need?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source