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
@@ -6,7 +6,7 @@ module Chewy
|
|
6
6
|
class HasRelation < Expr
|
7
7
|
include Compose
|
8
8
|
|
9
|
-
def initialize
|
9
|
+
def initialize(type, outer = nil)
|
10
10
|
@type = type.to_s
|
11
11
|
@outer = outer
|
12
12
|
@query_mode = :must
|
@@ -15,26 +15,23 @@ module Chewy
|
|
15
15
|
@filters = []
|
16
16
|
end
|
17
17
|
|
18
|
-
def query_mode
|
18
|
+
def query_mode(mode)
|
19
19
|
@query_mode = mode
|
20
20
|
self
|
21
21
|
end
|
22
22
|
|
23
|
-
def filter_mode
|
23
|
+
def filter_mode(mode)
|
24
24
|
@filter_mode = mode
|
25
25
|
self
|
26
26
|
end
|
27
27
|
|
28
|
-
def query
|
29
|
-
if block
|
30
|
-
|
31
|
-
else
|
32
|
-
@queries.push(params)
|
33
|
-
end
|
28
|
+
def query(params = nil, &block)
|
29
|
+
raise 'Query DLS is not supported yet' if block
|
30
|
+
@queries.push(params)
|
34
31
|
self
|
35
32
|
end
|
36
33
|
|
37
|
-
def filter
|
34
|
+
def filter(params = nil, &block)
|
38
35
|
if block
|
39
36
|
@filters.push(Chewy::Query::Filters.new(@outer, &block).__render__)
|
40
37
|
else
|
@@ -48,12 +45,13 @@ module Chewy
|
|
48
45
|
filters = _filters_join @filters, @filter_mode
|
49
46
|
|
50
47
|
body = if filters && !queries
|
51
|
-
{filter: filters}
|
48
|
+
{ filter: filters }
|
52
49
|
else
|
53
50
|
_filtered_query(queries, filters)
|
54
|
-
end
|
51
|
+
end
|
52
|
+
body ||= {}
|
55
53
|
|
56
|
-
{_relation => body.merge(type: @type)}
|
54
|
+
{ _relation => body.merge(type: @type) }
|
57
55
|
end
|
58
56
|
end
|
59
57
|
end
|
@@ -2,7 +2,7 @@ module Chewy
|
|
2
2
|
class Query
|
3
3
|
module Nodes
|
4
4
|
class Missing < Expr
|
5
|
-
def initialize
|
5
|
+
def initialize(name, options = {})
|
6
6
|
@name = name.to_s
|
7
7
|
@options = options.reverse_merge(existence: true, null_value: false)
|
8
8
|
end
|
@@ -12,7 +12,7 @@ module Chewy
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def __render__
|
15
|
-
{missing: {field: @name}.merge(@options.slice(:existence, :null_value))}
|
15
|
+
{ missing: { field: @name }.merge(@options.slice(:existence, :null_value)) }
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -2,7 +2,7 @@ module Chewy
|
|
2
2
|
class Query
|
3
3
|
module Nodes
|
4
4
|
class Not < Expr
|
5
|
-
def initialize
|
5
|
+
def initialize(expr, options = {})
|
6
6
|
@expr = expr
|
7
7
|
@options = options
|
8
8
|
end
|
@@ -14,9 +14,9 @@ module Chewy
|
|
14
14
|
def __render__
|
15
15
|
expr = @expr.__render__
|
16
16
|
if @options.key?(:cache)
|
17
|
-
{not: {filter: expr, _cache: !!@options[:cache]}}
|
17
|
+
{ not: { filter: expr, _cache: !!@options[:cache] } }
|
18
18
|
else
|
19
|
-
{not: expr}
|
19
|
+
{ not: expr }
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
data/lib/chewy/query/nodes/or.rb
CHANGED
@@ -2,7 +2,7 @@ module Chewy
|
|
2
2
|
class Query
|
3
3
|
module Nodes
|
4
4
|
class Or < Expr
|
5
|
-
def initialize
|
5
|
+
def initialize(*nodes)
|
6
6
|
@options = nodes.extract_options!
|
7
7
|
@nodes = nodes.flatten.map { |node| node.is_a?(self.class) ? node.__nodes__ : node }.flatten
|
8
8
|
end
|
@@ -14,9 +14,9 @@ module Chewy
|
|
14
14
|
def __render__
|
15
15
|
nodes = @nodes.map(&:__render__)
|
16
16
|
if @options.key?(:cache)
|
17
|
-
{or: {filters: nodes, _cache: !!@options[:cache]}}
|
17
|
+
{ or: { filters: nodes, _cache: !!@options[:cache] } }
|
18
18
|
else
|
19
|
-
{or: nodes}
|
19
|
+
{ or: nodes }
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
@@ -2,13 +2,14 @@ module Chewy
|
|
2
2
|
class Query
|
3
3
|
module Nodes
|
4
4
|
class Prefix < Expr
|
5
|
-
def initialize
|
5
|
+
def initialize(name, value, options = {})
|
6
6
|
@name = name.to_s
|
7
|
-
@value
|
7
|
+
@value = value
|
8
|
+
@options = options
|
8
9
|
end
|
9
10
|
|
10
11
|
def __render__
|
11
|
-
filter = {prefix: {@name => @value}}
|
12
|
+
filter = { prefix: { @name => @value } }
|
12
13
|
filter[:prefix][:_cache] = !!@options[:cache] if @options.key?(:cache)
|
13
14
|
filter
|
14
15
|
end
|
@@ -2,16 +2,16 @@ module Chewy
|
|
2
2
|
class Query
|
3
3
|
module Nodes
|
4
4
|
class Query < Expr
|
5
|
-
def initialize
|
5
|
+
def initialize(query, options = {})
|
6
6
|
@query = query
|
7
7
|
@options = options
|
8
8
|
end
|
9
9
|
|
10
10
|
def __render__
|
11
11
|
if @options.key?(:cache)
|
12
|
-
{fquery: {query: @query, _cache: !!@options[:cache]}}
|
12
|
+
{ fquery: { query: @query, _cache: !!@options[:cache] } }
|
13
13
|
else
|
14
|
-
{query: @query}
|
14
|
+
{ query: @query }
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
@@ -3,22 +3,22 @@ module Chewy
|
|
3
3
|
module Nodes
|
4
4
|
class Range < Expr
|
5
5
|
EXECUTION = {
|
6
|
-
:
|
7
|
-
:
|
8
|
-
:
|
9
|
-
:
|
10
|
-
}
|
6
|
+
i: :index,
|
7
|
+
index: :index,
|
8
|
+
f: :fielddata,
|
9
|
+
fielddata: :fielddata
|
10
|
+
}.freeze
|
11
11
|
|
12
|
-
def initialize
|
12
|
+
def initialize(name, *args)
|
13
13
|
@name = name.to_s
|
14
14
|
@options = args.extract_options!
|
15
|
-
@range = @options.reject { |k,
|
16
|
-
@bounds = @options.reject { |k,
|
15
|
+
@range = @options.reject { |k, _v| ![:gt, :lt].include?(k) }
|
16
|
+
@bounds = @options.reject { |k, _v| ![:left_closed, :right_closed].include?(k) }
|
17
17
|
execution = EXECUTION[args.first.to_sym] if args.first
|
18
18
|
@options[:execution] = execution if execution
|
19
19
|
end
|
20
20
|
|
21
|
-
def &
|
21
|
+
def &(other)
|
22
22
|
if other.is_a?(self.class) && other.__name__ == @name
|
23
23
|
state = __state__.merge(other.__state__)
|
24
24
|
|
@@ -51,11 +51,11 @@ module Chewy
|
|
51
51
|
body[@bounds[:left_closed] ? :gte : :gt] = @range[:gt] if @range.key?(:gt)
|
52
52
|
body[@bounds[:right_closed] ? :lte : :lt] = @range[:lt] if @range.key?(:lt)
|
53
53
|
|
54
|
-
filter = {@name => body}
|
54
|
+
filter = { @name => body }
|
55
55
|
filter[:_cache] = !!@options[:cache] if @options.key?(:cache)
|
56
56
|
filter.merge!(@options.slice(:execution))
|
57
57
|
|
58
|
-
{range: filter}
|
58
|
+
{ range: filter }
|
59
59
|
end
|
60
60
|
end
|
61
61
|
end
|
@@ -2,28 +2,32 @@ module Chewy
|
|
2
2
|
class Query
|
3
3
|
module Nodes
|
4
4
|
class Regexp < Expr
|
5
|
-
FLAGS = %w(all anystring automaton complement empty intersection interval none)
|
5
|
+
FLAGS = %w(all anystring automaton complement empty intersection interval none).freeze
|
6
6
|
|
7
|
-
def initialize
|
7
|
+
def initialize(name, regexp, *args)
|
8
8
|
@name = name.to_s
|
9
9
|
@regexp = regexp.respond_to?(:source) ? regexp.source : regexp.to_s
|
10
10
|
@options = args.extract_options!
|
11
|
-
if args.
|
12
|
-
|
13
|
-
end
|
11
|
+
return if args.blank? && @options[:flags].blank?
|
12
|
+
@options[:flags] = FLAGS & (args.present? ? args.flatten : @options[:flags]).map(&:to_s).map(&:downcase)
|
14
13
|
end
|
15
14
|
|
16
15
|
def __render__
|
17
|
-
body = @options[:flags]
|
18
|
-
{value: @regexp, flags: @options[:flags].map(&:to_s).map(&:upcase).uniq.join('|')}
|
16
|
+
body = if @options[:flags]
|
17
|
+
{ value: @regexp, flags: @options[:flags].map(&:to_s).map(&:upcase).uniq.join('|') }
|
18
|
+
else
|
19
19
|
@regexp
|
20
|
-
|
20
|
+
end
|
21
|
+
filter = { @name => body }
|
21
22
|
if @options.key?(:cache)
|
22
23
|
filter[:_cache] = !!@options[:cache]
|
23
|
-
filter[:_cache_key] = @options[:cache].is_a?(TrueClass) || @options[:cache].is_a?(FalseClass)
|
24
|
-
@regexp.underscore
|
24
|
+
filter[:_cache_key] = if @options[:cache].is_a?(TrueClass) || @options[:cache].is_a?(FalseClass)
|
25
|
+
@regexp.underscore
|
26
|
+
else
|
27
|
+
@options[:cache]
|
28
|
+
end
|
25
29
|
end
|
26
|
-
{regexp: filter}
|
30
|
+
{ regexp: filter }
|
27
31
|
end
|
28
32
|
end
|
29
33
|
end
|
@@ -2,17 +2,17 @@ module Chewy
|
|
2
2
|
class Query
|
3
3
|
module Nodes
|
4
4
|
class Script < Expr
|
5
|
-
def initialize
|
5
|
+
def initialize(script, params = {})
|
6
6
|
@script = script
|
7
7
|
@params = params
|
8
|
-
@options = params.reject { |k,
|
8
|
+
@options = params.reject { |k, _v| ![:cache].include?(k) }
|
9
9
|
end
|
10
10
|
|
11
11
|
def __render__
|
12
|
-
script = {script: @script}
|
13
|
-
script
|
14
|
-
script
|
15
|
-
{script: script}
|
12
|
+
script = { script: @script }
|
13
|
+
script[:params] = @params if @params.present?
|
14
|
+
script[:_cache] = !!@options[:cache] if @options.key?(:cache)
|
15
|
+
{ script: script }
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -7,12 +7,12 @@ module Chewy
|
|
7
7
|
|
8
8
|
attr_reader :current_page, :per_page
|
9
9
|
|
10
|
-
def paginate(options={})
|
10
|
+
def paginate(options = {})
|
11
11
|
@current_page = ::WillPaginate::PageNumber(options[:page] || @current_page || 1)
|
12
12
|
@page_multiplier = @current_page - 1
|
13
13
|
@per_page = (options[:per_page] || @per_page || ::WillPaginate.per_page).to_i
|
14
14
|
|
15
|
-
#call Chewy::Query methods to limit results
|
15
|
+
# call Chewy::Query methods to limit results
|
16
16
|
limit(@per_page).offset(@page_multiplier * @per_page)
|
17
17
|
end
|
18
18
|
|
data/lib/chewy/railtie.rb
CHANGED
@@ -51,7 +51,7 @@ module Chewy
|
|
51
51
|
end
|
52
52
|
|
53
53
|
initializer 'chewy.logger', after: 'active_record.logger' do
|
54
|
-
ActiveSupport.on_load(:active_record)
|
54
|
+
ActiveSupport.on_load(:active_record) { Chewy.logger ||= ActiveRecord::Base.logger }
|
55
55
|
end
|
56
56
|
|
57
57
|
initializer 'chewy.migration_strategy' do
|
@@ -70,9 +70,9 @@ module Chewy
|
|
70
70
|
app.config.middleware.insert_after(Rails::Rack::Logger, RequestStrategy)
|
71
71
|
end
|
72
72
|
|
73
|
-
initializer 'chewy.
|
73
|
+
initializer 'chewy.add_indices_path' do |_app|
|
74
74
|
Chewy::Railtie.all_engines.each do |engine|
|
75
|
-
engine.paths.add
|
75
|
+
engine.paths.add Chewy.configuration[:indices_path]
|
76
76
|
end
|
77
77
|
end
|
78
78
|
end
|
data/lib/chewy/rake_helper.rb
CHANGED
@@ -1,26 +1,32 @@
|
|
1
1
|
module Chewy
|
2
2
|
module RakeHelper
|
3
3
|
class << self
|
4
|
-
|
5
|
-
|
6
|
-
callback = ->(name, start, finish, id, payload) do
|
4
|
+
def subscribed_task_stats
|
5
|
+
import_callback = lambda do |_name, start, finish, _id, payload|
|
7
6
|
duration = (finish - start).round(2)
|
8
7
|
puts " Imported #{payload[:type]} for #{duration}s, documents total: #{payload[:import].try(:[], :index).to_i}"
|
9
|
-
payload[:errors]
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
if payload[:errors]
|
9
|
+
payload[:errors].each do |action, errors|
|
10
|
+
puts " #{action.to_s.humanize} errors:"
|
11
|
+
errors.each do |error, documents|
|
12
|
+
puts " `#{error}`"
|
13
|
+
puts " on #{documents.count} documents: #{documents}"
|
14
|
+
end
|
14
15
|
end
|
15
|
-
end
|
16
|
+
end
|
16
17
|
end
|
17
|
-
|
18
|
-
|
18
|
+
journal_callback = lambda do |_, _, _, _, payload|
|
19
|
+
puts "Applying journal. Stage #{payload[:stage]}"
|
20
|
+
end
|
21
|
+
ActiveSupport::Notifications.subscribed(journal_callback, 'apply_journal.chewy') do
|
22
|
+
ActiveSupport::Notifications.subscribed(import_callback, 'import_objects.chewy') do
|
23
|
+
yield
|
24
|
+
end
|
19
25
|
end
|
20
26
|
end
|
21
27
|
|
22
28
|
def eager_load_chewy!
|
23
|
-
dirs = Chewy::Railtie.all_engines.map { |engine| engine.paths[
|
29
|
+
dirs = Chewy::Railtie.all_engines.map { |engine| engine.paths[Chewy.configuration[:indices_path]] }.compact.map(&:existent).flatten.uniq
|
24
30
|
|
25
31
|
dirs.each do |dir|
|
26
32
|
Dir.glob(File.join(dir, '**/*.rb')).each do |file|
|
@@ -29,36 +35,51 @@ module Chewy
|
|
29
35
|
end
|
30
36
|
end
|
31
37
|
|
32
|
-
def
|
38
|
+
def all_indexes
|
39
|
+
eager_load_chewy!
|
40
|
+
Chewy::Index.descendants
|
41
|
+
end
|
42
|
+
|
43
|
+
def normalize_index(index)
|
44
|
+
return index if index.is_a?(Chewy::Index)
|
33
45
|
"#{index.to_s.gsub(/index\z/i, '').camelize}Index".constantize
|
34
46
|
end
|
35
47
|
|
48
|
+
def normalize_indexes(*indexes)
|
49
|
+
indexes.flatten.map { |index| normalize_index(index) }
|
50
|
+
end
|
51
|
+
|
36
52
|
# Performs zero downtime reindexing of all documents in the specified index.
|
37
|
-
def reset_index
|
38
|
-
|
39
|
-
|
40
|
-
|
53
|
+
def reset_index(*indexes)
|
54
|
+
normalize_indexes(indexes).each do |index|
|
55
|
+
puts "Resetting #{index}"
|
56
|
+
time = Time.now
|
57
|
+
index.reset!((time.to_f * 1000).round)
|
58
|
+
if index.journal?
|
59
|
+
Chewy::Journal.create
|
60
|
+
Chewy::Journal::Apply.since(time, only: [index])
|
61
|
+
end
|
62
|
+
end
|
41
63
|
end
|
42
64
|
|
43
65
|
# Performs zero downtime reindexing of all documents across all indices.
|
44
|
-
def reset_all
|
45
|
-
|
46
|
-
Chewy::Index.descendants.each { |index| reset_index index }
|
66
|
+
def reset_all(*except)
|
67
|
+
reset_index(all_indexes - normalize_indexes(except))
|
47
68
|
end
|
48
69
|
|
49
|
-
def update_index
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
70
|
+
def update_index(*indexes)
|
71
|
+
normalize_indexes(indexes).each do |index|
|
72
|
+
puts "Updating #{index}"
|
73
|
+
if index.exists?
|
74
|
+
index.import
|
75
|
+
else
|
76
|
+
puts "Index `#{index.index_name}` does not exists. Use rake chewy:reset[#{index.index_name}] to create and update it."
|
77
|
+
end
|
56
78
|
end
|
57
79
|
end
|
58
80
|
|
59
|
-
def update_all
|
60
|
-
|
61
|
-
Chewy::Index.descendants.each { |index| update_index index }
|
81
|
+
def update_all(*except)
|
82
|
+
update_index(all_indexes - normalize_indexes(except))
|
62
83
|
end
|
63
84
|
end
|
64
85
|
end
|