Carleas reported that Discourse search results unexpectedly omit posts when sorted by criteria other than ‘Most Liked.’ While ‘Most Liked’ returns every matching post, other sort orders appear to aggregate results by topic, showing only one post per topic. Carleas traced this to aggregate_search: true being applied in /lib/search.rb unless the order is set to likes, noting that the UI and documentation provide no indication of this behavior or how to change it.
awesomerobot clarified that grouping by topics is intentional and long-standing, designed to prevent a single topic with many matches from pushing relevant topics further down the result list. They suggested using the in:all-posts filter as a workaround to force a post-level search.
Carleas acknowledged in:all-posts as a valid solution but argued that the current UX is confusing. Despite results displaying individual posts and the advanced search tab being labeled ‘Posts,’ users are unaware that the backend aggregates by topic. Carleas highlighted several inconsistencies: the UI doesn’t explain why specific posts are selected from a topic, the in:all-posts option is not discoverable via the interface, and official documentation often describes search as returning ‘posts’ without clarifying the default aggregation behavior. The core issue is not just the functionality, but the lack of transparency regarding how search results are filtered and presented.
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.