mirror of
https://github.com/discourse/discourse.git
synced 2025-06-03 19:39:30 +08:00
FIX: Order categories in SQL for Categories#search (#26810)
Otherwise, the results don't make sense if the number of categories is more than the limit provided.
This commit is contained in:

committed by
GitHub

parent
c1f6ec5f62
commit
b2fe8510e4
@ -412,6 +412,13 @@ class CategoriesController < ApplicationController
|
||||
)
|
||||
.joins("LEFT JOIN topics t on t.id = categories.topic_id")
|
||||
.select("categories.*, t.slug topic_slug")
|
||||
.order(
|
||||
"starts_with(lower(categories.name), #{ActiveRecord::Base.connection.quote(term)}) DESC",
|
||||
"categories.parent_category_id IS NULL DESC",
|
||||
"categories.id IS NOT DISTINCT FROM #{ActiveRecord::Base.connection.quote(prioritized_category_id)} DESC",
|
||||
"categories.parent_category_id IS NOT DISTINCT FROM #{ActiveRecord::Base.connection.quote(prioritized_category_id)} DESC",
|
||||
"categories.id ASC",
|
||||
)
|
||||
.limit(limit)
|
||||
.offset((page - 1) * limit)
|
||||
|
||||
@ -421,19 +428,6 @@ class CategoriesController < ApplicationController
|
||||
|
||||
Category.preload_user_fields!(guardian, categories)
|
||||
|
||||
# Prioritize categories that start with the term, then top-level
|
||||
# categories, then subcategories
|
||||
categories =
|
||||
categories.to_a.sort_by do |category|
|
||||
[
|
||||
category.name.downcase.starts_with?(term) ? 0 : 1,
|
||||
category.parent_category_id.blank? ? 0 : 1,
|
||||
category.id == prioritized_category_id ? 0 : 1,
|
||||
category.parent_category_id == prioritized_category_id ? 0 : 1,
|
||||
category.id,
|
||||
]
|
||||
end
|
||||
|
||||
response = {
|
||||
categories_count: categories_count,
|
||||
categories: serialize_data(categories, SiteCategorySerializer, scope: guardian),
|
||||
|
Reference in New Issue
Block a user