account_search_service.rb 4.6 KB

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