similar_profiles_source.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # frozen_string_literal: true
  2. class AccountSuggestions::SimilarProfilesSource < AccountSuggestions::Source
  3. class QueryBuilder < AccountSearchService::QueryBuilder
  4. def must_clauses
  5. [
  6. {
  7. more_like_this: {
  8. fields: %w(text text.stemmed),
  9. like: @query.map { |id| { _index: 'accounts', _id: id } },
  10. },
  11. },
  12. {
  13. term: {
  14. properties: 'discoverable',
  15. },
  16. },
  17. ]
  18. end
  19. def must_not_clauses
  20. [
  21. {
  22. terms: {
  23. id: following_ids,
  24. },
  25. },
  26. {
  27. term: {
  28. properties: 'bot',
  29. },
  30. },
  31. ]
  32. end
  33. def should_clauses
  34. {
  35. term: {
  36. properties: {
  37. value: 'verified',
  38. boost: 2,
  39. },
  40. },
  41. }
  42. end
  43. end
  44. def get(account, limit: DEFAULT_LIMIT)
  45. recently_followed_account_ids = account.active_relationships.recent.limit(5).pluck(:target_account_id)
  46. if Chewy.enabled? && !recently_followed_account_ids.empty?
  47. ids_from_es = QueryBuilder.new(recently_followed_account_ids, account).build.limit(limit).hits.pluck('_id').map(&:to_i)
  48. base_account_scope(account).where(id: ids_from_es).pluck(:id).zip([key].cycle)
  49. else
  50. []
  51. end
  52. rescue Faraday::ConnectionFailed
  53. []
  54. end
  55. private
  56. def key
  57. :similar_to_recently_followed
  58. end
  59. end