Sort by price and stock

Hi.

I have a catalog of products and i'm need to sort the result by price and stock. The products with stock equal 0 need to be in end of result but need to be ordered by price.
Example:

NAME -> PRICE -> STOCK
Product1 -> 150,00 -> 10
Product2 -> 140,00 -> 12
Product3 -> 135,00 -> 10
Product4 -> 160,00 -> 0
Product5 -> 150,00 -> 0
Product6 -> 140,00 -> 0

The query i'm using is this:

   {
	"from": 0,
	"size": "12",
	"query": {
		"boosting": {
			"positive": {
				"filtered": {
					"query": {
						"bool": {
							"must": [{
								"bool": {
									"should": [{"all queries here"}]
								}
							}]
						}
					},
					"filter": {
						"bool": {
							"some": "filters"
						}
					}
				}
			},
			"negative": {
				"match": {
					"stock_total": 0
				}
			},
			"negative_boost": 0
		}
	},
	"aggs": {
		"some": "aggregations"
	},
	"sort": [
		{"price_facet": {"order": "desc" } }, 
		{"stock_total": {"order": "desc"} },
        {"_score": {"order": "desc"} } 
    ],
	"track_scores": true
}

You can first sort on a script field if the stock price is equal to zero.
You can also create such a field ahead of time, so that you can have better
performance at the cost of index room.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.