Carleas reported that Discourse search results omit certain matching posts unless sorted by ‘Most Liked.’ Analyzing the code, they identified that the aggregate_post_sql method groups results by topic (showing one post per topic) for most sort orders, while ‘Most Liked’ skips this aggregation. awesomerobot clarified that this topic-based grouping is intentional to prevent a single large topic from dominating results, but noted that users can use the undocumented in:all-posts operator to view every matching post.
Carleas argued that this behavior is confusing because the UI labels the tab ‘Posts’ and displays individual posts, with no indication that results are aggregated or how to bypass this. chapoi agreed the UX is unclear and proposed displaying a summary note (e.g., “Showing 12 topics with 47 matching posts”) and adding a visible toggle or button to “Show every post,” rather than requiring users to know the magic search operator.
Carleas supported these ideas, suggesting additional UI elements like a “See all X matching posts in this topic” link next to each result or a generic “Search this topic” link to help users understand that more relevant posts might exist within the same topic.
After user complaints, I noticed that search results do not include all posts containing the search terms. I found this from 2018 mentioning that sorting by ‘Most Liked’ does return all posts, which I can confirm is still the case.
This behavior is unexpected, and seems unintended. There is no option to change this behavior, nothing in the docs about it, and nothing in the UI suggests that search results have been omitted.
From /lib/search.rb:
def aggregate_post_sql(opts)
...
query =
if @order == :likes
# likes are a pain to aggregate so skip
posts_query(limit, type_filter: opts[:type_filter]).select("topics.id", "posts.post_number")
else
posts_query(limit, aggregate_search: true, type_filter: opts[:type_filter]).select(
"topics.id",
"#{min_or_max}(posts.post_number) post_number",
).group("topics.id")
end
...
end
This is the only place where ‘Most Liked’ is clearly treated differently from other search orderings, so it’s probably the root cause.
It looks like aggregate_search is intended to be applied when a search should return topics instead of posts, but it’s being applied to all searches unless sorted by likes.
I believe Discourse has always grouped results by topics, since its first release, and it’s intentional because Discourse is a topic-based system. If the first topic has 20 matching posts, the next relevant topic could show up as result 21 instead of result 2. That would be frustrating if the first topic is not what you’re looking for.
There is another way around this beyond using “most liked,” you can add in:all-posts to your search.
Thank you, that is a useful workaround, and searching on that string I can find the documentation (though it doesn’t mention the different behavior for ‘Most Liked’).
I don’t think it is clear to users that search is topic-based:
The advanced search tab is labeled ‘Posts’
The results are posts (rather than topics or even OPs)
There is no mention in the UI that results are one-post-per-topic, or that the topics in the results may have other posts fitting the search parameters
It isn’t clear why a particular post from a given topic is given priority over any others that match the search terms, nor how to modify the search to prioritize different posts
The in:all-posts option isn’t presented to users, and seems only available by manually adding it
Even the documentation linked above repeatedly uses “returns posts” to describe search behavior (though it does describe in:all-posts as returning “every post that matches the query, even when multiple posts match within the same topic”)
I can see the rationale behind a topic-based search. But nothing suggested that’s what was happening other than users complaining that they could not find posts they knew they’d made.
That would make it a lot clearer. I’d assumed there was nothing like in:all-posts because there was no way to toggle it from the menus.
Another thought I had would be a link beside each result, ‘See all X matching posts in this topic’.
If getting the count of matches for each topic isn’t realistic, even a generic ‘Search this topic’ link would help communicate that there may be other responsive posts that aren’t included in the results, and make it easier to drill down.