similar_profiles_source.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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: 10)
  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. QueryBuilder.new(recently_followed_account_ids, account).build.limit(limit).hits.pluck('_id').map(&:to_i).zip([key].cycle)
  48. else
  49. []
  50. end
  51. rescue Faraday::ConnectionFailed
  52. []
  53. end
  54. private
  55. def key
  56. :similar_to_recently_followed
  57. end
  58. end