Guest User

Untitled

a guest
May 26th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. require 'thinking_sphinx/active_record/delta'
  2. require 'thinking_sphinx/active_record/search'
  3. require 'thinking_sphinx/active_record/has_many_association'
  4.  
  5. module ThinkingSphinx
  6. # Core additions to ActiveRecord models - define_index for creating indexes
  7. # for models. If you want to interrogate the index objects created for the
  8. # model, you can use the class-level accessor :indexes.
  9. #
  10. module ActiveRecord
  11. def self.included(base)
  12. base.class_eval do
  13. class_inheritable_array :indexes
  14. class << self
  15. # Allows creation of indexes for Sphinx. If you don't do this, there
  16. # isn't much point trying to search (or using this plugin at all,
  17. # really).
  18. #
  19. # An example or two:
  20. #
  21. # define_index
  22. # indexes :id, :as => :model_id
  23. # indexes name
  24. # end
  25. #
  26. # You can also grab fields from associations - multiple levels deep
  27. # if necessary.
  28. #
  29. # define_index do
  30. # indexes tags.name, :as => :tag
  31. # indexes articles.content
  32. # indexes orders.line_items.product.name, :as => :product
  33. # end
  34. #
  35. # And it will automatically concatenate multiple fields:
  36. #
  37. # define_index do
  38. # indexes [author.first_name, author.last_name], :as => :author
  39. # end
  40. #
  41. # The #indexes method is for fields - if you want attributes, use
  42. # #has instead. All the same rules apply - but keep in mind that
  43. # attributes are for sorting, grouping and filtering, not searching.
  44. #
  45. # define_index do
  46. # # fields ...
  47. #
  48. # has created_at, updated_at
  49. # end
  50. #
  51. # One last feature is the delta index. This requires the model to
  52. # have a boolean field named 'delta', and is enabled as follows:
  53. #
  54. # define_index do
  55. # # fields ...
  56. # # attributes ...
  57. #
  58. # set_property :delta => true
  59. # end
  60. #
  61. # Check out the more detailed documentation for each of these methods
  62. # at ThinkingSphinx::Index::Builder.
  63. #
  64. def define_index(&block)
  65. return unless ThinkingSphinx.define_indexes?
  66.  
  67. self.indexes ||= []
  68. index = Index.new(self, &block)
  69.  
  70. self.indexes << index
  71. unless ThinkingSphinx.indexed_models.include?(self.name)
  72. ThinkingSphinx.indexed_models << self.name
  73. end
  74.  
  75. if index.delta?
  76. before_save :toggle_delta
  77. after_commit :index_delta
  78. end
  79.  
  80. after_destroy :toggle_deleted
  81.  
  82. index
  83. end
  84. alias_method :sphinx_index, :define_index
  85.  
  86. # Generate a unique CRC value for the model's name, to use to
  87. # determine which Sphinx documents belong to which AR records.
  88. #
  89. # Really only written for internal use - but hey, if it's useful to
  90. # you in some other way, awesome.
  91. #
  92. def to_crc32
  93. result = 0xFFFFFFFF
  94. self.name.each_byte do |byte|
  95. result ^= byte
  96. 8.times do
  97. result = (result >> 1) ^ (0xEDB88320 * (result & 1))
  98. end
  99. end
  100. result ^ 0xFFFFFFFF
  101. end
  102. end
  103. end
  104.  
  105. base.send(:include, ThinkingSphinx::ActiveRecord::Delta)
  106. base.send(:include, ThinkingSphinx::ActiveRecord::Search)
  107.  
  108. ::ActiveRecord::Associations::HasManyAssociation.send(
  109. :include, ThinkingSphinx::ActiveRecord::HasManyAssociation
  110. )
  111. ::ActiveRecord::Associations::HasManyThroughAssociation.send(
  112. :include, ThinkingSphinx::ActiveRecord::HasManyAssociation
  113. )
  114. end
  115.  
  116. def in_core_index?
  117. @in_core_index ||= self.class.search_for_id(self.id, "#{self.class.name.downcase}_core")
  118. end
  119.  
  120. def toggle_deleted
  121. return unless ThinkingSphinx.updates_enabled?
  122.  
  123. config = ThinkingSphinx::Configuration.new
  124. client = Riddle::Client.new config.address, config.port
  125.  
  126. client.update(
  127. "#{self.class.indexes.first.name}_core",
  128. ['sphinx_deleted'],
  129. {self.id => 1}
  130. ) if self.in_core_index?
  131.  
  132. client.update(
  133. "#{self.class.indexes.first.name}_delta",
  134. ['sphinx_deleted'],
  135. {self.id => 1}
  136. ) if ThinkingSphinx.deltas_enabled? &&
  137. self.class.indexes.any? { |index| index.delta? } &&
  138. self.delta?
  139. end
  140. end
  141. end
Add Comment
Please, Sign In to add comment