Skip to content

Commit e90ec97

Browse files
authored
Add extra order by clause when sorting by rating (#288)
1 parent ccebf2f commit e90ec97

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

db/recipe-postgres.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ func (d *postgresRecipeDriver) Find(filter *models.SearchFilter, page int64, cou
177177
if filter.SortDir == models.SortDirDesc {
178178
orderStmt += " DESC"
179179
}
180+
// Need a special case for rating, since the way the execution plan works can
181+
// cause uncertain results due to many recipes having the same rating (ties).
182+
// By adding an additional sort to show recently modified recipes first,
183+
// this ensures a consistent result.
184+
if filter.SortBy == models.SortRecipeByRating {
185+
orderStmt += ", r.modified_at DESC"
186+
}
187+
180188
orderStmt += " LIMIT ? OFFSET ?"
181189

182190
selectStmt := d.postgresDriver.Db.Rebind("SELECT " +

db/recipe-sqlite.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@ func (d *sqliteRecipeDriver) Find(filter *models.SearchFilter, page int64, count
178178
if filter.SortDir == models.SortDirDesc {
179179
orderStmt += " DESC"
180180
}
181+
// Need a special case for rating, since the way the execution plan works can
182+
// cause uncertain results due to many recipes having the same rating (ties).
183+
// By adding an additional sort to show recently modified recipes first,
184+
// this ensures a consistent result.
185+
if filter.SortBy == models.SortRecipeByRating {
186+
orderStmt += ", r.modified_at DESC"
187+
}
188+
181189
orderStmt += " LIMIT ? OFFSET ?"
182190

183191
selectStmt := d.sqliteDriver.Db.Rebind("SELECT " +

0 commit comments

Comments
 (0)