You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
4.3 KiB

  1. # frozen_string_literal: true
  2. class AccountSearchService < BaseService
  3. attr_reader :query, :limit, :offset, :options, :account
  4. def call(query, account = nil, options = {})
  5. @acct_hint = query&.start_with?('@')
  6. @query = query&.strip&.gsub(/\A@/, '')
  7. @limit = options[:limit].to_i
  8. @offset = options[:offset].to_i
  9. @options = options
  10. @account = account
  11. search_service_results.compact.uniq
  12. end
  13. private
  14. def search_service_results
  15. return [] if query.blank? || limit < 1
  16. [exact_match] + search_results
  17. end
  18. def exact_match
  19. return unless offset.zero? && username_complete?
  20. return @exact_match if defined?(@exact_match)
  21. match = begin
  22. if options[:resolve]
  23. ResolveAccountService.new.call(query)
  24. elsif domain_is_local?
  25. Account.find_local(query_username)
  26. else
  27. Account.find_remote(query_username, query_domain)
  28. end
  29. end
  30. match = nil if !match.nil? && !account.nil? && options[:following] && !account.following?(match)
  31. @exact_match = match
  32. end
  33. def search_results
  34. return [] if limit_for_non_exact_results.zero?
  35. @search_results ||= begin
  36. results = from_elasticsearch if Chewy.enabled?
  37. results ||= from_database
  38. results
  39. end
  40. end
  41. def from_database
  42. if account
  43. advanced_search_results
  44. else
  45. simple_search_results
  46. end
  47. end
  48. def advanced_search_results
  49. Account.advanced_search_for(terms_for_query, account, limit_for_non_exact_results, options[:following], offset)
  50. end
  51. def simple_search_results
  52. Account.search_for(terms_for_query, limit_for_non_exact_results, offset)
  53. end
  54. def from_elasticsearch
  55. must_clauses = [{ multi_match: { query: terms_for_query, fields: likely_acct? ? %w(acct.edge_ngram acct) : %w(acct.edge_ngram acct display_name.edge_ngram display_name), type: 'most_fields', operator: 'and' } }]
  56. should_clauses = []
  57. if account
  58. return [] if options[:following] && following_ids.empty?
  59. if options[:following]
  60. must_clauses << { terms: { id: following_ids } }
  61. elsif following_ids.any?
  62. should_clauses << { terms: { id: following_ids, boost: 100 } }
  63. end
  64. end
  65. query = { bool: { must: must_clauses, should: should_clauses } }
  66. functions = [reputation_score_function, followers_score_function, time_distance_function]
  67. records = AccountsIndex.query(function_score: { query: query, functions: functions, boost_mode: 'multiply', score_mode: 'avg' })
  68. .limit(limit_for_non_exact_results)
  69. .offset(offset)
  70. .objects
  71. .compact
  72. ActiveRecord::Associations::Preloader.new.preload(records, :account_stat)
  73. records
  74. rescue Faraday::ConnectionFailed, Parslet::ParseFailed
  75. nil
  76. end
  77. def reputation_score_function
  78. {
  79. script_score: {
  80. script: {
  81. source: "(doc['followers_count'].value + 0.0) / (doc['followers_count'].value + doc['following_count'].value + 1)",
  82. },
  83. },
  84. }
  85. end
  86. def followers_score_function
  87. {
  88. field_value_factor: {
  89. field: 'followers_count',
  90. modifier: 'log2p',
  91. missing: 0,
  92. },
  93. }
  94. end
  95. def time_distance_function
  96. {
  97. gauss: {
  98. last_status_at: {
  99. scale: '30d',
  100. offset: '30d',
  101. decay: 0.3,
  102. },
  103. },
  104. }
  105. end
  106. def following_ids
  107. @following_ids ||= account.active_relationships.pluck(:target_account_id) + [account.id]
  108. end
  109. def limit_for_non_exact_results
  110. if exact_match?
  111. limit - 1
  112. else
  113. limit
  114. end
  115. end
  116. def terms_for_query
  117. if domain_is_local?
  118. query_username
  119. else
  120. query
  121. end
  122. end
  123. def split_query_string
  124. @split_query_string ||= query.split('@')
  125. end
  126. def query_username
  127. @query_username ||= split_query_string.first || ''
  128. end
  129. def query_domain
  130. @query_domain ||= query_without_split? ? nil : split_query_string.last
  131. end
  132. def query_without_split?
  133. split_query_string.size == 1
  134. end
  135. def domain_is_local?
  136. @domain_is_local ||= TagManager.instance.local_domain?(query_domain)
  137. end
  138. def exact_match?
  139. exact_match.present?
  140. end
  141. def username_complete?
  142. query.include?('@') && "@#{query}" =~ /\A#{Account::MENTION_RE}\Z/
  143. end
  144. def likely_acct?
  145. @acct_hint || username_complete?
  146. end
  147. end