elastic_ar_sync 0.1.0 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +117 -3
- data/lib/elastic_ar_sync/elastic/syncable.rb +32 -6
- data/lib/elastic_ar_sync/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 892ccfa7c17e4a02cf8c774eec3ec71eb6dfaf16006052e0d82381b4aec48bb5
|
4
|
+
data.tar.gz: 1c948f281659aec4a29bdbb28471a55127f3fab23080fca7b63fb43454b3d140
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4524209a6682d5bf32f8543d54e138b0eb0e271a2ae04e417a8fa0ee905ac32788dfbd92300b2c5431ad54274222935b20b0268e71b37e542c8545e75e21eb46
|
7
|
+
data.tar.gz: cbd985be6b920d6c0f9e910a636ab391261e9187d9d56dce6323906be4cc1d8482e8028f489e2eb355e1cf4e6500cab301368a9ede05434c6636965ab3b0329c
|
data/README.md
CHANGED
@@ -1,8 +1,122 @@
|
|
1
1
|
# ElasticArSync
|
2
|
-
|
3
|
-
|
2
|
+
This repository contains easy set up modules for RDB and elasticsearch index with Active Record.
|
3
|
+
This is based on `gem elasticsearch-rails`.
|
4
4
|
## Usage
|
5
|
-
|
5
|
+
### Preparation
|
6
|
+
Install `gem sidekiq` and `gem redis-rails` to use Asynchronous processing for your app.
|
7
|
+
Install `gem elasticsearch-rails`, `gem elasticsearch-model` and `gem elasticsearch` for your app.
|
8
|
+
|
9
|
+
|
10
|
+
Include module `ElasticArSync::Elastic::Syncable` on your Model class which is inherited ActiveRecord.
|
11
|
+
|
12
|
+
and define `index_config` after `include ElasticArSync::Elastic::Syncable` if you use this module normally.
|
13
|
+
|
14
|
+
you can override index_config described after.
|
15
|
+
|
16
|
+
example below
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
class XXXXX < ActiveRecord
|
20
|
+
include ElasticArSync::Elastic::Syncable
|
21
|
+
index_config
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
then your Model class is included modules which is for sync model's table with elasticsearch index.
|
26
|
+
|
27
|
+
when you create new record elasticsearch index save new document in sync.
|
28
|
+
update and delete is same as create.
|
29
|
+
|
30
|
+
this module contains class method `index_setup` to setup index.
|
31
|
+
|
32
|
+
setup means create new index and put alias to new index and sync whole records of RDB with new index,
|
33
|
+
then you can use new index without any command.
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
XXXXX.index_setup
|
37
|
+
```
|
38
|
+
|
39
|
+
### usable methods
|
40
|
+
if your Model class included `ElasticArSync::Elastic::Syncable`, it can use class methods below.
|
41
|
+
|
42
|
+
- index_setup (class method)
|
43
|
+
→ to setup index to elsticsearch
|
44
|
+
|
45
|
+
- create_index (class method)
|
46
|
+
→ to create new index to elasticsearch
|
47
|
+
|
48
|
+
- delete_index({index_name}) (class method)
|
49
|
+
→ to delete index. needed argument index name which you delete
|
50
|
+
|
51
|
+
- import_all_record(index_name) (class method)
|
52
|
+
→ to sync whole record with elasticsearch
|
53
|
+
|
54
|
+
- switch_alias(new_index_name) (class method)
|
55
|
+
→ to switch using index, you needed create more than two index
|
56
|
+
|
57
|
+
- mapping_list_keys (class method)
|
58
|
+
→ to get mapping list array of your index
|
59
|
+
|
60
|
+
- get_aliases (class method)
|
61
|
+
→ to get aliase list array which you created
|
62
|
+
|
63
|
+
### Customize
|
64
|
+
if you change process of after commit, you can override instance methods below.
|
65
|
+
default is provided by `ElasticArSync::Elastic::Worker::IndexWorker`
|
66
|
+
|
67
|
+
- document_sync_create
|
68
|
+
→ for callback of create
|
69
|
+
|
70
|
+
- document_sync_update
|
71
|
+
→ for callback of update
|
72
|
+
|
73
|
+
- document_sync_delete
|
74
|
+
→ for callback of destroy
|
75
|
+
|
76
|
+
if you define original mapping of index like type, attribute, etc,
|
77
|
+
you can use `index_config` after `include ElasticArSync::Elastic::Syncable`.
|
78
|
+
|
79
|
+
example below
|
80
|
+
|
81
|
+
you can override args `dynamic`, `number_of_shards`, `attr_mappings`.
|
82
|
+
especially default mapping is whole attributes of your Model class, so you can customize mapping attributes by overriding attr_mappings.
|
83
|
+
```ruby
|
84
|
+
index_config(dynamic: 'false', number_of_shards: 1, attr_mappings: { id: 'integer', name: 'text', birth: 'date' })
|
85
|
+
```
|
86
|
+
|
87
|
+
you can also override part of attributes by `override_mappings` like below
|
88
|
+
|
89
|
+
*As a premise, your model contain [id: integer, name: string, birth: date]
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
index_config(override_mappings: { birth: :keyword })
|
93
|
+
```
|
94
|
+
|
95
|
+
then birth will mapping as keyword type in index of elasticsearch.
|
96
|
+
|
97
|
+
if you want add mapping which is not defined as schema column of model, you can save method result as well like below.
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
index_config(override_mappings: { full_name: :keyword })
|
101
|
+
```
|
102
|
+
|
103
|
+
*`full_name` is instance method
|
104
|
+
|
105
|
+
then full_name will mapped to index mapping as keyword type. but you have to define instance method full_name to your model class.
|
106
|
+
|
107
|
+
if you define your original mapping additionally, you can also define like below.
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
settings index: { number_of_shards: number_of_shards } do
|
111
|
+
mappings do
|
112
|
+
indexes key, type: value
|
113
|
+
end
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
117
|
+
tips: just reference here.
|
118
|
+
|
119
|
+
https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model
|
6
120
|
|
7
121
|
## Installation
|
8
122
|
Add this line to your application's Gemfile:
|
@@ -37,7 +37,9 @@ module ElasticArSync
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def as_indexed_json(_option = {})
|
40
|
-
|
40
|
+
hash = {}
|
41
|
+
self.class.mapping_list_keys.each { |key| hash[key.to_s] = send(key) }
|
42
|
+
hash.as_json
|
41
43
|
end
|
42
44
|
end
|
43
45
|
|
@@ -67,24 +69,40 @@ module ElasticArSync
|
|
67
69
|
ElasticArSync::Elastic::Services::IndexHandler.new(self).switch_alias(alias_name: index_name, new_index_name: new_index_name)
|
68
70
|
end
|
69
71
|
|
70
|
-
def index_config(dynamic: 'false',
|
71
|
-
|
72
|
+
def index_config(dynamic: 'false', override_settings: {}, attr_mappings: default_index_mapping, override_mappings: {})
|
73
|
+
attr_mappings.merge!(override_mappings) if override_mappings.present?
|
74
|
+
|
75
|
+
settings default_index_setting.merge!(override_settings) do
|
72
76
|
# ES6からStringが使えないのでtextかkeywordにする。
|
73
77
|
mappings dynamic: dynamic do
|
74
78
|
attr_mappings.each do |key, value|
|
75
|
-
|
79
|
+
next unless value.is_a?(Hash)
|
80
|
+
|
81
|
+
if value[:type].to_s == 'nested'
|
82
|
+
indexes key ,value.reject { |k, _| k.to_sym == :nested_attr } do
|
83
|
+
value[:nested_attr].each { |key, value| indexes key, value }
|
84
|
+
end
|
85
|
+
next
|
86
|
+
end
|
87
|
+
|
88
|
+
indexes key, value
|
76
89
|
end
|
77
90
|
end
|
78
91
|
end
|
79
92
|
end
|
80
93
|
|
94
|
+
def default_index_setting
|
95
|
+
{ index: { number_of_shards: 1 } }
|
96
|
+
end
|
97
|
+
|
81
98
|
def default_index_mapping
|
82
99
|
mapping = {}
|
83
100
|
attribute_types.each do |attribute, active_model_type|
|
84
101
|
type = active_model_type.type
|
85
|
-
type = :text if (active_model_type.type.to_sym == :string)
|
102
|
+
type = :text if (active_model_type.type.to_sym == :string)
|
103
|
+
type = :keyword if type == :integer && defined_enums.symbolize_keys.keys.include?(attribute.to_sym)
|
86
104
|
type = :date if active_model_type.type == :datetime
|
87
|
-
mapping[attribute.to_sym] = type
|
105
|
+
mapping[attribute.to_sym] = { type: type }
|
88
106
|
end
|
89
107
|
mapping
|
90
108
|
end
|
@@ -100,6 +118,14 @@ module ElasticArSync
|
|
100
118
|
raise Elasticsearch::Transport::Transport::Errors::NotFound, "インデックスがありません alias_name: #{alias_name}"
|
101
119
|
end
|
102
120
|
end
|
121
|
+
|
122
|
+
def current_index
|
123
|
+
get_aliases.select { |_, value| value["aliases"].present? }.keys.first
|
124
|
+
end
|
125
|
+
|
126
|
+
def current_mapping
|
127
|
+
__elasticsearch__.client.indices.get_mapping[current_index]["mappings"]["_doc"]["properties"]
|
128
|
+
end
|
103
129
|
end
|
104
130
|
end
|
105
131
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elastic_ar_sync
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- KitakatsuTed
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|