I have this document and I want to sort this way :
- if the value 1 is in the field id_status_viewed, then the document's weight for sorting is 1
- if the value 1 is in the field id_status_new, then the document's weight for sorting is 0
I want to sort based on the weight, in a top hits aggregation. Example :
{
    "string_ticket_name": "ticket D",
    "object_tasks": [
        {
            "user_id": 1,
            "id_status_viewed": [
                1
            ],
            "id_status_new": [0]
        }
    ]
}
This is what I tried :
{
    "aggs": {
        "object_tasks": {
            "nested": {
                "path": "object_tasks"
            },
            "aggs": {
                "names": {
                    "top_hits": {
                        "sort": [
                            {
                                "_script": {
                                    "type": "number",
                                    "script": {
                                        "lang": "painless",
                                        "source": "for (item in params._source.id_status_viewed) { if (item == 1) {return 2;}} for (item in params._source.id_status_new) { if (item == 1) {return 0;}}"
                                    },
                                    "order": "asc"
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
}
but for some reason I can't access id_status_viewed
"script_stack": [
                    "for (item in params._source.id_status_viewed) { ",
                    "                           ^---- HERE"
                ],
"reason": "Cannot invoke \"Object.getClass()\" because \"receiver\" is null"
more details below (mapping)
{
    "mappings": {
        "properties": {
            "object_views": {
                "type": "nested",
                "properties": {
                    "number_rank": {
                        "type": "double"
                    },
                    "string_status": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword"
                            }
                        }
                    },
                    "user_id": {
                        "type": "long"
                    }
                }
            },
            "string_ticket_name": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword"
                    }
                }
            },
            "object_tasks": {
                "type": "nested",
                "properties": {
                    "string_ticket_name": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword"
                            }
                        }
                    },
                    "user_id": {
                        "type": "long"
                    }
                }
            }
        }
    }
}
 
    