search: apply aliases after parsing searches.

Make PostQueryBuilder apply aliases earlier, immediately after parsing
the search.

On the post index page there are multiple places where we need to apply
aliases:

* When running the search with PostQueryBuilder#build.
* When calculating the search count with PostQueryBuilder#fast_count.
* When calculating the related tags for the sidebar.
* When tracking missed searches and popular searches for Reportbooru.
* When looking up wiki excerpts.

Applying aliases after parsing ensures we only have to apply aliases
once for all of these things.

We also normalize the order of tags in searches and strip repeated tags.
This is so that we have consistent cache keys for fast_count.

* Fixes searches for aliased tags being counted as missed searches (fixes #4433).
* Fixes wiki excerpts not showing up when searching for aliased tags.
This commit is contained in:
evazion
2020-05-07 13:30:04 -05:00
parent f38c38f26e
commit 67aab0236d
13 changed files with 72 additions and 56 deletions

View File

@@ -604,7 +604,7 @@ class Post < ApplicationRecord
# If someone else committed changes to this post before we did,
# then try to merge the tag changes together.
current_tags = tag_string_was.split
new_tags = PostQueryBuilder.new(tag_string).split_query
new_tags = PostQueryBuilder.new(tag_string, normalize_aliases: false).parse_tag_edit
old_tags = old_tag_string.split
kept_tags = current_tags & new_tags
@@ -642,7 +642,7 @@ class Post < ApplicationRecord
end
def normalize_tags
normalized_tags = PostQueryBuilder.new(tag_string).split_query
normalized_tags = PostQueryBuilder.new(tag_string, normalize_aliases: false).parse_tag_edit
normalized_tags = apply_casesensitive_metatags(normalized_tags)
normalized_tags = normalized_tags.map(&:downcase)
normalized_tags = filter_metatags(normalized_tags)

View File

@@ -134,17 +134,17 @@ class SavedSearch < ApplicationRecord
def queries_for(user_id, label: nil, options: {})
searches = SavedSearch.where(user_id: user_id)
searches = searches.labeled(label) if label.present?
queries = searches.pluck(:query).map { |query| PostQueryBuilder.new(query).normalize_query(normalize_aliases: true, sort: true) }
queries = searches.pluck(:query).map { |query| PostQueryBuilder.new(query).to_s }
queries.sort.uniq
end
end
def normalized_query
PostQueryBuilder.new(query).normalize_query(sort: true)
PostQueryBuilder.new(query, normalize_aliases: false).to_s
end
def normalize_query
self.query = PostQueryBuilder.new(query).normalize_query(normalize_aliases: true, sort: false)
self.query = PostQueryBuilder.new(query, normalize_order: false).to_s
end
end

View File

@@ -237,7 +237,7 @@ class Upload < ApplicationRecord
include SourceMethods
def assign_rating_from_tags
if rating = PostQueryBuilder.new(tag_string).find_metatag(:rating)
if rating = PostQueryBuilder.new(tag_string, normalize_aliases: false).find_metatag(:rating)
self.rating = rating.downcase.first
end
end