elastic_ar_sync 0.1.2 → 0.1.8
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 +36 -4
- data/lib/elastic_ar_sync/elastic/syncable.rb +49 -7
- 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: 53be1f275059f78080af2ed902fbbf5d10af6fcf62b31b189fe22294b9623742
|
4
|
+
data.tar.gz: ef20e4a2d9ee446e9ea741b6758c4609a3f4f015ec0e5815435fa706b9cc2beb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90c15465ed3b20a2055e10cf09c1d6719c5d6c21d7ae070f373e5afbe04f1053fc0d4eda106a905b9d21ae80a7a19e4f2bae1d5d40453abb523f3f8e964bf7c1
|
7
|
+
data.tar.gz: 814e9fb8c93ab4532d546d61f1063f134b4448154be246089cdf45291f0108ac4c1485a3a7853be38f3bde1265c3b7eb65ad3d81028b479c3a7bc536f3f7506e
|
data/README.md
CHANGED
@@ -9,11 +9,16 @@ Install `gem elasticsearch-rails`, `gem elasticsearch-model` and `gem elasticsea
|
|
9
9
|
|
10
10
|
Include module `ElasticArSync::Elastic::Syncable` on your Model class which is inherited ActiveRecord.
|
11
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
|
+
|
12
16
|
example below
|
13
17
|
|
14
18
|
```ruby
|
15
19
|
class XXXXX < ActiveRecord
|
16
20
|
include ElasticArSync::Elastic::Syncable
|
21
|
+
index_config
|
17
22
|
end
|
18
23
|
```
|
19
24
|
|
@@ -24,6 +29,13 @@ update and delete is same as create.
|
|
24
29
|
|
25
30
|
this module contains class method `index_setup` to setup index.
|
26
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
|
+
|
27
39
|
### usable methods
|
28
40
|
if your Model class included `ElasticArSync::Elastic::Syncable`, it can use class methods below.
|
29
41
|
|
@@ -66,17 +78,34 @@ you can use `index_config` after `include ElasticArSync::Elastic::Syncable`.
|
|
66
78
|
|
67
79
|
example below
|
68
80
|
|
69
|
-
you can override
|
81
|
+
you can override args `dynamic`, `number_of_shards`, `attr_mappings`.
|
70
82
|
especially default mapping is whole attributes of your Model class, so you can customize mapping attributes by overriding attr_mappings.
|
71
83
|
```ruby
|
72
84
|
index_config(dynamic: 'false', number_of_shards: 1, attr_mappings: { id: 'integer', name: 'text', birth: 'date' })
|
73
85
|
```
|
74
86
|
|
75
|
-
|
87
|
+
you can also override part of attributes by `override_mappings` like below
|
76
88
|
|
77
|
-
|
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.
|
78
108
|
|
79
|
-
https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model
|
80
109
|
```ruby
|
81
110
|
settings index: { number_of_shards: number_of_shards } do
|
82
111
|
mappings do
|
@@ -85,6 +114,9 @@ https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model
|
|
85
114
|
end
|
86
115
|
```
|
87
116
|
|
117
|
+
tips: just reference here.
|
118
|
+
|
119
|
+
https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model
|
88
120
|
|
89
121
|
## Installation
|
90
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,30 +69,66 @@ 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', override_settings: {}, attr_mappings: default_index_mapping, override_mappings: {})
|
72
|
+
def index_config(dynamic: 'false', override_settings: {}, attr_mappings: default_index_mapping, override_mappings: {}, kuromoji_default: false)
|
71
73
|
attr_mappings.merge!(override_mappings) if override_mappings.present?
|
72
74
|
|
73
|
-
settings default_index_setting.merge!(override_settings) do
|
75
|
+
settings default_index_setting(kuromoji_default).merge!(override_settings) do
|
74
76
|
# ES6からStringが使えないのでtextかkeywordにする。
|
75
77
|
mappings dynamic: dynamic do
|
76
78
|
attr_mappings.each do |key, value|
|
77
79
|
next unless value.is_a?(Hash)
|
78
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
|
+
|
79
88
|
indexes key, value
|
80
89
|
end
|
81
90
|
end
|
82
91
|
end
|
83
92
|
end
|
84
93
|
|
85
|
-
def default_index_setting
|
86
|
-
{ index: { number_of_shards: 1 } }
|
94
|
+
def default_index_setting(kuromoji_default = false)
|
95
|
+
setting_attr = { index: { number_of_shards: 1 } }
|
96
|
+
setting_attr.merge!(ja_analyze_default) if kuromoji_default
|
97
|
+
setting_attr
|
98
|
+
end
|
99
|
+
|
100
|
+
def ja_analyze_default
|
101
|
+
{
|
102
|
+
"analysis": {
|
103
|
+
"analyzer": {
|
104
|
+
"normal_ja_analyzer": {
|
105
|
+
"type": "custom",
|
106
|
+
"tokenizer": "kuromoji_tokenizer",
|
107
|
+
"mode": "search",
|
108
|
+
"char_filter": [
|
109
|
+
"icu_normalizer",
|
110
|
+
"kuromoji_iteration_mark"
|
111
|
+
],
|
112
|
+
"filter": [
|
113
|
+
"kuromoji_baseform",
|
114
|
+
"kuromoji_part_of_speech",
|
115
|
+
"ja_stop",
|
116
|
+
"lowercase",
|
117
|
+
"kuromoji_number",
|
118
|
+
"kuromoji_stemmer"
|
119
|
+
]
|
120
|
+
}
|
121
|
+
}
|
122
|
+
}
|
123
|
+
}
|
87
124
|
end
|
88
125
|
|
89
126
|
def default_index_mapping
|
90
127
|
mapping = {}
|
91
128
|
attribute_types.each do |attribute, active_model_type|
|
92
129
|
type = active_model_type.type
|
93
|
-
type = :text if (active_model_type.type.to_sym == :string)
|
130
|
+
type = :text if (active_model_type.type.to_sym == :string)
|
131
|
+
type = :keyword if type == :integer && defined_enums.symbolize_keys.keys.include?(attribute.to_sym)
|
94
132
|
type = :date if active_model_type.type == :datetime
|
95
133
|
mapping[attribute.to_sym] = { type: type }
|
96
134
|
end
|
@@ -110,12 +148,16 @@ module ElasticArSync
|
|
110
148
|
end
|
111
149
|
|
112
150
|
def current_index
|
113
|
-
|
151
|
+
__elasticsearch__.client.indices.get_alias(index: index_name).keys.first
|
114
152
|
end
|
115
153
|
|
116
154
|
def current_mapping
|
117
155
|
__elasticsearch__.client.indices.get_mapping[current_index]["mappings"]["_doc"]["properties"]
|
118
156
|
end
|
157
|
+
|
158
|
+
def current_settings
|
159
|
+
__elasticsearch__.client.indices.get_settings[current_index]
|
160
|
+
end
|
119
161
|
end
|
120
162
|
end
|
121
163
|
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.8
|
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
|