Add sort operator, default to newest first
This commit is contained in:
parent
40519dac1a
commit
26d465175a
2 changed files with 32 additions and 3 deletions
|
@ -65,6 +65,7 @@ class StatusesIndex < Chewy::Index
|
||||||
root date_detection: false do
|
root date_detection: false do
|
||||||
field :id, type: 'long'
|
field :id, type: 'long'
|
||||||
field :account_id, type: 'long'
|
field :account_id, type: 'long'
|
||||||
|
field :created_at, type: 'date'
|
||||||
field :visibility, type: 'keyword'
|
field :visibility, type: 'keyword'
|
||||||
|
|
||||||
field :text, type: 'text', value: ->(status) { status.searchable_text } do
|
field :text, type: 'text', value: ->(status) { status.searchable_text } do
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
class SearchQueryTransformer < Parslet::Transform
|
class SearchQueryTransformer < Parslet::Transform
|
||||||
class Query
|
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)
|
def initialize(clauses)
|
||||||
grouped = clauses.chunk(&:operator).to_h
|
grouped = clauses.chunk(&:operator).to_h
|
||||||
|
@ -10,6 +10,7 @@ class SearchQueryTransformer < Parslet::Transform
|
||||||
@must_not_clauses = grouped.fetch(:must_not, [])
|
@must_not_clauses = grouped.fetch(:must_not, [])
|
||||||
@must_clauses = grouped.fetch(:must, [])
|
@must_clauses = grouped.fetch(:must, [])
|
||||||
@filter_clauses = grouped.fetch(:filter, [])
|
@filter_clauses = grouped.fetch(:filter, [])
|
||||||
|
@order_clauses = grouped.fetch(:order, [])
|
||||||
end
|
end
|
||||||
|
|
||||||
def apply(search)
|
def apply(search)
|
||||||
|
@ -17,6 +18,12 @@ class SearchQueryTransformer < Parslet::Transform
|
||||||
must_clauses.each { |clause| search = search.query.must(clause_to_query(clause)) }
|
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)) }
|
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)) }
|
filter_clauses.each { |clause| search = search.filter(**clause_to_filter(clause)) }
|
||||||
|
if order_clauses.empty?
|
||||||
|
# Default to most recent results first.
|
||||||
|
search = search.order(created_at: :desc)
|
||||||
|
else
|
||||||
|
order_clauses.each { |clause| search = search.order(**clause_to_order(clause)) }
|
||||||
|
end
|
||||||
search.query.minimum_should_match(1)
|
search.query.minimum_should_match(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -41,6 +48,15 @@ class SearchQueryTransformer < Parslet::Transform
|
||||||
raise "Unexpected clause type: #{clause}"
|
raise "Unexpected clause type: #{clause}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def clause_to_order(clause)
|
||||||
|
case clause
|
||||||
|
when PrefixClause
|
||||||
|
{ clause.term => clause.order }
|
||||||
|
else
|
||||||
|
raise "Unexpected clause type: #{clause}"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Operator
|
class Operator
|
||||||
|
@ -81,12 +97,12 @@ class SearchQueryTransformer < Parslet::Transform
|
||||||
end
|
end
|
||||||
|
|
||||||
class PrefixClause
|
class PrefixClause
|
||||||
attr_reader :filter, :operator, :term
|
attr_reader :filter, :operator, :term, :order
|
||||||
|
|
||||||
def initialize(prefix, term)
|
def initialize(prefix, term)
|
||||||
@operator = :filter
|
|
||||||
case prefix
|
case prefix
|
||||||
when 'from'
|
when 'from'
|
||||||
|
@operator = :filter
|
||||||
@filter = :account_id
|
@filter = :account_id
|
||||||
|
|
||||||
username, domain = term.gsub(/\A@/, '').split('@')
|
username, domain = term.gsub(/\A@/, '').split('@')
|
||||||
|
@ -94,6 +110,18 @@ class SearchQueryTransformer < Parslet::Transform
|
||||||
account = Account.find_remote!(username, domain)
|
account = Account.find_remote!(username, domain)
|
||||||
|
|
||||||
@term = account.id
|
@term = account.id
|
||||||
|
when 'sort'
|
||||||
|
@operator = :order
|
||||||
|
@term = :created_at
|
||||||
|
|
||||||
|
case term
|
||||||
|
when 'oldest'
|
||||||
|
@order = :asc
|
||||||
|
when 'newest'
|
||||||
|
@order = :desc
|
||||||
|
else
|
||||||
|
raise Mastodon::SyntaxError
|
||||||
|
end
|
||||||
else
|
else
|
||||||
raise Mastodon::SyntaxError
|
raise Mastodon::SyntaxError
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue