es-elasticity 0.12.0 → 0.13.3.pre1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -31,7 +31,7 @@ Or install it yourself as:
31
31
  ### Version Support
32
32
  This gem has [elasticsearch-ruby](https://github.com/elastic/elasticsearch-ruby) as a dependency. In order to use different versions of elasticsearch you will need to match your version of elasticsearch-ruby to the version of elasticsearch you want to use ([see here](https://github.com/elastic/elasticsearch-ruby#compatibility). Elasticity should work across all versions of elastisearch-ruby, although they have not all been tested so there are likely edge cases.
33
33
 
34
- Currently tests are run on travis ci against elasticsearch 5.1.1 with elasticsearch-ruby 5.0.3.
34
+ Currently tests are run on CirlceCI against elasticsearch 6.8.2 with elasticsearch-ruby 7.2.0.
35
35
 
36
36
  ### Configuration
37
37
 
@@ -194,7 +194,7 @@ adults = adults.active_records(User)
194
194
  #### Search Args
195
195
 
196
196
  ##### explain: true
197
- For `search` definitions we support passing `{ explain: true }` to the search as a second argument in order to surface the reason a search result was returned.
197
+ For `search` definitions we support passing `{ explain: true }` to the search as a second argument in order to surface the reason a search result was returned.
198
198
 
199
199
  ```ruby
200
200
  # example in single search
@@ -359,7 +359,7 @@ For example:
359
359
  Search::User.from_active_record(self).update
360
360
  end
361
361
 
362
- def remove_index_document
362
+ def delete_index_document
363
363
  Search::User.delete(self.id)
364
364
  end
365
365
  end
@@ -443,6 +443,20 @@ The default persistence strategy changed from SingleIndex to AliasIndex in versi
443
443
  5. Create a new Pull Request
444
444
  6. Sign the CLA if you haven't yet. See CONTRIBUTING.md
445
445
 
446
+ ## Gem documentation
447
+
448
+ You can find the documentation by going to CircleCI, looking for the `build` job, going to Artifacts and clicking on `index.html`. A visual guide on this can be found in our wiki at [Gems Development: Where to find documentation for our gems](https://wiki.doximity.com/articles/gems-development-where-to-find-documentation-for-our-gems).
449
+
450
+ ## Gem development
451
+
452
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `rake spec` to run the tests.
453
+ You can also run `bundle console` for an interactive prompt that will allow you to experiment.
454
+
455
+ This repository uses a gem publishing mechanism on the CI configuration, meaning most work related with cutting a new
456
+ version is done automatically.
457
+
458
+ To release a new version, follow the [wiki instructions](https://wiki.doximity.com/articles/gems-development-releasing-new-versions).
459
+
446
460
  ## License
447
461
 
448
462
  MPN is licensed under an Apache 2 license. Contributors are required to sign an contributor license agreement. See LICENSE.txt and CONTRIBUTING.md for more information.
data/Rakefile CHANGED
@@ -1,6 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
2
4
  require "rspec/core/rake_task"
3
5
 
4
- RSpec::Core::RakeTask.new
6
+ FileList["tasks/*.rake"].each { |task| load task }
7
+
8
+ RSpec::Core::RakeTask.new(:spec)
5
9
 
6
10
  task default: :spec
@@ -14,6 +14,9 @@ Gem::Specification.new do |spec|
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(bin|test|spec|vendor|tmp|coverage)/})
19
+ end
17
20
  # spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
21
  spec.executables = []
19
22
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
@@ -28,8 +31,11 @@ Gem::Specification.new do |spec|
28
31
  spec.add_development_dependency "byebug"
29
32
  spec.add_development_dependency "codeclimate-test-reporter"
30
33
  spec.add_development_dependency "redis"
34
+ spec.add_development_dependency "timecop"
35
+ spec.add_development_dependency "rspec_junit_formatter"
31
36
 
32
37
  spec.add_dependency "activesupport", ">= 4.0.0", "< 6"
33
38
  spec.add_dependency "activemodel", ">= 4.0.0", "< 6"
39
+ spec.add_dependency "activerecord", ">= 4.0.0", "< 6"
34
40
  spec.add_dependency "elasticsearch", ">= 1.0"
35
41
  end
data/lib/elasticity.rb CHANGED
@@ -17,6 +17,7 @@ require "bundler/setup"
17
17
  Bundler.setup
18
18
 
19
19
  require "active_support"
20
+ require "arel"
20
21
  require "active_support/core_ext"
21
22
  require "active_model"
22
23
  require "elasticsearch"
@@ -10,7 +10,7 @@ module Elasticity
10
10
  VERSION_FOR_SUBCLASS_ERROR = "7.0.0".freeze
11
11
  ATTRS = [
12
12
  :index_base_name, :document_type, :mapping, :strategy, :subclasses,
13
- :settings
13
+ :settings, :use_new_timestamp_format, :include_type_name_on_create
14
14
  ].freeze
15
15
  VALIDATABLE_ATTRS = [:index_base_name, :document_type, :strategy].freeze
16
16
 
@@ -39,7 +39,8 @@ module Elasticity
39
39
  def definition
40
40
  return @definition if defined?(@definition)
41
41
  @definition = {
42
- settings: merge_settings, mappings: { @document_type => @mapping.nil? ? {} : @mapping.deep_stringify_keys }
42
+ settings: merge_settings,
43
+ mappings: { @document_type => @mapping.nil? ? {} : @mapping.deep_stringify_keys }
43
44
  }
44
45
  subclasses.each do |doc_type, subclass|
45
46
  @definition[:mappings][doc_type] = subclass.constantize.mapping
@@ -11,6 +11,7 @@ module Elasticity
11
11
  :index_exists?,
12
12
  :remap!,
13
13
  :flush_index,
14
+ :refresh_index,
14
15
  :index_document,
15
16
  :search,
16
17
  :get,
@@ -27,7 +28,7 @@ module Elasticity
27
28
  def initialize(document_klass, index_config)
28
29
  @document_klass = document_klass
29
30
  @index_config = index_config
30
- @strategy = @index_config.strategy.new(@index_config.client, @index_config.fq_index_base_name, @index_config.document_type)
31
+ @strategy = @index_config.strategy.new(@index_config.client, @index_config.fq_index_base_name, @index_config.document_type, @index_config.use_new_timestamp_format, @index_config.include_type_name_on_create)
31
32
  end
32
33
 
33
34
  delegate(
@@ -71,10 +72,16 @@ module Elasticity
71
72
  end
72
73
 
73
74
  # Flushes the index, forcing any writes
75
+ # note that v7 no longer forces any writes on flush
74
76
  def flush_index
75
77
  @strategy.flush
76
78
  end
77
79
 
80
+ # Resfreshes the index, forcing any writes
81
+ def refresh_index
82
+ @strategy.refresh
83
+ end
84
+
78
85
  # Index the given document
79
86
  def index_document(id, document_hash)
80
87
  @strategy.index_document(document_type, id, document_hash)
@@ -1,6 +1,6 @@
1
1
  module Elasticity
2
2
  class InstrumentedClient
3
- INDICES_METHODS = %w(exists create delete get_settings get_mapping flush get_alias get_aliases put_alias delete_alias exists_alias update_aliases)
3
+ INDICES_METHODS = %w(exists create delete get_settings get_mapping flush refresh get_alias get_aliases put_alias delete_alias exists_alias update_aliases)
4
4
  INDEX_METHODS = %w(index delete get mget search count msearch scroll delete_by_query bulk)
5
5
 
6
6
  def initialize(client)
@@ -147,7 +147,12 @@ module Elasticity
147
147
  end
148
148
 
149
149
  def total
150
- search["hits"]["total"]
150
+ res = search["hits"]["total"]
151
+ if res.is_a?(::Hash)
152
+ res["value"]
153
+ else
154
+ res
155
+ end
151
156
  end
152
157
 
153
158
  def each_batch
@@ -193,7 +198,7 @@ module Elasticity
193
198
  if ids.any?
194
199
  id_col = "#{relation.connection.quote_column_name(relation.table_name)}.#{relation.connection.quote_column_name(relation.klass.primary_key)}"
195
200
  id_vals = ids.map { |id| relation.connection.quote(id) }
196
- Relation.new(relation.where("#{id_col} IN (?)", ids).order("FIELD(#{id_col}, #{id_vals.join(',')})"), body, response)
201
+ Relation.new(relation.where("#{id_col} IN (?)", ids).order(Arel.sql("FIELD(#{id_col}, #{id_vals.join(',')})")), body, response)
197
202
  else
198
203
  Relation.new(relation.none, body, response)
199
204
  end
@@ -237,7 +242,12 @@ module Elasticity
237
242
  end
238
243
 
239
244
  def total
240
- metadata[:total]
245
+ res = metadata[:total]
246
+ if res.is_a?(::Hash)
247
+ res["value"]
248
+ else
249
+ res
250
+ end
241
251
  end
242
252
 
243
253
  def suggestions
@@ -311,7 +321,12 @@ module Elasticity
311
321
  end
312
322
 
313
323
  def total
314
- @response["hits"]["total"]
324
+ res = @response["hits"]["total"]
325
+ if res.is_a?(::Hash)
326
+ res["value"]
327
+ else
328
+ res
329
+ end
315
330
  end
316
331
  alias_method :total_entries, :total
317
332
 
@@ -11,11 +11,15 @@ module Elasticity
11
11
 
12
12
  STATUSES = [:missing, :ok]
13
13
 
14
- def initialize(client, index_base_name, document_type)
14
+ def initialize(client, index_base_name, document_type, use_new_timestamp_format = false, include_type_name_on_create = true)
15
15
  @client = client
16
16
  @main_alias = index_base_name
17
17
  @update_alias = "#{index_base_name}_update"
18
18
  @document_type = document_type
19
+
20
+ # included for compatibility with v7
21
+ @use_new_timestamp_format = use_new_timestamp_format
22
+ @include_type_name_on_create = include_type_name_on_create
19
23
  end
20
24
 
21
25
  def ref_index_name
@@ -186,11 +190,12 @@ module Elasticity
186
190
 
187
191
  def create(index_def)
188
192
  if missing?
189
- index_name = create_index(index_def)
193
+ name = create_index(index_def)
194
+ @created_index_name = name
190
195
  @client.index_update_aliases(body: {
191
196
  actions: [
192
- { add: { index: index_name, alias: @main_alias } },
193
- { add: { index: index_name, alias: @update_alias } },
197
+ { add: { index: name, alias: @main_alias } },
198
+ { add: { index: name, alias: @update_alias } },
194
199
  ]
195
200
  })
196
201
  else
@@ -257,6 +262,10 @@ module Elasticity
257
262
  @client.index_flush(index: @update_alias)
258
263
  end
259
264
 
265
+ def refresh
266
+ @client.index_refresh(index: @update_alias)
267
+ end
268
+
260
269
  def settings
261
270
  @client.index_get_settings(index: @main_alias, type: @document_type).values.first
262
271
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
@@ -279,11 +288,20 @@ module Elasticity
279
288
 
280
289
  private
281
290
 
291
+ def build_index_name
292
+ ts = String.new
293
+ if @use_new_timestamp_format == true
294
+ ts = Time.now.utc.strftime("%Y%m%d%H%M%S%6N")
295
+ else
296
+ ts = Time.now.utc.strftime("%Y-%m-%d_%H:%M:%S.%6N")
297
+ end
298
+ "#{@main_alias}-#{ts}"
299
+ end
300
+
282
301
  def create_index(index_def)
283
- ts = Time.now.utc.strftime("%Y-%m-%d_%H:%M:%S.%6N")
284
- index_name = "#{@main_alias}-#{ts}"
285
- @client.index_create(index: index_name, body: index_def)
286
- index_name
302
+ name = build_index_name
303
+ @client.index_create(index: name, body: index_def, include_type_name: @include_type_name_on_create)
304
+ name
287
305
  end
288
306
 
289
307
  def retryable_error?(e)
@@ -3,10 +3,16 @@ module Elasticity
3
3
  class SingleIndex
4
4
  STATUSES = [:missing, :ok]
5
5
 
6
- def initialize(client, index_name, document_type)
6
+ def initialize(client, index_name, document_type, use_new_timestamp_format = false, include_type_name_on_create = true)
7
7
  @client = client
8
8
  @index_name = index_name
9
9
  @document_type = document_type
10
+
11
+ # included for compatibility with v7
12
+ @include_type_name_on_create = include_type_name_on_create
13
+
14
+ # not currently used. included for argument compatiblity with AliasStrategy
15
+ @use_new_timestamp_format = use_new_timestamp_format
10
16
  end
11
17
 
12
18
  def ref_index_name
@@ -23,7 +29,7 @@ module Elasticity
23
29
 
24
30
  def create(index_def)
25
31
  if missing?
26
- @client.index_create(index: @index_name, body: index_def)
32
+ @client.index_create(index: @index_name, body: index_def, include_type_name: @include_type_name_on_create)
27
33
  else
28
34
  raise IndexError.new(@index_name, "index already exist")
29
35
  end
@@ -101,6 +107,10 @@ module Elasticity
101
107
  def flush
102
108
  @client.index_flush(index: @index_name)
103
109
  end
110
+
111
+ def refresh
112
+ @client.index_refresh(index: @index_name)
113
+ end
104
114
  end
105
115
  end
106
116
  end
@@ -1,3 +1,3 @@
1
1
  module Elasticity
2
- VERSION = "0.12.0"
2
+ VERSION = "0.13.3.pre1"
3
3
  end
data/tasks/ci.rake ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :ci do
4
+ desc "Run specs"
5
+ task :specs do
6
+ reports = "tmp/test-results/rspec"
7
+ sh "mkdir -p #{reports}"
8
+ sh "bundle exec rspec ./spec " \
9
+ "--format progress "\
10
+ "-o #{reports}/results.xml"
11
+ end
12
+ end
data/tasks/rdoc.rake ADDED
@@ -0,0 +1,14 @@
1
+ # frozeon_string_literal: true
2
+
3
+ require "rdoc/task"
4
+
5
+ RDoc::Task.new do |rdoc|
6
+ rdoc.main = "README.md"
7
+ rdoc.markup = "tomdoc"
8
+ rdoc.options << "--github --encoding=UTF-8"
9
+ rdoc.rdoc_dir = "doc"
10
+ rdoc.rdoc_files.exclude("vendor", "tmp")
11
+ rdoc.rdoc_files.include("README.md", "lib", "*.rb")
12
+ rdoc.template = "rails"
13
+ rdoc.title = "Documentation"
14
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: es-elasticity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.13.3.pre1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Kochenburger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-31 00:00:00.000000000 Z
11
+ date: 2020-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -136,6 +136,34 @@ dependencies:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: timecop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rspec_junit_formatter
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
139
167
  - !ruby/object:Gem::Dependency
140
168
  name: activesupport
141
169
  requirement: !ruby/object:Gem::Requirement
@@ -176,6 +204,26 @@ dependencies:
176
204
  - - "<"
177
205
  - !ruby/object:Gem::Version
178
206
  version: '6'
207
+ - !ruby/object:Gem::Dependency
208
+ name: activerecord
209
+ requirement: !ruby/object:Gem::Requirement
210
+ requirements:
211
+ - - ">="
212
+ - !ruby/object:Gem::Version
213
+ version: 4.0.0
214
+ - - "<"
215
+ - !ruby/object:Gem::Version
216
+ version: '6'
217
+ type: :runtime
218
+ prerelease: false
219
+ version_requirements: !ruby/object:Gem::Requirement
220
+ requirements:
221
+ - - ">="
222
+ - !ruby/object:Gem::Version
223
+ version: 4.0.0
224
+ - - "<"
225
+ - !ruby/object:Gem::Version
226
+ version: '6'
179
227
  - !ruby/object:Gem::Dependency
180
228
  name: elasticsearch
181
229
  requirement: !ruby/object:Gem::Requirement
@@ -198,20 +246,19 @@ executables: []
198
246
  extensions: []
199
247
  extra_rdoc_files: []
200
248
  files:
249
+ - ".circleci/config.yml"
201
250
  - ".gitignore"
202
251
  - ".rspec"
252
+ - ".rubocop.yml"
203
253
  - ".simplecov"
204
- - ".travis.yml"
205
- - CHANGELOG
254
+ - CHANGELOG.md
206
255
  - CONTRIBUTING.md
207
256
  - Gemfile
257
+ - Gemfile.lock
208
258
  - LICENSE.txt
209
259
  - README.md
210
260
  - Rakefile
211
- - bin/rake
212
- - bin/rspec
213
- - elasticity.gemspec
214
- - es-elasticity-0.12.0.pre.rc1.gem
261
+ - es-elasticity.gemspec
215
262
  - lib/elasticity.rb
216
263
  - lib/elasticity/base_document.rb
217
264
  - lib/elasticity/bulk.rb
@@ -231,17 +278,8 @@ files:
231
278
  - lib/elasticity/strategies/alias_index.rb
232
279
  - lib/elasticity/strategies/single_index.rb
233
280
  - lib/elasticity/version.rb
234
- - spec/functional/persistence_spec.rb
235
- - spec/functional/search_spec.rb
236
- - spec/functional/segmented_spec.rb
237
- - spec/rspec_config.rb
238
- - spec/units/document_spec.rb
239
- - spec/units/index_config_spec.rb
240
- - spec/units/index_mapper_spec.rb
241
- - spec/units/multi_search_response_parser_spec.rb
242
- - spec/units/multi_search_spec.rb
243
- - spec/units/search_spec.rb
244
- - spec/units/strategies/single_index_spec.rb
281
+ - tasks/ci.rake
282
+ - tasks/rdoc.rake
245
283
  homepage: ''
246
284
  licenses:
247
285
  - MIT
@@ -257,24 +295,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
257
295
  version: '0'
258
296
  required_rubygems_version: !ruby/object:Gem::Requirement
259
297
  requirements:
260
- - - ">="
298
+ - - ">"
261
299
  - !ruby/object:Gem::Version
262
- version: '0'
300
+ version: 1.3.1
263
301
  requirements: []
264
- rubyforge_project:
265
- rubygems_version: 2.6.13
302
+ rubygems_version: 3.1.2
266
303
  signing_key:
267
304
  specification_version: 4
268
305
  summary: ActiveModel-based library for working with Elasticsearch
269
- test_files:
270
- - spec/functional/persistence_spec.rb
271
- - spec/functional/search_spec.rb
272
- - spec/functional/segmented_spec.rb
273
- - spec/rspec_config.rb
274
- - spec/units/document_spec.rb
275
- - spec/units/index_config_spec.rb
276
- - spec/units/index_mapper_spec.rb
277
- - spec/units/multi_search_response_parser_spec.rb
278
- - spec/units/multi_search_spec.rb
279
- - spec/units/search_spec.rb
280
- - spec/units/strategies/single_index_spec.rb
306
+ test_files: []