diff --git a/app/services/search_service.rb b/app/services/search_service.rb index 18d9ec8f54..93513ccb11 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -3,6 +3,8 @@ class SearchService < BaseService def call(query, account, limit, options = {}) @query = query&.strip + pull_query_commands + @account = account @options = options @limit = limit.to_i @@ -26,6 +28,7 @@ class SearchService < BaseService private MIN_SCORE = 0.7 + MIN_SCORE_RE = /MINSCORE=((([\d]+\.[\d]+)|([\d]+)){1,6})/.freeze def perform_accounts_search! AccountSearchService.new.call( @@ -38,22 +41,22 @@ class SearchService < BaseService end 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. case @searchability 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: '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: 'direct' }).filter(term: { account_id: @account.id }).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 }).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 }).track_scores(true).min_score(@min_score)) 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(term: { searchability: 'direct' }).filter(term: { account_id: @account.id }).min_score(MIN_SCORE)) + 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 }).track_scores(true).min_score(@min_score)) 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 - 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.and(privacy_definition) @@ -130,6 +133,17 @@ class SearchService < BaseService @options[:type].blank? || @options[:type] == 'statuses' 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 SearchQueryTransformer.new.apply(SearchQueryParser.new.parse(@query)) end