chewy 5.1.0 → 7.2.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (234) hide show
  1. checksums.yaml +4 -4
  2. data/.github/CODEOWNERS +1 -0
  3. data/.github/ISSUE_TEMPLATE/bug_report.md +39 -0
  4. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  5. data/.github/PULL_REQUEST_TEMPLATE.md +16 -0
  6. data/.github/workflows/ruby.yml +73 -0
  7. data/.rubocop.yml +13 -8
  8. data/.rubocop_todo.yml +110 -22
  9. data/CHANGELOG.md +449 -347
  10. data/CODE_OF_CONDUCT.md +14 -0
  11. data/CONTRIBUTING.md +63 -0
  12. data/Gemfile +3 -7
  13. data/Guardfile +3 -1
  14. data/LICENSE.txt +1 -1
  15. data/README.md +423 -311
  16. data/chewy.gemspec +8 -10
  17. data/gemfiles/rails.5.2.activerecord.gemfile +9 -14
  18. data/gemfiles/rails.6.0.activerecord.gemfile +11 -0
  19. data/gemfiles/rails.6.1.activerecord.gemfile +13 -0
  20. data/gemfiles/rails.7.0.activerecord.gemfile +13 -0
  21. data/lib/chewy/config.rb +42 -60
  22. data/lib/chewy/errors.rb +4 -10
  23. data/lib/chewy/fields/base.rb +80 -20
  24. data/lib/chewy/fields/root.rb +7 -17
  25. data/lib/chewy/index/actions.rb +62 -35
  26. data/lib/chewy/{type → index}/adapter/active_record.rb +18 -4
  27. data/lib/chewy/{type → index}/adapter/base.rb +2 -3
  28. data/lib/chewy/{type → index}/adapter/object.rb +28 -32
  29. data/lib/chewy/{type → index}/adapter/orm.rb +26 -24
  30. data/lib/chewy/index/aliases.rb +14 -5
  31. data/lib/chewy/{type → index}/crutch.rb +5 -5
  32. data/lib/chewy/index/import/bulk_builder.rb +311 -0
  33. data/lib/chewy/{type → index}/import/bulk_request.rb +6 -7
  34. data/lib/chewy/{type → index}/import/journal_builder.rb +11 -12
  35. data/lib/chewy/{type → index}/import/routine.rb +17 -16
  36. data/lib/chewy/{type → index}/import.rb +51 -33
  37. data/lib/chewy/{type → index}/mapping.rb +32 -37
  38. data/lib/chewy/index/observe/active_record_methods.rb +87 -0
  39. data/lib/chewy/index/observe/callback.rb +34 -0
  40. data/lib/chewy/index/observe.rb +17 -0
  41. data/lib/chewy/index/specification.rb +1 -0
  42. data/lib/chewy/{type → index}/syncer.rb +61 -62
  43. data/lib/chewy/{type → index}/witchcraft.rb +15 -9
  44. data/lib/chewy/{type → index}/wrapper.rb +13 -3
  45. data/lib/chewy/index.rb +46 -96
  46. data/lib/chewy/journal.rb +25 -14
  47. data/lib/chewy/minitest/helpers.rb +86 -13
  48. data/lib/chewy/minitest/search_index_receiver.rb +22 -26
  49. data/lib/chewy/multi_search.rb +62 -0
  50. data/lib/chewy/railtie.rb +6 -20
  51. data/lib/chewy/rake_helper.rb +136 -108
  52. data/lib/chewy/rspec/build_query.rb +12 -0
  53. data/lib/chewy/rspec/helpers.rb +55 -0
  54. data/lib/chewy/rspec/update_index.rb +55 -44
  55. data/lib/chewy/rspec.rb +2 -0
  56. data/lib/chewy/runtime.rb +1 -1
  57. data/lib/chewy/search/loader.rb +19 -41
  58. data/lib/chewy/search/parameters/collapse.rb +16 -0
  59. data/lib/chewy/search/parameters/concerns/query_storage.rb +2 -2
  60. data/lib/chewy/search/parameters/ignore_unavailable.rb +27 -0
  61. data/lib/chewy/search/parameters/indices.rb +12 -57
  62. data/lib/chewy/search/parameters/none.rb +1 -3
  63. data/lib/chewy/search/parameters/order.rb +6 -19
  64. data/lib/chewy/search/parameters/source.rb +5 -1
  65. data/lib/chewy/search/parameters/track_total_hits.rb +16 -0
  66. data/lib/chewy/search/parameters.rb +7 -4
  67. data/lib/chewy/search/query_proxy.rb +9 -2
  68. data/lib/chewy/search/request.rb +180 -154
  69. data/lib/chewy/search/response.rb +5 -5
  70. data/lib/chewy/search/scoping.rb +7 -8
  71. data/lib/chewy/search/scrolling.rb +16 -13
  72. data/lib/chewy/search.rb +7 -22
  73. data/lib/chewy/stash.rb +19 -30
  74. data/lib/chewy/strategy/active_job.rb +2 -2
  75. data/lib/chewy/strategy/atomic_no_refresh.rb +18 -0
  76. data/lib/chewy/strategy/base.rb +10 -0
  77. data/lib/chewy/strategy/lazy_sidekiq.rb +64 -0
  78. data/lib/chewy/strategy/sidekiq.rb +3 -2
  79. data/lib/chewy/strategy.rb +5 -19
  80. data/lib/chewy/version.rb +1 -1
  81. data/lib/chewy.rb +36 -80
  82. data/lib/generators/chewy/install_generator.rb +1 -1
  83. data/lib/tasks/chewy.rake +26 -32
  84. data/migration_guide.md +56 -0
  85. data/spec/chewy/config_spec.rb +15 -61
  86. data/spec/chewy/fields/base_spec.rb +432 -145
  87. data/spec/chewy/fields/root_spec.rb +20 -28
  88. data/spec/chewy/fields/time_fields_spec.rb +5 -5
  89. data/spec/chewy/index/actions_spec.rb +388 -55
  90. data/spec/chewy/{type → index}/adapter/active_record_spec.rb +110 -44
  91. data/spec/chewy/{type → index}/adapter/object_spec.rb +21 -6
  92. data/spec/chewy/index/aliases_spec.rb +3 -3
  93. data/spec/chewy/index/import/bulk_builder_spec.rb +494 -0
  94. data/spec/chewy/{type → index}/import/bulk_request_spec.rb +5 -12
  95. data/spec/chewy/{type → index}/import/journal_builder_spec.rb +14 -22
  96. data/spec/chewy/{type → index}/import/routine_spec.rb +19 -19
  97. data/spec/chewy/{type → index}/import_spec.rb +149 -96
  98. data/spec/chewy/index/mapping_spec.rb +135 -0
  99. data/spec/chewy/index/observe/active_record_methods_spec.rb +68 -0
  100. data/spec/chewy/index/observe/callback_spec.rb +139 -0
  101. data/spec/chewy/index/observe_spec.rb +143 -0
  102. data/spec/chewy/index/settings_spec.rb +3 -1
  103. data/spec/chewy/index/specification_spec.rb +20 -30
  104. data/spec/chewy/{type → index}/syncer_spec.rb +14 -19
  105. data/spec/chewy/{type → index}/witchcraft_spec.rb +34 -21
  106. data/spec/chewy/index/wrapper_spec.rb +100 -0
  107. data/spec/chewy/index_spec.rb +69 -137
  108. data/spec/chewy/journal_spec.rb +46 -91
  109. data/spec/chewy/minitest/helpers_spec.rb +122 -14
  110. data/spec/chewy/minitest/search_index_receiver_spec.rb +24 -26
  111. data/spec/chewy/multi_search_spec.rb +84 -0
  112. data/spec/chewy/rake_helper_spec.rb +293 -101
  113. data/spec/chewy/rspec/build_query_spec.rb +34 -0
  114. data/spec/chewy/rspec/helpers_spec.rb +61 -0
  115. data/spec/chewy/rspec/update_index_spec.rb +106 -102
  116. data/spec/chewy/runtime_spec.rb +2 -2
  117. data/spec/chewy/search/loader_spec.rb +19 -53
  118. data/spec/chewy/search/pagination/kaminari_examples.rb +3 -5
  119. data/spec/chewy/search/pagination/kaminari_spec.rb +1 -1
  120. data/spec/chewy/search/parameters/collapse_spec.rb +5 -0
  121. data/spec/chewy/search/parameters/ignore_unavailable_spec.rb +67 -0
  122. data/spec/chewy/search/parameters/indices_spec.rb +26 -118
  123. data/spec/chewy/search/parameters/none_spec.rb +1 -1
  124. data/spec/chewy/search/parameters/order_spec.rb +18 -11
  125. data/spec/chewy/search/parameters/query_storage_examples.rb +67 -21
  126. data/spec/chewy/search/parameters/search_after_spec.rb +4 -1
  127. data/spec/chewy/search/parameters/source_spec.rb +8 -2
  128. data/spec/chewy/search/parameters/track_total_hits_spec.rb +5 -0
  129. data/spec/chewy/search/parameters_spec.rb +23 -7
  130. data/spec/chewy/search/query_proxy_spec.rb +68 -17
  131. data/spec/chewy/search/request_spec.rb +344 -149
  132. data/spec/chewy/search/response_spec.rb +35 -25
  133. data/spec/chewy/search/scrolling_spec.rb +28 -26
  134. data/spec/chewy/search_spec.rb +69 -59
  135. data/spec/chewy/stash_spec.rb +16 -26
  136. data/spec/chewy/strategy/active_job_spec.rb +23 -10
  137. data/spec/chewy/strategy/atomic_no_refresh_spec.rb +60 -0
  138. data/spec/chewy/strategy/atomic_spec.rb +9 -10
  139. data/spec/chewy/strategy/lazy_sidekiq_spec.rb +214 -0
  140. data/spec/chewy/strategy/sidekiq_spec.rb +14 -10
  141. data/spec/chewy/strategy_spec.rb +19 -15
  142. data/spec/chewy_spec.rb +17 -110
  143. data/spec/spec_helper.rb +6 -29
  144. data/spec/support/active_record.rb +43 -5
  145. metadata +102 -198
  146. data/.travis.yml +0 -45
  147. data/Appraisals +0 -81
  148. data/LEGACY_DSL.md +0 -497
  149. data/gemfiles/rails.4.0.activerecord.gemfile +0 -15
  150. data/gemfiles/rails.4.1.activerecord.gemfile +0 -15
  151. data/gemfiles/rails.4.2.activerecord.gemfile +0 -16
  152. data/gemfiles/rails.4.2.mongoid.5.2.gemfile +0 -16
  153. data/gemfiles/rails.5.0.activerecord.gemfile +0 -16
  154. data/gemfiles/rails.5.0.mongoid.6.1.gemfile +0 -16
  155. data/gemfiles/rails.5.1.activerecord.gemfile +0 -16
  156. data/gemfiles/rails.5.1.mongoid.6.3.gemfile +0 -16
  157. data/gemfiles/sequel.4.45.gemfile +0 -11
  158. data/lib/chewy/backports/deep_dup.rb +0 -46
  159. data/lib/chewy/backports/duplicable.rb +0 -91
  160. data/lib/chewy/query/compose.rb +0 -68
  161. data/lib/chewy/query/criteria.rb +0 -191
  162. data/lib/chewy/query/filters.rb +0 -244
  163. data/lib/chewy/query/loading.rb +0 -110
  164. data/lib/chewy/query/nodes/and.rb +0 -25
  165. data/lib/chewy/query/nodes/base.rb +0 -17
  166. data/lib/chewy/query/nodes/bool.rb +0 -34
  167. data/lib/chewy/query/nodes/equal.rb +0 -34
  168. data/lib/chewy/query/nodes/exists.rb +0 -20
  169. data/lib/chewy/query/nodes/expr.rb +0 -28
  170. data/lib/chewy/query/nodes/field.rb +0 -110
  171. data/lib/chewy/query/nodes/has_child.rb +0 -15
  172. data/lib/chewy/query/nodes/has_parent.rb +0 -15
  173. data/lib/chewy/query/nodes/has_relation.rb +0 -59
  174. data/lib/chewy/query/nodes/match_all.rb +0 -11
  175. data/lib/chewy/query/nodes/missing.rb +0 -20
  176. data/lib/chewy/query/nodes/not.rb +0 -25
  177. data/lib/chewy/query/nodes/or.rb +0 -25
  178. data/lib/chewy/query/nodes/prefix.rb +0 -19
  179. data/lib/chewy/query/nodes/query.rb +0 -20
  180. data/lib/chewy/query/nodes/range.rb +0 -63
  181. data/lib/chewy/query/nodes/raw.rb +0 -15
  182. data/lib/chewy/query/nodes/regexp.rb +0 -35
  183. data/lib/chewy/query/nodes/script.rb +0 -20
  184. data/lib/chewy/query/pagination.rb +0 -25
  185. data/lib/chewy/query.rb +0 -1142
  186. data/lib/chewy/search/pagination/will_paginate.rb +0 -43
  187. data/lib/chewy/search/parameters/types.rb +0 -20
  188. data/lib/chewy/strategy/resque.rb +0 -27
  189. data/lib/chewy/strategy/shoryuken.rb +0 -40
  190. data/lib/chewy/type/actions.rb +0 -43
  191. data/lib/chewy/type/adapter/mongoid.rb +0 -67
  192. data/lib/chewy/type/adapter/sequel.rb +0 -93
  193. data/lib/chewy/type/import/bulk_builder.rb +0 -122
  194. data/lib/chewy/type/observe.rb +0 -82
  195. data/lib/chewy/type.rb +0 -117
  196. data/lib/sequel/plugins/chewy_observe.rb +0 -63
  197. data/spec/chewy/query/criteria_spec.rb +0 -700
  198. data/spec/chewy/query/filters_spec.rb +0 -201
  199. data/spec/chewy/query/loading_spec.rb +0 -124
  200. data/spec/chewy/query/nodes/and_spec.rb +0 -12
  201. data/spec/chewy/query/nodes/bool_spec.rb +0 -14
  202. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  203. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  204. data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
  205. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
  206. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  207. data/spec/chewy/query/nodes/missing_spec.rb +0 -16
  208. data/spec/chewy/query/nodes/not_spec.rb +0 -14
  209. data/spec/chewy/query/nodes/or_spec.rb +0 -12
  210. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  211. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  212. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  213. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  214. data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
  215. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  216. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
  217. data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
  218. data/spec/chewy/query/pagination_spec.rb +0 -39
  219. data/spec/chewy/query_spec.rb +0 -637
  220. data/spec/chewy/search/pagination/will_paginate_examples.rb +0 -63
  221. data/spec/chewy/search/pagination/will_paginate_spec.rb +0 -23
  222. data/spec/chewy/search/parameters/types_spec.rb +0 -5
  223. data/spec/chewy/strategy/resque_spec.rb +0 -46
  224. data/spec/chewy/strategy/shoryuken_spec.rb +0 -66
  225. data/spec/chewy/type/actions_spec.rb +0 -50
  226. data/spec/chewy/type/adapter/mongoid_spec.rb +0 -372
  227. data/spec/chewy/type/adapter/sequel_spec.rb +0 -472
  228. data/spec/chewy/type/import/bulk_builder_spec.rb +0 -279
  229. data/spec/chewy/type/mapping_spec.rb +0 -173
  230. data/spec/chewy/type/observe_spec.rb +0 -137
  231. data/spec/chewy/type/wrapper_spec.rb +0 -98
  232. data/spec/chewy/type_spec.rb +0 -55
  233. data/spec/support/mongoid.rb +0 -93
  234. data/spec/support/sequel.rb +0 -80
data/Appraisals DELETED
@@ -1,81 +0,0 @@
1
- %w[4.0 4.1 4.2].each do |activesupport|
2
- appraise "rails.#{activesupport}.activerecord" do
3
- gem 'activerecord', "~> #{activesupport}.0"
4
- gem 'activesupport', "~> #{activesupport}.0"
5
-
6
- gem 'activejob', "~> #{activesupport}.0" if activesupport >= '4.2'
7
- gem 'resque', require: false
8
- gem 'shoryuken', require: false
9
- gem 'aws-sdk-sqs', require: false
10
- gem 'sidekiq', require: false
11
-
12
- gem 'kaminari', '~> 0.17.0', require: false
13
- gem 'will_paginate', require: false
14
-
15
- gem 'parallel', require: false
16
- end
17
- end
18
-
19
- %w[5.0 5.1 5.2].each do |activesupport|
20
- appraise "rails.#{activesupport}.activerecord" do
21
- gem 'activerecord', "~> #{activesupport}.0"
22
- gem 'activesupport', "~> #{activesupport}.0"
23
-
24
- gem 'activejob', "~> #{activesupport}.0"
25
- gem 'resque', require: false
26
- gem 'shoryuken', require: false
27
- gem 'aws-sdk-sqs', require: false
28
- gem 'sidekiq', require: false
29
-
30
- gem 'kaminari-core', '~> 1.1.0', require: false
31
- gem 'will_paginate', require: false
32
-
33
- gem 'parallel', require: false
34
- end
35
- end
36
-
37
- appraise 'rails.4.2.mongoid.5.2' do
38
- gem 'mongoid', '~> 5.2.0'
39
- gem 'activesupport', '~> 4.2.0'
40
-
41
- gem 'activejob', '~> 4.2.0'
42
- gem 'resque', require: false
43
- gem 'shoryuken', require: false
44
- gem 'aws-sdk-sqs', require: false
45
- gem 'sidekiq', require: false
46
-
47
- gem 'kaminari', '~> 0.17.0', require: false
48
- gem 'will_paginate', require: false
49
-
50
- gem 'parallel', require: false
51
- end
52
-
53
- {'5.0' => '6.1', '5.1' => '6.3'}.each do |activesupport, mongoid|
54
- appraise "rails.#{activesupport}.mongoid.#{mongoid}" do
55
- gem 'mongoid', "~> #{mongoid}.0"
56
- gem 'activesupport', "~> #{activesupport}.0"
57
-
58
- gem 'activejob', "~> #{activesupport}.0"
59
- gem 'resque', require: false
60
- gem 'shoryuken', require: false
61
- gem 'aws-sdk-sqs', require: false
62
- gem 'sidekiq', require: false
63
-
64
- gem 'kaminari-core', '~> 1.1.0', require: false
65
- gem 'will_paginate', require: false
66
-
67
- gem 'parallel', require: false
68
- end
69
- end
70
-
71
- %w[4.45].each do |sequel|
72
- appraise "sequel.#{sequel}" do
73
- gem 'sequel', "~> #{sequel}.0"
74
- gem 'activesupport', '~> 5.1.0'
75
-
76
- gem 'kaminari-core', '~> 1.1.0', require: false
77
- gem 'will_paginate', require: false
78
-
79
- gem 'parallel', require: false
80
- end
81
- end
data/LEGACY_DSL.md DELETED
@@ -1,497 +0,0 @@
1
- # Previous DSL readme.
2
-
3
- See `Chewy::Query` for details.
4
-
5
- * [Index querying](#index-querying)
6
- * [Additional query action.](#additional-query-action)
7
- * [Filters query DSL](#filters-query-dsl)
8
- * [Faceting](#faceting)
9
- * [Aggregations](#aggregations)
10
- * [Script fields](#script-fields)
11
- * [Script scoring](#script-scoring)
12
- * [Boost Factor](#boost-factor)
13
- * [Objects loading](#objects-loading)
14
-
15
- ## Index querying
16
-
17
- ```ruby
18
- scope = UsersIndex.query(term: {name: 'foo'})
19
- .filter(range: {rating: {gte: 100}})
20
- .order(created: :desc)
21
- .limit(20).offset(100)
22
-
23
- scope.to_a # => will produce array of UserIndex::User or other types instances
24
- scope.map { |user| user.email }
25
- scope.total_count # => will return total objects count
26
-
27
- scope.per(10).page(3) # supports kaminari pagination
28
- scope.explain.map { |user| user._explanation }
29
- scope.only(:id, :email) # returns ids and emails only
30
-
31
- scope.merge(other_scope) # queries could be merged
32
- ```
33
-
34
- Also, queries can be performed on a type individually:
35
-
36
- ```ruby
37
- UsersIndex::User.filter(term: {name: 'foo'}) # will return UserIndex::User collection only
38
- ```
39
-
40
- If you are performing more than one `filter` or `query` in the chain, all the filters and queries will be concatenated in the way specified by
41
- `filter_mode` and `query_mode` respectively.
42
-
43
- The default `filter_mode` is `:and` and the default `query_mode` is `bool`.
44
-
45
- Available filter modes are: `:and`, `:or`, `:must`, `:should` and any minimum_should_match-acceptable value
46
-
47
- Available query modes are: `:must`, `:should`, `:dis_max`, any minimum_should_match-acceptable value or float value for dis_max query with tie_breaker specified.
48
-
49
- ```ruby
50
- UsersIndex::User.filter{ name == 'Fred' }.filter{ age < 42 } # will be wrapped with `and` filter
51
- UsersIndex::User.filter{ name == 'Fred' }.filter{ age < 42 }.filter_mode(:should) # will be wrapped with bool `should` filter
52
- UsersIndex::User.filter{ name == 'Fred' }.filter{ age < 42 }.filter_mode('75%') # will be wrapped with bool `should` filter with `minimum_should_match: '75%'`
53
- ```
54
-
55
- See [query.rb](lib/chewy/query.rb) for more details.
56
-
57
- ## Additional query action.
58
-
59
- You may also perform additional actions on the query scope, such as deleting of all the scope documents:
60
-
61
- ```ruby
62
- UsersIndex.delete_all
63
- UsersIndex::User.delete_all
64
- UsersIndex.filter{ age < 42 }.delete_all
65
- UsersIndex::User.filter{ age < 42 }.delete_all
66
- ```
67
-
68
- ## Filters query DSL
69
-
70
- There is a test version of the filter-creating DSL:
71
-
72
- ```ruby
73
- UsersIndex.filter{ name == 'Fred' } # will produce `term` filter.
74
- UsersIndex.filter{ age <= 42 } # will produce `range` filter.
75
- ```
76
-
77
- The basis of the DSL is the expression. There are 2 types of expressions:
78
-
79
- * Simple function
80
-
81
- ```ruby
82
- UsersIndex.filter{ s('doc["num"] > 1') } # script expression
83
- UsersIndex.filter{ q(query_string: {query: 'lazy fox'}) } # query expression
84
- ```
85
-
86
- * Field-dependent composite expression
87
- Consists of the field name (with or without dot notation), a value, and an action operator between them. The field name might take additional options for passing to the resulting expression.
88
-
89
- ```ruby
90
- UsersIndex.filter{ name == 'Name' } # simple field term filter
91
- UsersIndex.filter{ name(:bool) == ['Name1', 'Name2'] } # terms query with `execution: :bool` option passed
92
- UsersIndex.filter{ answers.title =~ /regexp/ } # regexp filter for `answers.title` field
93
- ```
94
-
95
- You can combine expressions as you wish with the help of combination operators.
96
-
97
- ```ruby
98
- UsersIndex.filter{ (name == 'Name') & (email == 'Email') } # combination produces `and` filter
99
- UsersIndex.filter{
100
- must(
101
- should(name =~ 'Fr').should_not(name == 'Fred') & (age == 42), email =~ /gmail\.com/
102
- ) | ((roles.admin == true) & name?)
103
- } # many of the combination possibilities
104
- ```
105
-
106
- There is also a special syntax for cache enabling:
107
-
108
- ```ruby
109
- UsersIndex.filter{ ~name == 'Name' } # you can apply tilde to the field name
110
- UsersIndex.filter{ ~(name == 'Name') } # or to the whole expression
111
-
112
- # if you are applying cache to the one part of range filter
113
- # the whole filter will be cached:
114
- UsersIndex.filter{ ~(age > 42) & (age <= 50) }
115
-
116
- # You can pass cache options as a field option also.
117
- UsersIndex.filter{ name(cache: true) == 'Name' }
118
- UsersIndex.filter{ name(cache: false) == 'Name' }
119
-
120
- # With regexp filter you can pass _cache_key
121
- UsersIndex.filter{ name(cache: 'name_regexp') =~ /Name/ }
122
- # Or not
123
- UsersIndex.filter{ name(cache: true) =~ /Name/ }
124
- ```
125
-
126
- Compliance cheatsheet for filters and DSL expressions:
127
-
128
- * Term filter
129
-
130
- ```json
131
- {"term": {"name": "Fred"}}
132
- {"not": {"term": {"name": "Johny"}}}
133
- ```
134
-
135
- ```ruby
136
- UsersIndex.filter{ name == 'Fred' }
137
- UsersIndex.filter{ name != 'Johny' }
138
- ```
139
-
140
- * Terms filter
141
-
142
- ```json
143
- {"terms": {"name": ["Fred", "Johny"]}}
144
- {"not": {"terms": {"name": ["Fred", "Johny"]}}}
145
-
146
- {"terms": {"name": ["Fred", "Johny"], "execution": "or"}}
147
-
148
- {"terms": {"name": ["Fred", "Johny"], "execution": "and"}}
149
-
150
- {"terms": {"name": ["Fred", "Johny"], "execution": "bool"}}
151
-
152
- {"terms": {"name": ["Fred", "Johny"], "execution": "fielddata"}}
153
- ```
154
-
155
- ```ruby
156
- UsersIndex.filter{ name == ['Fred', 'Johny'] }
157
- UsersIndex.filter{ name != ['Fred', 'Johny'] }
158
-
159
- UsersIndex.filter{ name(:|) == ['Fred', 'Johny'] }
160
- UsersIndex.filter{ name(:or) == ['Fred', 'Johny'] }
161
- UsersIndex.filter{ name(execution: :or) == ['Fred', 'Johny'] }
162
-
163
- UsersIndex.filter{ name(:&) == ['Fred', 'Johny'] }
164
- UsersIndex.filter{ name(:and) == ['Fred', 'Johny'] }
165
- UsersIndex.filter{ name(execution: :and) == ['Fred', 'Johny'] }
166
-
167
- UsersIndex.filter{ name(:b) == ['Fred', 'Johny'] }
168
- UsersIndex.filter{ name(:bool) == ['Fred', 'Johny'] }
169
- UsersIndex.filter{ name(execution: :bool) == ['Fred', 'Johny'] }
170
-
171
- UsersIndex.filter{ name(:f) == ['Fred', 'Johny'] }
172
- UsersIndex.filter{ name(:fielddata) == ['Fred', 'Johny'] }
173
- UsersIndex.filter{ name(execution: :fielddata) == ['Fred', 'Johny'] }
174
- ```
175
-
176
- * Regexp filter (== and =~ are equivalent)
177
-
178
- ```json
179
- {"regexp": {"name.first": "s.*y"}}
180
-
181
- {"not": {"regexp": {"name.first": "s.*y"}}}
182
-
183
- {"regexp": {"name.first": {"value": "s.*y", "flags": "ANYSTRING|INTERSECTION"}}}
184
- ```
185
-
186
- ```ruby
187
- UsersIndex.filter{ name.first == /s.*y/ }
188
- UsersIndex.filter{ name.first =~ /s.*y/ }
189
-
190
- UsersIndex.filter{ name.first != /s.*y/ }
191
- UsersIndex.filter{ name.first !~ /s.*y/ }
192
-
193
- UsersIndex.filter{ name.first(:anystring, :intersection) == /s.*y/ }
194
- UsersIndex.filter{ name.first(flags: [:anystring, :intersection]) == /s.*y/ }
195
- ```
196
-
197
- * Prefix filter
198
-
199
- ```json
200
- {"prefix": {"name": "Fre"}}
201
- {"not": {"prefix": {"name": "Joh"}}}
202
- ```
203
-
204
- ```ruby
205
- UsersIndex.filter{ name =~ re' }
206
- UsersIndex.filter{ name !~ 'Joh' }
207
- ```
208
-
209
- * Exists filter
210
-
211
- ```json
212
- {"exists": {"field": "name"}}
213
- ```
214
-
215
- ```ruby
216
- UsersIndex.filter{ name? }
217
- UsersIndex.filter{ !!name }
218
- UsersIndex.filter{ !!name? }
219
- UsersIndex.filter{ name != nil }
220
- UsersIndex.filter{ !(name == nil) }
221
- ```
222
-
223
- * Missing filter
224
-
225
- ```json
226
- {"missing": {"field": "name", "existence": true, "null_value": false}}
227
- {"missing": {"field": "name", "existence": true, "null_value": true}}
228
- {"missing": {"field": "name", "existence": false, "null_value": true}}
229
- ```
230
-
231
- ```ruby
232
- UsersIndex.filter{ !name }
233
- UsersIndex.filter{ !name? }
234
- UsersIndex.filter{ name == nil }
235
- ```
236
-
237
- * Range
238
-
239
- ```json
240
- {"range": {"age": {"gt": 42}}}
241
- {"range": {"age": {"gte": 42}}}
242
- {"range": {"age": {"lt": 42}}}
243
- {"range": {"age": {"lte": 42}}}
244
-
245
- {"range": {"age": {"gt": 40, "lt": 50}}}
246
- {"range": {"age": {"gte": 40, "lte": 50}}}
247
-
248
- {"range": {"age": {"gt": 40, "lte": 50}}}
249
- {"range": {"age": {"gte": 40, "lt": 50}}}
250
- ```
251
-
252
- ```ruby
253
- UsersIndex.filter{ age > 42 }
254
- UsersIndex.filter{ age >= 42 }
255
- UsersIndex.filter{ age < 42 }
256
- UsersIndex.filter{ age <= 42 }
257
-
258
- UsersIndex.filter{ age == (40..50) }
259
- UsersIndex.filter{ (age > 40) & (age < 50) }
260
- UsersIndex.filter{ age == [40..50] }
261
- UsersIndex.filter{ (age >= 40) & (age <= 50) }
262
-
263
- UsersIndex.filter{ (age > 40) & (age <= 50) }
264
- UsersIndex.filter{ (age >= 40) & (age < 50) }
265
- ```
266
-
267
- * Bool filter
268
-
269
- ```json
270
- {"bool": {
271
- "must": [{"term": {"name": "Name"}}],
272
- "should": [{"term": {"age": 42}}, {"term": {"age": 45}}]
273
- }}
274
- ```
275
-
276
- ```ruby
277
- UsersIndex.filter{ must(name == 'Name').should(age == 42, age == 45) }
278
- ```
279
-
280
- * And filter
281
-
282
- ```json
283
- {"and": [{"term": {"name": "Name"}}, {"range": {"age": {"lt": 42}}}]}
284
- ```
285
-
286
- ```ruby
287
- UsersIndex.filter{ (name == 'Name') & (age < 42) }
288
- ```
289
-
290
- * Or filter
291
-
292
- ```json
293
- {"or": [{"term": {"name": "Name"}}, {"range": {"age": {"lt": 42}}}]}
294
- ```
295
-
296
- ```ruby
297
- UsersIndex.filter{ (name == 'Name') | (age < 42) }
298
- ```
299
-
300
- ```json
301
- {"not": {"term": {"name": "Name"}}}
302
- {"not": {"range": {"age": {"lt": 42}}}}
303
- ```
304
-
305
- ```ruby
306
- UsersIndex.filter{ !(name == 'Name') } # or UsersIndex.filter{ name != 'Name' }
307
- UsersIndex.filter{ !(age < 42) }
308
- ```
309
-
310
- * Match all filter
311
-
312
- ```json
313
- {"match_all": {}}
314
- ```
315
-
316
- ```ruby
317
- UsersIndex.filter{ match_all }
318
- ```
319
-
320
- * Has child filter
321
-
322
- ```json
323
- {"has_child": {"type": "blog_tag", "query": {"term": {"tag": "something"}}}
324
- {"has_child": {"type": "comment", "filter": {"term": {"user": "john"}}}
325
- ```
326
-
327
- ```ruby
328
- UsersIndex.filter{ has_child(:blog_tag).query(term: {tag: 'something'}) }
329
- UsersIndex.filter{ has_child(:comment).filter{ user == 'john' } }
330
- ```
331
-
332
- * Has parent filter
333
-
334
- ```json
335
- {"has_parent": {"type": "blog", "query": {"term": {"tag": "something"}}}}
336
- {"has_parent": {"type": "blog", "filter": {"term": {"text": "bonsai three"}}}}
337
- ```
338
-
339
- ```ruby
340
- UsersIndex.filter{ has_parent(:blog).query(term: {tag: 'something'}) }
341
- UsersIndex.filter{ has_parent(:blog).filter{ text == 'bonsai three' } }
342
- ```
343
-
344
- See [filters.rb](lib/chewy/query/filters.rb) for more details.
345
-
346
- ## Faceting
347
-
348
- Facets are an optional sidechannel you can request from Elasticsearch describing certain fields of the resulting collection. The most common use for facets is to allow the user to continue filtering specifically within the subset, as opposed to the global index.
349
-
350
- For instance, let's request the `country` field as a facet along with our users collection. We can do this with the #facets method like so:
351
-
352
- ```ruby
353
- UsersIndex.filter{ [...] }.facets({countries: {terms: {field: 'country'}}})
354
- ```
355
-
356
- Let's look at what we asked from Elasticsearch. The facets setter method accepts a hash. You can choose custom/semantic key names for this hash for your own convenience (in this case I used the plural version of the actual field), in our case `countries`. The following nested hash tells ES to grab and aggregate values (terms) from the `country` field on our indexed records.
357
-
358
- The response will include the `:facets` sidechannel:
359
-
360
- ```
361
- < { ... ,"facets":{"countries":{"_type":"terms","missing":?,"total":?,"other":?,"terms":[{"term":"USA","count":?},{"term":"Brazil","count":?}, ...}}
362
- ```
363
-
364
- ## Aggregations
365
-
366
- Aggregations are part of the optional sidechannel that can be requested with a query.
367
-
368
- You interact with aggregations using the composable #aggregations method (or its alias #aggs)
369
-
370
- Let's look at an example.
371
-
372
- ```ruby
373
- class UsersIndex < Chewy::Index
374
- define_type User do
375
- field :name
376
- field :rating
377
- end
378
- end
379
-
380
- all_johns = UsersIndex::User.filter { name == 'john' }.aggs({ avg_rating: { avg: { field: 'rating' } } })
381
-
382
- avg_johns_rating = all_johns.aggs
383
- # => {"avg_rating"=>{"value"=>3.5}}
384
- ```
385
-
386
- It is convenient to name aggregations that you intend to reuse regularly. This is achieve with the .aggregation method,
387
- which is also available under the .agg alias method.
388
-
389
- Here's the same example from before
390
-
391
- ```ruby
392
- class UsersIndex < Chewy::Index
393
- define_type User do
394
- field :name
395
- field :rating, type: "long"
396
- agg :avg_rating do
397
- { avg: { field: 'rating' } }
398
- end
399
- end
400
- end
401
-
402
- all_johns = UsersIndex::User.filter { name == 'john' }.aggs(:avg_rating)
403
-
404
- avg_johns_rating = all_johns.aggs
405
- # => {"avg_rating"=>{"value"=>3.5}}
406
- ```
407
-
408
- It is possible to run into collisions between named aggregations. This occurs when there is more than one aggregation
409
- with the same name. To explicitly reference an aggregation you provide a string to the #aggs method of the form:
410
- `index_name#document_type.aggregation_name`
411
-
412
- Consider this example where there are two separate aggregations named `avg_rating`
413
-
414
- ```ruby
415
- class UsersIndex < Chewy::Index
416
- define_type User do
417
- field :name
418
- field :rating, type: "long"
419
- agg :avg_rating do
420
- { avg: { field: 'rating' } }
421
- end
422
- end
423
- define_type Post do
424
- field :title
425
- field :body
426
- field :comments do
427
- field :message
428
- field :rating, type: "long"
429
- end
430
- agg :avg_rating do
431
- { avg: { field: 'comments.rating' } }
432
- end
433
- end
434
- end
435
-
436
- all_docs = UsersIndex.filter {match_all}.aggs("users#user.avg_rating")
437
- all_docs.aggs
438
- # => {"users#user.avg_rating"=>{"value"=>3.5}}
439
- ```
440
-
441
- ## Script fields
442
-
443
- Script fields allow you to execute Elasticsearch's scripting languages such as groovy and javascript. More about supported languages and what scripting is [here](https://www.elastic.co/guide/en/elasticsearch/reference/0.90/modules-scripting.html). This feature allows you to calculate the distance between geo points, for example. This is how to use the DSL:
444
-
445
- ```ruby
446
- UsersIndex.script_fields(
447
- distance: {
448
- params: {
449
- lat: 37.569976,
450
- lon: -122.351591
451
- },
452
- script: "doc['coordinates'].distanceInMiles(lat, lon)"
453
- }
454
- )
455
- ```
456
- Here, `coordinates` is a field with type `geo_point`. There will be a `distance` field for the index's model in the search result.
457
-
458
- ## Script scoring
459
-
460
- Script scoring is used to score the search results. All scores are added to the search request and combined according to boost mode and score mode. This can be useful if, for example, a score function is computationally expensive and it is sufficient to compute the score on a filtered set of documents. For example, you might want to multiply the score by another numeric field in the doc:
461
-
462
- ```ruby
463
- UsersIndex.script_score("_score * doc['my_numeric_field'].value")
464
- ```
465
-
466
- ## Boost Factor
467
-
468
- Boost factors are a way to add a boost to a query where documents match the filter. If you have some users who are experts and some who are regular users, you might want to give the experts a higher score and boost to the top of the search results. You can accomplish this by using the #boost_factor method and adding a boost score of 5 for an expert user:
469
-
470
- ```ruby
471
- UsersIndex.boost_factor(5, filter: {term: {type: 'Expert'}})
472
- ```
473
-
474
- ## Objects loading
475
-
476
- It is possible to load source objects from the database for every search result:
477
-
478
- ```ruby
479
- scope = UsersIndex.filter(range: {rating: {gte: 100}})
480
-
481
- scope.load # => scope is marked to return User instances array
482
- scope.load.query(...) # => since objects are loaded lazily you can complete scope
483
- scope.load(user: { scope: ->{ includes(:country) }}) # you can also pass loading scopes for each
484
- # possibly returned type
485
- scope.load(user: { scope: User.includes(:country) }) # the second scope passing way.
486
- scope.load(scope: ->{ includes(:country) }) # and more common scope applied to every loaded object type.
487
-
488
- scope.only(:id).load # it is optimal to request ids only if you are not planning to use type objects
489
- ```
490
-
491
- The `preload` method takes the same options as `load` and ORM/ODM objects will be loaded, but the scope will still return an array of Chewy wrappers. To access real objects use the `_object` wrapper method:
492
-
493
- ```ruby
494
- UsersIndex.filter(range: {rating: {gte: 100}}).preload(...).query(...).map(&:_object)
495
- ```
496
-
497
- See [loading.rb](lib/chewy/query/loading.rb) for more details.
@@ -1,15 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "activerecord", "~> 4.0.0"
6
- gem "activesupport", "~> 4.0.0"
7
- gem "resque", require: false
8
- gem "shoryuken", require: false
9
- gem "aws-sdk-sqs", require: false
10
- gem "sidekiq", require: false
11
- gem "kaminari", "~> 0.17.0", require: false
12
- gem "will_paginate", require: false
13
- gem "parallel", require: false
14
-
15
- gemspec path: "../"
@@ -1,15 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "activerecord", "~> 4.1.0"
6
- gem "activesupport", "~> 4.1.0"
7
- gem "resque", require: false
8
- gem "shoryuken", require: false
9
- gem "aws-sdk-sqs", require: false
10
- gem "sidekiq", require: false
11
- gem "kaminari", "~> 0.17.0", require: false
12
- gem "will_paginate", require: false
13
- gem "parallel", require: false
14
-
15
- gemspec path: "../"
@@ -1,16 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "activerecord", "~> 4.2.0"
6
- gem "activesupport", "~> 4.2.0"
7
- gem "activejob", "~> 4.2.0"
8
- gem "resque", require: false
9
- gem "shoryuken", require: false
10
- gem "aws-sdk-sqs", require: false
11
- gem "sidekiq", require: false
12
- gem "kaminari", "~> 0.17.0", require: false
13
- gem "will_paginate", require: false
14
- gem "parallel", require: false
15
-
16
- gemspec path: "../"
@@ -1,16 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "mongoid", "~> 5.2.0"
6
- gem "activesupport", "~> 4.2.0"
7
- gem "activejob", "~> 4.2.0"
8
- gem "resque", require: false
9
- gem "shoryuken", require: false
10
- gem "aws-sdk-sqs", require: false
11
- gem "sidekiq", require: false
12
- gem "kaminari", "~> 0.17.0", require: false
13
- gem "will_paginate", require: false
14
- gem "parallel", require: false
15
-
16
- gemspec path: "../"
@@ -1,16 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "activerecord", "~> 5.0.0"
6
- gem "activesupport", "~> 5.0.0"
7
- gem "activejob", "~> 5.0.0"
8
- gem "resque", require: false
9
- gem "shoryuken", require: false
10
- gem "aws-sdk-sqs", require: false
11
- gem "sidekiq", require: false
12
- gem "kaminari-core", "~> 1.1.0", require: false
13
- gem "will_paginate", require: false
14
- gem "parallel", require: false
15
-
16
- gemspec path: "../"
@@ -1,16 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "mongoid", "~> 6.1.0"
6
- gem "activesupport", "~> 5.0.0"
7
- gem "activejob", "~> 5.0.0"
8
- gem "resque", require: false
9
- gem "shoryuken", require: false
10
- gem "aws-sdk-sqs", require: false
11
- gem "sidekiq", require: false
12
- gem "kaminari-core", "~> 1.1.0", require: false
13
- gem "will_paginate", require: false
14
- gem "parallel", require: false
15
-
16
- gemspec path: "../"
@@ -1,16 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "activerecord", "~> 5.1.0"
6
- gem "activesupport", "~> 5.1.0"
7
- gem "activejob", "~> 5.1.0"
8
- gem "resque", require: false
9
- gem "shoryuken", require: false
10
- gem "aws-sdk-sqs", require: false
11
- gem "sidekiq", require: false
12
- gem "kaminari-core", "~> 1.1.0", require: false
13
- gem "will_paginate", require: false
14
- gem "parallel", require: false
15
-
16
- gemspec path: "../"