Module: ThinkingSphinx::ActiveRecord

Defined in:
lib/thinking_sphinx/active_record.rb,
lib/thinking_sphinx/active_record/delta.rb,
lib/thinking_sphinx/active_record/search.rb,
lib/thinking_sphinx/active_record/has_many_association.rb

Overview

Core additions to ActiveRecord models - define_index for creating indexes for models. If you want to interrogate the index objects created for the model, you can use the class-level accessor :sphinx_indexes.

Defined Under Namespace

Modules: Delta, HasManyAssociation, Search

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/thinking_sphinx/active_record.rb', line 11

def self.included(base)
  base.class_eval do
    class_inheritable_array :sphinx_indexes
    class << self
      # Allows creation of indexes for Sphinx. If you don't do this, there
      # isn't much point trying to search (or using this plugin at all,
      # really).
      #
      # An example or two:
      #
      #   define_index
      #     indexes :id, :as => :model_id
      #     indexes name
      #   end
      #
      # You can also grab fields from associations - multiple levels deep
      # if necessary.
      #
      #   define_index do
      #     indexes tags.name, :as => :tag
      #     indexes articles.content
      #     indexes orders.line_items.product.name, :as => :product
      #   end
      #
      # And it will automatically concatenate multiple fields:
      #
      #   define_index do
      #     indexes [author.first_name, author.last_name], :as => :author
      #   end
      #
      # The #indexes method is for fields - if you want attributes, use
      # #has instead. All the same rules apply - but keep in mind that
      # attributes are for sorting, grouping and filtering, not searching.
      #
      #   define_index do
      #     # fields ...
      #     
      #     has created_at, updated_at
      #   end
      #
      # One last feature is the delta index. This requires the model to
      # have a boolean field named 'delta', and is enabled as follows:
      #
      #   define_index do
      #     # fields ...
      #     # attributes ...
      #     
      #     set_property :delta => true
      #   end
      #
      # Check out the more detailed documentation for each of these methods
      # at ThinkingSphinx::Index::Builder.
      # 
      def define_index(&block)
        return unless ThinkingSphinx.define_indexes?
        
        self.sphinx_indexes ||= []
        index = Index.new(self, &block)
        
        self.sphinx_indexes << index
        unless ThinkingSphinx.indexed_models.include?(self.name)
          ThinkingSphinx.indexed_models << self.name
        end
        
        if index.delta?
          before_save   :toggle_delta
          after_commit  :index_delta
        end
        
        after_destroy :toggle_deleted
        
        index
      end
      alias_method :sphinx_index, :define_index
      
      # Generate a unique CRC value for the model's name, to use to
      # determine which Sphinx documents belong to which AR records.
      # 
      # Really only written for internal use - but hey, if it's useful to
      # you in some other way, awesome.
      # 
      def to_crc32
        result = 0xFFFFFFFF
        self.name.each_byte do |byte|
          result ^= byte
          8.times do
            result = (result >> 1) ^ (0xEDB88320 * (result & 1))
          end
        end
        result ^ 0xFFFFFFFF
      end
      
      def to_crc32s
        (subclasses << self).collect { |klass| klass.to_crc32 }
      end
    end
  end
  
  base.send(:include, ThinkingSphinx::ActiveRecord::Delta)
  base.send(:include, ThinkingSphinx::ActiveRecord::Search)
  
  ::ActiveRecord::Associations::HasManyAssociation.send(
    :include, ThinkingSphinx::ActiveRecord::HasManyAssociation
  )
  ::ActiveRecord::Associations::HasManyThroughAssociation.send(
    :include, ThinkingSphinx::ActiveRecord::HasManyAssociation
  )
end

Instance Method Details

#in_core_index?Boolean

Returns:

  • (Boolean)


120
121
122
123
124
125
# File 'lib/thinking_sphinx/active_record.rb', line 120

def in_core_index?
  self.class.search_for_id(
    self.sphinx_document_id,
    "#{self.class.name.underscore.tr(':/\\', '_')}_core"
  )
end

#sphinx_document_idObject



148
149
150
151
# File 'lib/thinking_sphinx/active_record.rb', line 148

def sphinx_document_id
  (self.id * ThinkingSphinx.indexed_models.size) +
    ThinkingSphinx.indexed_models.index(self.class.name)
end

#toggle_deletedObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/thinking_sphinx/active_record.rb', line 127

def toggle_deleted
  return unless ThinkingSphinx.updates_enabled?
  
  config = ThinkingSphinx::Configuration.instance
  client = Riddle::Client.new config.address, config.port
  
  client.update(
    "#{self.class.sphinx_indexes.first.name}_core",
    ['sphinx_deleted'],
    {self.sphinx_document_id => 1}
  ) if self.in_core_index?
  
  client.update(
    "#{self.class.sphinx_indexes.first.name}_delta",
    ['sphinx_deleted'],
    {self.sphinx_document_id => 1}
  ) if ThinkingSphinx.deltas_enabled? &&
    self.class.sphinx_indexes.any? { |index| index.delta? } &&
    self.delta?
end