Module: Mongoid::Haystack::Util

Extended by:
Util
Included in:
Mongoid::Haystack, Util
Defined in:
lib/mongoid-haystack/util.rb

Instance Method Summary collapse

Instance Method Details

#connect!Object



56
57
58
59
60
# File 'lib/mongoid-haystack/util.rb', line 56

def connect!
  Mongoid.configure do |config|
    config.connect_to('mongoid-haystack')
  end
end

#create_indexesObject



31
32
33
# File 'lib/mongoid-haystack/util.rb', line 31

def create_indexes
  models.each{|model| model.create_indexes}
end

#destroy_allObject



35
36
37
# File 'lib/mongoid-haystack/util.rb', line 35

def destroy_all
  models.map{|model| model.destroy_all}
end

#find_or_create(finder, creator) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mongoid-haystack/util.rb', line 40

def find_or_create(finder, creator)
  doc = finder.call()
  return doc if doc

  n, max = 0, 2

  begin
    creator.call()
  rescue Object => e
    n += 1
    raise if n > max
    sleep(rand(0.1))
    finder.call() or retry
  end
end

#modelsObject



4
5
6
7
8
9
10
# File 'lib/mongoid-haystack/util.rb', line 4

def models
  [
    Mongoid::Haystack::Index,
    Mongoid::Haystack::Token,
    Mongoid::Haystack::Sequence
  ]
end

#phrases_for(*args, &block) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/mongoid-haystack/util.rb', line 126

def phrases_for(*args, &block)
  string = args.join(' ')
  string.strip!

  phrases = string.split(/\s+/)

  list = []

  phrases.each do |phrase|
    strip!(phrase)
    next if phrase.empty?
    block ? block.call(phrase) : list.push(phrase)
  end

  block ? nil : list
end

#reset!Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mongoid-haystack/util.rb', line 12

def reset!
  models.each do |model|
    begin
      model.collection.indexes.drop
    rescue Object => e
    end

    begin
      model.collection.drop
    rescue Object => e
    end

    begin
      model.create_indexes
    rescue Object => e
    end
  end
end

#search_for(*args, &block) ⇒ Object



163
164
165
# File 'lib/mongoid-haystack/util.rb', line 163

def search_for(*args, &block)
  phrases_for(*args).map{|phrase| [phrase, stems_for(phrase)]}.flatten.compact.uniq
end

#stems_for(*args, &block) ⇒ Object



159
160
161
# File 'lib/mongoid-haystack/util.rb', line 159

def stems_for(*args, &block)
  Stemming.stem(*args, &block)
end

#stopword?(word) ⇒ Boolean

Returns:

  • (Boolean)


167
168
169
170
# File 'lib/mongoid-haystack/util.rb', line 167

def stopword?(word)
  word = UnicodeUtils.nfkd(word.to_s.strip.downcase)
  word.empty? or Stemming::Stopwords.stopword?(word)
end

#strip!(word) ⇒ Object



172
173
174
175
176
177
# File 'lib/mongoid-haystack/util.rb', line 172

def strip!(word)
  word.replace(UnicodeUtils.nfkd(word.to_s.strip))
  word.gsub!(/\A(?:[^\w]|_|\s)+/, '')  # leading punctuation/spaces
  word.gsub!(/(?:[^\w]|_|\s+)+\Z/, '') # trailing punctuation/spaces
  word
end

#token_tree_for(*args, &block) ⇒ Object



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
# File 'lib/mongoid-haystack/util.rb', line 62

def token_tree_for(*args, &block)
  tree = []

  phrases_for(*args) do |phrase|
    #next if stopword?(phrase)

    if block
      block.call(:phrase, phrase)
    else
      tree.push([phrase, words = []])
    end

    words_for(phrase) do |word|
      #next if phrase == word
      #next if stopword?(word)

      if block
        block.call(:word, word)
      else
        words.push([word, stems = []])
      end

      stems_for(word) do |stem|
        #next if word == stem

        if block
          block.call(:stem, stem)
        else
          stems.push(stem)
        end
      end
    end
  end

  block ? nil : tree
end

#tokens_for(*args, &block) ⇒ Object



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
# File 'lib/mongoid-haystack/util.rb', line 99

def tokens_for(*args, &block)
  list = []

  token_tree_for(*args).each do |phrase, words|
    next if stopword?(phrase)
    block ? block.call(phrase) : list.push(phrase) 

    words.each do |word, stems|
      next if stopword?(word)

      unless word == phrase
        block ? block.call(word) : list.push(word) 
      end

      stems.each do |stem|
        next if stopword?(stem)

        unless stem == phrase or stem == word
          block ? block.call(stem) : list.push(stem)
        end
      end
    end
  end

  block ? nil : list
end

#words_for(*args, &block) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/mongoid-haystack/util.rb', line 143

def words_for(*args, &block)
  string = args.join(' ')
  string.gsub!(/_+/, '-')
  string.gsub!(/[^\w]/, ' ')

  list = []

  UnicodeUtils.each_word(string) do |word|
    strip!(word)
    next if word.empty?
    block ? block.call(word) : list.push(word)
  end

  block ? nil : list
end