Rewrite the implementation of related tags to be simpler, faster, and more accurate: * The related tags are now calculated by taking a random sample of 1000 posts, finding the top 250 most frequent tags among those posts, then ordering those tags by cosine similarity. * Related tags can generally be calculated in 50-300ms at these sample sizes. Very high sample sizes (25000+ posts) are still relatively fast (1-3 seconds), but generally they don't improve accuracy much. * Related tags are now cached in redis rather than in the tags table. The related_tags column in the tags table is no longer used. * Only the related tags in the search taglist are cached. The related tags returned by the 'Related tags' button are not cached. * The cache lifetime is a fixed 4 hours. * The 'Related tags' button now works with metatags. * The /related_tag page now works with metatags and multitag searches. Fixes #4134, #4146.
67 lines
1.7 KiB
Ruby
67 lines
1.7 KiB
Ruby
module PostSetPresenters
|
|
class Post < Base
|
|
MAX_TAGS = 25
|
|
|
|
attr_accessor :post_set
|
|
delegate :posts, :to => :post_set
|
|
|
|
def initialize(post_set)
|
|
@post_set = post_set
|
|
end
|
|
|
|
def tag_set_presenter
|
|
@tag_set_presenter ||= TagSetPresenter.new(related_tags.take(MAX_TAGS))
|
|
end
|
|
|
|
def post_previews_html(template, options = {})
|
|
super(template, options.merge(show_cropped: true))
|
|
end
|
|
|
|
def related_tags
|
|
if post_set.is_pattern_search?
|
|
pattern_tags
|
|
elsif post_set.is_saved_search?
|
|
saved_search_tags
|
|
elsif post_set.is_empty_tag? || post_set.tag_string == "order:rank"
|
|
popular_tags
|
|
elsif post_set.is_single_tag?
|
|
similar_tags
|
|
else
|
|
frequent_tags
|
|
end
|
|
end
|
|
|
|
def popular_tags
|
|
if PopularSearchService.enabled?
|
|
PopularSearchService.new(Date.today).tags
|
|
else
|
|
Tag.trending
|
|
end
|
|
end
|
|
|
|
def similar_tags
|
|
Cache.get("similar_tags:#{post_set.tag_string}", 4.hours, race_condition_ttl: 60.seconds) do
|
|
ApplicationRecord.with_timeout(1_000, []) do
|
|
RelatedTagCalculator.similar_tags_for_search(post_set.tag_string).take(MAX_TAGS).pluck(:name)
|
|
end
|
|
end
|
|
end
|
|
|
|
def frequent_tags
|
|
RelatedTagCalculator.frequent_tags_for_posts(post_set.posts).take(MAX_TAGS)
|
|
end
|
|
|
|
def pattern_tags
|
|
Tag.name_matches(post_set.tag_string).order(post_count: :desc).limit(MAX_TAGS).pluck(:name)
|
|
end
|
|
|
|
def saved_search_tags
|
|
["search:all"] + SavedSearch.labels_for(CurrentUser.user.id).map {|x| "search:#{x}"}
|
|
end
|
|
|
|
def tag_list_html(**options)
|
|
tag_set_presenter.tag_list_html(name_only: post_set.is_saved_search?, **options)
|
|
end
|
|
end
|
|
end
|