Module: Mongoid::Haystack

Extended by:
Haystack, Util
Included in:
Haystack
Defined in:
lib/mongoid-haystack.rb,
lib/mongoid-haystack/util.rb,
lib/mongoid-haystack/index.rb,
lib/mongoid-haystack/token.rb,
lib/mongoid-haystack/search.rb,
lib/mongoid-haystack/sequence.rb,
lib/mongoid-haystack/stemming.rb

Defined Under Namespace

Modules: Denormalization, Pagination, Search, Stemming, Util Classes: Engine, Index, Sequence, Token

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

connect!, create_indexes, destroy_all, find_or_create, models, phrases_for, reset!, search_for, stems_for, stopword?, strip!, token_tree_for, tokens_for, words_for

Class Method Details

.denormalize(results) ⇒ Object



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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/mongoid-haystack/search.rb', line 248

def Haystack.denormalize(results)
  queries = Hash.new{|h,k| h[k] = []}

  results.each do |result|
    model_type = result[:model_type]
    model_id = result[:model_id]
    model_class = eval(model_type) rescue next
    queries[model_class].push(model_id)
  end

=begin
  index = Hash.new{|h,k| h[k] = {}}
=end

  models =
    queries.map do |model_class, model_ids|
      model_class_models =
        begin
          model_class.find(model_ids)
        rescue Mongoid::Errors::DocumentNotFound
          model_ids.map do |model_id|
            begin
              model_class.find(model_id)
            rescue Mongoid::Errors::DocumentNotFound
              nil
            end
          end
        end

=begin
      model_class_models.each do |model|
        index[model.class.name] ||= Hash.new
        next unless model
        index[model.class.name][model.id.to_s] = model
      end
=end

      model_class_models
    end

  models.flatten!
  models.compact!
  models

=begin
  to_ignore = []

  results.each_with_index do |result, i|
    model = index[result['model_type']][result['model_id'].to_s]

    if model.nil?
      to_ignore.push(i)
      next
    else
      result.model = model
    end

    result.model
    result
  end

  to_ignore.reverse.each do |index|
    models.delete_at(index)
  end
=end

  models
end

.dependenciesObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mongoid-haystack.rb', line 12

def dependencies
  {
    'mongoid'       => [ 'mongoid'       , '~> 3.1' ] ,
    'moped'         => [ 'moped'         , '~> 1.3' ] ,
    'origin'        => [ 'origin'        , '~> 1.0' ] ,
    'map'           => [ 'map'           , '~> 6.2' ] ,
    'fattr'         => [ 'fattr'         , '~> 2.2' ] ,
    'coerce'        => [ 'coerce'        , '~> 0.0' ] ,
    'unicode_utils' => [ 'unicode_utils' , '~> 1.4' ] ,
    'threadify'     => [ 'threadify'     , '~> 1.3' ] ,
  }
end

.expand(*args, &block) ⇒ Object



317
318
319
# File 'lib/mongoid-haystack/search.rb', line 317

def Haystack.expand(*args, &block)
  Haystack.denormalize(*args, &block)
end

.included(other) ⇒ Object



84
85
86
# File 'lib/mongoid-haystack.rb', line 84

def Haystack.included(other)
  other.send(:include, Search)
end

.index(*args, &block) ⇒ Object



3
4
5
# File 'lib/mongoid-haystack/index.rb', line 3

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

.libdir(*args, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mongoid-haystack.rb', line 25

def libdir(*args, &block)
  @libdir ||= File.expand_path(__FILE__).sub(/\.rb$/,'')
  args.empty? ? @libdir : File.join(@libdir, *args)
ensure
  if block
    begin
      $LOAD_PATH.unshift(@libdir)
      block.call()
    ensure
      $LOAD_PATH.shift()
    end
  end
end

.load(*libs) ⇒ Object



39
40
41
42
# File 'lib/mongoid-haystack.rb', line 39

def load(*libs)
  libs = libs.join(' ').scan(/[^\s+]+/)
  libdir{ libs.each{|lib| Kernel.load(lib) } }
end

.models_for(*args, &block) ⇒ Object



321
322
323
# File 'lib/mongoid-haystack/search.rb', line 321

def Haystack.models_for(*args, &block)
  Haystack.denormalize(*args, &block)
end

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



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mongoid-haystack/index.rb', line 11

def Haystack.reindex!(*args, &block)
  Index.all.each do |index|
    model =
      begin
        index.model
      rescue Object => e
        index.destroy
        next
      end

    index(model)
  end
end

.unindex(*args, &block) ⇒ Object



7
8
9
# File 'lib/mongoid-haystack/index.rb', line 7

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

.versionObject



8
9
10
# File 'lib/mongoid-haystack.rb', line 8

def version
  const_get :Version
end

Instance Method Details

#search(*args, &block) ⇒ Object



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/mongoid-haystack/search.rb', line 78

def search(*args, &block)
#
  options = Map.options_for!(args)
  search = args.join(' ')

  conditions = {}
  order = []

  op = :token_ids.in

#
  case
    when options[:all]
      op = :token_ids.all
      search += Coerce.string(options[:all])

    when options[:any]
      op = :token_ids.in
      search += Coerce.string(options[:any])

    when options[:in]
      op = :token_ids.in
      search += Coerce.string(options[:in])
  end

#
  tokens = search_tokens_for(search)
  token_ids = tokens.map{|token| token.id}

#
  conditions[op] = token_ids

#
  order.push(["score", :desc])

  tokens.each do |token|
    order.push(["keyword_scores.#{ token.id }", :desc])
  end

  tokens.each do |token|
    order.push(["fulltext_scores.#{ token.id }", :desc])
  end

  order.push(["size", :asc])

#
  if options[:facets]
    conditions[:facets] = {'$elemMatch' => options[:facets]}
  end

#
  if options[:types]
    model_types = Array(options[:types]).map{|type| type.name}
    conditions[:model_type.in] = model_types
  end

#
  query =
    Index.where(conditions)
      .order_by(order)
        .only(:_id, :model_type, :model_id)

  query.extend(Pagination)

  query.extend(Denormalization)

  query
end

#search_tokens_for(search) ⇒ 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
# File 'lib/mongoid-haystack/search.rb', line 222

def search_tokens_for(search)
  #values = Token.values_for(search.to_s)
  #values = Util.phrases_for(search).map{|phrase| Util.stems_for(phrase)}.flatten
  values = Util.search_for(search)
  tokens = []

  Token.where(:value.in => values).each do |token|
    index = values.index(token.value)
    tokens[index] = token
  end

  tokens.compact!

  total = Token.total.to_f

  rarity = {}
  tokens.map{|token| rarity[token] = token.rarity_bin(total)}

  position = {}
  tokens.each_with_index{|token, i| position[token] = i + 1}

  tokens.sort!{ |a, b| [rarity[b], position[a]] <=> [rarity[a], position[b]] }

  tokens
end