Skip to content

Commit

Permalink
FIX: Load categories with search topic results (discourse#25700)
Browse files Browse the repository at this point in the history
Add categories to the serialized search results together with the topics
when lazy load categories is enabled. This is necessary in order for the
results to be rendered correctly and display the category information.
  • Loading branch information
nbianca authored Feb 21, 2024
1 parent d8c3924 commit 9199c52
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
6 changes: 6 additions & 0 deletions app/assets/javascripts/discourse/app/lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import userSearch from "discourse/lib/user-search";
import { escapeExpression } from "discourse/lib/utilities";
import Category from "discourse/models/category";
import Post from "discourse/models/post";
import Site from "discourse/models/site";
import Topic from "discourse/models/topic";
import User from "discourse/models/user";
import getURL from "discourse-common/lib/get-url";
Expand Down Expand Up @@ -56,10 +57,15 @@ export function translateResults(results, opts) {

results.categories = results.categories
.map(function (category) {
Site.current().updateCategory(category);
return Category.list().findBy("id", category.id || category.model.id);
})
.compact();

results.grouped_search_result?.extra?.categories?.forEach((category) =>
Site.current().updateCategory(category)
);

results.groups = results.groups
.map((group) => {
const name = escapeExpression(group.name);
Expand Down
16 changes: 15 additions & 1 deletion app/serializers/grouped_search_result_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class GroupedSearchResultSerializer < ApplicationSerializer
:search_log_id,
:more_full_page_results,
:can_create_topic,
:error
:error,
:extra

def search_log_id
object.search_log_id
Expand All @@ -30,4 +31,17 @@ def include_tags?
def can_create_topic
scope.can_create?(Topic)
end

def extra
extra = {}

if object.can_lazy_load_categories
extra[:categories] = ActiveModel::ArraySerializer.new(
object.extra_categories,
each_serializer: BasicCategorySerializer,
)
end

extra
end
end
3 changes: 2 additions & 1 deletion lib/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def initialize(term, opts = nil)
search_context: @search_context,
blurb_length: @blurb_length,
is_header_search: !use_full_page_limit,
can_lazy_load_categories: @guardian.can_lazy_load_categories?,
)
end

Expand Down Expand Up @@ -1447,7 +1448,7 @@ def topic_search

def posts_eager_loads(query)
query = query.includes(:user, :post_search_data)
topic_eager_loads = [:category]
topic_eager_loads = [{ category: :parent_category }]

topic_eager_loads << :tags if SiteSetting.tagging_enabled

Expand Down
22 changes: 21 additions & 1 deletion lib/search/grouped_search_results.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class TextHelper
:type_filter,
:posts,
:categories,
:extra_categories,
:users,
:tags,
:groups,
Expand All @@ -25,6 +26,7 @@ class TextHelper
:more_full_page_results,
:error,
:use_pg_headlines_for_excerpt,
:can_lazy_load_categories,
)

attr_accessor :search_log_id
Expand All @@ -38,7 +40,8 @@ def initialize(
blurb_length: nil,
blurb_term: nil,
is_header_search: false,
use_pg_headlines_for_excerpt: SiteSetting.use_pg_headlines_for_excerpt
use_pg_headlines_for_excerpt: SiteSetting.use_pg_headlines_for_excerpt,
can_lazy_load_categories: false
)
@type_filter = type_filter
@term = term
Expand All @@ -47,12 +50,14 @@ def initialize(
@blurb_length = blurb_length || BLURB_LENGTH
@posts = []
@categories = []
@extra_categories = Set.new
@users = []
@tags = []
@groups = []
@error = nil
@is_header_search = is_header_search
@use_pg_headlines_for_excerpt = use_pg_headlines_for_excerpt
@can_lazy_load_categories = can_lazy_load_categories
end

def error=(error)
Expand Down Expand Up @@ -101,6 +106,21 @@ def add(object)
else
(self.public_send(type)) << object
end

if can_lazy_load_categories
category =
case type
when "posts"
object.topic.category
when "topics"
object.category
end

if category
extra_categories << category.parent_category if category.parent_category
extra_categories << category
end
end
end

def self.blurb_for(cooked: nil, term: nil, blurb_length: BLURB_LENGTH, scrub: true)
Expand Down
11 changes: 11 additions & 0 deletions spec/requests/api/schemas/json/search_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@
"null"
]
},
"extra": {
"type": "object",
"properties": {
"categories": {
"type": [
"array",
"null"
]
}
}
},
"post_ids": {
"type": "array",
"items": {
Expand Down
33 changes: 33 additions & 0 deletions spec/requests/search_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,39 @@ def limited_request(ip_address = "1.2.3.4")
end
end
end

context "with lazy loaded categories" do
fab!(:parent_category) { Fabricate(:category) }
fab!(:category) { Fabricate(:category, parent_category: parent_category) }
fab!(:other_category) { Fabricate(:category, parent_category: parent_category) }

fab!(:post) do
with_search_indexer_enabled do
topic = Fabricate(:topic, category: category)
Fabricate(:post, topic: topic, raw: "hello world. first topic")
end
end

fab!(:other_post) do
with_search_indexer_enabled do
topic = Fabricate(:topic, category: other_category)
Fabricate(:post, topic: topic, raw: "hello world. second topic")
end
end

before { SiteSetting.lazy_load_categories_groups = "#{Group::AUTO_GROUPS[:everyone]}" }

it "returns extra categories and parent categories" do
get "/search.json", params: { q: "hello" }

categories = response.parsed_body["grouped_search_result"]["extra"]["categories"]
expect(categories.map { |c| c["id"] }).to contain_exactly(
parent_category.id,
category.id,
other_category.id,
)
end
end
end

context "with search priority" do
Expand Down

0 comments on commit 9199c52

Please sign in to comment.