post queries: optimize zero tag and single tag searches.

Avoid going through the full post query parser for empty searches or
simple single-tag searches.
This commit is contained in:
evazion
2022-04-09 03:54:03 -05:00
parent 652db0cd9f
commit ca5dd61728
2 changed files with 15 additions and 2 deletions

View File

@@ -24,8 +24,17 @@ class PostQuery
alias_method :to_s, :to_infix
# Return a new PostQuery with aliases replaced.
def self.normalize(...)
PostQuery.new(...).replace_aliases.rewrite_opts.trim
def self.normalize(search, ...)
search = search.to_s.strip
# Optimize zero tag and single tag searches
if search.blank?
PostQuery.new(AST.all, ...)
elsif search.match?(%r{\A[a-zA-Z0-9][a-zA-Z0-9();/+!?&'._~-]*\z}) && !search.downcase.in?(["and", "or"])
PostQuery.new(AST.tag(search), ...).replace_aliases
else
PostQuery.new(search, ...).replace_aliases.rewrite_opts.trim
end
end
# Perform a search and return the resulting posts

View File

@@ -64,6 +64,10 @@ class PostQuery
concerning :ConstructorMethods do
class_methods do
def all
AST.new(:all, [])
end
def tag(name)
AST.new(:tag, [name])
end