statuses_index.rb 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. index_scope ::Status.unscoped.kept.without_reblogs.includes(:media_attachments, :preloadable_poll)
  34. crutch :mentions do |collection|
  35. data = ::Mention.where(status_id: collection.map(&:id)).where(account: Account.local, silent: false).pluck(:status_id, :account_id)
  36. data.each.with_object({}) { |(id, name), result| (result[id] ||= []).push(name) }
  37. end
  38. crutch :favourites do |collection|
  39. data = ::Favourite.where(status_id: collection.map(&:id)).where(account: Account.local).pluck(:status_id, :account_id)
  40. data.each.with_object({}) { |(id, name), result| (result[id] ||= []).push(name) }
  41. end
  42. crutch :reblogs do |collection|
  43. data = ::Status.where(reblog_of_id: collection.map(&:id)).where(account: Account.local).pluck(:reblog_of_id, :account_id)
  44. data.each.with_object({}) { |(id, name), result| (result[id] ||= []).push(name) }
  45. end
  46. crutch :bookmarks do |collection|
  47. data = ::Bookmark.where(status_id: collection.map(&:id)).where(account: Account.local).pluck(:status_id, :account_id)
  48. data.each.with_object({}) { |(id, name), result| (result[id] ||= []).push(name) }
  49. end
  50. crutch :votes do |collection|
  51. data = ::PollVote.joins(:poll).where(poll: { status_id: collection.map(&:id) }).where(account: Account.local).pluck(:status_id, :account_id)
  52. data.each.with_object({}) { |(id, name), result| (result[id] ||= []).push(name) }
  53. end
  54. root date_detection: false do
  55. field :id, type: 'long'
  56. field :account_id, type: 'long'
  57. field :text, type: 'text', value: ->(status) { status.searchable_text } do
  58. field :stemmed, type: 'text', analyzer: 'content'
  59. end
  60. field :searchable_by, type: 'long', value: ->(status, crutches) { status.searchable_by(crutches) }
  61. end
  62. end