Commit 10138016fe9d5bf3d43422687cc931e527117cfb

Authored by Andrew
1 parent 597345bd

Better organization [skip ci]

Showing 1 changed file with 45 additions and 45 deletions   Show diff stats
lib/searchkick/index.rb
... ... @@ -54,6 +54,23 @@ module Searchkick
54 54 client.indices.put_settings index: name, body: settings
55 55 end
56 56  
  57 + def tokens(text, options = {})
  58 + client.indices.analyze(body: {text: text}.merge(options), index: name)["tokens"].map { |t| t["token"] }
  59 + end
  60 +
  61 + def total_docs
  62 + response =
  63 + client.search(
  64 + index: name,
  65 + body: {
  66 + query: {match_all: {}},
  67 + size: 0
  68 + }
  69 + )
  70 +
  71 + response["hits"]["total"]
  72 + end
  73 +
57 74 def promote(new_name, update_refresh_interval: false)
58 75 if update_refresh_interval
59 76 new_index = Searchkick::Index.new(new_name)
... ... @@ -73,6 +90,34 @@ module Searchkick
73 90 end
74 91 alias_method :swap, :promote
75 92  
  93 + def retrieve(record)
  94 + client.get(
  95 + index: name,
  96 + type: document_type(record),
  97 + id: search_id(record)
  98 + )["_source"]
  99 + end
  100 +
  101 + def all_indices(unaliased: false)
  102 + indices =
  103 + begin
  104 + client.indices.get_aliases
  105 + rescue Elasticsearch::Transport::Transport::Errors::NotFound
  106 + {}
  107 + end
  108 + indices = indices.select { |_k, v| v.empty? || v["aliases"].empty? } if unaliased
  109 + indices.select { |k, _v| k =~ /\A#{Regexp.escape(name)}_\d{14,17}\z/ }.keys
  110 + end
  111 +
  112 + # remove old indices that start w/ index_name
  113 + def clean_indices
  114 + indices = all_indices(unaliased: true)
  115 + indices.each do |index|
  116 + Searchkick::Index.new(index).delete
  117 + end
  118 + indices
  119 + end
  120 +
76 121 # record based
77 122 # use helpers for notifications
78 123  
... ... @@ -101,14 +146,6 @@ module Searchkick
101 146 bulk_indexer.bulk_update(records, method_name)
102 147 end
103 148  
104   - def retrieve(record)
105   - client.get(
106   - index: name,
107   - type: document_type(record),
108   - id: search_id(record)
109   - )["_source"]
110   - end
111   -
112 149 def search_id(record)
113 150 RecordData.new(self, record).search_id
114 151 end
... ... @@ -169,39 +206,6 @@ module Searchkick
169 206 index
170 207 end
171 208  
172   - def all_indices(unaliased: false)
173   - indices =
174   - begin
175   - client.indices.get_aliases
176   - rescue Elasticsearch::Transport::Transport::Errors::NotFound
177   - {}
178   - end
179   - indices = indices.select { |_k, v| v.empty? || v["aliases"].empty? } if unaliased
180   - indices.select { |k, _v| k =~ /\A#{Regexp.escape(name)}_\d{14,17}\z/ }.keys
181   - end
182   -
183   - # remove old indices that start w/ index_name
184   - def clean_indices
185   - indices = all_indices(unaliased: true)
186   - indices.each do |index|
187   - Searchkick::Index.new(index).delete
188   - end
189   - indices
190   - end
191   -
192   - def total_docs
193   - response =
194   - client.search(
195   - index: name,
196   - body: {
197   - query: {match_all: {}},
198   - size: 0
199   - }
200   - )
201   -
202   - response["hits"]["total"]
203   - end
204   -
205 209 def import_scope(scope, **options)
206 210 bulk_indexer.import_scope(scope, **options)
207 211 end
... ... @@ -212,10 +216,6 @@ module Searchkick
212 216  
213 217 # other
214 218  
215   - def tokens(text, options = {})
216   - client.indices.analyze(body: {text: text}.merge(options), index: name)["tokens"].map { |t| t["token"] }
217   - end
218   -
219 219 def klass_document_type(klass, ignore_type = false)
220 220 @klass_document_type[[klass, ignore_type]] ||= begin
221 221 if !ignore_type && klass.searchkick_klass.searchkick_options[:_type]
... ...