chewy 0.9.0 → 5.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (275) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +214 -0
  3. data/.gitignore +1 -0
  4. data/.rubocop.yml +41 -19
  5. data/.rubocop_todo.yml +2 -2
  6. data/.yardopts +5 -0
  7. data/Appraisals +58 -28
  8. data/CHANGELOG.md +153 -12
  9. data/Gemfile +20 -12
  10. data/LEGACY_DSL.md +497 -0
  11. data/LICENSE.txt +1 -1
  12. data/README.md +338 -528
  13. data/chewy.gemspec +11 -12
  14. data/gemfiles/rails.5.2.activerecord.gemfile +17 -0
  15. data/gemfiles/rails.5.2.mongoid.6.4.gemfile +17 -0
  16. data/gemfiles/rails.6.0.activerecord.gemfile +17 -0
  17. data/gemfiles/rails.6.1.activerecord.gemfile +19 -0
  18. data/gemfiles/ruby3.gemfile +10 -0
  19. data/gemfiles/sequel.4.45.gemfile +11 -0
  20. data/lib/chewy.rb +79 -44
  21. data/lib/chewy/backports/duplicable.rb +1 -1
  22. data/lib/chewy/config.rb +43 -17
  23. data/lib/chewy/errors.rb +2 -2
  24. data/lib/chewy/fields/base.rb +56 -31
  25. data/lib/chewy/fields/root.rb +44 -11
  26. data/lib/chewy/index.rb +237 -149
  27. data/lib/chewy/index/actions.rb +100 -35
  28. data/lib/chewy/index/aliases.rb +2 -1
  29. data/lib/chewy/index/settings.rb +11 -5
  30. data/lib/chewy/index/specification.rb +60 -0
  31. data/lib/chewy/journal.rb +40 -92
  32. data/lib/chewy/minitest/helpers.rb +6 -6
  33. data/lib/chewy/minitest/search_index_receiver.rb +17 -17
  34. data/lib/chewy/query.rb +182 -122
  35. data/lib/chewy/query/compose.rb +13 -13
  36. data/lib/chewy/query/criteria.rb +13 -13
  37. data/lib/chewy/query/filters.rb +21 -4
  38. data/lib/chewy/query/loading.rb +1 -2
  39. data/lib/chewy/query/nodes/and.rb +2 -2
  40. data/lib/chewy/query/nodes/bool.rb +1 -1
  41. data/lib/chewy/query/nodes/equal.rb +2 -2
  42. data/lib/chewy/query/nodes/exists.rb +1 -1
  43. data/lib/chewy/query/nodes/field.rb +1 -1
  44. data/lib/chewy/query/nodes/has_relation.rb +2 -2
  45. data/lib/chewy/query/nodes/match_all.rb +1 -1
  46. data/lib/chewy/query/nodes/missing.rb +1 -1
  47. data/lib/chewy/query/nodes/not.rb +2 -2
  48. data/lib/chewy/query/nodes/or.rb +2 -2
  49. data/lib/chewy/query/nodes/prefix.rb +1 -1
  50. data/lib/chewy/query/nodes/query.rb +2 -2
  51. data/lib/chewy/query/nodes/range.rb +4 -4
  52. data/lib/chewy/query/nodes/regexp.rb +4 -4
  53. data/lib/chewy/query/nodes/script.rb +3 -3
  54. data/lib/chewy/query/pagination.rb +10 -1
  55. data/lib/chewy/railtie.rb +4 -3
  56. data/lib/chewy/rake_helper.rb +265 -48
  57. data/lib/chewy/rspec/update_index.rb +33 -27
  58. data/lib/chewy/search.rb +79 -26
  59. data/lib/chewy/search/loader.rb +83 -0
  60. data/lib/chewy/{query → search}/pagination/kaminari.rb +13 -5
  61. data/lib/chewy/search/pagination/will_paginate.rb +43 -0
  62. data/lib/chewy/search/parameters.rb +168 -0
  63. data/lib/chewy/search/parameters/aggs.rb +16 -0
  64. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  65. data/lib/chewy/search/parameters/concerns/bool_storage.rb +24 -0
  66. data/lib/chewy/search/parameters/concerns/hash_storage.rb +23 -0
  67. data/lib/chewy/search/parameters/concerns/integer_storage.rb +14 -0
  68. data/lib/chewy/search/parameters/concerns/query_storage.rb +238 -0
  69. data/lib/chewy/search/parameters/concerns/string_array_storage.rb +23 -0
  70. data/lib/chewy/search/parameters/concerns/string_storage.rb +14 -0
  71. data/lib/chewy/search/parameters/docvalue_fields.rb +12 -0
  72. data/lib/chewy/search/parameters/explain.rb +16 -0
  73. data/lib/chewy/search/parameters/filter.rb +47 -0
  74. data/lib/chewy/search/parameters/highlight.rb +16 -0
  75. data/lib/chewy/search/parameters/indices.rb +123 -0
  76. data/lib/chewy/search/parameters/indices_boost.rb +52 -0
  77. data/lib/chewy/search/parameters/limit.rb +17 -0
  78. data/lib/chewy/search/parameters/load.rb +32 -0
  79. data/lib/chewy/search/parameters/min_score.rb +16 -0
  80. data/lib/chewy/search/parameters/none.rb +27 -0
  81. data/lib/chewy/search/parameters/offset.rb +17 -0
  82. data/lib/chewy/search/parameters/order.rb +64 -0
  83. data/lib/chewy/search/parameters/post_filter.rb +19 -0
  84. data/lib/chewy/search/parameters/preference.rb +16 -0
  85. data/lib/chewy/search/parameters/profile.rb +16 -0
  86. data/lib/chewy/search/parameters/query.rb +19 -0
  87. data/lib/chewy/search/parameters/request_cache.rb +27 -0
  88. data/lib/chewy/search/parameters/rescore.rb +29 -0
  89. data/lib/chewy/search/parameters/script_fields.rb +16 -0
  90. data/lib/chewy/search/parameters/search_after.rb +20 -0
  91. data/lib/chewy/search/parameters/search_type.rb +16 -0
  92. data/lib/chewy/search/parameters/source.rb +73 -0
  93. data/lib/chewy/search/parameters/storage.rb +95 -0
  94. data/lib/chewy/search/parameters/stored_fields.rb +63 -0
  95. data/lib/chewy/search/parameters/suggest.rb +16 -0
  96. data/lib/chewy/search/parameters/terminate_after.rb +16 -0
  97. data/lib/chewy/search/parameters/timeout.rb +16 -0
  98. data/lib/chewy/search/parameters/track_scores.rb +16 -0
  99. data/lib/chewy/search/parameters/types.rb +20 -0
  100. data/lib/chewy/search/parameters/version.rb +16 -0
  101. data/lib/chewy/search/query_proxy.rb +257 -0
  102. data/lib/chewy/search/request.rb +1046 -0
  103. data/lib/chewy/search/response.rb +119 -0
  104. data/lib/chewy/search/scoping.rb +50 -0
  105. data/lib/chewy/search/scrolling.rb +134 -0
  106. data/lib/chewy/stash.rb +79 -0
  107. data/lib/chewy/strategy.rb +10 -3
  108. data/lib/chewy/strategy/active_job.rb +2 -1
  109. data/lib/chewy/strategy/atomic.rb +2 -4
  110. data/lib/chewy/strategy/bypass.rb +1 -1
  111. data/lib/chewy/strategy/resque.rb +1 -0
  112. data/lib/chewy/strategy/shoryuken.rb +40 -0
  113. data/lib/chewy/strategy/sidekiq.rb +13 -3
  114. data/lib/chewy/type.rb +29 -7
  115. data/lib/chewy/type/actions.rb +26 -2
  116. data/lib/chewy/type/adapter/active_record.rb +44 -29
  117. data/lib/chewy/type/adapter/base.rb +27 -7
  118. data/lib/chewy/type/adapter/mongoid.rb +19 -10
  119. data/lib/chewy/type/adapter/object.rb +187 -26
  120. data/lib/chewy/type/adapter/orm.rb +59 -32
  121. data/lib/chewy/type/adapter/sequel.rb +33 -19
  122. data/lib/chewy/type/crutch.rb +1 -1
  123. data/lib/chewy/type/import.rb +146 -191
  124. data/lib/chewy/type/import/bulk_builder.rb +122 -0
  125. data/lib/chewy/type/import/bulk_request.rb +78 -0
  126. data/lib/chewy/type/import/journal_builder.rb +45 -0
  127. data/lib/chewy/type/import/routine.rb +138 -0
  128. data/lib/chewy/type/mapping.rb +51 -35
  129. data/lib/chewy/type/observe.rb +17 -13
  130. data/lib/chewy/type/syncer.rb +222 -0
  131. data/lib/chewy/type/witchcraft.rb +32 -16
  132. data/lib/chewy/type/wrapper.rb +30 -4
  133. data/lib/chewy/version.rb +1 -1
  134. data/lib/sequel/plugins/chewy_observe.rb +4 -19
  135. data/lib/tasks/chewy.rake +84 -26
  136. data/spec/chewy/config_spec.rb +98 -1
  137. data/spec/chewy/fields/base_spec.rb +170 -135
  138. data/spec/chewy/fields/root_spec.rb +124 -20
  139. data/spec/chewy/fields/time_fields_spec.rb +2 -3
  140. data/spec/chewy/index/actions_spec.rb +214 -52
  141. data/spec/chewy/index/aliases_spec.rb +2 -2
  142. data/spec/chewy/index/settings_spec.rb +67 -38
  143. data/spec/chewy/index/specification_spec.rb +169 -0
  144. data/spec/chewy/index_spec.rb +108 -64
  145. data/spec/chewy/journal_spec.rb +150 -55
  146. data/spec/chewy/minitest/helpers_spec.rb +4 -4
  147. data/spec/chewy/minitest/search_index_receiver_spec.rb +1 -1
  148. data/spec/chewy/query/criteria_spec.rb +179 -179
  149. data/spec/chewy/query/filters_spec.rb +16 -16
  150. data/spec/chewy/query/loading_spec.rb +22 -20
  151. data/spec/chewy/query/nodes/and_spec.rb +2 -2
  152. data/spec/chewy/query/nodes/bool_spec.rb +4 -4
  153. data/spec/chewy/query/nodes/equal_spec.rb +19 -19
  154. data/spec/chewy/query/nodes/exists_spec.rb +6 -6
  155. data/spec/chewy/query/nodes/has_child_spec.rb +19 -19
  156. data/spec/chewy/query/nodes/has_parent_spec.rb +19 -19
  157. data/spec/chewy/query/nodes/missing_spec.rb +5 -5
  158. data/spec/chewy/query/nodes/not_spec.rb +4 -2
  159. data/spec/chewy/query/nodes/or_spec.rb +2 -2
  160. data/spec/chewy/query/nodes/prefix_spec.rb +5 -5
  161. data/spec/chewy/query/nodes/query_spec.rb +2 -2
  162. data/spec/chewy/query/nodes/range_spec.rb +18 -18
  163. data/spec/chewy/query/nodes/raw_spec.rb +1 -1
  164. data/spec/chewy/query/nodes/regexp_spec.rb +14 -14
  165. data/spec/chewy/query/nodes/script_spec.rb +4 -4
  166. data/spec/chewy/query/pagination/kaminari_spec.rb +3 -55
  167. data/spec/chewy/query/pagination/will_paginate_spec.rb +5 -0
  168. data/spec/chewy/query/pagination_spec.rb +25 -21
  169. data/spec/chewy/query_spec.rb +503 -561
  170. data/spec/chewy/rake_helper_spec.rb +381 -0
  171. data/spec/chewy/repository_spec.rb +4 -4
  172. data/spec/chewy/rspec/update_index_spec.rb +89 -56
  173. data/spec/chewy/runtime_spec.rb +2 -2
  174. data/spec/chewy/search/loader_spec.rb +117 -0
  175. data/spec/chewy/search/pagination/kaminari_examples.rb +71 -0
  176. data/spec/chewy/search/pagination/kaminari_spec.rb +21 -0
  177. data/spec/chewy/search/pagination/will_paginate_examples.rb +63 -0
  178. data/spec/chewy/search/pagination/will_paginate_spec.rb +23 -0
  179. data/spec/chewy/search/parameters/aggs_spec.rb +5 -0
  180. data/spec/chewy/search/parameters/bool_storage_examples.rb +53 -0
  181. data/spec/chewy/search/parameters/docvalue_fields_spec.rb +5 -0
  182. data/spec/chewy/search/parameters/explain_spec.rb +5 -0
  183. data/spec/chewy/search/parameters/filter_spec.rb +5 -0
  184. data/spec/chewy/search/parameters/hash_storage_examples.rb +59 -0
  185. data/spec/chewy/search/parameters/highlight_spec.rb +5 -0
  186. data/spec/chewy/search/parameters/indices_spec.rb +191 -0
  187. data/spec/chewy/search/parameters/integer_storage_examples.rb +32 -0
  188. data/spec/chewy/search/parameters/limit_spec.rb +5 -0
  189. data/spec/chewy/search/parameters/load_spec.rb +60 -0
  190. data/spec/chewy/search/parameters/min_score_spec.rb +32 -0
  191. data/spec/chewy/search/parameters/none_spec.rb +5 -0
  192. data/spec/chewy/search/parameters/offset_spec.rb +5 -0
  193. data/spec/chewy/search/parameters/order_spec.rb +65 -0
  194. data/spec/chewy/search/parameters/post_filter_spec.rb +5 -0
  195. data/spec/chewy/search/parameters/preference_spec.rb +5 -0
  196. data/spec/chewy/search/parameters/profile_spec.rb +5 -0
  197. data/spec/chewy/search/parameters/query_spec.rb +5 -0
  198. data/spec/chewy/search/parameters/query_storage_examples.rb +388 -0
  199. data/spec/chewy/search/parameters/request_cache_spec.rb +67 -0
  200. data/spec/chewy/search/parameters/rescore_spec.rb +62 -0
  201. data/spec/chewy/search/parameters/script_fields_spec.rb +5 -0
  202. data/spec/chewy/search/parameters/search_after_spec.rb +32 -0
  203. data/spec/chewy/search/parameters/search_type_spec.rb +5 -0
  204. data/spec/chewy/search/parameters/source_spec.rb +156 -0
  205. data/spec/chewy/search/parameters/storage_spec.rb +60 -0
  206. data/spec/chewy/search/parameters/stored_fields_spec.rb +126 -0
  207. data/spec/chewy/search/parameters/string_array_storage_examples.rb +63 -0
  208. data/spec/chewy/search/parameters/string_storage_examples.rb +32 -0
  209. data/spec/chewy/search/parameters/suggest_spec.rb +5 -0
  210. data/spec/chewy/search/parameters/terminate_after_spec.rb +5 -0
  211. data/spec/chewy/search/parameters/timeout_spec.rb +5 -0
  212. data/spec/chewy/search/parameters/track_scores_spec.rb +5 -0
  213. data/spec/chewy/search/parameters/types_spec.rb +5 -0
  214. data/spec/chewy/search/parameters/version_spec.rb +5 -0
  215. data/spec/chewy/search/parameters_spec.rb +147 -0
  216. data/spec/chewy/search/query_proxy_spec.rb +68 -0
  217. data/spec/chewy/search/request_spec.rb +685 -0
  218. data/spec/chewy/search/response_spec.rb +198 -0
  219. data/spec/chewy/search/scrolling_spec.rb +169 -0
  220. data/spec/chewy/search_spec.rb +33 -16
  221. data/spec/chewy/stash_spec.rb +95 -0
  222. data/spec/chewy/strategy/active_job_spec.rb +21 -2
  223. data/spec/chewy/strategy/resque_spec.rb +6 -0
  224. data/spec/chewy/strategy/shoryuken_spec.rb +70 -0
  225. data/spec/chewy/strategy/sidekiq_spec.rb +13 -1
  226. data/spec/chewy/strategy_spec.rb +6 -6
  227. data/spec/chewy/type/actions_spec.rb +29 -10
  228. data/spec/chewy/type/adapter/active_record_spec.rb +203 -91
  229. data/spec/chewy/type/adapter/mongoid_spec.rb +112 -54
  230. data/spec/chewy/type/adapter/object_spec.rb +101 -28
  231. data/spec/chewy/type/adapter/sequel_spec.rb +149 -82
  232. data/spec/chewy/type/import/bulk_builder_spec.rb +279 -0
  233. data/spec/chewy/type/import/bulk_request_spec.rb +102 -0
  234. data/spec/chewy/type/import/journal_builder_spec.rb +95 -0
  235. data/spec/chewy/type/import/routine_spec.rb +110 -0
  236. data/spec/chewy/type/import_spec.rb +356 -271
  237. data/spec/chewy/type/mapping_spec.rb +96 -29
  238. data/spec/chewy/type/observe_spec.rb +9 -5
  239. data/spec/chewy/type/syncer_spec.rb +123 -0
  240. data/spec/chewy/type/witchcraft_spec.rb +61 -29
  241. data/spec/chewy/type/wrapper_spec.rb +63 -23
  242. data/spec/chewy/type_spec.rb +28 -7
  243. data/spec/chewy_spec.rb +75 -7
  244. data/spec/spec_helper.rb +17 -3
  245. data/spec/support/active_record.rb +5 -1
  246. data/spec/support/class_helpers.rb +0 -14
  247. data/spec/support/mongoid.rb +15 -3
  248. data/spec/support/sequel.rb +6 -1
  249. metadata +219 -58
  250. data/.travis.yml +0 -36
  251. data/gemfiles/rails.3.2.activerecord.gemfile +0 -16
  252. data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +0 -15
  253. data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +0 -15
  254. data/gemfiles/rails.4.2.activerecord.gemfile +0 -17
  255. data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +0 -16
  256. data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +0 -16
  257. data/gemfiles/rails.4.2.mongoid.4.0.gemfile +0 -16
  258. data/gemfiles/rails.4.2.mongoid.4.0.kaminari.gemfile +0 -15
  259. data/gemfiles/rails.4.2.mongoid.4.0.will_paginate.gemfile +0 -15
  260. data/gemfiles/rails.4.2.mongoid.5.1.gemfile +0 -16
  261. data/gemfiles/rails.4.2.mongoid.5.1.kaminari.gemfile +0 -15
  262. data/gemfiles/rails.4.2.mongoid.5.1.will_paginate.gemfile +0 -15
  263. data/gemfiles/rails.5.0.activerecord.gemfile +0 -17
  264. data/gemfiles/rails.5.0.activerecord.kaminari.gemfile +0 -16
  265. data/gemfiles/rails.5.0.activerecord.will_paginate.gemfile +0 -16
  266. data/gemfiles/sequel.4.38.gemfile +0 -14
  267. data/lib/chewy/journal/apply.rb +0 -31
  268. data/lib/chewy/journal/clean.rb +0 -24
  269. data/lib/chewy/journal/entry.rb +0 -83
  270. data/lib/chewy/journal/query.rb +0 -87
  271. data/lib/chewy/query/pagination/will_paginate.rb +0 -27
  272. data/lib/chewy/query/scoping.rb +0 -20
  273. data/spec/chewy/journal/apply_spec.rb +0 -120
  274. data/spec/chewy/journal/entry_spec.rb +0 -237
  275. data/spec/chewy/query/pagination/will_paginage_spec.rb +0 -59
@@ -6,14 +6,14 @@ module Chewy
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  # Assert that an index *changes* during a block.
9
- # @param (Chewy::Type) index the index / type to watch, eg EntitiesIndex::Entity.
10
- # @param (Symbol) strategy the Chewy strategy to use around the block. See Chewy docs.
11
- # @param (boolean) assert the index changes
12
- # @param (boolean) bypass_actual_index
9
+ # @param index [Chewy::Type] the index / type to watch, eg EntitiesIndex::Entity.
10
+ # @param strategy [Symbol] the Chewy strategy to use around the block. See Chewy docs.
11
+ # @param bypass_actual_index [true, false]
13
12
  # True to preempt the http call to Elastic, false otherwise.
14
13
  # Should be set to true unless actually testing search functionality.
15
14
  #
16
- # @return (SearchIndexReceiver) for optional further assertions on the nature of the index changes.
15
+ # @return [SearchIndexReceiver] for optional further assertions on the nature of the index changes.
16
+ #
17
17
  def assert_indexes(index, strategy: :atomic, bypass_actual_index: true)
18
18
  type = Chewy.derive_type index
19
19
  receiver = SearchIndexReceiver.new
@@ -43,7 +43,7 @@ module Chewy
43
43
 
44
44
  # Run indexing for the database changes during the block provided.
45
45
  # By default, indexing is run at the end of the block.
46
- # @param (Symbol) strategy the Chewy index update strategy see Chewy docs.
46
+ # @param strategy [Symbol] the Chewy index update strategy see Chewy docs.
47
47
  def run_indexing(strategy: :atomic)
48
48
  Chewy.strategy strategy do
49
49
  yield
@@ -1,17 +1,17 @@
1
1
  # Test helper class to provide minitest hooks for Chewy::Index testing.
2
2
  #
3
3
  # @note Intended to be used in conjunction with a test helper which mocks over the #bulk
4
- # method on a Chewy::Type class. (See SearchTestHelper)
4
+ # method on a {Chewy::Type} class. (See SearchTestHelper)
5
5
  #
6
- # The class will capture the data from the *param on the Chewy::Type#bulk method and
6
+ # The class will capture the data from the *param on the Chewy::Type.bulk method and
7
7
  # aggregate the data for test analysis.
8
8
  class SearchIndexReceiver
9
9
  def initialize
10
10
  @mutations = {}
11
11
  end
12
12
 
13
- # @param bulk_params the bulk_params that should be sent to the Chewy::Type#bulk method.
14
- # @param (Chewy::Type) type the Index::Type executing this query.
13
+ # @param bulk_params [Hash] the bulk_params that should be sent to the Chewy::Type.bulk method.
14
+ # @param type [Chewy::Type] the type executing this query.
15
15
  def catch(bulk_params, type)
16
16
  Array.wrap(bulk_params).map { |y| y[:body] }.flatten.each do |update|
17
17
  if update[:delete]
@@ -22,8 +22,8 @@ class SearchIndexReceiver
22
22
  end
23
23
  end
24
24
 
25
- # @param index return only index requests to the specified Chewy::Type index.
26
- # @return the index changes captured by the mock.
25
+ # @param index [Chewy::Index] return only index requests to the specified {Chewy::Type} index.
26
+ # @return [Hash] the index changes captured by the mock.
27
27
  def indexes_for(index = nil)
28
28
  if index
29
29
  mutation_for(index).indexes
@@ -35,8 +35,8 @@ class SearchIndexReceiver
35
35
  end
36
36
  alias_method :indexes, :indexes_for
37
37
 
38
- # @param index return only delete requests to the specified Chewy::Type index.
39
- # @return the index deletes captured by the mock.
38
+ # @param index [Chewy::Index] return only delete requests to the specified {Chewy::Type} index.
39
+ # @return [Hash] the index deletes captured by the mock.
40
40
  def deletes_for(index = nil)
41
41
  if index
42
42
  mutation_for(index).deletes
@@ -49,22 +49,22 @@ class SearchIndexReceiver
49
49
  alias_method :deletes, :deletes_for
50
50
 
51
51
  # Check to see if a given object has been indexed.
52
- # @param (#id) obj the object to look for.
53
- # @param Chewy::Type what type the object should be indexed as.
54
- # @return bool if the object was indexed.
52
+ # @param obj [#id] obj the object to look for.
53
+ # @param type [Chewy::Type] what type the object should be indexed as.
54
+ # @return [true, false] if the object was indexed.
55
55
  def indexed?(obj, type)
56
56
  indexes_for(type).map { |i| i[:_id] }.include? obj.id
57
57
  end
58
58
 
59
59
  # Check to see if a given object has been deleted.
60
- # @param (#id) obj the object to look for.
61
- # @param Chewy::Type what type the object should have been deleted from.
62
- # @return bool if the object was deleted.
60
+ # @param obj [#id] obj the object to look for.
61
+ # @param type [Chewy::Type] what type the object should have been deleted from.
62
+ # @return [true, false] if the object was deleted.
63
63
  def deleted?(obj, type)
64
64
  deletes_for(type).include? obj.id
65
65
  end
66
66
 
67
- # @return a list of Chewy::Type indexes changed.
67
+ # @return [Array<Chewy::Type>] a list of types indexes changed.
68
68
  def updated_indexes
69
69
  @mutations.keys
70
70
  end
@@ -72,8 +72,8 @@ class SearchIndexReceiver
72
72
  private
73
73
 
74
74
  # Get the mutation object for a given type.
75
- # @param (Chewy::Type) type the index type to fetch.
76
- # @return (#indexes, #deletes) an object with a list of indexes and a list of deletes.
75
+ # @param type [Chewy::Type] the index type to fetch.
76
+ # @return [#indexes, #deletes] an object with a list of indexes and a list of deletes.
77
77
  def mutation_for(type)
78
78
  @mutations[type] ||= OpenStruct.new(indexes: [], deletes: [])
79
79
  end
@@ -1,6 +1,5 @@
1
1
  require 'chewy/query/criteria'
2
2
  require 'chewy/query/filters'
3
- require 'chewy/query/scoping'
4
3
  require 'chewy/query/loading'
5
4
  require 'chewy/query/pagination'
6
5
 
@@ -9,14 +8,24 @@ module Chewy
9
8
  # chainable DSL. Queries are lazy evaluated and might be merged.
10
9
  # The same DSL is used for whole index or individual types query build.
11
10
  #
11
+ # @example
12
12
  # UsersIndex.filter{ age < 42 }.query(text: {name: 'Alex'}).limit(20)
13
13
  # UsersIndex::User.filter{ age < 42 }.query(text: {name: 'Alex'}).limit(20)
14
14
  #
15
15
  class Query
16
16
  include Enumerable
17
- include Scoping
18
17
  include Loading
19
18
  include Pagination
19
+ include Chewy::Search::Scoping
20
+
21
+ DELEGATED_METHODS = %i[
22
+ explain query_mode filter_mode post_filter_mode
23
+ timeout limit offset highlight min_score rescore facets script_score
24
+ boost_factor weight random_score field_value_factor decay aggregations
25
+ suggest none strategy query filter post_filter boost_mode
26
+ score_mode order reorder only types delete_all find total
27
+ total_count total_entries unlimited script_fields track_scores preference
28
+ ].to_set.freeze
20
29
 
21
30
  delegate :each, :count, :size, to: :_collection
22
31
  alias_method :to_ary, :to_a
@@ -31,10 +40,16 @@ module Chewy
31
40
  @criteria = Criteria.new
32
41
  end
33
42
 
43
+ # A compatibility layer with the new request DSL.
44
+ def render
45
+ _request
46
+ end
47
+
34
48
  # Comparation with other query or collection
35
49
  # If other is collection - search request is executed and
36
50
  # result is used for comparation
37
51
  #
52
+ # @example
38
53
  # UsersIndex.filter(term: {name: 'Johny'}) == UsersIndex.filter(term: {name: 'Johny'}) # => true
39
54
  # UsersIndex.filter(term: {name: 'Johny'}) == UsersIndex.filter(term: {name: 'Johny'}).to_a # => true
40
55
  # UsersIndex.filter(term: {name: 'Johny'}) == UsersIndex.filter(term: {name: 'Winnie'}) # => false
@@ -43,23 +58,27 @@ module Chewy
43
58
  super || other.is_a?(self.class) ? other.criteria == criteria : other == to_a
44
59
  end
45
60
 
46
- # Adds <tt>explain</tt> parameter to search request.
61
+ # Adds `explain` parameter to search request.
47
62
  #
63
+ # @example
48
64
  # UsersIndex.filter(term: {name: 'Johny'}).explain
49
65
  # UsersIndex.filter(term: {name: 'Johny'}).explain(true)
50
66
  # UsersIndex.filter(term: {name: 'Johny'}).explain(false)
51
67
  #
52
68
  # Calling explain without any arguments sets explanation flag to true.
53
- # With <tt>explain: true</tt>, every result object has <tt>_explanation</tt>
69
+ # With `explain: true`, every result object has `_explanation`
54
70
  # method
55
71
  #
72
+ # @example
56
73
  # UsersIndex::User.filter(term: {name: 'Johny'}).explain.first._explanation # => {...}
57
74
  #
58
75
  def explain(value = nil)
59
76
  chain { criteria.update_request_options explain: (value.nil? ? true : value) }
60
77
  end
61
78
 
62
- # Adds <tt>script_fields</tt> parameter to search request.
79
+ # Adds `script_fields` parameter to search request.
80
+ #
81
+ # @example
63
82
  # UsersIndex.script_fields(
64
83
  # distance: {
65
84
  # params: {
@@ -77,31 +96,28 @@ module Chewy
77
96
  # Not used if only one filter for search is specified.
78
97
  # Possible values:
79
98
  #
80
- # * <tt>:must</tt>
81
- # Default value. Query compiles into a bool <tt>must</tt> query.
82
- #
83
- # Ex:
99
+ # * `:must`
100
+ # Default value. Query compiles into a bool `must` query.
84
101
  #
102
+ # @example
85
103
  # UsersIndex.query(text: {name: 'Johny'}).query(range: {age: {lte: 42}})
86
104
  # # => {body: {
87
105
  # query: {bool: {must: [{text: {name: 'Johny'}}, {range: {age: {lte: 42}}}]}}
88
106
  # }}
89
107
  #
90
- # * <tt>:should</tt>
91
- # Query compiles into a bool <tt>should</tt> query.
92
- #
93
- # Ex:
108
+ # * `:should`
109
+ # Query compiles into a bool `should` query.
94
110
  #
111
+ # @example
95
112
  # UsersIndex.query(text: {name: 'Johny'}).query(range: {age: {lte: 42}}).query_mode(:should)
96
113
  # # => {body: {
97
114
  # query: {bool: {should: [{text: {name: 'Johny'}}, {range: {age: {lte: 42}}}]}}
98
115
  # }}
99
116
  #
100
- # * Any acceptable <tt>minimum_should_match</tt> value (1, '2', '75%')
101
- # Query compiles into a bool <tt>should</tt> query with <tt>minimum_should_match</tt> set.
102
- #
103
- # Ex:
117
+ # * Any acceptable `minimum_should_match` value (1, '2', '75%')
118
+ # Query compiles into a bool `should` query with `minimum_should_match` set.
104
119
  #
120
+ # @example
105
121
  # UsersIndex.query(text: {name: 'Johny'}).query(range: {age: {lte: 42}}).query_mode('50%')
106
122
  # # => {body: {
107
123
  # query: {bool: {
@@ -110,21 +126,19 @@ module Chewy
110
126
  # }}
111
127
  # }}
112
128
  #
113
- # * <tt>:dis_max</tt>
114
- # Query compiles into a <tt>dis_max</tt> query.
115
- #
116
- # Ex:
129
+ # * `:dis_max`
130
+ # Query compiles into a `dis_max` query.
117
131
  #
132
+ # @example
118
133
  # UsersIndex.query(text: {name: 'Johny'}).query(range: {age: {lte: 42}}).query_mode(:dis_max)
119
134
  # # => {body: {
120
135
  # query: {dis_max: {queries: [{text: {name: 'Johny'}}, {range: {age: {lte: 42}}}]}}
121
136
  # }}
122
137
  #
123
138
  # * Any Float value (0.0, 0.7, 1.0)
124
- # Query compiles into a <tt>dis_max</tt> query with <tt>tie_breaker</tt> option set.
125
- #
126
- # Ex:
139
+ # Query compiles into a `dis_max` query with `tie_breaker` option set.
127
140
  #
141
+ # @example
128
142
  # UsersIndex.query(text: {name: 'Johny'}).query(range: {age: {lte: 42}}).query_mode(0.7)
129
143
  # # => {body: {
130
144
  # query: {dis_max: {
@@ -133,9 +147,10 @@ module Chewy
133
147
  # }}
134
148
  # }}
135
149
  #
136
- # Default value for <tt>:query_mode</tt> might be changed
137
- # with <tt>Chewy.query_mode</tt> config option.
150
+ # Default value for `:query_mode` might be changed
151
+ # with `Chewy.query_mode` config option.
138
152
  #
153
+ # @example
139
154
  # Chewy.query_mode = :dis_max
140
155
  # Chewy.query_mode = '50%'
141
156
  #
@@ -147,55 +162,50 @@ module Chewy
147
162
  # Not used if only one filter for search is specified.
148
163
  # Possible values:
149
164
  #
150
- # * <tt>:and</tt>
151
- # Default value. Filter compiles into an <tt>and</tt> filter.
152
- #
153
- # Ex:
165
+ # * `:and`
166
+ # Default value. Filter compiles into an `and` filter.
154
167
  #
168
+ # @example
155
169
  # UsersIndex.filter{ name == 'Johny' }.filter{ age <= 42 }
156
170
  # # => {body: {query: {filtered: {
157
171
  # query: {...},
158
172
  # filter: {and: [{term: {name: 'Johny'}}, {range: {age: {lte: 42}}}]}
159
173
  # }}}}
160
174
  #
161
- # * <tt>:or</tt>
162
- # Filter compiles into an <tt>or</tt> filter.
163
- #
164
- # Ex:
175
+ # * `:or`
176
+ # Filter compiles into an `or` filter.
165
177
  #
178
+ # @example
166
179
  # UsersIndex.filter{ name == 'Johny' }.filter{ age <= 42 }.filter_mode(:or)
167
180
  # # => {body: {query: {filtered: {
168
181
  # query: {...},
169
182
  # filter: {or: [{term: {name: 'Johny'}}, {range: {age: {lte: 42}}}]}
170
183
  # }}}}
171
184
  #
172
- # * <tt>:must</tt>
173
- # Filter compiles into a bool <tt>must</tt> filter.
174
- #
175
- # Ex:
185
+ # * `:must`
186
+ # Filter compiles into a bool `must` filter.
176
187
  #
188
+ # @example
177
189
  # UsersIndex.filter{ name == 'Johny' }.filter{ age <= 42 }.filter_mode(:must)
178
190
  # # => {body: {query: {filtered: {
179
191
  # query: {...},
180
192
  # filter: {bool: {must: [{term: {name: 'Johny'}}, {range: {age: {lte: 42}}}]}}
181
193
  # }}}}
182
194
  #
183
- # * <tt>:should</tt>
184
- # Filter compiles into a bool <tt>should</tt> filter.
185
- #
186
- # Ex:
195
+ # * `:should`
196
+ # Filter compiles into a bool `should` filter.
187
197
  #
198
+ # @example
188
199
  # UsersIndex.filter{ name == 'Johny' }.filter{ age <= 42 }.filter_mode(:should)
189
200
  # # => {body: {query: {filtered: {
190
201
  # query: {...},
191
202
  # filter: {bool: {should: [{term: {name: 'Johny'}}, {range: {age: {lte: 42}}}]}}
192
203
  # }}}}
193
204
  #
194
- # * Any acceptable <tt>minimum_should_match</tt> value (1, '2', '75%')
195
- # Filter compiles into bool <tt>should</tt> filter with <tt>minimum_should_match</tt> set.
196
- #
197
- # Ex:
205
+ # * Any acceptable `minimum_should_match` value (1, '2', '75%')
206
+ # Filter compiles into bool `should` filter with `minimum_should_match` set.
198
207
  #
208
+ # @example
199
209
  # UsersIndex.filter{ name == 'Johny' }.filter{ age <= 42 }.filter_mode('50%')
200
210
  # # => {body: {query: {filtered: {
201
211
  # query: {...},
@@ -205,9 +215,10 @@ module Chewy
205
215
  # }}
206
216
  # }}}}
207
217
  #
208
- # Default value for <tt>:filter_mode</tt> might be changed
209
- # with <tt>Chewy.filter_mode</tt> config option.
218
+ # Default value for `:filter_mode` might be changed
219
+ # with `Chewy.filter_mode` config option.
210
220
  #
221
+ # @example
211
222
  # Chewy.filter_mode = :should
212
223
  # Chewy.filter_mode = '50%'
213
224
  #
@@ -219,6 +230,7 @@ module Chewy
219
230
  # Note that it fallbacks by default to `Chewy.filter_mode` if
220
231
  # `Chewy.post_filter_mode` is nil.
221
232
  #
233
+ # @example
222
234
  # UsersIndex.post_filter{ name == 'Johny' }.post_filter{ age <= 42 }.post_filter_mode(:and)
223
235
  # UsersIndex.post_filter{ name == 'Johny' }.post_filter{ age <= 42 }.post_filter_mode(:should)
224
236
  # UsersIndex.post_filter{ name == 'Johny' }.post_filter{ age <= 42 }.post_filter_mode('50%')
@@ -242,6 +254,7 @@ module Chewy
242
254
  # The response to a search request will indicate whether the search timed
243
255
  # out and how many shards responded successfully:
244
256
  #
257
+ # @example
245
258
  # ...
246
259
  # "timed_out": true,
247
260
  # "_shards": {
@@ -259,6 +272,7 @@ module Chewy
259
272
  # responding with an error. The timeout parameter can be used to explicitly
260
273
  # specify how long it waits.
261
274
  #
275
+ # @example
262
276
  # UsersIndex.timeout("5000ms")
263
277
  #
264
278
  # Timeout is not a circuit breaker.
@@ -276,9 +290,10 @@ module Chewy
276
290
  chain { criteria.update_request_options timeout: value }
277
291
  end
278
292
 
279
- # Sets elasticsearch <tt>size</tt> search request param
293
+ # Sets elasticsearch `size` search request param
280
294
  # Default value is set in the elasticsearch and is 10.
281
295
  #
296
+ # @example
282
297
  # UsersIndex.filter{ name == 'Johny' }.limit(100)
283
298
  # # => {body: {
284
299
  # query: {...},
@@ -289,8 +304,9 @@ module Chewy
289
304
  chain { criteria.update_request_options size: block || Integer(value) }
290
305
  end
291
306
 
292
- # Sets elasticsearch <tt>from</tt> search request param
307
+ # Sets elasticsearch `from` search request param
293
308
  #
309
+ # @example
294
310
  # UsersIndex.filter{ name == 'Johny' }.offset(300)
295
311
  # # => {body: {
296
312
  # query: {...},
@@ -303,6 +319,7 @@ module Chewy
303
319
 
304
320
  # Elasticsearch highlight query option support
305
321
  #
322
+ # @example
306
323
  # UsersIndex.query(...).highlight(fields: { ... })
307
324
  #
308
325
  def highlight(value)
@@ -311,6 +328,7 @@ module Chewy
311
328
 
312
329
  # Elasticsearch rescore query option support
313
330
  #
331
+ # @example
314
332
  # UsersIndex.query(...).rescore(query: { ... })
315
333
  #
316
334
  def rescore(value)
@@ -319,7 +337,8 @@ module Chewy
319
337
 
320
338
  # Elasticsearch minscore option support
321
339
  #
322
- # UsersIndex.query(...).min_score(0.5)
340
+ # @example
341
+ # UsersIndex.query(...).min_score(0.5)
323
342
  #
324
343
  def min_score(value)
325
344
  chain { criteria.update_request_options min_score: value }
@@ -327,7 +346,8 @@ module Chewy
327
346
 
328
347
  # Elasticsearch track_scores option support
329
348
  #
330
- # UsersIndex.query(...).track_scores(true)
349
+ # @example
350
+ # UsersIndex.query(...).track_scores(true)
331
351
  #
332
352
  def track_scores(value)
333
353
  chain { criteria.update_request_options track_scores: value }
@@ -337,6 +357,7 @@ module Chewy
337
357
  # All the chained facets a merged and added to the
338
358
  # search request
339
359
  #
360
+ # @example
340
361
  # UsersIndex.facets(tags: {terms: {field: 'tags'}}).facets(ages: {terms: {field: 'age'}})
341
362
  # # => {body: {
342
363
  # query: {...},
@@ -357,8 +378,9 @@ module Chewy
357
378
 
358
379
  # Adds a script function to score the search request. All scores are
359
380
  # added to the search request and combinded according to
360
- # <tt>boost_mode</tt> and <tt>score_mode</tt>
381
+ # `boost_mode` and `score_mode`
361
382
  #
383
+ # @example
362
384
  # UsersIndex.script_score("doc['boost'].value", params: { modifier: 2 })
363
385
  # # => {body:
364
386
  # query: {
@@ -373,17 +395,18 @@ module Chewy
373
395
  # }]
374
396
  # } } }
375
397
  def script_score(script, options = {})
376
- scoring = { script_score: { script: script }.merge(options) }
398
+ scoring = {script_score: {script: script}.merge(options)}
377
399
  chain { criteria.update_scores scoring }
378
400
  end
379
401
 
380
402
  # Adds a boost factor to the search request. All scores are
381
403
  # added to the search request and combinded according to
382
- # <tt>boost_mode</tt> and <tt>score_mode</tt>
404
+ # `boost_mode` and `score_mode`
383
405
  #
384
406
  # This probably only makes sense if you specify a filter
385
407
  # for the boost factor as well
386
408
  #
409
+ # @example
387
410
  # UsersIndex.boost_factor(23, filter: { term: { foo: :bar} })
388
411
  # # => {body:
389
412
  # query: {
@@ -401,11 +424,12 @@ module Chewy
401
424
 
402
425
  # Add a weight scoring function to the search. All scores are
403
426
  # added to the search request and combinded according to
404
- # <tt>boost_mode</tt> and <tt>score_mode</tt>
427
+ # `boost_mode` and `score_mode`
405
428
  #
406
429
  # This probably only makes sense if you specify a filter
407
430
  # for the weight as well.
408
431
  #
432
+ # @example
409
433
  # UsersIndex.weight(23, filter: { term: { foo: :bar} })
410
434
  # # => {body:
411
435
  # query: {
@@ -423,13 +447,14 @@ module Chewy
423
447
 
424
448
  # Adds a random score to the search request. All scores are
425
449
  # added to the search request and combinded according to
426
- # <tt>boost_mode</tt> and <tt>score_mode</tt>
450
+ # `boost_mode` and `score_mode`
427
451
  #
428
452
  # This probably only makes sense if you specify a filter
429
453
  # for the random score as well.
430
454
  #
431
455
  # If you do not pass in a seed value, Time.now will be used
432
456
  #
457
+ # @example
433
458
  # UsersIndex.random_score(23, filter: { foo: :bar})
434
459
  # # => {body:
435
460
  # query: {
@@ -441,17 +466,18 @@ module Chewy
441
466
  # }]
442
467
  # } } }
443
468
  def random_score(seed = Time.now, options = {})
444
- scoring = options.merge(random_score: { seed: seed.to_i })
469
+ scoring = options.merge(random_score: {seed: seed.to_i})
445
470
  chain { criteria.update_scores scoring }
446
471
  end
447
472
 
448
473
  # Add a field value scoring to the search. All scores are
449
474
  # added to the search request and combinded according to
450
- # <tt>boost_mode</tt> and <tt>score_mode</tt>
475
+ # `boost_mode` and `score_mode`
451
476
  #
452
477
  # This function is only available in Elasticsearch 1.2 and
453
478
  # greater
454
479
  #
480
+ # @example
455
481
  # UsersIndex.field_value_factor(
456
482
  # {
457
483
  # field: :boost,
@@ -478,11 +504,12 @@ module Chewy
478
504
 
479
505
  # Add a decay scoring to the search. All scores are
480
506
  # added to the search request and combinded according to
481
- # <tt>boost_mode</tt> and <tt>score_mode</tt>
507
+ # `boost_mode` and `score_mode`
482
508
  #
483
509
  # The parameters have default values, but those may not
484
510
  # be very useful for most applications.
485
511
  #
512
+ # @example
486
513
  # UsersIndex.decay(
487
514
  # :gauss,
488
515
  # :field,
@@ -510,22 +537,24 @@ module Chewy
510
537
  def decay(function, field, options = {})
511
538
  field_options = options.extract!(:origin, :scale, :offset, :decay).delete_if { |_, v| v.nil? }
512
539
  scoring = options.merge(function => {
513
- field => field_options
514
- })
540
+ field => field_options
541
+ })
515
542
  chain { criteria.update_scores scoring }
516
543
  end
517
544
 
518
- # Sets <tt>preference</tt> for request.
519
- # For instance, one can use <tt>preference=_primary</tt> to execute only on the primary shards.
545
+ # Sets `preference` for request.
546
+ # For instance, one can use `preference=_primary` to execute only on the primary shards.
520
547
  #
548
+ # @example
521
549
  # scope = UsersIndex.preference(:_primary)
522
550
  #
523
551
  def preference(value)
524
552
  chain { criteria.update_search_options preference: value }
525
553
  end
526
554
 
527
- # Sets elasticsearch <tt>aggregations</tt> search request param
555
+ # Sets elasticsearch `aggregations` search request param
528
556
  #
557
+ # @example
529
558
  # UsersIndex.filter{ name == 'Johny' }.aggregations(category_id: {terms: {field: 'category_ids'}})
530
559
  # # => {body: {
531
560
  # query: {...},
@@ -540,8 +569,8 @@ module Chewy
540
569
  @_named_aggs ||= _build_named_aggs
541
570
  @_fully_qualified_named_aggs ||= _build_fqn_aggs
542
571
  if params
543
- params = { params => @_named_aggs[params] } if params.is_a?(Symbol)
544
- params = { params => _get_fully_qualified_named_agg(params) } if params.is_a?(String) && params =~ /\A\S+#\S+\.\S+\z/
572
+ params = {params => @_named_aggs[params]} if params.is_a?(Symbol)
573
+ params = {params => _get_fully_qualified_named_agg(params)} if params.is_a?(String) && params =~ /\A\S+#\S+\.\S+\z/
545
574
  chain { criteria.update_aggregations params }
546
575
  else
547
576
  _response['aggregations'] || {}
@@ -584,8 +613,9 @@ module Chewy
584
613
  @_fully_qualified_named_aggs[idx][type][agg_name]
585
614
  end
586
615
 
587
- # Sets elasticsearch <tt>suggest</tt> search request param
616
+ # Sets elasticsearch `suggest` search request param
588
617
  #
618
+ # @example
589
619
  # UsersIndex.suggest(name: {text: 'Joh', term: {field: 'name'}})
590
620
  # # => {body: {
591
621
  # query: {...},
@@ -605,10 +635,11 @@ module Chewy
605
635
  end
606
636
  end
607
637
 
608
- # Marks the criteria as having zero records. This scope always returns empty array
638
+ # Marks the criteria as having zero documents. This scope always returns empty array
609
639
  # without touching the elasticsearch server.
610
640
  # All the chained calls of methods don't affect the result
611
641
  #
642
+ # @example
612
643
  # UsersIndex.none.to_a
613
644
  # # => []
614
645
  # UsersIndex.query(text: {name: 'Johny'}).none.to_a
@@ -622,6 +653,7 @@ module Chewy
622
653
 
623
654
  # Setups strategy for top-level filtered query
624
655
  #
656
+ # @example
625
657
  # UsersIndex.filter { name == 'Johny'}.strategy(:leap_frog)
626
658
  # # => {body: {
627
659
  # query: { filtered: {
@@ -637,11 +669,12 @@ module Chewy
637
669
  # Adds one or more query to the search request
638
670
  # Internally queries are stored as an array
639
671
  # While the full query compilation this array compiles
640
- # according to <tt>:query_mode</tt> option value
672
+ # according to `:query_mode` option value
641
673
  #
642
- # By default it joines inside <tt>must</tt> query
643
- # See <tt>#query_mode</tt> chainable method for more info.
674
+ # By default it joines inside `must` query
675
+ # See `#query_mode` chainable method for more info.
644
676
  #
677
+ # @example
645
678
  # UsersIndex.query(text: {name: 'Johny'}).query(range: {age: {lte: 42}})
646
679
  # UsersIndex::User.query(text: {name: 'Johny'}).query(range: {age: {lte: 42}})
647
680
  # # => {body: {
@@ -651,6 +684,7 @@ module Chewy
651
684
  # If only one query was specified, it will become a result
652
685
  # query as is, without joining.
653
686
  #
687
+ # @example
654
688
  # UsersIndex.query(text: {name: 'Johny'})
655
689
  # # => {body: {
656
690
  # query: {text: {name: 'Johny'}}
@@ -663,14 +697,15 @@ module Chewy
663
697
  # Adds one or more filter to the search request
664
698
  # Internally filters are stored as an array
665
699
  # While the full query compilation this array compiles
666
- # according to <tt>:filter_mode</tt> option value
700
+ # according to `:filter_mode` option value
667
701
  #
668
- # By default it joins inside <tt>and</tt> filter
669
- # See <tt>#filter_mode</tt> chainable method for more info.
702
+ # By default it joins inside `and` filter
703
+ # See `#filter_mode` chainable method for more info.
670
704
  #
671
705
  # Also this method supports block DSL.
672
- # See <tt>Chewy::Query::Filters</tt> for more info.
706
+ # See `Chewy::Query::Filters` for more info.
673
707
  #
708
+ # @example
674
709
  # UsersIndex.filter(term: {name: 'Johny'}).filter(range: {age: {lte: 42}})
675
710
  # UsersIndex::User.filter(term: {name: 'Johny'}).filter(range: {age: {lte: 42}})
676
711
  # UsersIndex.filter{ name == 'Johny' }.filter{ age <= 42 }
@@ -682,6 +717,7 @@ module Chewy
682
717
  # If only one filter was specified, it will become a result
683
718
  # filter as is, without joining.
684
719
  #
720
+ # @example
685
721
  # UsersIndex.filter(term: {name: 'Johny'})
686
722
  # # => {body: {query: {filtered: {
687
723
  # query: {...},
@@ -696,14 +732,15 @@ module Chewy
696
732
  # Adds one or more post_filter to the search request
697
733
  # Internally post_filters are stored as an array
698
734
  # While the full query compilation this array compiles
699
- # according to <tt>:post_filter_mode</tt> option value
735
+ # according to `:post_filter_mode` option value
700
736
  #
701
- # By default it joins inside <tt>and</tt> filter
702
- # See <tt>#post_filter_mode</tt> chainable method for more info.
737
+ # By default it joins inside `and` filter
738
+ # See `#post_filter_mode` chainable method for more info.
703
739
  #
704
740
  # Also this method supports block DSL.
705
- # See <tt>Chewy::Query::Filters</tt> for more info.
741
+ # See `Chewy::Query::Filters` for more info.
706
742
  #
743
+ # @example
707
744
  # UsersIndex.post_filter(term: {name: 'Johny'}).post_filter(range: {age: {lte: 42}})
708
745
  # UsersIndex::User.post_filter(term: {name: 'Johny'}).post_filter(range: {age: {lte: 42}})
709
746
  # UsersIndex.post_filter{ name == 'Johny' }.post_filter{ age <= 42 }
@@ -714,6 +751,7 @@ module Chewy
714
751
  # If only one post_filter was specified, it will become a result
715
752
  # post_filter as is, without joining.
716
753
  #
754
+ # @example
717
755
  # UsersIndex.post_filter(term: {name: 'Johny'})
718
756
  # # => {body: {
719
757
  # post_filter: {term: {name: 'Johny'}}
@@ -728,11 +766,10 @@ module Chewy
728
766
  # Not used if no score functions are specified
729
767
  # Possible values:
730
768
  #
731
- # * <tt>:multiply</tt>
769
+ # * `:multiply`
732
770
  # Default value. Query score and function result are multiplied.
733
771
  #
734
- # Ex:
735
- #
772
+ # @example
736
773
  # UsersIndex.boost_mode('multiply').script_score('doc['boost'].value')
737
774
  # # => {body: {query: function_score: {
738
775
  # query: {...},
@@ -740,23 +777,23 @@ module Chewy
740
777
  # functions: [ ... ]
741
778
  # }}}
742
779
  #
743
- # * <tt>:replace</tt>
780
+ # * `:replace`
744
781
  # Only function result is used, query score is ignored.
745
782
  #
746
- # * <tt>:sum</tt>
783
+ # * `:sum`
747
784
  # Query score and function score are added.
748
785
  #
749
- # * <tt>:avg</tt>
786
+ # * `:avg`
750
787
  # Average of query and function score.
751
788
  #
752
- # * <tt>:max</tt>
789
+ # * `:max`
753
790
  # Max of query and function score.
754
791
  #
755
- # * <tt>:min</tt>
792
+ # * `:min`
756
793
  # Min of query and function score.
757
794
  #
758
- # Default value for <tt>:boost_mode</tt> might be changed
759
- # with <tt>Chewy.score_mode</tt> config option.
795
+ # Default value for `:boost_mode` might be changed
796
+ # with `Chewy.score_mode` config option.
760
797
  def boost_mode(value)
761
798
  chain { criteria.update_options boost_mode: value }
762
799
  end
@@ -765,11 +802,10 @@ module Chewy
765
802
  # Not used if no score functions are specified.
766
803
  # Possible values:
767
804
  #
768
- # * <tt>:multiply</tt>
805
+ # * `:multiply`
769
806
  # Default value. Scores are multiplied.
770
807
  #
771
- # Ex:
772
- #
808
+ # @example
773
809
  # UsersIndex.score_mode('multiply').script_score('doc['boost'].value')
774
810
  # # => {body: {query: function_score: {
775
811
  # query: {...},
@@ -777,24 +813,25 @@ module Chewy
777
813
  # functions: [ ... ]
778
814
  # }}}
779
815
  #
780
- # * <tt>:sum</tt>
816
+ # * `:sum`
781
817
  # Scores are summed.
782
818
  #
783
- # * <tt>:avg</tt>
819
+ # * `:avg`
784
820
  # Scores are averaged.
785
821
  #
786
- # * <tt>:first</tt>
822
+ # * `:first`
787
823
  # The first function that has a matching filter is applied.
788
824
  #
789
- # * <tt>:max</tt>
825
+ # * `:max`
790
826
  # Maximum score is used.
791
827
  #
792
- # * <tt>:min</tt>
828
+ # * `:min`
793
829
  # Minimum score is used
794
830
  #
795
- # Default value for <tt>:score_mode</tt> might be changed
796
- # with <tt>Chewy.score_mode</tt> config option.
831
+ # Default value for `:score_mode` might be changed
832
+ # with `Chewy.score_mode` config option.
797
833
  #
834
+ # @example
798
835
  # Chewy.score_mode = :first
799
836
  #
800
837
  def score_mode(value)
@@ -803,6 +840,7 @@ module Chewy
803
840
 
804
841
  # Sets search request sorting
805
842
  #
843
+ # @example
806
844
  # UsersIndex.order(:first_name, :last_name).order(age: :desc).order(price: {order: :asc, mode: :avg})
807
845
  # # => {body: {
808
846
  # query: {...},
@@ -815,6 +853,7 @@ module Chewy
815
853
 
816
854
  # Cleans up previous search sorting and sets the new one
817
855
  #
856
+ # @example
818
857
  # UsersIndex.order(:first_name, :last_name).order(age: :desc).reorder(price: {order: :asc, mode: :avg})
819
858
  # # => {body: {
820
859
  # query: {...},
@@ -827,6 +866,7 @@ module Chewy
827
866
 
828
867
  # Sets search request field list
829
868
  #
869
+ # @example
830
870
  # UsersIndex.only(:first_name, :last_name).only(:age)
831
871
  # # => {body: {
832
872
  # query: {...},
@@ -839,6 +879,7 @@ module Chewy
839
879
 
840
880
  # Cleans up previous search field list and sets the new one
841
881
  #
882
+ # @example
842
883
  # UsersIndex.only(:first_name, :last_name).only!(:age)
843
884
  # # => {body: {
844
885
  # query: {...},
@@ -850,9 +891,10 @@ module Chewy
850
891
  end
851
892
 
852
893
  # Specify types participating in the search result
853
- # Works via <tt>types</tt> filter. Always merged with another filters
854
- # with the <tt>and</tt> filter.
894
+ # Works via `types` filter. Always merged with another filters
895
+ # with the `and` filter.
855
896
  #
897
+ # @example
856
898
  # UsersIndex.types(:admin, :manager).filters{ name == 'Johny' }.filters{ age <= 42 }
857
899
  # # => {body: {query: {filtered: {
858
900
  # query: {...},
@@ -885,8 +927,9 @@ module Chewy
885
927
  chain { criteria.update_types params }
886
928
  end
887
929
 
888
- # Acts the same way as <tt>types</tt>, but cleans up previously set types
930
+ # Acts the same way as `types`, but cleans up previously set types
889
931
  #
932
+ # @example
890
933
  # UsersIndex.types(:admin).types!(:manager)
891
934
  # # => {body: {query: {filtered: {
892
935
  # query: {...},
@@ -897,11 +940,12 @@ module Chewy
897
940
  chain { criteria.update_types params, purge: true }
898
941
  end
899
942
 
900
- # Sets <tt>search_type</tt> for request.
901
- # For instance, one can use <tt>search_type=count</tt> to fetch only total count of records or to fetch only aggregations without fetching records.
943
+ # Sets `search_type` for request.
944
+ # For instance, one can use `search_type=count` to fetch only total count of documents or to fetch only aggregations without fetching documents.
902
945
  #
946
+ # @example
903
947
  # scope = UsersIndex.search_type(:count)
904
- # scope.count == 0 # no records actually fetched
948
+ # scope.count == 0 # no documents actually fetched
905
949
  # scope.total == 10 # but we know a total count of them
906
950
  #
907
951
  # scope = UsersIndex.aggs(max_age: { max: { field: 'age' } }).search_type(:count)
@@ -914,6 +958,7 @@ module Chewy
914
958
  # Merges two queries.
915
959
  # Merges all the values in criteria with the same rules as values added manually.
916
960
  #
961
+ # @example
917
962
  # scope1 = UsersIndex.filter{ name == 'Johny' }
918
963
  # scope2 = UsersIndex.filter{ age <= 42 }
919
964
  # scope3 = UsersIndex.filter{ name == 'Johny' }.filter{ age <= 42 }
@@ -924,29 +969,42 @@ module Chewy
924
969
  chain { criteria.merge!(other.criteria) }
925
970
  end
926
971
 
927
- # Deletes all records matching a query.
972
+ # Deletes all documents matching a query.
928
973
  #
974
+ # @example
929
975
  # UsersIndex.delete_all
930
976
  # UsersIndex.filter{ age <= 42 }.delete_all
931
977
  # UsersIndex::User.delete_all
932
978
  # UsersIndex::User.filter{ age <= 42 }.delete_all
933
979
  #
934
980
  def delete_all
935
- if Runtime.version > '2.0'
981
+ if Runtime.version >= '2.0'
936
982
  plugins = Chewy.client.nodes.info(plugins: true)['nodes'].values.map { |item| item['plugins'] }.flatten
937
983
  raise PluginMissing, 'install delete-by-query plugin' unless plugins.find { |item| item['name'] == 'delete-by-query' }
938
984
  end
985
+
939
986
  request = chain { criteria.update_options simple: true }.send(:_request)
987
+
940
988
  ActiveSupport::Notifications.instrument 'delete_query.chewy',
941
989
  request: request, indexes: _indexes, types: _types,
942
990
  index: _indexes.one? ? _indexes.first : _indexes,
943
991
  type: _types.one? ? _types.first : _types do
944
- Chewy.client.delete_by_query(request)
945
- end
992
+ if Runtime.version >= '2.0'
993
+ path = Elasticsearch::API::Utils.__pathify(
994
+ Elasticsearch::API::Utils.__listify(request[:index]),
995
+ Elasticsearch::API::Utils.__listify(request[:type]),
996
+ '/_query'
997
+ )
998
+ Chewy.client.perform_request(Elasticsearch::API::HTTP_DELETE, path, {}, request[:body]).body
999
+ else
1000
+ Chewy.client.delete_by_query(request)
1001
+ end
1002
+ end
946
1003
  end
947
1004
 
948
- # Find all records matching a query.
1005
+ # Find all documents matching a query.
949
1006
  #
1007
+ # @example
950
1008
  # UsersIndex.find(42)
951
1009
  # UsersIndex.filter{ age <= 42 }.find(42)
952
1010
  # UsersIndex::User.find(42)
@@ -955,6 +1013,7 @@ module Chewy
955
1013
  # In all the previous examples find will return a single object.
956
1014
  # To get a collection - pass an array of ids.
957
1015
  #
1016
+ # @example
958
1017
  # UsersIndex::User.find(42, 7, 3) # array of objects with ids in [42, 7, 3]
959
1018
  # UsersIndex::User.find([8, 13]) # array of objects with ids in [8, 13]
960
1019
  # UsersIndex::User.find([42]) # array of the object with id == 42
@@ -968,6 +1027,7 @@ module Chewy
968
1027
 
969
1028
  # Returns true if there are at least one document that matches the query
970
1029
  #
1030
+ # @example
971
1031
  # PlacesIndex.query(...).filter(...).exists?
972
1032
  #
973
1033
  def exists?
@@ -976,6 +1036,7 @@ module Chewy
976
1036
 
977
1037
  # Sets limit to be equal to total documents count
978
1038
  #
1039
+ # @example
979
1040
  # PlacesIndex.query(...).filter(...).unlimited
980
1041
  #
981
1042
 
@@ -986,6 +1047,7 @@ module Chewy
986
1047
 
987
1048
  # Returns request total time elapsed as reported by elasticsearch
988
1049
  #
1050
+ # @example
989
1051
  # UsersIndex.query(...).filter(...).took
990
1052
  #
991
1053
  def took
@@ -1000,6 +1062,7 @@ module Chewy
1000
1062
  # important to you than complete results, you can specify a timeout as 10 or
1001
1063
  # "10ms" (10 milliseconds), or "1s" (1 second). See #timeout method.
1002
1064
  #
1065
+ # @example
1003
1066
  # UsersIndex.query(...).filter(...).timed_out
1004
1067
  #
1005
1068
  def timed_out
@@ -1008,8 +1071,8 @@ module Chewy
1008
1071
 
1009
1072
  protected
1010
1073
 
1011
- def initialize_clone(other)
1012
- @criteria = other.criteria.clone
1074
+ def initialize_clone(origin)
1075
+ @criteria = origin.criteria.clone
1013
1076
  reset
1014
1077
  end
1015
1078
 
@@ -1026,7 +1089,7 @@ module Chewy
1026
1089
  def _request
1027
1090
  @_request ||= begin
1028
1091
  request = criteria.request_body
1029
- request[:index] = _indexes.map(&:index_name)
1092
+ request[:index] = _indexes_hash.keys
1030
1093
  request[:type] = _types.map(&:type_name)
1031
1094
  request
1032
1095
  end
@@ -1048,14 +1111,7 @@ module Chewy
1048
1111
 
1049
1112
  def _results
1050
1113
  @_results ||= (criteria.none? || _response == {} ? [] : _response['hits']['hits']).map do |hit|
1051
- attributes = (hit['_source'] || {})
1052
- .reverse_merge(id: hit['_id'])
1053
- .merge!(_score: hit['_score'])
1054
- .merge!(_explanation: hit['_explanation'])
1055
-
1056
- wrapper = _derive_index(hit['_index']).type(hit['_type']).new(attributes)
1057
- wrapper._data = hit
1058
- wrapper
1114
+ _derive_type(hit['_index'], hit['_type']).build(hit)
1059
1115
  end
1060
1116
  end
1061
1117
 
@@ -1070,6 +1126,10 @@ module Chewy
1070
1126
  end
1071
1127
  end
1072
1128
 
1129
+ def _derive_type(index, type)
1130
+ (@types_cache ||= {})[[index, type]] ||= _derive_index(index).type(type)
1131
+ end
1132
+
1073
1133
  def _derive_index(index_name)
1074
1134
  (@derive_index ||= {})[index_name] ||= _indexes_hash[index_name] ||
1075
1135
  _indexes_hash[_indexes_hash.keys.sort_by(&:length).reverse.detect { |name| index_name.start_with?(name) }]