New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FIX: Select earliest post when aggregating posts in a topic for search. #11914
Conversation
This is a revert of discourse@d8c796b and discourse@5bf0a08. Linking to the post within a topic that has the highest rank was confusing users and hard to explain because ranking is determined via the PG ranking function. See the following meta topics for the complaints after we switch to the new ordering: 1. https://meta.discourse.org/t/title-search-not-working-as-expected/157737 2. https://meta.discourse.org/t/search-results-should-prioritize-first-post-in-topic-when-title-matches-search-term/175154
da2b464
to
6bb51ed
Compare
Search.advanced_order(:chars) do |posts| | ||
posts.reorder("(SELECT LENGTH(raw) FROM posts WHERE posts.topic_id = subquery.topic_id) DESC") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lis2 Which plugin was using this advanced order? I need to fix that plugin as the query will break.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pull request has been mentioned on Discourse Meta. There might be relevant details there: |
"MAX(subquery.created_at) created_at" | ||
) | ||
.order("created_at DESC") | ||
posts = posts.order("MAX(posts.created_at) DESC") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is that MAX function used here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're doing aggregation with group
, we need to use an aggregation function in the ordering as well. In this case, the latest post in the topic is returned.
This is a revert of
d8c796b
and
5bf0a08.
Linking to the post within a topic that has the highest rank was
confusing users and hard to explain because ranking is determined via
the PG ranking function. See the following meta topics for the
complaints after we switch to the new ordering: