Class: Mongoid::FTS::Index

Inherits:
Object
  • Object
show all
Includes:
Document
Defined in:
lib/mongoid-fts/index.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add(*args, &block) ⇒ Object



195
196
197
# File 'lib/mongoid-fts/index.rb', line 195

def Index.add(*args, &block)
  add!(*args, &block)
end

.add!(model) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/mongoid-fts/index.rb', line 136

def Index.add!(model)
  to_search = Index.to_search(model)

  literals         = to_search.has_key?(:literals) ?  Coerce.list_of_strings(to_search[:literals]) : nil

  title            = to_search.has_key?(:title) ?  Coerce.string(to_search[:title]) : nil

  keywords         = to_search.has_key?(:keywords) ?  Coerce.list_of_strings(to_search[:keywords]) : nil

  fuzzy            = to_search.has_key?(:fuzzy) ?  Coerce.list_of_strings(to_search[:fuzzy]) : nil

  fulltext         = to_search.has_key?(:fulltext) ?  Coerce.string(to_search[:fulltext]) : nil

  context_type = model.class.name.to_s
  context_id   = model.id

  conditions = {
    :context_type => context_type,
    :context_id   => context_id
  }

  attributes = {
    :literals         => literals,
    :title            => title,
    :keywords         => keywords,
    :fuzzy            => fuzzy,
    :fulltext         => fulltext
  }

  index = nil
  n = 42

  n.times do |i|
    index = where(conditions).first
    break if index

    begin
      index = create!(conditions)
      break if index
    rescue Object
      nil
    end

    sleep(rand) if i < (n - 1)
  end

  if index
    begin
      index.update_attributes!(attributes)
    rescue Object
      raise Error.new("failed to update index for #{ conditions.inspect }")
    end
  else
    raise Error.new("failed to create index for #{ conditions.inspect }")
  end

  index
end

.rebuild!Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/mongoid-fts/index.rb', line 120

def Index.rebuild!
  batches = Hash.new{|h,k| h[k] = []}

  each do |index|
    context_type, context_id = index.context_type, index.context_id
    next unless context_type && context_id
    (batches[context_type] ||= []).push(context_id)
  end

  models = FTS.find_in_batches(batches)

  reset!

  models.each{|model| add(model)}
end

.remove(*args, &block) ⇒ Object



218
219
220
# File 'lib/mongoid-fts/index.rb', line 218

def Index.remove(*args, &block)
  remove!(*args, &block)
end

.remove!(*args, &block) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/mongoid-fts/index.rb', line 199

def Index.remove!(*args, &block)
  options = args.extract_options!.to_options!
  models = args.flatten.compact

  model_ids = {}

  models.each do |model|
    model_name = model.class.name.to_s
    model_ids[model_name] ||= []
    model_ids[model_name].push(model.id)
  end

  conditions = model_ids.map do |model_name, model_ids|
    {:context_type => model_name, :context_id.in => model_ids}
  end

  any_of(conditions).destroy_all
end

.reset!Object



115
116
117
118
# File 'lib/mongoid-fts/index.rb', line 115

def Index.reset!
  teardown!
  setup!
end

.setup!Object



111
112
113
# File 'lib/mongoid-fts/index.rb', line 111

def Index.setup!
  Index.create_indexes
end

.teardown!Object



106
107
108
109
# File 'lib/mongoid-fts/index.rb', line 106

def Index.teardown!
  Index.remove_indexes
  Index.destroy_all
end

.to_search(model) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/mongoid-fts/index.rb', line 222

def Index.to_search(model)
#
  to_search = nil

#
  if model.respond_to?(:to_search)
    to_search = Map.for(model.to_search)
  else
    to_search = Map.new

    to_search[:literals] =
      %w( id ).map do |attr|
        model.send(attr) if model.respond_to?(attr)
      end

    to_search[:title] =
      %w( title ).map do |attr|
        model.send(attr) if model.respond_to?(attr)
      end

    to_search[:keywords] =
      %w( keywords tags ).map do |attr|
        model.send(attr) if model.respond_to?(attr)
      end

    to_search[:fulltext] =
      %w( fulltext text content body description ).map do |attr|
        model.send(attr) if model.respond_to?(attr)
      end
  end

#
  required = %w( literals title keywords fuzzy fulltext )
  actual = to_search.keys

=begin
  missing = required - actual
  unless missing.empty?
    raise ArgumentError, "#{ model.class.inspect }#to_search missing keys #{ missing.inspect }"
  end
=end

  invalid = actual - required
  unless invalid.empty?
    raise ArgumentError, "#{ model.class.inspect }#to_search invalid keys #{ invalid.inspect }"
  end

#
  literals = FTS.normalized_array(to_search[:literals])
  title    = FTS.normalized_array(to_search[:title])
  keywords = FTS.normalized_array(to_search[:keywords])
  fuzzy    = FTS.normalized_array(to_search[:fuzzy])
  fulltext = FTS.normalized_array(to_search[:fulltext])

#
  if to_search[:fuzzy].nil?
    fuzzy = [title, keywords]
  end

#
  to_search[:literals]         = FTS.literals_for(literals).uniq
  to_search[:title]            = (title + FTS.terms_for(title)).uniq
  to_search[:keywords]         = (keywords + FTS.terms_for(keywords)).uniq
  to_search[:fuzzy]            = FTS.fuzzy_for(fuzzy).uniq
  to_search[:fulltext]         = (FTS.terms_for(fulltext, :subterms => true)).uniq

#
  to_search
end

Instance Method Details

#inspect(*args, &block) ⇒ Object



102
103
104
# File 'lib/mongoid-fts/index.rb', line 102

def inspect(*args, &block)
  Map.for(as_document).inspect(*args, &block)
end

#normalizeObject



86
87
88
89
90
# File 'lib/mongoid-fts/index.rb', line 86

def normalize
  if !defined?(@normalized) or !@normalized
    normalize!
  end
end

#normalize!Object



92
93
94
95
96
97
98
99
100
# File 'lib/mongoid-fts/index.rb', line 92

def normalize!
  index = self

  %w( literals title keywords fulltext ).each do |attr|
    index[attr] = FTS.list_of_strings(index[attr])
  end
ensure
  @normalized = true
end