First of all, you need a time based metric to solve this.
Overall trending products does not make much sense particularly for a ecommerce app.
Lets say, you want to do find the trending products for a particular day (say today).
Now this is not a easy problem to solve. And there is no just 1 direct way to solve it. For each company, one method works!
Ideally , what could be a simple solution here would be to create a score value based on combination of factors. For example, number of buys, number of views, number of clicks and so on.
Here, you have the datePosted parameter. Now, create a date_score for this. So, the basic idea here is a product that was posted yesterday has more score than the one posted a week back. The values you put need to tweaked and checked for your algorithm's customization.
Similarly, implemeent a similar score for avgRating and noViews.
Once you have these scores ready, create weights for these scores. Now the weighted average for these out of 100 is the final score.
So, a final example solution:
Date Posted:
- If Date is in the last 3 days (score = 100)
- If Date is the last week (score= 75)
- If Date in the previous week (score = 50)
- Else default score = 25.
Star Rating:
numberOfViews:
- Here , you can use percentiles.
- Lets say max views among all the product is x.
- Then, the view score is (views/x) *100
Now, handle the corner case of what happens when two scores match.
After this, you can simply order by the score parameter.
Also, make sure everything you do is dynamic as static thresholds dont provide a great resul generally!