1
0

statuses_index.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # frozen_string_literal: true
  2. class StatusesIndex < Chewy::Index
  3. include FormattingHelper
  4. settings index: { refresh_interval: '30s' }, analysis: {
  5. filter: {
  6. english_stop: {
  7. type: 'stop',
  8. stopwords: '_english_',
  9. },
  10. english_stemmer: {
  11. type: 'stemmer',
  12. language: 'english',
  13. },
  14. english_possessive_stemmer: {
  15. type: 'stemmer',
  16. language: 'possessive_english',
  17. },
  18. },
  19. analyzer: {
  20. content: {
  21. tokenizer: 'uax_url_email',
  22. filter: %w(
  23. english_possessive_stemmer
  24. lowercase
  25. asciifolding
  26. cjk_width
  27. english_stop
  28. english_stemmer
  29. ),
  30. },
  31. },
  32. }
  33. # We do not use delete_if option here because it would call a method that we
  34. # expect to be called with crutches without crutches, causing n+1 queries
  35. index_scope ::Status.unscoped.kept.without_reblogs.includes(:media_attachments, :preloadable_poll)
  36. crutch :mentions do |collection|
  37. data = ::Mention.where(status_id: collection.map(&:id)).where(account: Account.local, silent: false).pluck(:status_id, :account_id)
  38. data.each.with_object({}) { |(id, name), result| (result[id] ||= []).push(name) }
  39. end
  40. crutch :favourites do |collection|
  41. data = ::Favourite.where(status_id: collection.map(&:id)).where(account: Account.local).pluck(:status_id, :account_id)
  42. data.each.with_object({}) { |(id, name), result| (result[id] ||= []).push(name) }
  43. end
  44. crutch :reblogs do |collection|
  45. data = ::Status.where(reblog_of_id: collection.map(&:id)).where(account: Account.local).pluck(:reblog_of_id, :account_id)
  46. data.each.with_object({}) { |(id, name), result| (result[id] ||= []).push(name) }
  47. end
  48. crutch :bookmarks do |collection|
  49. data = ::Bookmark.where(status_id: collection.map(&:id)).where(account: Account.local).pluck(:status_id, :account_id)
  50. data.each.with_object({}) { |(id, name), result| (result[id] ||= []).push(name) }
  51. end
  52. crutch :votes do |collection|
  53. data = ::PollVote.joins(:poll).where(poll: { status_id: collection.map(&:id) }).where(account: Account.local).pluck(:status_id, :account_id)
  54. data.each.with_object({}) { |(id, name), result| (result[id] ||= []).push(name) }
  55. end
  56. root date_detection: false do
  57. field :id, type: 'long'
  58. field :account_id, type: 'long'
  59. field :text, type: 'text', value: ->(status) { status.searchable_text } do
  60. field :stemmed, type: 'text', analyzer: 'content'
  61. end
  62. field :searchable_by, type: 'long', value: ->(status, crutches) { status.searchable_by(crutches) }
  63. end
  64. end