Add search options (WIP)

This commit is contained in:
noellabo 2023-01-23 08:58:10 +09:00
parent 64fa347f35
commit 211f037931
3 changed files with 57 additions and 11 deletions

View File

@ -69,11 +69,29 @@ class AccountSearchQueryTransformer < Parslet::Transform
end
end
class PrefixClause
attr_reader :filter, :operator, :term
def initialize(prefix, term)
@operator = :filter
case prefix
when 'from'
when 'order'
@filter = nil
@term = ''
else
raise "Unknown prefix: #{prefix}"
end
end
end
rule(clause: subtree(:clause)) do
prefix = clause[:prefix][:term].to_s if clause[:prefix]
operator = clause[:operator]&.to_s
if clause[:term]
if clause[:prefix]
PrefixClause.new(prefix, clause[:term].to_s)
elsif clause[:term]
TermClause.new(prefix, operator, clause[:term].to_s)
elsif clause[:shortcode]
TermClause.new(prefix, operator, ":#{clause[:term]}:")

View File

@ -2,7 +2,7 @@
class SearchQueryTransformer < Parslet::Transform
class Query
attr_reader :should_clauses, :must_not_clauses, :must_clauses, :filter_clauses
attr_reader :should_clauses, :must_not_clauses, :must_clauses, :filter_clauses, :order_clauses
def initialize(clauses)
grouped = clauses.chunk(&:operator).to_h
@ -10,6 +10,7 @@ class SearchQueryTransformer < Parslet::Transform
@must_not_clauses = grouped.fetch(:must_not, [])
@must_clauses = grouped.fetch(:must, [])
@filter_clauses = grouped.fetch(:filter, [])
@order_clauses = grouped.fetch(:order, [])
end
def apply(search)
@ -17,6 +18,7 @@ class SearchQueryTransformer < Parslet::Transform
must_clauses.each { |clause| search = search.query.must(clause_to_query(clause)) }
must_not_clauses.each { |clause| search = search.query.must_not(clause_to_query(clause)) }
filter_clauses.each { |clause| search = search.filter(**clause_to_filter(clause)) }
order_clauses.each { |clause| search = search.order(**clause_to_order(clause)) }
search.query.minimum_should_match(1)
end
@ -41,6 +43,15 @@ class SearchQueryTransformer < Parslet::Transform
raise "Unexpected clause type: #{clause}"
end
end
def clause_to_order(clause)
case clause
when PrefixClause
{ id: clause.term }
else
raise "Unexpected clause type: #{clause}"
end
end
end
class Operator
@ -84,9 +95,9 @@ class SearchQueryTransformer < Parslet::Transform
attr_reader :filter, :operator, :term
def initialize(prefix, term)
@operator = :filter
case prefix
when 'from'
@operator = :filter
@filter = :account_id
username, domain = term.split('@')
account = Account.find_remote(username, domain)
@ -94,6 +105,11 @@ class SearchQueryTransformer < Parslet::Transform
raise "Account not found: #{term}" unless account
@term = account.id
when 'order'
raise "Unknown order: #{term}" unless %w(asc desc).include?(term)
@operator = :order
@term = term
else
raise "Unknown prefix: #{prefix}"
end

View File

@ -68,20 +68,24 @@ class SearchService < BaseService
end
def perform_statuses_search!
definition = parsed_query.apply(StatusesIndex.filter(term: { searchable_by: @account.id }))
privacy_definition = StatusesIndex.filter(term: { searchable_by: @account.id })
case @searchability
when 'public'
definition = definition.or(StatusesIndex.filter(term: { searchability: 'public' }))
definition = definition.or(StatusesIndex.filter(terms: { searchability: %w(unlisted private) }).filter(terms: { account_id: following_account_ids})) unless following_account_ids.empty?
privacy_definition = privacy_definition.or(StatusesIndex.filter(term: { searchability: 'public' }))
privacy_definition = privacy_definition.or(StatusesIndex.filter(terms: { searchability: %w(unlisted private) }).filter(terms: { account_id: following_account_ids})) unless following_account_ids.empty?
when 'unlisted', 'private'
definition = definition.or(StatusesIndex.filter(terms: { searchability: %w(public unlisted private) }).filter(terms: { account_id: following_account_ids})) unless following_account_ids.empty?
privacy_definition = privacy_definition.or(StatusesIndex.filter(terms: { searchability: %w(public unlisted private) }).filter(terms: { account_id: following_account_ids})) unless following_account_ids.empty?
end
definition = parsed_query.apply(StatusesIndex)
if @options[:account_id].present?
definition = definition.filter(term: { account_id: @options[:account_id] })
end
definition = definition.and(privacy_definition)
if @options[:min_id].present? || @options[:max_id].present?
range = {}
range[:gt] = @options[:min_id].to_i if @options[:min_id].present?
@ -132,21 +136,29 @@ class SearchService < BaseService
def full_text_searchable?
return false unless Chewy.enabled?
statuses_search? && !@account.nil? && !((@query.start_with?('#') || @query.include?('@')) && !@query.include?(' '))
statuses_search? && !@account.nil? && !account_search_explicit_pattern? && !hashtag_search_explicit_pattern?
end
def account_full_text_searchable?
return false unless Chewy.enabled?
(!@profile && account_search? || profiles_search?) && !@account.nil? && !((@query.start_with?('#') || @query.include?('@')) && !@query.include?(' '))
(!@profile && account_search? || profiles_search?) && !@account.nil? && !account_search_explicit_pattern? && !hashtag_search_explicit_pattern?
end
def account_searchable?
account_search? && !(@query.start_with?('#') || (@query.include?('@') && @query.include?(' ')))
account_search? && (account_search_explicit_pattern? || @query.match?(/\A#{Account::USERNAME_RE}\Z/))
end
def hashtag_searchable?
hashtag_search? && !@query.include?('@')
hashtag_search? && !account_search_explicit_pattern?
end
def account_search_explicit_pattern?
@query.start_with?('@') || @query.include?('@') && "@#{@query}".match?(/\A#{Account::MENTION_RE}\Z/)
end
def hashtag_search_explicit_pattern?
@query.start_with?('#') || @query.match?(/\A#{Tag::HASHTAG_RE}\Z/)
end
def account_search?