Sorting script with tiebreaker

Hi,

We have multiple documents in an index with duplicate field value (product_id). I want to sort based on product_id with a condition that for 2 records having same product_id, sort based on product_cost.

Recreation steps:

PUT /product
{
    "mappings" : {
        "properties" : {
			"product_id": { "type" : "long" },
			"product_cost": { "type" : "long" }
        }}}
        

POST product/_doc
{
			"product_id": 22,
			"product_cost": 1232
}

POST product/_doc
{
			"product_id": 36,
			"product_cost": 2314
}

POST product/_doc
{
			"product_id": 36,
			"product_cost": 8892
}

POST product/_doc
{
			"product_id": 87,
			"product_cost": 1287
}

POST product/_doc
{
			"product_id": 100,
			"product_cost": 6372
}

POST product/_doc
{
			"product_cost": 3452
}

Please note that there are dirty records with no product_name filed and for those cases I want to assign default value of -9999 and sort accordingly.

Can you please help me with a sort script. Challenge that I'm facing is with the tie breaker in the script.

Hi, can someone help here.