sequel-elasticsearch 0.4.8 → 0.4.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7d297d1a447dae85be685cef2d12b2f38ba9734259318b1bb7b2bd2458b6325e
4
- data.tar.gz: 61aef4399f9f5faeb8bcb37ac74dedf386277d533dfb75996d7744fa1a4ab0f4
3
+ metadata.gz: 181431ca4bddeae82a6126453bf9439679feeb08243cf6d8a3158438cf5aed40
4
+ data.tar.gz: 80660bb4500762573c1e1cfc60b0242fff8cbb4ba0cf2d5bc922084eabd15a4a
5
5
  SHA512:
6
- metadata.gz: f6d774d669be05c6b1030df3ca8ba83a2ce0ee62e3685d9b8d8cb0163a3b542584fb10f09569046529dd5795de3b5efb5cd1f7079009e68df6d867324dc498bd
7
- data.tar.gz: b18e6af022f73da17285030c2a7ff880eec87b8df183481efbb8e49915cdff03ad2d5e1294d1092fc8fc9a5c5590ede4649f6fe9c9f9f6e36d043fc6d12b52ef
6
+ metadata.gz: 31071ac5cbeab5900fb4f505bd7d4357b5c45823a839fb7fa25a7192ed4aaa76cd77d3400322acc2bd70aeb213ebd6d12671f99e97b7bb9986823ae1aefdf000
7
+ data.tar.gz: 5e326ca98cad26b326e169a87d550d6ebcf00a12f01b22c2906a4dae7af71570080e469e79d8446b969fe93fb64cdbab34a2bd1074f7704f7eb182b859eabc40
data/README.md CHANGED
@@ -105,6 +105,22 @@ while (scroll = Document.es(scroll, scroll: '1m')) && scroll.empty? == false do
105
105
  end
106
106
  ```
107
107
 
108
+ ### Import
109
+
110
+ You can import the whole dataset, or specify a dataset to be imported. This will create a new, timestamped index for your dataset, and import all the records from that dataset into the index. An alias will be created (or updated) to point to the newly created index.
111
+
112
+ ```ruby
113
+ Document.import! # Import all the Document records. Use the default settings.
114
+
115
+ Document.import!(dataset: Document.where(active: true)) # Import all the active Document records
116
+
117
+ Document.import!(
118
+ index: 'active-documents', # Use the active-documents index
119
+ dataset: Document.where(active: true), # Only index active documents
120
+ batch_size: 20 # Send documents to Elasticsearch in batches of 20 records
121
+ )
122
+ ```
123
+
108
124
  ## Development
109
125
 
110
126
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -16,17 +16,21 @@ module Sequel
16
16
  module Elasticsearch
17
17
  # Apply the plugin to the specified model
18
18
  def self.apply(model, _opts = OPTS)
19
- model.instance_variable_set(:@elasticsearch_opts, {})
20
- model.instance_variable_set(:@elasticsearch_index, nil)
21
- model.instance_variable_set(:@elasticsearch_type, '_doc')
22
19
  model
23
20
  end
24
21
 
25
22
  # Configure the plugin
26
23
  def self.configure(model, opts = OPTS)
24
+ environment_scoped = if opts[:environment_scoped].nil?
25
+ model.environment != 'test'
26
+ else
27
+ opts[:environment_scoped]
28
+ end
29
+
27
30
  model.elasticsearch_opts = opts[:elasticsearch] || {}
28
31
  model.elasticsearch_index = (opts[:index] || model.table_name).to_sym
29
32
  model.elasticsearch_type = (opts[:type] || :_doc).to_sym
33
+ model.elasticsearch_environment_scoped = environment_scoped
30
34
  model
31
35
  end
32
36
 
@@ -35,9 +39,11 @@ module Sequel
35
39
  # The extra options that will be passed to the Elasticsearch client.
36
40
  attr_accessor :elasticsearch_opts
37
41
  # The Elasticsearch index to which the documents will be written.
38
- attr_accessor :elasticsearch_index
42
+ attr_writer :elasticsearch_index
39
43
  # The Elasticsearch type to which the documents will be written.
40
44
  attr_accessor :elasticsearch_type
45
+ # If generated indices should include the environment name
46
+ attr_accessor :elasticsearch_environment_scoped
41
47
 
42
48
  # Return the Elasticsearch client used to communicate with the cluster.
43
49
  def es_client
@@ -137,22 +143,41 @@ module Sequel
137
143
  es_client.indices.get_alias(name: elasticsearch_index)&.keys&.sort&.first
138
144
  end
139
145
 
146
+ def elasticsearch_index
147
+ return @elasticsearch_index unless elasticsearch_environment_scoped
148
+
149
+ "#{@elasticsearch_index}-#{environment}".to_sym
150
+ end
151
+
140
152
  # Generate a timestamped index name according to the environment.
141
- # This will use the +APP_ENV+ ENV variable and a timestamp to construct
142
- # index names like this:
153
+ # This will use the +APP_ENV+ or +RACK_ENV+ ENV variable and a timestamp
154
+ # to construct index names like this:
143
155
  #
144
156
  # base-name-staging-20191004.123456 # This is a staging index
145
- # base-name-20191005.171213 # This is a production index
157
+ # base-name-production-20191005.171213 # This is a production index
146
158
  #
159
+ # The adding of the environment name to the index can be turned off by
160
+ # setting +elasticsearch_environment_scoped+ to false.
147
161
  def timestamped_index
148
162
  time_str = Time.now.strftime('%Y%m%d.%H%M%S')
149
- env_str = ENV['APP_ENV'] == 'production' ? nil : ENV['APP_ENV']
150
- [elasticsearch_index, env_str, time_str].compact.join('-')
163
+ "#{elasticsearch_index}-#{time_str}".to_sym
164
+ end
165
+
166
+ def environment
167
+ ENV['APP_ENV'] || ENV['RACK_ENV'] || 'development'
151
168
  end
152
169
  end
153
170
 
154
171
  # The instance methods that will be added to the Sequel::Model
155
172
  module InstanceMethods
173
+ def elasticsearch_index
174
+ self.class.elasticsearch_index
175
+ end
176
+
177
+ def elasticsearch_type
178
+ self.class.elasticsearch_type
179
+ end
180
+
156
181
  # Sequel::Model after_create hook to add the new record to the Elasticsearch index.
157
182
  # It's "safe" in that it won't raise an error if it fails.
158
183
  def after_create
@@ -210,8 +235,8 @@ module Sequel
210
235
  # Determine the complete path to a document (/index/type/id) in the Elasticsearch cluster.
211
236
  def document_path(opts = {})
212
237
  {
213
- index: opts.delete(:index) || self.class.elasticsearch_index,
214
- type: opts.delete(:type) || self.class.elasticsearch_type,
238
+ index: opts.delete(:index) || elasticsearch_index,
239
+ type: opts.delete(:type) || elasticsearch_type,
215
240
  id: opts.delete(:id) || document_id
216
241
  }
217
242
  end
@@ -4,6 +4,6 @@ module Sequel
4
4
  # See https://jrgns.github.io/sequel-elasticsearch
5
5
  module Elasticsearch
6
6
  # The Gem's version.
7
- VERSION = '0.4.8'.freeze
7
+ VERSION = '0.4.9'.freeze
8
8
  end
9
9
  end
@@ -30,4 +30,5 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency 'rubocop', '~> 0.52'
31
31
  spec.add_development_dependency 'simplecov', '~> 0.15'
32
32
  spec.add_development_dependency 'webmock', '~> 3.2'
33
+ spec.add_development_dependency 'sqlite3', '~> 1.4'
33
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-elasticsearch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.8
4
+ version: 0.4.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jurgens du Toit
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-04 00:00:00.000000000 Z
11
+ date: 2019-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: elasticsearch
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: '3.2'
125
+ - !ruby/object:Gem::Dependency
126
+ name: sqlite3
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.4'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.4'
125
139
  description: A plugin for the Sequel gem to sync data to Elasticsearch.
126
140
  email:
127
141
  - jrgns@jadeit.co.za