elastic_ar_sync 0.1.6 → 0.1.9

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: 892ccfa7c17e4a02cf8c774eec3ec71eb6dfaf16006052e0d82381b4aec48bb5
4
- data.tar.gz: 1c948f281659aec4a29bdbb28471a55127f3fab23080fca7b63fb43454b3d140
3
+ metadata.gz: bce44a711b1391e382ac02278af2203b1a0cc304f161c6d3f1c6b68f2a3c24b2
4
+ data.tar.gz: 1f356f4086089f0a7edff3b9355fc35a31a89ad3bc98e01080939c62cca18618
5
5
  SHA512:
6
- metadata.gz: 4524209a6682d5bf32f8543d54e138b0eb0e271a2ae04e417a8fa0ee905ac32788dfbd92300b2c5431ad54274222935b20b0268e71b37e542c8545e75e21eb46
7
- data.tar.gz: cbd985be6b920d6c0f9e910a636ab391261e9187d9d56dce6323906be4cc1d8482e8028f489e2eb355e1cf4e6500cab301368a9ede05434c6636965ab3b0329c
6
+ metadata.gz: 92a604d4d3e7fcdf6645317c8dbfb4dc2633b5994c8523c2ab66eb773dfc5cca4dee3a82d1d5339208d947a05a6e17a6582d52f1985038dfebb371cf1edbe900
7
+ data.tar.gz: 4fd9baff81fd0707100e12a9c8d56048d788e4cea1e1b9b9cdb2cc80020fc1b1eb1dac39e4b78549bcd646deb60b9412a8843f05b3d0f107ca9cc2e141f45f10
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # ElasticArSync
2
2
  This repository contains easy set up modules for RDB and elasticsearch index with Active Record.
3
3
  This is based on `gem elasticsearch-rails`.
4
+
5
+ 日本語での説明はこちら
6
+ https://ted-tech.hateblo.jp/entry/2020/08/11/103155
7
+
4
8
  ## Usage
5
9
  ### Preparation
6
10
  Install `gem sidekiq` and `gem redis-rails` to use Asynchronous processing for your app.
@@ -13,27 +13,27 @@ module ElasticArSync
13
13
  # after_commitでRDBを操作した時にESのインデックスも同期させる
14
14
  # アプリのサーバーに負荷をかけ無いように非同期で実行させる
15
15
  after_commit on: [:create] do
16
- document_sync_create(self.class, id)
16
+ document_sync_create(self.class.to_s, id)
17
17
  end
18
18
 
19
19
  after_commit on: [:update] do
20
- document_sync_update(self.class, id)
20
+ document_sync_update(self.class.to_s, id)
21
21
  end
22
22
 
23
23
  after_commit on: [:destroy] do
24
- document_sync_delete(self.class, id)
24
+ document_sync_delete(self.class.to_s, id)
25
25
  end
26
26
 
27
- def document_sync_create(klass, record_id)
28
- ElasticArSync::Elastic::Worker::IndexWorker.perform_async(klass, :index, record_id)
27
+ def document_sync_create(klass_str, record_id)
28
+ ElasticArSync::Elastic::Worker::IndexWorker.perform_async(klass_str, :index, record_id)
29
29
  end
30
30
 
31
- def document_sync_update(klass, record_id)
32
- ElasticArSync::Elastic::Worker::IndexWorker.perform_async(klass, :index, record_id)
31
+ def document_sync_update(klass_str, record_id)
32
+ ElasticArSync::Elastic::Worker::IndexWorker.perform_async(klass_str, :index, record_id)
33
33
  end
34
34
 
35
- def document_sync_delete(klass, record_id)
36
- ElasticArSync::Elastic::Worker::IndexWorker.perform_async(klass, :delete, record_id)
35
+ def document_sync_delete(klass_str, record_id)
36
+ ElasticArSync::Elastic::Worker::IndexWorker.perform_async(klass_str, :delete, record_id)
37
37
  end
38
38
 
39
39
  def as_indexed_json(_option = {})
@@ -60,7 +60,7 @@ module ElasticArSync
60
60
 
61
61
  # DBの内容をESのインデックスに同期する
62
62
  def import_all_record(target_index: ,batch_size: 100)
63
- ElasticArSync::Elastic::Worker::IndexImportWorker.perform_async(self, target_index, batch_size)
63
+ ElasticArSync::Elastic::Worker::IndexImportWorker.perform_async(self.to_s, target_index, batch_size)
64
64
  end
65
65
 
66
66
  # ダウンタイムなしでインデックスを切り替える
@@ -69,10 +69,10 @@ module ElasticArSync
69
69
  ElasticArSync::Elastic::Services::IndexHandler.new(self).switch_alias(alias_name: index_name, new_index_name: new_index_name)
70
70
  end
71
71
 
72
- 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)
73
73
  attr_mappings.merge!(override_mappings) if override_mappings.present?
74
74
 
75
- settings default_index_setting.merge!(override_settings) do
75
+ settings default_index_setting(kuromoji_default).merge!(override_settings) do
76
76
  # ES6からStringが使えないのでtextかkeywordにする。
77
77
  mappings dynamic: dynamic do
78
78
  attr_mappings.each do |key, value|
@@ -91,8 +91,36 @@ module ElasticArSync
91
91
  end
92
92
  end
93
93
 
94
- def default_index_setting
95
- { 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
+ }
96
124
  end
97
125
 
98
126
  def default_index_mapping
@@ -120,12 +148,16 @@ module ElasticArSync
120
148
  end
121
149
 
122
150
  def current_index
123
- get_aliases.select { |_, value| value["aliases"].present? }.keys.first
151
+ __elasticsearch__.client.indices.get_alias(index: index_name).keys.first
124
152
  end
125
153
 
126
154
  def current_mapping
127
155
  __elasticsearch__.client.indices.get_mapping[current_index]["mappings"]["_doc"]["properties"]
128
156
  end
157
+
158
+ def current_settings
159
+ __elasticsearch__.client.indices.get_settings[current_index]
160
+ end
129
161
  end
130
162
  end
131
163
  end
@@ -2,11 +2,11 @@ class ElasticArSync::Elastic::Worker::IndexImportWorker
2
2
  include Sidekiq::Worker
3
3
  sidekiq_options queue: :elasticsearch, retry: false
4
4
 
5
- def perform(klass, target_index, batch_size)
5
+ def perform(klass_str, target_index, batch_size)
6
6
  Rails.logger.debug "[elastic IndexImportWorker] start import #{target_index}"
7
7
 
8
8
  begin
9
- ElasticArSync::Elastic::Services::IndexHandler.new(Object.const_get(klass)).import_all_record(target_index, batch_size)
9
+ ElasticArSync::Elastic::Services::IndexHandler.new(Object.const_get(klass_str)).import_all_record(target_index, batch_size)
10
10
  rescue => e
11
11
  Rails.logger.debug "[elastic IndexImportWorker] error occur #{target_index} \n #{e.message}"
12
12
  end
@@ -2,8 +2,8 @@ class ElasticArSync::Elastic::Worker::IndexWorker
2
2
  include Sidekiq::Worker
3
3
  sidekiq_options queue: :elasticsearch, retry: false
4
4
 
5
- def perform(klass, operation, record_id)
6
- Rails.logger.debug "[elasticsearch IndexWorker] operation: #{operation} #{klass} ID: #{record_id}"
7
- ElasticArSync::Elastic::Services::DocumentIndexer.new.index_document(klass, operation, record_id)
5
+ def perform(klass_str, operation, record_id)
6
+ Rails.logger.debug "[elasticsearch IndexWorker] operation: #{operation} #{klass_str} ID: #{record_id}"
7
+ ElasticArSync::Elastic::Services::DocumentIndexer.new.index_document(klass_str.constantize, operation, record_id)
8
8
  end
9
9
  end
@@ -1,3 +1,3 @@
1
1
  module ElasticArSync
2
- VERSION = '0.1.6'
2
+ VERSION = '0.1.9'
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.6
4
+ version: 0.1.9
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-14 00:00:00.000000000 Z
11
+ date: 2022-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails