Module: Searchkick::Reindex

Defined in:
lib/searchkick/reindex.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



50
51
52
53
# File 'lib/searchkick/reindex.rb', line 50

def self.extended(klass)
  @descendents ||= []
  @descendents << klass unless @descendents.include?(klass)
end

Instance Method Details

#clean_indicesObject

remove old indices that start w/ index_name



41
42
43
44
45
46
47
48
# File 'lib/searchkick/reindex.rb', line 41

def clean_indices
  all_indices = Searchkick.client.indices.get_aliases
  indices = all_indices.select{|k, v| v["aliases"].empty? && k =~ /\A#{Regexp.escape(searchkick_index.name)}_\d{14,17}\z/ }.keys
  indices.each do |index|
    Searchkick::Index.new(index).delete
  end
  indices
end

#reindex(options = {}) ⇒ Object



6
7
8
9
10
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
# File 'lib/searchkick/reindex.rb', line 6

def reindex(options = {})
  skip_import = options[:import] == false

  alias_name = searchkick_index.name
  new_name = alias_name + "_" + Time.now.strftime("%Y%m%d%H%M%S%L")
  index = Searchkick::Index.new(new_name)

  clean_indices

  index.create searchkick_index_options

  # check if alias exists
  if Searchkick.client.indices.exists_alias(name: alias_name)
    # import before swap
    searchkick_import(index: index) unless skip_import

    # get existing indices to remove
    old_indices = Searchkick.client.indices.get_alias(name: alias_name).keys
    actions = old_indices.map{|name| {remove: {index: name, alias: alias_name}} } + [{add: {index: new_name, alias: alias_name}}]
    Searchkick.client.indices.update_aliases body: {actions: actions}
    clean_indices
  else
    searchkick_index.delete if searchkick_index.exists?
    Searchkick.client.indices.update_aliases body: {actions: [{add: {index: new_name, alias: alias_name}}]}

    # import after swap
    searchkick_import(index: index) unless skip_import
  end

  index.refresh

  true
end

#searchkick_import(options = {}) ⇒ Object



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
# File 'lib/searchkick/reindex.rb', line 55

def searchkick_import(options = {})
  index = options[:index] || searchkick_index
  batch_size = searchkick_options[:batch_size] || 1000

  # use scope for import
  scope = searchkick_klass
  scope = scope.search_import if scope.respond_to?(:search_import)
  if scope.respond_to?(:find_in_batches)
    scope.find_in_batches batch_size: batch_size do |batch|
      index.import batch.select{|item| item.should_index? }
    end
  else
    # https://github.com/karmi/tire/blob/master/lib/tire/model/import.rb
    # use cursor for Mongoid
    items = []
    scope.all.each do |item|
      items << item if item.should_index?
      if items.length == batch_size
        index.import items
        items = []
      end
    end
    index.import items
  end
end

#searchkick_index_optionsObject



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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/searchkick/reindex.rb', line 81

def searchkick_index_options
  options = searchkick_options

  if options[:mappings] and !options[:merge_mappings]
    settings = options[:settings] || {}
    mappings = options[:mappings]
  else
    settings = {
      analysis: {
        analyzer: {
          searchkick_keyword: {
            type: "custom",
            tokenizer: "keyword",
            filter: ["lowercase", "searchkick_stemmer"]
          },
          default_index: {
            type: "custom",
            tokenizer: "standard",
            # synonym should come last, after stemming and shingle
            # shingle must come before searchkick_stemmer
            filter: ["standard", "lowercase", "asciifolding", "searchkick_index_shingle", "searchkick_stemmer"]
          },
          searchkick_search: {
            type: "custom",
            tokenizer: "standard",
            filter: ["standard", "lowercase", "asciifolding", "searchkick_search_shingle", "searchkick_stemmer"]
          },
          searchkick_search2: {
            type: "custom",
            tokenizer: "standard",
            filter: ["standard", "lowercase", "asciifolding", "searchkick_stemmer"]
          },
          # https://github.com/leschenko/elasticsearch_autocomplete/blob/master/lib/elasticsearch_autocomplete/analyzers.rb
          searchkick_autocomplete_index: {
            type: "custom",
            tokenizer: "searchkick_autocomplete_ngram",
            filter: ["lowercase", "asciifolding"]
          },
          searchkick_autocomplete_search: {
            type: "custom",
            tokenizer: "keyword",
            filter: ["lowercase", "asciifolding"]
          },
          searchkick_word_search: {
            type: "custom",
            tokenizer: "standard",
            filter: ["lowercase", "asciifolding"]
          },
          searchkick_suggest_index: {
            type: "custom",
            tokenizer: "standard",
            filter: ["lowercase", "asciifolding", "searchkick_suggest_shingle"]
          },
          searchkick_text_start_index: {
            type: "custom",
            tokenizer: "keyword",
            filter: ["lowercase", "asciifolding", "searchkick_edge_ngram"]
          },
          searchkick_text_middle_index: {
            type: "custom",
            tokenizer: "keyword",
            filter: ["lowercase", "asciifolding", "searchkick_ngram"]
          },
          searchkick_text_end_index: {
            type: "custom",
            tokenizer: "keyword",
            filter: ["lowercase", "asciifolding", "reverse", "searchkick_edge_ngram", "reverse"]
          },
          searchkick_word_start_index: {
            type: "custom",
            tokenizer: "standard",
            filter: ["lowercase", "asciifolding", "searchkick_edge_ngram"]
          },
          searchkick_word_middle_index: {
            type: "custom",
            tokenizer: "standard",
            filter: ["lowercase", "asciifolding", "searchkick_ngram"]
          },
          searchkick_word_end_index: {
            type: "custom",
            tokenizer: "standard",
            filter: ["lowercase", "asciifolding", "reverse", "searchkick_edge_ngram", "reverse"]
          }
        },
        filter: {
          searchkick_index_shingle: {
            type: "shingle",
            token_separator: ""
          },
          # lucky find http://web.archiveorange.com/archive/v/AAfXfQ17f57FcRINsof7
          searchkick_search_shingle: {
            type: "shingle",
            token_separator: "",
            output_unigrams: false,
            output_unigrams_if_no_shingles: true
          },
          searchkick_suggest_shingle: {
            type: "shingle",
            max_shingle_size: 5
          },
          searchkick_edge_ngram: {
            type: "edgeNGram",
            min_gram: 1,
            max_gram: 50
          },
          searchkick_ngram: {
            type: "nGram",
            min_gram: 1,
            max_gram: 50
          },
          searchkick_stemmer: {
            type: "snowball",
            language: options[:language] || "English"
          }
        },
        tokenizer: {
          searchkick_autocomplete_ngram: {
            type: "edgeNGram",
            min_gram: 1,
            max_gram: 50
          }
        }
      }
    }

    if searchkick_env == "test"
      settings.merge!(number_of_shards: 1, number_of_replicas: 0)
    end

    settings.deep_merge!(options[:settings] || {})

    # synonyms
    synonyms = options[:synonyms] || []
    if synonyms.any?
      settings[:analysis][:filter][:searchkick_synonym] = {
        type: "synonym",
        synonyms: synonyms.select{|s| s.size > 1 }.map{|s| s.join(",") }
      }
      # choosing a place for the synonym filter when stemming is not easy
      # https://groups.google.com/forum/#!topic/elasticsearch/p7qcQlgHdB8
      # TODO use a snowball stemmer on synonyms when creating the token filter

      # http://elasticsearch-users.115913.n3.nabble.com/synonym-multi-words-search-td4030811.html
      # I find the following approach effective if you are doing multi-word synonyms (synonym phrases):
      # - Only apply the synonym expansion at index time
      # - Don't have the synonym filter applied search
      # - Use directional synonyms where appropriate. You want to make sure that you're not injecting terms that are too general.
      settings[:analysis][:analyzer][:default_index][:filter].insert(4, "searchkick_synonym")
      settings[:analysis][:analyzer][:default_index][:filter] << "searchkick_synonym"
    end

    if options[:wordnet]
      settings[:analysis][:filter][:searchkick_wordnet] = {
        type: "synonym",
        format: "wordnet",
        synonyms_path: Searchkick.wordnet_path
      }

      settings[:analysis][:analyzer][:default_index][:filter].insert(4, "searchkick_wordnet")
      settings[:analysis][:analyzer][:default_index][:filter] << "searchkick_wordnet"
    end

    if options[:special_characters] == false
      settings[:analysis][:analyzer].each do |analyzer, analyzer_settings|
        analyzer_settings[:filter].reject!{|f| f == "asciifolding" }
      end
    end

    mapping = {}

    # conversions
    if options[:conversions]
      mapping[:conversions] = {
        type: "nested",
        properties: {
          query: {type: "string", analyzer: "searchkick_keyword"},
          count: {type: "integer"}
        }
      }
    end

    mapping_options = Hash[
      [:autocomplete, :suggest, :text_start, :text_middle, :text_end, :word_start, :word_middle, :word_end, :highlight]
        .map{|type| [type, (options[type] || []).map(&:to_s)] }
    ]

    mapping_options.values.flatten.uniq.each do |field|
      field_mapping = {
        type: "multi_field",
        fields: {
          field => {type: "string", index: "not_analyzed"},
          "analyzed" => {type: "string", index: "analyzed"}
          # term_vector: "with_positions_offsets" for fast / correct highlighting
          # http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-highlighting.html#_fast_vector_highlighter
        }
      }

      mapping_options.except(:highlight).each do |type, fields|
        if fields.include?(field)
          field_mapping[:fields][type] = {type: "string", index: "analyzed", analyzer: "searchkick_#{type}_index"}
        end
      end

      if mapping_options[:highlight].include?(field)
        field_mapping[:fields]["analyzed"][:term_vector] = "with_positions_offsets"
      end

      mapping[field] = field_mapping
    end

    (options[:locations] || []).each do |field|
      mapping[field] = {
        type: "geo_point"
      }
    end

    mappings = {
      _default_: {
        properties: mapping,
        # https://gist.github.com/kimchy/2898285
        dynamic_templates: [
          {
            string_template: {
              match: "*",
              match_mapping_type: "string",
              mapping: {
                # http://www.elasticsearch.org/guide/reference/mapping/multi-field-type/
                type: "multi_field",
                fields: {
                  # analyzed field must be the default field for include_in_all
                  # http://www.elasticsearch.org/guide/reference/mapping/multi-field-type/
                  # however, we can include the not_analyzed field in _all
                  # and the _all index analyzer will take care of it
                  "{name}" => {type: "string", index: "not_analyzed"},
                  "analyzed" => {type: "string", index: "analyzed"}
                }
              }
            }
          }
        ]
      }
    }.deep_merge(options[:mappings] || {})
  end

  {
    settings: settings,
    mappings: mappings
  }
end