chewy 0.8.4 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +34 -0
- data/.rubocop_todo.yml +44 -0
- data/.travis.yml +20 -60
- data/Appraisals +15 -40
- data/CHANGELOG.md +42 -0
- data/Gemfile +1 -0
- data/Guardfile +5 -5
- data/README.md +155 -6
- data/Rakefile +11 -1
- data/chewy.gemspec +5 -7
- data/gemfiles/rails.3.2.activerecord.gemfile +1 -0
- data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +1 -0
- data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +1 -0
- data/gemfiles/rails.4.2.activerecord.gemfile +1 -0
- data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +1 -0
- data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +1 -0
- data/gemfiles/{rails.4.2.mongoid.4.0.0.gemfile → rails.4.2.mongoid.4.0.gemfile} +2 -1
- data/gemfiles/{rails.4.2.mongoid.4.0.0.kaminari.gemfile → rails.4.2.mongoid.4.0.kaminari.gemfile} +2 -1
- data/gemfiles/{rails.4.2.mongoid.4.0.0.will_paginate.gemfile → rails.4.2.mongoid.4.0.will_paginate.gemfile} +2 -1
- data/gemfiles/{rails.4.2.mongoid.5.1.0.gemfile → rails.4.2.mongoid.5.1.gemfile} +2 -1
- data/gemfiles/{rails.4.2.mongoid.5.1.0.kaminari.gemfile → rails.4.2.mongoid.5.1.kaminari.gemfile} +2 -1
- data/gemfiles/{rails.4.2.mongoid.5.1.0.will_paginate.gemfile → rails.4.2.mongoid.5.1.will_paginate.gemfile} +2 -1
- data/gemfiles/{rails.4.0.activerecord.gemfile → rails.5.0.activerecord.gemfile} +4 -2
- data/gemfiles/{rails.4.0.mongoid.4.0.0.kaminari.gemfile → rails.5.0.activerecord.kaminari.gemfile} +4 -2
- data/gemfiles/{rails.4.0.mongoid.4.0.0.will_paginate.gemfile → rails.5.0.activerecord.will_paginate.gemfile} +4 -2
- data/gemfiles/{sequel.4.31.gemfile → sequel.4.38.gemfile} +3 -2
- data/lib/chewy.rb +24 -16
- data/lib/chewy/backports/deep_dup.rb +1 -1
- data/lib/chewy/backports/duplicable.rb +1 -0
- data/lib/chewy/config.rb +13 -7
- data/lib/chewy/errors.rb +4 -4
- data/lib/chewy/fields/base.rb +19 -14
- data/lib/chewy/fields/root.rb +11 -9
- data/lib/chewy/index.rb +38 -25
- data/lib/chewy/index/actions.rb +17 -17
- data/lib/chewy/index/settings.rb +3 -4
- data/lib/chewy/journal.rb +107 -0
- data/lib/chewy/journal/apply.rb +31 -0
- data/lib/chewy/journal/clean.rb +24 -0
- data/lib/chewy/journal/entry.rb +83 -0
- data/lib/chewy/journal/query.rb +87 -0
- data/lib/chewy/log_subscriber.rb +8 -8
- data/lib/chewy/minitest.rb +1 -0
- data/lib/chewy/minitest/helpers.rb +77 -0
- data/lib/chewy/minitest/search_index_receiver.rb +80 -0
- data/lib/chewy/query.rb +116 -60
- data/lib/chewy/query/compose.rb +5 -6
- data/lib/chewy/query/criteria.rb +26 -16
- data/lib/chewy/query/filters.rb +9 -9
- data/lib/chewy/query/loading.rb +2 -2
- data/lib/chewy/query/nodes/and.rb +3 -3
- data/lib/chewy/query/nodes/base.rb +1 -1
- data/lib/chewy/query/nodes/bool.rb +6 -4
- data/lib/chewy/query/nodes/equal.rb +6 -6
- data/lib/chewy/query/nodes/exists.rb +2 -2
- data/lib/chewy/query/nodes/expr.rb +2 -2
- data/lib/chewy/query/nodes/field.rb +35 -31
- data/lib/chewy/query/nodes/has_child.rb +1 -0
- data/lib/chewy/query/nodes/has_parent.rb +1 -0
- data/lib/chewy/query/nodes/has_relation.rb +11 -13
- data/lib/chewy/query/nodes/match_all.rb +1 -1
- data/lib/chewy/query/nodes/missing.rb +2 -2
- data/lib/chewy/query/nodes/not.rb +3 -3
- data/lib/chewy/query/nodes/or.rb +3 -3
- data/lib/chewy/query/nodes/prefix.rb +4 -3
- data/lib/chewy/query/nodes/query.rb +3 -3
- data/lib/chewy/query/nodes/range.rb +11 -11
- data/lib/chewy/query/nodes/raw.rb +1 -1
- data/lib/chewy/query/nodes/regexp.rb +15 -11
- data/lib/chewy/query/nodes/script.rb +6 -6
- data/lib/chewy/query/pagination/will_paginate.rb +2 -2
- data/lib/chewy/railtie.rb +3 -3
- data/lib/chewy/rake_helper.rb +51 -30
- data/lib/chewy/repository.rb +2 -2
- data/lib/chewy/rspec.rb +1 -1
- data/lib/chewy/rspec/update_index.rb +46 -47
- data/lib/chewy/runtime/version.rb +4 -4
- data/lib/chewy/search.rb +7 -5
- data/lib/chewy/strategy.rb +10 -8
- data/lib/chewy/strategy/atomic.rb +2 -2
- data/lib/chewy/strategy/base.rb +4 -4
- data/lib/chewy/strategy/bypass.rb +1 -2
- data/lib/chewy/strategy/sidekiq.rb +2 -0
- data/lib/chewy/strategy/urgent.rb +1 -1
- data/lib/chewy/type.rb +51 -45
- data/lib/chewy/type/adapter/active_record.rb +23 -12
- data/lib/chewy/type/adapter/base.rb +4 -4
- data/lib/chewy/type/adapter/mongoid.rb +6 -6
- data/lib/chewy/type/adapter/object.rb +15 -12
- data/lib/chewy/type/adapter/orm.rb +24 -15
- data/lib/chewy/type/adapter/sequel.rb +11 -7
- data/lib/chewy/type/crutch.rb +4 -3
- data/lib/chewy/type/import.rb +51 -32
- data/lib/chewy/type/mapping.rb +17 -17
- data/lib/chewy/type/observe.rb +9 -7
- data/lib/chewy/type/witchcraft.rb +62 -23
- data/lib/chewy/type/wrapper.rb +20 -14
- data/lib/chewy/version.rb +1 -1
- data/lib/generators/chewy/install_generator.rb +3 -3
- data/lib/tasks/chewy.rake +28 -23
- data/spec/chewy/config_spec.rb +33 -12
- data/spec/chewy/fields/base_spec.rb +143 -154
- data/spec/chewy/fields/root_spec.rb +22 -20
- data/spec/chewy/fields/time_fields_spec.rb +11 -9
- data/spec/chewy/index/actions_spec.rb +27 -4
- data/spec/chewy/index/aliases_spec.rb +2 -2
- data/spec/chewy/index/settings_spec.rb +72 -50
- data/spec/chewy/index_spec.rb +95 -43
- data/spec/chewy/journal/apply_spec.rb +120 -0
- data/spec/chewy/journal/entry_spec.rb +237 -0
- data/spec/chewy/journal_spec.rb +173 -0
- data/spec/chewy/minitest/helpers_spec.rb +90 -0
- data/spec/chewy/minitest/search_index_receiver_spec.rb +120 -0
- data/spec/chewy/query/criteria_spec.rb +504 -237
- data/spec/chewy/query/filters_spec.rb +94 -66
- data/spec/chewy/query/loading_spec.rb +76 -40
- data/spec/chewy/query/nodes/and_spec.rb +3 -7
- data/spec/chewy/query/nodes/bool_spec.rb +5 -13
- data/spec/chewy/query/nodes/equal_spec.rb +20 -20
- data/spec/chewy/query/nodes/exists_spec.rb +7 -7
- data/spec/chewy/query/nodes/has_child_spec.rb +42 -23
- data/spec/chewy/query/nodes/has_parent_spec.rb +42 -23
- data/spec/chewy/query/nodes/match_all_spec.rb +2 -2
- data/spec/chewy/query/nodes/missing_spec.rb +6 -5
- data/spec/chewy/query/nodes/not_spec.rb +3 -7
- data/spec/chewy/query/nodes/or_spec.rb +3 -7
- data/spec/chewy/query/nodes/prefix_spec.rb +6 -6
- data/spec/chewy/query/nodes/query_spec.rb +3 -3
- data/spec/chewy/query/nodes/range_spec.rb +19 -19
- data/spec/chewy/query/nodes/raw_spec.rb +2 -2
- data/spec/chewy/query/nodes/regexp_spec.rb +31 -19
- data/spec/chewy/query/nodes/script_spec.rb +5 -5
- data/spec/chewy/query/pagination/kaminari_spec.rb +2 -2
- data/spec/chewy/query/pagination/will_paginage_spec.rb +6 -7
- data/spec/chewy/query/pagination_spec.rb +2 -3
- data/spec/chewy/query_spec.rb +208 -145
- data/spec/chewy/repository_spec.rb +8 -8
- data/spec/chewy/rspec/update_index_spec.rb +180 -111
- data/spec/chewy/search_spec.rb +8 -8
- data/spec/chewy/strategy/active_job_spec.rb +2 -2
- data/spec/chewy/strategy/atomic_spec.rb +4 -1
- data/spec/chewy/strategy/resque_spec.rb +2 -2
- data/spec/chewy/strategy/sidekiq_spec.rb +2 -2
- data/spec/chewy/type/actions_spec.rb +1 -1
- data/spec/chewy/type/adapter/active_record_spec.rb +255 -149
- data/spec/chewy/type/adapter/mongoid_spec.rb +169 -108
- data/spec/chewy/type/adapter/object_spec.rb +56 -40
- data/spec/chewy/type/adapter/sequel_spec.rb +248 -163
- data/spec/chewy/type/import_spec.rb +78 -47
- data/spec/chewy/type/mapping_spec.rb +6 -6
- data/spec/chewy/type/observe_spec.rb +20 -14
- data/spec/chewy/type/witchcraft_spec.rb +89 -43
- data/spec/chewy/type_spec.rb +4 -3
- data/spec/chewy_spec.rb +10 -8
- data/spec/spec_helper.rb +3 -0
- data/spec/support/active_record.rb +1 -1
- data/spec/support/class_helpers.rb +10 -11
- data/spec/support/mongoid.rb +2 -2
- data/spec/support/sequel.rb +1 -1
- metadata +65 -35
- data/gemfiles/rails.4.0.activerecord.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.0.activerecord.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.0.mongoid.4.0.0.gemfile +0 -15
- data/gemfiles/rails.4.0.mongoid.5.1.0.gemfile +0 -15
- data/gemfiles/rails.4.0.mongoid.5.1.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.0.mongoid.5.1.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.1.activerecord.gemfile +0 -15
- data/gemfiles/rails.4.1.activerecord.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.1.activerecord.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.4.0.0.gemfile +0 -15
- data/gemfiles/rails.4.1.mongoid.4.0.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.4.0.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.5.1.0.gemfile +0 -15
- data/gemfiles/rails.4.1.mongoid.5.1.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.5.1.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.5.0.0.beta3.activerecord.gemfile +0 -16
- data/gemfiles/rails.5.0.0.beta3.activerecord.kaminari.gemfile +0 -16
- data/gemfiles/rails.5.0.0.beta3.activerecord.will_paginate.gemfile +0 -15
data/lib/chewy/errors.rb
CHANGED
@@ -12,7 +12,7 @@ module Chewy
|
|
12
12
|
end
|
13
13
|
|
14
14
|
class UndefinedUpdateStrategy < Error
|
15
|
-
def initialize
|
15
|
+
def initialize(_type)
|
16
16
|
super <<-MESSAGE
|
17
17
|
Index update strategy is undefined in current context.
|
18
18
|
Please wrap your code with `Chewy.strategy(:strategy_name) block.`
|
@@ -24,11 +24,11 @@ Please wrap your code with `Chewy.strategy(:strategy_name) block.`
|
|
24
24
|
end
|
25
25
|
|
26
26
|
class ImportFailed < Error
|
27
|
-
def initialize
|
27
|
+
def initialize(type, import_errors)
|
28
28
|
message = "Import failed for `#{type}` with:\n"
|
29
|
-
|
29
|
+
import_errors.each do |action, action_errors|
|
30
30
|
message << " #{action.to_s.humanize} errors:\n"
|
31
|
-
|
31
|
+
action_errors.each do |error, documents|
|
32
32
|
message << " `#{error}`\n"
|
33
33
|
message << " on #{documents.count} documents: #{documents}\n"
|
34
34
|
end
|
data/lib/chewy/fields/base.rb
CHANGED
@@ -5,7 +5,8 @@ module Chewy
|
|
5
5
|
attr_accessor :parent
|
6
6
|
|
7
7
|
def initialize(name, options = {})
|
8
|
-
@name
|
8
|
+
@name = name.to_sym
|
9
|
+
@options = options.deep_symbolize_keys
|
9
10
|
@value = @options.delete(:value)
|
10
11
|
@children = []
|
11
12
|
end
|
@@ -15,23 +16,25 @@ module Chewy
|
|
15
16
|
end
|
16
17
|
|
17
18
|
def object_field?
|
18
|
-
(children.present? && options[:type].blank?) ||
|
19
|
+
(children.present? && options[:type].blank?) || %w(object nested).include?(options[:type].to_s)
|
19
20
|
end
|
20
21
|
|
21
22
|
def mappings_hash
|
22
|
-
mapping = children.present?
|
23
|
-
(multi_field? ? :fields : :properties) => children.map(&:mappings_hash).inject(:merge)
|
24
|
-
|
23
|
+
mapping = if children.present?
|
24
|
+
{ (multi_field? ? :fields : :properties) => children.map(&:mappings_hash).inject(:merge) }
|
25
|
+
else
|
26
|
+
{}
|
27
|
+
end
|
25
28
|
mapping.reverse_merge!(options)
|
26
29
|
mapping.reverse_merge!(type: (children.present? ? 'object' : 'string'))
|
27
|
-
{name => mapping}
|
30
|
+
{ name => mapping }
|
28
31
|
end
|
29
32
|
|
30
33
|
def compose(object, *parent_objects)
|
31
34
|
objects = ([object] + parent_objects.flatten).uniq
|
32
35
|
|
33
36
|
result = if value && value.is_a?(Proc)
|
34
|
-
if value.arity
|
37
|
+
if value.arity.zero?
|
35
38
|
object.instance_exec(&value)
|
36
39
|
elsif value.arity < 0
|
37
40
|
value.call(*object)
|
@@ -39,7 +42,7 @@ module Chewy
|
|
39
42
|
value.call(*objects.first(value.arity))
|
40
43
|
end
|
41
44
|
elsif object.is_a?(Hash)
|
42
|
-
if object.
|
45
|
+
if object.key?(name)
|
43
46
|
object[name]
|
44
47
|
else
|
45
48
|
object[name.to_s]
|
@@ -48,13 +51,15 @@ module Chewy
|
|
48
51
|
object.send(name)
|
49
52
|
end
|
50
53
|
|
51
|
-
|
52
|
-
result
|
53
|
-
|
54
|
-
|
55
|
-
|
54
|
+
if children.present? && !multi_field?
|
55
|
+
result = if result.respond_to?(:to_ary)
|
56
|
+
result.to_ary.map { |item| compose_children(item, *objects) }
|
57
|
+
else
|
58
|
+
compose_children(result, *objects)
|
59
|
+
end
|
60
|
+
end
|
56
61
|
|
57
|
-
{name => result
|
62
|
+
{ name => result }
|
58
63
|
end
|
59
64
|
|
60
65
|
private
|
data/lib/chewy/fields/root.rb
CHANGED
@@ -12,11 +12,15 @@ module Chewy
|
|
12
12
|
@id = @options.delete(:id) || options.delete(:_id)
|
13
13
|
@parent = @options.delete(:parent) || options.delete(:_parent)
|
14
14
|
@parent_id = @options.delete(:parent_id)
|
15
|
-
@value ||= ->(
|
15
|
+
@value ||= ->(val) { val }
|
16
16
|
@dynamic_templates = []
|
17
17
|
@options.delete(:type)
|
18
18
|
end
|
19
19
|
|
20
|
+
def compose(*args)
|
21
|
+
super.as_json
|
22
|
+
end
|
23
|
+
|
20
24
|
def mappings_hash
|
21
25
|
mappings = super
|
22
26
|
mappings[name].delete(:type)
|
@@ -30,11 +34,11 @@ module Chewy
|
|
30
34
|
mappings
|
31
35
|
end
|
32
36
|
|
33
|
-
def dynamic_template
|
37
|
+
def dynamic_template(*args)
|
34
38
|
options = args.extract_options!.deep_symbolize_keys
|
35
39
|
if args.first
|
36
40
|
template_name = :"template_#{dynamic_templates.count.next}"
|
37
|
-
template = {template_name => {mapping: options}}
|
41
|
+
template = { template_name => { mapping: options } }
|
38
42
|
|
39
43
|
template[template_name][:match_mapping_type] = args.second.to_s if args.second.present?
|
40
44
|
|
@@ -52,15 +56,13 @@ module Chewy
|
|
52
56
|
end
|
53
57
|
|
54
58
|
def compose_parent(object)
|
55
|
-
|
56
|
-
|
57
|
-
end
|
59
|
+
return unless parent_id
|
60
|
+
parent_id.arity.zero? ? object.instance_exec(&parent_id) : parent_id.call(object)
|
58
61
|
end
|
59
62
|
|
60
63
|
def compose_id(object)
|
61
|
-
|
62
|
-
|
63
|
-
end
|
64
|
+
return unless id
|
65
|
+
id.arity.zero? ? object.instance_exec(&id) : id.call(object)
|
64
66
|
end
|
65
67
|
end
|
66
68
|
end
|
data/lib/chewy/index.rb
CHANGED
@@ -29,21 +29,25 @@ module Chewy
|
|
29
29
|
# UsersIndex.index_name # => 'dudes'
|
30
30
|
#
|
31
31
|
def self.index_name(suggest = nil)
|
32
|
+
raise UndefinedIndex unless _index_name(suggest)
|
33
|
+
@index_name ||= build_index_name(_index_name, prefix: default_prefix)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self._index_name(suggest = nil)
|
32
37
|
if suggest
|
33
|
-
@
|
34
|
-
|
35
|
-
@
|
36
|
-
build_index_name(
|
37
|
-
name.sub(/Index\Z/, '').demodulize.underscore,
|
38
|
-
prefix: default_prefix
|
39
|
-
) if name
|
40
|
-
end
|
38
|
+
@_index_name = suggest
|
39
|
+
elsif name
|
40
|
+
@_index_name ||= name.sub(/Index\Z/, '').demodulize.underscore
|
41
41
|
end
|
42
|
-
@
|
42
|
+
@_index_name
|
43
43
|
end
|
44
44
|
|
45
|
-
|
46
|
-
|
45
|
+
def self.derivable_index_name
|
46
|
+
@_derivable_index_name ||= name.sub(/Index\Z/, '').underscore
|
47
|
+
end
|
48
|
+
|
49
|
+
# Setups or returns pure Elasticsearch index name
|
50
|
+
# without any prefixes/suffixes
|
47
51
|
def self.default_prefix
|
48
52
|
Chewy.configuration[:prefix]
|
49
53
|
end
|
@@ -86,14 +90,13 @@ module Chewy
|
|
86
90
|
type_class = Chewy.create_type(self, target, options, &block)
|
87
91
|
self.type_hash = type_hash.merge(type_class.type_name => type_class)
|
88
92
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
end
|
93
|
+
return if respond_to?(type_class.type_name)
|
94
|
+
class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
95
|
+
def self.#{type_class.type_name}
|
96
|
+
ActiveSupport::Deprecation.warn("`#{self}.#{type_class.type_name}` accessor is deprecated and will be removed soon. Use `#{type_class}` directly instead.")
|
97
|
+
type_hash['#{type_class.type_name}']
|
98
|
+
end
|
99
|
+
METHOD
|
97
100
|
end
|
98
101
|
|
99
102
|
# Types method has double usage.
|
@@ -107,9 +110,9 @@ module Chewy
|
|
107
110
|
# UsersIndex.filters { name =~ 'ro' }.types(:admin, :manager)
|
108
111
|
# UsersIndex.types(:admin, :manager).filters { name =~ 'ro' } # the same as the first example
|
109
112
|
#
|
110
|
-
def self.types
|
113
|
+
def self.types(*args)
|
111
114
|
if args.present?
|
112
|
-
all.types
|
115
|
+
all.types(*args)
|
113
116
|
else
|
114
117
|
type_hash.values
|
115
118
|
end
|
@@ -123,6 +126,14 @@ module Chewy
|
|
123
126
|
type_hash.keys
|
124
127
|
end
|
125
128
|
|
129
|
+
# Returns named type:
|
130
|
+
#
|
131
|
+
# UserIndex.type('admin') # => UsersIndex::Admin
|
132
|
+
#
|
133
|
+
def self.type(type_name)
|
134
|
+
type_hash.fetch(type_name) { raise UndefinedType, "Unknown type in #{name}: #{type_name}" }
|
135
|
+
end
|
136
|
+
|
126
137
|
# Used as a part of index definition DSL. Defines settings:
|
127
138
|
#
|
128
139
|
# class UsersIndex < Chewy::Index
|
@@ -152,11 +163,13 @@ module Chewy
|
|
152
163
|
public_methods - Chewy::Index.public_methods - type_names.map(&:to_sym)
|
153
164
|
end
|
154
165
|
|
155
|
-
|
166
|
+
def self.journal?
|
167
|
+
types.any?(&:journal?)
|
168
|
+
end
|
156
169
|
|
157
|
-
def self.build_index_name
|
170
|
+
def self.build_index_name(*args)
|
158
171
|
options = args.extract_options!
|
159
|
-
[options[:prefix], args.first || index_name, options[:suffix]].reject(&:blank?).join(
|
172
|
+
[options[:prefix], args.first || index_name, options[:suffix]].reject(&:blank?).join('_')
|
160
173
|
end
|
161
174
|
|
162
175
|
def self.settings_hash
|
@@ -165,7 +178,7 @@ module Chewy
|
|
165
178
|
|
166
179
|
def self.mappings_hash
|
167
180
|
mappings = types.map(&:mappings_hash).inject(:merge)
|
168
|
-
mappings.present? ? {mappings: mappings} : {}
|
181
|
+
mappings.present? ? { mappings: mappings } : {}
|
169
182
|
end
|
170
183
|
|
171
184
|
def self.index_params
|
data/lib/chewy/index/actions.rb
CHANGED
@@ -9,7 +9,7 @@ module Chewy
|
|
9
9
|
module ClassMethods
|
10
10
|
# Checks index existance. Returns true or false
|
11
11
|
#
|
12
|
-
# UsersIndex.
|
12
|
+
# UsersIndex.exists? #=> true
|
13
13
|
#
|
14
14
|
def exists?
|
15
15
|
client.indices.exists(index: index_name)
|
@@ -30,8 +30,8 @@ module Chewy
|
|
30
30
|
# Suffixed index names might be used for zero-downtime mapping change, for example.
|
31
31
|
# Description: (http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/).
|
32
32
|
#
|
33
|
-
def create
|
34
|
-
create!
|
33
|
+
def create(*args)
|
34
|
+
create!(*args)
|
35
35
|
rescue Elasticsearch::Transport::Transport::Errors::BadRequest
|
36
36
|
false
|
37
37
|
end
|
@@ -52,13 +52,13 @@ module Chewy
|
|
52
52
|
# Suffixed index names might be used for zero-downtime mapping change, for example.
|
53
53
|
# Description: (http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/).
|
54
54
|
#
|
55
|
-
def create!
|
55
|
+
def create!(*args)
|
56
56
|
options = args.extract_options!.reverse_merge!(alias: true)
|
57
57
|
name = build_index_name(suffix: args.first)
|
58
58
|
|
59
59
|
if Chewy::Runtime.version >= 1.1
|
60
60
|
body = index_params
|
61
|
-
body
|
61
|
+
body[:aliases] = { index_name => {} } if options[:alias] && name != index_name
|
62
62
|
result = client.indices.create(index: name, body: body)
|
63
63
|
else
|
64
64
|
result = client.indices.create(index: name, body: index_params)
|
@@ -77,7 +77,7 @@ module Chewy
|
|
77
77
|
#
|
78
78
|
# UsersIndex.delete '01-2014' # deletes `users_01-2014` index
|
79
79
|
#
|
80
|
-
def delete
|
80
|
+
def delete(suffix = nil)
|
81
81
|
result = client.indices.delete index: build_index_name(suffix: suffix)
|
82
82
|
Chewy.wait_for_status if result
|
83
83
|
result
|
@@ -96,7 +96,7 @@ module Chewy
|
|
96
96
|
#
|
97
97
|
# UsersIndex.delete '01-2014' # deletes `users_01-2014` index
|
98
98
|
#
|
99
|
-
def delete!
|
99
|
+
def delete!(suffix = nil)
|
100
100
|
# es-ruby >= 1.0.10 handles Elasticsearch::Transport::Transport::Errors::NotFound
|
101
101
|
# by itself, so it is raised here
|
102
102
|
delete(suffix) or raise Elasticsearch::Transport::Transport::Errors::NotFound
|
@@ -108,7 +108,7 @@ module Chewy
|
|
108
108
|
# UsersIndex.purge # deletes and creates `users` index
|
109
109
|
# UsersIndex.purge '01-2014' # deletes `users` and `users_01-2014` indexes, creates `users_01-2014`
|
110
110
|
#
|
111
|
-
def purge
|
111
|
+
def purge(suffix = nil)
|
112
112
|
delete if suffix.present?
|
113
113
|
delete suffix
|
114
114
|
create suffix
|
@@ -121,7 +121,7 @@ module Chewy
|
|
121
121
|
# UsersIndex.purge! # deletes and creates `users` index
|
122
122
|
# UsersIndex.purge! '01-2014' # deletes `users` and `users_01-2014` indexes, creates `users_01-2014`
|
123
123
|
#
|
124
|
-
def purge!
|
124
|
+
def purge!(suffix = nil)
|
125
125
|
delete if suffix.present? && exists?
|
126
126
|
delete suffix
|
127
127
|
create! suffix
|
@@ -158,23 +158,23 @@ module Chewy
|
|
158
158
|
# zero-downtime index resetting (described here:
|
159
159
|
# http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/).
|
160
160
|
#
|
161
|
-
# UsersIndex.reset! Time.now.to_i
|
161
|
+
# UsersIndex.reset! Time.now.to_i, journal: true
|
162
162
|
#
|
163
|
-
def reset!
|
163
|
+
def reset!(suffix = nil, journal: false)
|
164
164
|
if suffix.present? && (indexes = self.indexes).present?
|
165
165
|
create! suffix, alias: false
|
166
|
-
result = import suffix: suffix
|
167
|
-
client.indices.update_aliases body: {actions: [
|
166
|
+
result = import suffix: suffix, journal: journal
|
167
|
+
client.indices.update_aliases body: { actions: [
|
168
168
|
*indexes.map do |index|
|
169
|
-
{remove: {index: index, alias: index_name}}
|
169
|
+
{ remove: { index: index, alias: index_name } }
|
170
170
|
end,
|
171
|
-
{add: {index: build_index_name(suffix: suffix), alias: index_name}}
|
172
|
-
]}
|
171
|
+
{ add: { index: build_index_name(suffix: suffix), alias: index_name } }
|
172
|
+
] }
|
173
173
|
client.indices.delete index: indexes if indexes.present?
|
174
174
|
result
|
175
175
|
else
|
176
176
|
purge! suffix
|
177
|
-
import
|
177
|
+
import journal: journal
|
178
178
|
end
|
179
179
|
end
|
180
180
|
end
|
data/lib/chewy/index/settings.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
module Chewy
|
2
2
|
class Index
|
3
|
-
|
4
3
|
# Stores ElasticSearch index settings and resolves `analysis`
|
5
4
|
# hash. At first, you need to store some analyzers or other
|
6
5
|
# analysis options to the corresponding repository:
|
@@ -37,7 +36,7 @@ module Chewy
|
|
37
36
|
.deep_merge((settings[:index] || {}).deep_symbolize_keys)
|
38
37
|
end
|
39
38
|
|
40
|
-
settings.present? ? {settings: settings} : {}
|
39
|
+
settings.present? ? { settings: settings } : {}
|
41
40
|
end
|
42
41
|
|
43
42
|
private
|
@@ -51,7 +50,7 @@ module Chewy
|
|
51
50
|
result.merge!(type => resolved) if resolved.present?
|
52
51
|
end
|
53
52
|
|
54
|
-
options
|
53
|
+
options[:analyzer] = analyzer if analyzer.present?
|
55
54
|
options
|
56
55
|
end
|
57
56
|
|
@@ -67,7 +66,7 @@ module Chewy
|
|
67
66
|
else
|
68
67
|
name_or_hash = name_or_hash.to_sym
|
69
68
|
resolved = repository[name_or_hash]
|
70
|
-
resolved ? {name_or_hash => resolved} : {}
|
69
|
+
resolved ? { name_or_hash => resolved } : {}
|
71
70
|
end
|
72
71
|
result.merge!(options)
|
73
72
|
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'chewy/journal/entry'
|
2
|
+
require 'chewy/journal/query'
|
3
|
+
require 'chewy/journal/apply'
|
4
|
+
require 'chewy/journal/clean'
|
5
|
+
|
6
|
+
module Chewy
|
7
|
+
class Journal
|
8
|
+
JOURNAL_MAPPING = {
|
9
|
+
journal: {
|
10
|
+
properties: {
|
11
|
+
index_name: { type: 'string', index: 'not_analyzed' },
|
12
|
+
type_name: { type: 'string', index: 'not_analyzed' },
|
13
|
+
action: { type: 'string', index: 'not_analyzed' },
|
14
|
+
object_ids: { type: 'string', index: 'not_analyzed' },
|
15
|
+
created_at: { type: 'date' }
|
16
|
+
}
|
17
|
+
}
|
18
|
+
}.freeze
|
19
|
+
|
20
|
+
def initialize(index)
|
21
|
+
@records = []
|
22
|
+
@index = index
|
23
|
+
end
|
24
|
+
|
25
|
+
def add(action_objects)
|
26
|
+
@records +=
|
27
|
+
action_objects.map do |action, objects|
|
28
|
+
{
|
29
|
+
index_name: @index.derivable_index_name,
|
30
|
+
type_name: @index.type_name,
|
31
|
+
action: action,
|
32
|
+
object_ids: identify(objects),
|
33
|
+
created_at: Time.now.to_i
|
34
|
+
}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def bulk_body
|
39
|
+
@records.map do |record|
|
40
|
+
{
|
41
|
+
create: {
|
42
|
+
_index: self.class.index_name,
|
43
|
+
_type: self.class.type_name,
|
44
|
+
data: record
|
45
|
+
}
|
46
|
+
}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def any_records?
|
51
|
+
@records.any?
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def identify(objects)
|
57
|
+
@index.adapter.identify(objects)
|
58
|
+
end
|
59
|
+
|
60
|
+
class << self
|
61
|
+
def exists?
|
62
|
+
Chewy.client.indices.exists? index: index_name
|
63
|
+
end
|
64
|
+
|
65
|
+
def index_name
|
66
|
+
[
|
67
|
+
Chewy.configuration[:prefix],
|
68
|
+
Chewy.configuration[:journal_name] || 'chewy_journal'
|
69
|
+
].reject(&:blank?).join('_')
|
70
|
+
end
|
71
|
+
|
72
|
+
def type_name
|
73
|
+
JOURNAL_MAPPING.keys.first
|
74
|
+
end
|
75
|
+
|
76
|
+
def create
|
77
|
+
return if exists?
|
78
|
+
Chewy.client.indices.create index: index_name, body: { settings: { index: Chewy.configuration[:index] }, mappings: JOURNAL_MAPPING }
|
79
|
+
Chewy.wait_for_status
|
80
|
+
end
|
81
|
+
|
82
|
+
def delete!
|
83
|
+
delete or raise Elasticsearch::Transport::Transport::Errors::NotFound
|
84
|
+
end
|
85
|
+
|
86
|
+
def delete
|
87
|
+
result = Chewy.client.indices.delete index: index_name
|
88
|
+
Chewy.wait_for_status if result
|
89
|
+
result
|
90
|
+
rescue Elasticsearch::Transport::Transport::Errors::NotFound
|
91
|
+
false
|
92
|
+
end
|
93
|
+
|
94
|
+
def apply_changes_from(*args)
|
95
|
+
Apply.since(*args)
|
96
|
+
end
|
97
|
+
|
98
|
+
def entries_from(*args)
|
99
|
+
Entry.since(*args)
|
100
|
+
end
|
101
|
+
|
102
|
+
def clean_until(*args)
|
103
|
+
Clean.until(*args)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|