Add search command MINSCORE
This commit is contained in:
parent
8a412f0f59
commit
62036efc79
1 changed files with 22 additions and 8 deletions
|
@ -3,6 +3,8 @@
|
||||||
class SearchService < BaseService
|
class SearchService < BaseService
|
||||||
def call(query, account, limit, options = {})
|
def call(query, account, limit, options = {})
|
||||||
@query = query&.strip
|
@query = query&.strip
|
||||||
|
pull_query_commands
|
||||||
|
|
||||||
@account = account
|
@account = account
|
||||||
@options = options
|
@options = options
|
||||||
@limit = limit.to_i
|
@limit = limit.to_i
|
||||||
|
@ -26,6 +28,7 @@ class SearchService < BaseService
|
||||||
private
|
private
|
||||||
|
|
||||||
MIN_SCORE = 0.7
|
MIN_SCORE = 0.7
|
||||||
|
MIN_SCORE_RE = /MINSCORE=((([\d]+\.[\d]+)|([\d]+)){1,6})/.freeze
|
||||||
|
|
||||||
def perform_accounts_search!
|
def perform_accounts_search!
|
||||||
AccountSearchService.new.call(
|
AccountSearchService.new.call(
|
||||||
|
@ -38,22 +41,22 @@ class SearchService < BaseService
|
||||||
end
|
end
|
||||||
|
|
||||||
def perform_statuses_search!
|
def perform_statuses_search!
|
||||||
privacy_definition = parsed_query.apply(StatusesIndex.filter(term: { searchable_by: @account.id }).min_score(MIN_SCORE))
|
privacy_definition = parsed_query.apply(StatusesIndex.filter(term: { searchable_by: @account.id }).track_scores(true).min_score(@min_score))
|
||||||
|
|
||||||
# 'private' searchability posts are NOT in here because it's already added at previous line.
|
# 'private' searchability posts are NOT in here because it's already added at previous line.
|
||||||
case @searchability
|
case @searchability
|
||||||
when 'public'
|
when 'public'
|
||||||
privacy_definition = privacy_definition.or(StatusesIndex.filter(term: { searchability: 'public' }).min_score(MIN_SCORE))
|
privacy_definition = privacy_definition.or(StatusesIndex.filter(term: { searchability: 'public' }).track_scores(true).min_score(@min_score))
|
||||||
privacy_definition = privacy_definition.or(StatusesIndex.filter(term: { searchability: 'unlisted' }).filter(terms: { account_id: following_account_ids }).min_score(MIN_SCORE)) unless following_account_ids.empty?
|
privacy_definition = privacy_definition.or(StatusesIndex.filter(term: { searchability: 'unlisted' }).filter(terms: { account_id: following_account_ids }).track_scores(true).min_score(@min_score)) unless following_account_ids.empty?
|
||||||
privacy_definition = privacy_definition.or(StatusesIndex.filter(term: { searchability: 'direct' }).filter(term: { account_id: @account.id }).min_score(MIN_SCORE))
|
privacy_definition = privacy_definition.or(StatusesIndex.filter(term: { searchability: 'direct' }).filter(term: { account_id: @account.id }).track_scores(true).min_score(@min_score))
|
||||||
when 'unlisted', 'private'
|
when 'unlisted', 'private'
|
||||||
privacy_definition = privacy_definition.or(StatusesIndex.filter(terms: { searchability: %w(public unlisted) }).filter(terms: { account_id: following_account_ids }).min_score(MIN_SCORE)) unless following_account_ids.empty?
|
privacy_definition = privacy_definition.or(StatusesIndex.filter(terms: { searchability: %w(public unlisted) }).filter(terms: { account_id: following_account_ids }).track_scores(true).min_score(@min_score)) unless following_account_ids.empty?
|
||||||
privacy_definition = privacy_definition.or(StatusesIndex.filter(term: { searchability: 'direct' }).filter(term: { account_id: @account.id }).min_score(MIN_SCORE))
|
privacy_definition = privacy_definition.or(StatusesIndex.filter(term: { searchability: 'direct' }).filter(term: { account_id: @account.id }).track_scores(true).min_score(@min_score))
|
||||||
when 'direct'
|
when 'direct'
|
||||||
privacy_definition = privacy_definition.or(StatusesIndex.filter(term: { searchability: 'direct' }).filter(term: { account_id: @account.id }).min_score(MIN_SCORE))
|
privacy_definition = privacy_definition.or(StatusesIndex.filter(term: { searchability: 'direct' }).filter(term: { account_id: @account.id }).track_scores(true).min_score(@min_score))
|
||||||
end
|
end
|
||||||
|
|
||||||
definition = parsed_query.apply(StatusesIndex.min_score(MIN_SCORE).track_scores(true)).order(id: :desc)
|
definition = parsed_query.apply(StatusesIndex.min_score(@min_score).track_scores(true)).order(id: :desc)
|
||||||
definition = definition.filter(term: { account_id: @options[:account_id] }) if @options[:account_id].present?
|
definition = definition.filter(term: { account_id: @options[:account_id] }) if @options[:account_id].present?
|
||||||
|
|
||||||
definition = definition.and(privacy_definition)
|
definition = definition.and(privacy_definition)
|
||||||
|
@ -140,6 +143,17 @@ class SearchService < BaseService
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def pull_query_commands
|
||||||
|
@min_score = MIN_SCORE
|
||||||
|
|
||||||
|
min_score_result = @query.scan(MIN_SCORE_RE).first
|
||||||
|
if (min_score_result)
|
||||||
|
@min_score = min_score_result[1].to_f
|
||||||
|
@query = @query.gsub(MIN_SCORE_RE, '').strip
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
def parsed_query
|
def parsed_query
|
||||||
SearchQueryTransformer.new.apply(SearchQueryParser.new.parse(@query))
|
SearchQueryTransformer.new.apply(SearchQueryParser.new.parse(@query))
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue