elastic-rails 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +41 -2
- data/lib/elastic/commands/import_index_documents.rb +2 -2
- data/lib/elastic/configuration.rb +6 -1
- data/lib/elastic/core/adaptor.rb +15 -1
- data/lib/elastic/type.rb +26 -4
- data/lib/elastic/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0f277dfbb48f36dd5d37aec9678897754b6b348
|
4
|
+
data.tar.gz: 4f799186d83a23ab54aff5c11fcb482c461536e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a1ed07c2514ef377743f87b8761b1e4005529a56781e6441219e9010a98e9edb63b715d7dfdbeb641e0f058da8251d2a58a9b9bab0ef86f8ad96b40f82d7432
|
7
|
+
data.tar.gz: 86955585a269003324f70a85fea21bcffcd4fb284a3c50655bc078958c0b845569f5ea2f7b4c6978b1dfd78925d4bf579cefa84ebf1a9cbd7634bc5b244a5d9e
|
data/README.md
CHANGED
@@ -9,7 +9,7 @@ TODO: Delete this and the text above, and describe your gem
|
|
9
9
|
Add this line to your application's Gemfile:
|
10
10
|
|
11
11
|
```ruby
|
12
|
-
gem 'elastic'
|
12
|
+
gem 'elastic-rails'
|
13
13
|
```
|
14
14
|
|
15
15
|
And then execute:
|
@@ -18,10 +18,49 @@ And then execute:
|
|
18
18
|
|
19
19
|
Or install it yourself as:
|
20
20
|
|
21
|
-
$ gem install elastic
|
21
|
+
$ gem install elastic-rails
|
22
|
+
|
23
|
+
Finally execute the initialization script:
|
24
|
+
|
25
|
+
$ rails g es:init
|
22
26
|
|
23
27
|
## Usage
|
24
28
|
|
29
|
+
Start by creating an index over a model (You can also index non-activerecord objects)
|
30
|
+
|
31
|
+
rails g es:index Bike
|
32
|
+
|
33
|
+
This will generate a new index definition file in `app/indices`
|
34
|
+
|
35
|
+
Add some fields to the index
|
36
|
+
|
37
|
+
TODO
|
38
|
+
|
39
|
+
Every time you create or change and index you will neeed to synchronize the Elasticsearch index mappings:
|
40
|
+
|
41
|
+
rake es:migrate
|
42
|
+
|
43
|
+
If you already have some data that needs to be indexed then run the reindex task:
|
44
|
+
|
45
|
+
rake es:reindex
|
46
|
+
|
47
|
+
To add additional data call the index `import` or the model's `index_now` or `index_later` methods:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
some_bike.index_now # this will reindex only one record
|
51
|
+
some_bike.index_later # this will queue a reindexing job on the record
|
52
|
+
BikeIndex.import([bike_1, bike_2, bike_3]) # this will perform a bulk insertion
|
53
|
+
```
|
54
|
+
|
55
|
+
After some data has been added you can start searching:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
BikeIndex.must(brand: 'Trek', size: 'M').should(year: { gte: 2015 }).avg(:price)
|
59
|
+
|
60
|
+
BikeIndex.must(origin: 'China').segment(:brand).each { |brand, bikes| }
|
61
|
+
```
|
62
|
+
|
63
|
+
|
25
64
|
TODO: Write usage instructions here
|
26
65
|
|
27
66
|
## Development
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Elastic::Commands
|
2
2
|
class ImportIndexDocuments < Elastic::Support::Command.new(
|
3
|
-
:index, collection: nil,
|
3
|
+
:index, collection: nil, batch_size: 10000, verbose: false
|
4
4
|
)
|
5
5
|
def perform
|
6
6
|
if collection.present?
|
@@ -27,7 +27,7 @@ module Elastic::Commands
|
|
27
27
|
|
28
28
|
def queue(_object)
|
29
29
|
cache << render_for_es(_object)
|
30
|
-
flush if cache.length >=
|
30
|
+
flush if cache.length >= batch_size
|
31
31
|
end
|
32
32
|
|
33
33
|
def flush
|
@@ -4,7 +4,8 @@ module Elastic
|
|
4
4
|
host: '127.0.0.1',
|
5
5
|
port: 9200,
|
6
6
|
page_size: 20,
|
7
|
-
coord_similarity: true
|
7
|
+
coord_similarity: true,
|
8
|
+
import_batch_size: 10_000
|
8
9
|
}
|
9
10
|
|
10
11
|
extend self
|
@@ -47,6 +48,10 @@ module Elastic
|
|
47
48
|
@config[:logger] || default_logger
|
48
49
|
end
|
49
50
|
|
51
|
+
def import_batch_size
|
52
|
+
@config[:import_batch_size]
|
53
|
+
end
|
54
|
+
|
50
55
|
private
|
51
56
|
|
52
57
|
def config
|
data/lib/elastic/core/adaptor.rb
CHANGED
@@ -67,7 +67,10 @@ module Elastic::Core
|
|
67
67
|
{ 'index' => doc.merge('_index' => index_name) }
|
68
68
|
end
|
69
69
|
|
70
|
-
|
70
|
+
retry_on_temporary_error('bulk indexing') do
|
71
|
+
api_client.bulk body: body
|
72
|
+
end
|
73
|
+
|
71
74
|
self
|
72
75
|
end
|
73
76
|
|
@@ -90,6 +93,17 @@ module Elastic::Core
|
|
90
93
|
|
91
94
|
private
|
92
95
|
|
96
|
+
def retry_on_temporary_error(_action, retries: 3)
|
97
|
+
return yield
|
98
|
+
rescue Elasticsearch::Transport::Transport::Errors::ServiceUnavailable,
|
99
|
+
Elasticsearch::Transport::Transport::Errors::GatewayTimeout => exc
|
100
|
+
raise if retries <= 0
|
101
|
+
|
102
|
+
Configuration.logger.warn("#{exc.class} error during '#{_action}', retrying!")
|
103
|
+
retries -= 1
|
104
|
+
retry
|
105
|
+
end
|
106
|
+
|
93
107
|
def api_client
|
94
108
|
Elastic::Configuration.api_client
|
95
109
|
end
|
data/lib/elastic/type.rb
CHANGED
@@ -18,6 +18,14 @@ module Elastic
|
|
18
18
|
@suffix = _value
|
19
19
|
end
|
20
20
|
|
21
|
+
def self.import_batch_size
|
22
|
+
@import_batch_size || Configuration.import_batch_size
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.import_batch_size=(_value)
|
26
|
+
@import_batch_size = _value
|
27
|
+
end
|
28
|
+
|
21
29
|
def self.adaptor
|
22
30
|
@adaptor ||= Elastic::Core::Adaptor.new(suffix)
|
23
31
|
end
|
@@ -26,17 +34,31 @@ module Elastic
|
|
26
34
|
@mapping ||= load_mapping
|
27
35
|
end
|
28
36
|
|
29
|
-
def self.reindex(verbose: true)
|
37
|
+
def self.reindex(verbose: true, batch_size: nil)
|
30
38
|
drop
|
31
39
|
mapping.migrate
|
32
|
-
|
40
|
+
batch_size = batch_size || import_batch_size
|
41
|
+
|
42
|
+
Commands::ImportIndexDocuments.for(
|
43
|
+
index: self,
|
44
|
+
verbose: verbose,
|
45
|
+
batch_size: batch_size
|
46
|
+
)
|
47
|
+
|
33
48
|
ensure_full_mapping
|
34
49
|
self
|
35
50
|
end
|
36
51
|
|
37
|
-
def self.import(_collection)
|
52
|
+
def self.import(_collection, batch_size: nil)
|
38
53
|
enforce_mapping!
|
39
|
-
|
54
|
+
batch_size = batch_size || import_batch_size
|
55
|
+
|
56
|
+
Commands::ImportIndexDocuments.for(
|
57
|
+
index: self,
|
58
|
+
collection: _collection,
|
59
|
+
batch_size: batch_size
|
60
|
+
)
|
61
|
+
|
40
62
|
ensure_full_mapping
|
41
63
|
self
|
42
64
|
end
|
data/lib/elastic/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elastic-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ignacio Baixas
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-08-
|
11
|
+
date: 2016-08-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: elasticsearch
|