elastic_ar_sync 0.1.1 → 0.1.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 35a997c8ae813e7a3e34ab3d5dc557e5282322078416597e85608aa34b33c18a
4
- data.tar.gz: 3f0785cd76f8ca81283119fc6ddc04262f52275645db6986aab779fd1b4c2259
3
+ metadata.gz: cf6c9a44b983540db39c6a9542af4a731d49975ca4f1b12f46911eb1e1dd98d0
4
+ data.tar.gz: 40906b515c533360c1eb36301c22a8325dcb3570cd87a7aad6728641456f60c3
5
5
  SHA512:
6
- metadata.gz: f1ec8db00e1d659cf7884db47befbf49cd4a3b087b37c74ad1352c5510dd2b6667a469a36bad3d620549325c38307dcbcd08c725ae7e42a05ca349183db42bff
7
- data.tar.gz: 14d0ec670343d14d8d399ad3dcb00bca90987593a545f1d5dad888d1543ebfc180d618e1da95793aaf930c2910f42d2d6b34e50ec0bd8543147ef5371f2b3c07
6
+ metadata.gz: 625d2d7cbfc2c08bada8d98cf63302ea3f2bd7b3951696b21ceb3f1a9ff2b87ed1c826904a7e6031ddf0a2bb52eee57efd65d3c7480e2b01b12ba59b78d30670
7
+ data.tar.gz: b614f8ce783f01c6c32f68b11276668d31586da5aa6025a7e08db5e12070b53a2c10d418cd1b52711221ce8f341f8babaa8f06ad2908d608c097586eb8ad40bd
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 argus `dynamic`, `number_of_shards`, `attr_mappings`.
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
- if you define your original mapping additionally, you can define like below.
87
+ you can also override part of attributes by `override_mappings` like below
76
88
 
77
- tips: just reference here.
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
- attributes.symbolize_keys.select { |key, _| self.class.mapping_list_keys.include?(key) }
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,68 @@ 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', number_of_shards: 1, attr_mappings: default_index_mapping)
71
- settings index: { number_of_shards: number_of_shards } do
72
+ def index_config(dynamic: 'false', override_settings: {}, attr_mappings: default_index_mapping, override_mappings: {}, kuromoji_default: false)
73
+ attr_mappings.merge!(override_mappings) if override_mappings.present?
74
+
75
+ settings default_index_setting(kuromoji_default).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
- indexes key, type: value
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(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": "kuromiji_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
+ }
124
+ end
125
+
81
126
  def default_index_mapping
82
127
  mapping = {}
83
128
  attribute_types.each do |attribute, active_model_type|
84
129
  type = active_model_type.type
85
- type = :text if (active_model_type.type.to_sym == :string) || (type == :integer && defined_enums.symbolize_keys.keys.include?(attribute.to_sym))
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)
86
132
  type = :date if active_model_type.type == :datetime
87
- mapping[attribute.to_sym] = type
133
+ mapping[attribute.to_sym] = { type: type }
88
134
  end
89
135
  mapping
90
136
  end
@@ -100,6 +146,14 @@ module ElasticArSync
100
146
  raise Elasticsearch::Transport::Transport::Errors::NotFound, "インデックスがありません alias_name: #{alias_name}"
101
147
  end
102
148
  end
149
+
150
+ def current_index
151
+ get_aliases.select { |_, value| value["aliases"].present? }.keys.first
152
+ end
153
+
154
+ def current_mapping
155
+ __elasticsearch__.client.indices.get_mapping[current_index]["mappings"]["_doc"]["properties"]
156
+ end
103
157
  end
104
158
  end
105
159
  end
@@ -1,3 +1,3 @@
1
1
  module ElasticArSync
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.7'
3
3
  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.1
4
+ version: 0.1.7
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-07 00:00:00.000000000 Z
11
+ date: 2020-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails