views:

65

answers:

1

My simplified domain model in grails is like this:

Article {
    Integer totalViews
    static hasMany = [
        ratings: Rating
    ]
}

Rating {
    Integer value // 1 to 5
    User user
}

I am trying to find the 5 most popular articles based on totalViews and ratings, say 25% weightage on views and 75% weightage on ratings.

How would you do it in Groovy/Grails?

+2  A: 

I'd probably try that with an HQL query, the criteriaBuilder version would be pretty hairy.

I'm not sure what the best alogrithm for the weighting you want would be. But without that, you'd need something like:

from Article a order by avg(a.ratings.value) desc, a.totalViews desc

Then limit the query to 5 results

Article.executeQuery('from Article...', max:5)
leebutts