statuses_index.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # frozen_string_literal: true
  2. class StatusesIndex < Chewy::Index
  3. settings index: { refresh_interval: '15m' }, analysis: {
  4. filter: {
  5. english_stop: {
  6. type: 'stop',
  7. stopwords: '_english_',
  8. },
  9. english_stemmer: {
  10. type: 'stemmer',
  11. language: 'english',
  12. },
  13. english_possessive_stemmer: {
  14. type: 'stemmer',
  15. language: 'possessive_english',
  16. },
  17. },
  18. analyzer: {
  19. content: {
  20. tokenizer: 'uax_url_email',
  21. filter: %w(
  22. english_possessive_stemmer
  23. lowercase
  24. asciifolding
  25. cjk_width
  26. english_stop
  27. english_stemmer
  28. ),
  29. },
  30. },
  31. }
  32. define_type ::Status.unscoped.kept.without_reblogs.includes(:media_attachments, :preloadable_poll) do
  33. crutch :mentions do |collection|
  34. data = ::Mention.where(status_id: collection.map(&:id)).where(account: Account.local, silent: false).pluck(:status_id, :account_id)
  35. data.each.with_object({}) { |(id, name), result| (result[id] ||= []).push(name) }
  36. end
  37. crutch :favourites do |collection|
  38. data = ::Favourite.where(status_id: collection.map(&:id)).where(account: Account.local).pluck(:status_id, :account_id)
  39. data.each.with_object({}) { |(id, name), result| (result[id] ||= []).push(name) }
  40. end
  41. crutch :reblogs do |collection|
  42. data = ::Status.where(reblog_of_id: collection.map(&:id)).where(account: Account.local).pluck(:reblog_of_id, :account_id)
  43. data.each.with_object({}) { |(id, name), result| (result[id] ||= []).push(name) }
  44. end
  45. crutch :bookmarks do |collection|
  46. data = ::Bookmark.where(status_id: collection.map(&:id)).where(account: Account.local).pluck(:status_id, :account_id)
  47. data.each.with_object({}) { |(id, name), result| (result[id] ||= []).push(name) }
  48. end
  49. root date_detection: false do
  50. field :id, type: 'long'
  51. field :account_id, type: 'long'
  52. field :text, type: 'text', value: ->(status) { [status.spoiler_text, Formatter.instance.plaintext(status)].concat(status.media_attachments.map(&:description)).concat(status.preloadable_poll ? status.preloadable_poll.options : []).join("\n\n") } do
  53. field :stemmed, type: 'text', analyzer: 'content'
  54. end
  55. field :searchable_by, type: 'long', value: ->(status, crutches) { status.searchable_by(crutches) }
  56. end
  57. end
  58. end