diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index a694e7f75..5a5b11fed 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -3,6 +3,9 @@ require "strscan" class PostQueryBuilder extend Memoist + # How many tags a `blah*` search should match. + MAX_WILDCARD_TAGS = 100 + COUNT_METATAGS = %w[ comment_count deleted_comment_count active_comment_count note_count deleted_note_count active_note_count @@ -77,9 +80,9 @@ class PostQueryBuilder optional_tags = optional_tags.map(&:name) required_tags = required_tags.map(&:name) - negated_tags += negated_wildcard_tags.flat_map { |tag| Tag.wildcard_matches(tag.name) } - optional_tags += optional_wildcard_tags.flat_map { |tag| Tag.wildcard_matches(tag.name) } - optional_tags += required_wildcard_tags.flat_map { |tag| Tag.wildcard_matches(tag.name) } + negated_tags += negated_wildcard_tags.flat_map { |tag| Tag.wildcard_matches(tag.name).limit(MAX_WILDCARD_TAGS).pluck(:name) } + optional_tags += optional_wildcard_tags.flat_map { |tag| Tag.wildcard_matches(tag.name).limit(MAX_WILDCARD_TAGS).pluck(:name) } + optional_tags += required_wildcard_tags.flat_map { |tag| Tag.wildcard_matches(tag.name).limit(MAX_WILDCARD_TAGS).pluck(:name) } tsquery << "!(#{negated_tags.sort.uniq.map(&:to_escaped_for_tsquery).join(" | ")})" if negated_tags.present? tsquery << "(#{optional_tags.sort.uniq.map(&:to_escaped_for_tsquery).join(" | ")})" if optional_tags.present? diff --git a/app/logical/post_sets/post.rb b/app/logical/post_sets/post.rb index ff48e9c4c..69f1fcf5c 100644 --- a/app/logical/post_sets/post.rb +++ b/app/logical/post_sets/post.rb @@ -187,16 +187,19 @@ module PostSets RelatedTagCalculator.frequent_tags_for_post_array(posts).take(MAX_SIDEBAR_TAGS) end + # Wildcard searches can show up to 100 tags in the sidebar, not 25, + # because that's how many tags the search itself will use. def wildcard_tags - Tag.wildcard_matches(tag_string) + Tag.wildcard_matches(tag_string).limit(PostQueryBuilder::MAX_WILDCARD_TAGS).pluck(:name) end def saved_search_tags - ["search:all"] + SavedSearch.labels_for(CurrentUser.user.id).map {|x| "search:#{x}"} + searches = ["search:all"] + SavedSearch.labels_for(CurrentUser.user.id).map {|x| "search:#{x}"} + searches.take(MAX_SIDEBAR_TAGS) end def tag_set_presenter - @tag_set_presenter ||= TagSetPresenter.new(related_tags.take(MAX_SIDEBAR_TAGS)) + @tag_set_presenter ||= TagSetPresenter.new(related_tags) end def tag_list_html(**options) diff --git a/app/models/tag.rb b/app/models/tag.rb index c83d4e9b1..6106b774e 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -249,8 +249,8 @@ class Tag < ApplicationRecord name_matches(name).or(alias_matches(name)) end - def wildcard_matches(tag, limit: 25) - nonempty.name_matches(tag).order(post_count: :desc, name: :asc).limit(limit).pluck(:name) + def wildcard_matches(tag) + nonempty.name_matches(tag).order(post_count: :desc, name: :asc) end def abbreviation_matches(abbrev)