Commit 27b7ec6571cd49b01183d41fe1b331f3a51add8f

Authored by Andrew Kane
1 parent 41bb6c60

Refactored index

Showing 1 changed file with 48 additions and 40 deletions   Show diff stats
lib/searchkick/index.rb
... ... @@ -27,34 +27,6 @@ module Searchkick
27 27 client.indices.exists_alias name: name
28 28 end
29 29  
30   - def search_model(searchkick_klass, term = nil, options = {}, &block)
31   - query = Searchkick::Query.new(searchkick_klass, term, options)
32   - if block
33   - block.call(query.body)
34   - end
35   - if options[:execute] == false
36   - query
37   - else
38   - query.execute
39   - end
40   - end
41   -
42   - def similar_record(record, options = {})
43   - like_text = retrieve(record).to_hash
44   - .keep_if{|k,v| !options[:fields] || options[:fields].map(&:to_s).include?(k) }
45   - .values.compact.join(" ")
46   -
47   - # TODO deep merge method
48   - options[:where] ||= {}
49   - options[:where][:_id] ||= {}
50   - options[:where][:_id][:not] = record.id.to_s
51   - options[:limit] ||= 10
52   - options[:similar] = true
53   -
54   - # TODO use index class instead of record class
55   - search_model(record.class, like_text, options)
56   - end
57   -
58 30 def swap(new_name)
59 31 old_indices =
60 32 begin
... ... @@ -66,6 +38,8 @@ module Searchkick
66 38 client.indices.update_aliases body: {actions: actions}
67 39 end
68 40  
  41 + # record based
  42 +
69 43 def store(record)
70 44 client.index(
71 45 index: name,
... ... @@ -121,16 +95,42 @@ module Searchkick
121 95 end
122 96 end
123 97  
124   - def klass_document_type(klass)
125   - if klass.respond_to?(:document_type)
126   - klass.document_type
  98 + def similar_record(record, options = {})
  99 + like_text = retrieve(record).to_hash
  100 + .keep_if{|k,v| !options[:fields] || options[:fields].map(&:to_s).include?(k) }
  101 + .values.compact.join(" ")
  102 +
  103 + # TODO deep merge method
  104 + options[:where] ||= {}
  105 + options[:where][:_id] ||= {}
  106 + options[:where][:_id][:not] = record.id.to_s
  107 + options[:limit] ||= 10
  108 + options[:similar] = true
  109 +
  110 + # TODO use index class instead of record class
  111 + search_model(record.class, like_text, options)
  112 + end
  113 +
  114 + # search
  115 +
  116 + def search_model(searchkick_klass, term = nil, options = {}, &block)
  117 + query = Searchkick::Query.new(searchkick_klass, term, options)
  118 + if block
  119 + block.call(query.body)
  120 + end
  121 + if options[:execute] == false
  122 + query
127 123 else
128   - klass.model_name.to_s.underscore
  124 + query.execute
129 125 end
130 126 end
131 127  
132   - def tokens(text, options = {})
133   - client.indices.analyze({text: text, index: name}.merge(options))["tokens"].map{|t| t["token"] }
  128 + # reindex
  129 +
  130 + def create_index
  131 + index = Searchkick::Index.new("#{name}_#{Time.now.strftime('%Y%m%d%H%M%S%L')}", @options)
  132 + index.create(index_options)
  133 + index
134 134 end
135 135  
136 136 # remove old indices that start w/ index_name
... ... @@ -143,12 +143,6 @@ module Searchkick
143 143 indices
144 144 end
145 145  
146   - def create_index
147   - index = Searchkick::Index.new("#{name}_#{Time.now.strftime('%Y%m%d%H%M%S%L')}", @options)
148   - index.create(index_options)
149   - index
150   - end
151   -
152 146 # https://gist.github.com/jarosan/3124884
153 147 # http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/
154 148 def reindex_scope(scope, options = {})
... ... @@ -460,6 +454,20 @@ module Searchkick
460 454 }
461 455 end
462 456  
  457 + # other
  458 +
  459 + def tokens(text, options = {})
  460 + client.indices.analyze({text: text, index: name}.merge(options))["tokens"].map{|t| t["token"] }
  461 + end
  462 +
  463 + def klass_document_type(klass)
  464 + if klass.respond_to?(:document_type)
  465 + klass.document_type
  466 + else
  467 + klass.model_name.to_s.underscore
  468 + end
  469 + end
  470 +
463 471 protected
464 472  
465 473 def client
... ...