chewy 0.10.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (157) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +240 -0
  3. data/.rubocop.yml +25 -25
  4. data/Appraisals +12 -10
  5. data/CHANGELOG.md +252 -263
  6. data/Gemfile +5 -1
  7. data/LICENSE.txt +1 -1
  8. data/README.md +142 -78
  9. data/chewy.gemspec +10 -12
  10. data/gemfiles/{rails.4.2.mongoid.5.1.gemfile → rails.5.2.activerecord.gemfile} +6 -4
  11. data/gemfiles/{rails.4.2.activerecord.gemfile → rails.5.2.mongoid.6.4.gemfile} +6 -4
  12. data/gemfiles/{rails.4.0.activerecord.gemfile → rails.6.0.activerecord.gemfile} +6 -3
  13. data/gemfiles/rails.6.1.activerecord.gemfile +19 -0
  14. data/gemfiles/sequel.4.45.gemfile +2 -2
  15. data/lib/chewy.rb +2 -1
  16. data/lib/chewy/backports/duplicable.rb +1 -1
  17. data/lib/chewy/config.rb +10 -39
  18. data/lib/chewy/fields/base.rb +40 -28
  19. data/lib/chewy/fields/root.rb +18 -11
  20. data/lib/chewy/index.rb +3 -1
  21. data/lib/chewy/index/actions.rb +27 -15
  22. data/lib/chewy/index/settings.rb +2 -0
  23. data/lib/chewy/index/specification.rb +12 -10
  24. data/lib/chewy/minitest/helpers.rb +6 -6
  25. data/lib/chewy/minitest/search_index_receiver.rb +17 -17
  26. data/lib/chewy/multi_search.rb +62 -0
  27. data/lib/chewy/railtie.rb +4 -4
  28. data/lib/chewy/rake_helper.rb +5 -5
  29. data/lib/chewy/rspec/update_index.rb +3 -5
  30. data/lib/chewy/search.rb +4 -11
  31. data/lib/chewy/search/loader.rb +1 -1
  32. data/lib/chewy/search/pagination/will_paginate.rb +4 -2
  33. data/lib/chewy/search/parameters.rb +24 -6
  34. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  35. data/lib/chewy/search/parameters/concerns/query_storage.rb +4 -3
  36. data/lib/chewy/search/parameters/indices.rb +123 -0
  37. data/lib/chewy/search/parameters/none.rb +1 -3
  38. data/lib/chewy/search/request.rb +100 -74
  39. data/lib/chewy/search/scrolling.rb +7 -6
  40. data/lib/chewy/stash.rb +30 -21
  41. data/lib/chewy/strategy/active_job.rb +1 -1
  42. data/lib/chewy/strategy/atomic.rb +1 -1
  43. data/lib/chewy/strategy/sidekiq.rb +1 -1
  44. data/lib/chewy/type.rb +5 -2
  45. data/lib/chewy/type/adapter/active_record.rb +1 -1
  46. data/lib/chewy/type/adapter/base.rb +9 -9
  47. data/lib/chewy/type/adapter/mongoid.rb +2 -4
  48. data/lib/chewy/type/adapter/orm.rb +7 -4
  49. data/lib/chewy/type/adapter/sequel.rb +5 -7
  50. data/lib/chewy/type/crutch.rb +1 -1
  51. data/lib/chewy/type/import.rb +13 -11
  52. data/lib/chewy/type/import/bulk_builder.rb +1 -1
  53. data/lib/chewy/type/import/bulk_request.rb +4 -2
  54. data/lib/chewy/type/import/journal_builder.rb +3 -3
  55. data/lib/chewy/type/import/routine.rb +3 -3
  56. data/lib/chewy/type/mapping.rb +42 -36
  57. data/lib/chewy/type/observe.rb +16 -12
  58. data/lib/chewy/type/syncer.rb +15 -14
  59. data/lib/chewy/type/witchcraft.rb +11 -7
  60. data/lib/chewy/type/wrapper.rb +14 -4
  61. data/lib/chewy/version.rb +1 -1
  62. data/lib/sequel/plugins/chewy_observe.rb +4 -19
  63. data/migration_guide.md +18 -0
  64. data/spec/chewy/config_spec.rb +16 -21
  65. data/spec/chewy/fields/base_spec.rb +70 -70
  66. data/spec/chewy/fields/root_spec.rb +56 -9
  67. data/spec/chewy/index/actions_spec.rb +63 -7
  68. data/spec/chewy/index/specification_spec.rb +25 -16
  69. data/spec/chewy/index_spec.rb +75 -45
  70. data/spec/chewy/journal_spec.rb +33 -29
  71. data/spec/chewy/minitest/search_index_receiver_spec.rb +11 -9
  72. data/spec/chewy/multi_search_spec.rb +85 -0
  73. data/spec/chewy/rake_helper_spec.rb +123 -95
  74. data/spec/chewy/rspec/update_index_spec.rb +47 -46
  75. data/spec/chewy/runtime_spec.rb +2 -2
  76. data/spec/chewy/search/pagination/kaminari_spec.rb +7 -3
  77. data/spec/chewy/search/pagination/will_paginate_spec.rb +9 -3
  78. data/spec/chewy/search/parameters/indices_spec.rb +190 -0
  79. data/spec/chewy/search/parameters/none_spec.rb +1 -1
  80. data/spec/chewy/search/parameters_spec.rb +21 -4
  81. data/spec/chewy/search/request_spec.rb +101 -70
  82. data/spec/chewy/search/response_spec.rb +27 -17
  83. data/spec/chewy/search/scrolling_spec.rb +25 -16
  84. data/spec/chewy/search_spec.rb +49 -35
  85. data/spec/chewy/stash_spec.rb +15 -13
  86. data/spec/chewy/strategy/active_job_spec.rb +15 -2
  87. data/spec/chewy/strategy/shoryuken_spec.rb +8 -2
  88. data/spec/chewy/strategy/sidekiq_spec.rb +6 -2
  89. data/spec/chewy/type/adapter/active_record_spec.rb +16 -4
  90. data/spec/chewy/type/import/bulk_builder_spec.rb +9 -94
  91. data/spec/chewy/type/import/journal_builder_spec.rb +17 -15
  92. data/spec/chewy/type/import_spec.rb +6 -0
  93. data/spec/chewy/type/mapping_spec.rb +51 -18
  94. data/spec/chewy/type/observe_spec.rb +4 -4
  95. data/spec/chewy/type/witchcraft_spec.rb +31 -0
  96. data/spec/chewy/type/wrapper_spec.rb +3 -1
  97. data/spec/chewy_spec.rb +0 -7
  98. data/spec/spec_helper.rb +5 -1
  99. data/spec/support/active_record.rb +20 -0
  100. metadata +46 -116
  101. data/.travis.yml +0 -53
  102. data/LEGACY_DSL.md +0 -497
  103. data/gemfiles/rails.4.1.activerecord.gemfile +0 -14
  104. data/gemfiles/rails.5.0.activerecord.gemfile +0 -15
  105. data/gemfiles/rails.5.0.mongoid.6.0.gemfile +0 -15
  106. data/gemfiles/rails.5.1.activerecord.gemfile +0 -15
  107. data/gemfiles/rails.5.1.mongoid.6.1.gemfile +0 -15
  108. data/lib/chewy/query.rb +0 -1098
  109. data/lib/chewy/query/compose.rb +0 -68
  110. data/lib/chewy/query/criteria.rb +0 -191
  111. data/lib/chewy/query/filters.rb +0 -227
  112. data/lib/chewy/query/loading.rb +0 -111
  113. data/lib/chewy/query/nodes/and.rb +0 -25
  114. data/lib/chewy/query/nodes/base.rb +0 -17
  115. data/lib/chewy/query/nodes/bool.rb +0 -34
  116. data/lib/chewy/query/nodes/equal.rb +0 -34
  117. data/lib/chewy/query/nodes/exists.rb +0 -20
  118. data/lib/chewy/query/nodes/expr.rb +0 -28
  119. data/lib/chewy/query/nodes/field.rb +0 -110
  120. data/lib/chewy/query/nodes/has_child.rb +0 -15
  121. data/lib/chewy/query/nodes/has_parent.rb +0 -15
  122. data/lib/chewy/query/nodes/has_relation.rb +0 -59
  123. data/lib/chewy/query/nodes/match_all.rb +0 -11
  124. data/lib/chewy/query/nodes/missing.rb +0 -20
  125. data/lib/chewy/query/nodes/not.rb +0 -25
  126. data/lib/chewy/query/nodes/or.rb +0 -25
  127. data/lib/chewy/query/nodes/prefix.rb +0 -19
  128. data/lib/chewy/query/nodes/query.rb +0 -20
  129. data/lib/chewy/query/nodes/range.rb +0 -63
  130. data/lib/chewy/query/nodes/raw.rb +0 -15
  131. data/lib/chewy/query/nodes/regexp.rb +0 -35
  132. data/lib/chewy/query/nodes/script.rb +0 -20
  133. data/lib/chewy/query/pagination.rb +0 -25
  134. data/spec/chewy/query/criteria_spec.rb +0 -700
  135. data/spec/chewy/query/filters_spec.rb +0 -201
  136. data/spec/chewy/query/loading_spec.rb +0 -124
  137. data/spec/chewy/query/nodes/and_spec.rb +0 -12
  138. data/spec/chewy/query/nodes/bool_spec.rb +0 -14
  139. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  140. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  141. data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
  142. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
  143. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  144. data/spec/chewy/query/nodes/missing_spec.rb +0 -16
  145. data/spec/chewy/query/nodes/not_spec.rb +0 -13
  146. data/spec/chewy/query/nodes/or_spec.rb +0 -12
  147. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  148. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  149. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  150. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  151. data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
  152. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  153. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
  154. data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
  155. data/spec/chewy/query/pagination_spec.rb +0 -39
  156. data/spec/chewy/query_spec.rb +0 -636
  157. data/spec/chewy/search/parameters/indices_boost_spec.rb +0 -83
data/Gemfile CHANGED
@@ -8,9 +8,11 @@ gem 'activerecord'
8
8
 
9
9
  gem 'activejob', require: false
10
10
  gem 'resque', require: false
11
- gem 'shoryuken', require: false
12
11
  gem 'sidekiq', require: false
13
12
 
13
+ gem 'aws-sdk-sqs', require: false
14
+ gem 'shoryuken', require: false
15
+
14
16
  gem 'kaminari-core', require: false
15
17
  gem 'will_paginate', require: false
16
18
 
@@ -22,3 +24,5 @@ gem 'guard-rspec'
22
24
 
23
25
  gem 'redcarpet'
24
26
  gem 'yard'
27
+
28
+ gem 'rexml' if RUBY_VERSION >= '3.0.0'
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 pyromaniac
1
+ Copyright (c) 2013-2021 Toptal, LLC
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -1,14 +1,11 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/chewy.svg)](http://badge.fury.io/rb/chewy)
2
- [![Build Status](https://travis-ci.org/toptal/chewy.svg)](https://travis-ci.org/toptal/chewy)
2
+ [![CircleCI](https://circleci.com/gh/toptal/chewy/tree/master.svg?style=svg)](https://circleci.com/gh/toptal/chewy/tree/master)
3
3
  [![Code Climate](https://codeclimate.com/github/toptal/chewy.svg)](https://codeclimate.com/github/toptal/chewy)
4
4
  [![Inline docs](http://inch-ci.org/github/toptal/chewy.svg?branch=master)](http://inch-ci.org/github/toptal/chewy)
5
5
 
6
- <p align="right">Sponsored by</p>
7
- <p align="right"><a href="https://www.toptal.com/"><img src="https://www.toptal.com/assets/public/blocks/logo/big.png" alt="Toptal" width="105" height="34"></a></p>
8
-
9
6
  # Chewy
10
7
 
11
- Chewy is an ODM and wrapper for [the official Elasticsearch client](https://github.com/elastic/elasticsearch-ruby).
8
+ Chewy is an ODM (Object Document Mapper), built on top of the [the official Elasticsearch client](https://github.com/elastic/elasticsearch-ruby).
12
9
 
13
10
  ## Table of Contents
14
11
 
@@ -34,7 +31,7 @@ Chewy is an ODM and wrapper for [the official Elasticsearch client](https://gith
34
31
  * [Non-block notation](#non-block-notation)
35
32
  * [Designing your own strategies](#designing-your-own-strategies)
36
33
  * [Rails application strategies integration](#rails-application-strategies-integration)
37
- * [ActiveSupport::Notifications support](#activesupport-notifications-support)
34
+ * [ActiveSupport::Notifications support](#activesupportnotifications-support)
38
35
  * [NewRelic integration](#newrelic-integration)
39
36
  * [Search requests](#search-requests)
40
37
  * [Composing requests](#composing-requests)
@@ -42,7 +39,6 @@ Chewy is an ODM and wrapper for [the official Elasticsearch client](https://gith
42
39
  * [Named scopes](#named-scopes)
43
40
  * [Scroll API](#scroll-api)
44
41
  * [Loading objects](#loading-objects)
45
- * [Legacy DSL incompatibilities](#legacy-dsl-incompatibilities)
46
42
  * [Rake tasks](#rake-tasks)
47
43
  * [chewy:reset](#chewyreset)
48
44
  * [chewy:upgrade](#chewyupgrade)
@@ -51,13 +47,14 @@ Chewy is an ODM and wrapper for [the official Elasticsearch client](https://gith
51
47
  * [chewy:deploy](#chewydeploy)
52
48
  * [Parallelizing rake tasks](#parallelizing-rake-tasks)
53
49
  * [chewy:journal](#chewyjournal)
54
- * [Rspec integration](#rspec-integration)
50
+ * [RSpec integration](#rspec-integration)
55
51
  * [Minitest integration](#minitest-integration)
56
- * [TODO a.k.a coming soon](#todo-aka-coming-soon)
57
52
  * [Contributing](#contributing)
58
53
 
59
54
  ## Why Chewy?
60
55
 
56
+ In this section we'll cover why you might want to use Chewy instead of the official `elasticsearch-ruby` client gem.
57
+
61
58
  * Multi-model indices.
62
59
 
63
60
  Index classes are independent from ORM/ODM models. Now, implementing e.g. cross-model autocomplete is much easier. You can just define the index and work with it in an object-oriented style. You can define several types for index - one per indexed model.
@@ -76,10 +73,9 @@ Chewy is an ODM and wrapper for [the official Elasticsearch client](https://gith
76
73
 
77
74
  * Support for ActiveRecord, [Mongoid](https://github.com/mongoid/mongoid) and [Sequel](https://github.com/jeremyevans/sequel).
78
75
 
79
-
80
76
  ## Installation
81
77
 
82
- Add this line to your application's Gemfile:
78
+ Add this line to your application's `Gemfile`:
83
79
 
84
80
  gem 'chewy'
85
81
 
@@ -91,13 +87,33 @@ Or install it yourself as:
91
87
 
92
88
  $ gem install chewy
93
89
 
90
+ ## Compatibility
91
+
92
+ ### Ruby
93
+
94
+ Chewy is compatible with MRI 2.5-3.0¹.
95
+
96
+ > ¹ Ruby 3 is only supported with Rails 6.1
97
+
98
+ ### Elasticsearch
99
+
100
+ Chewy 5 is compatible with Elasticsearch 5.
101
+
102
+ Chewy 6 is compatible with Elasticsearch 6. See [Migration guide](migration_guide.md).
103
+
104
+ Future versions of Chewy will support Elasticsearch 7.
105
+
94
106
  ## Usage
95
107
 
96
108
  ### Client settings
97
109
 
98
- There are two ways to configure the Chewy client: the `Chewy.settings` hash and `chewy.yml`
110
+ There are two ways to configure the Chewy client:
111
+
112
+ * via the hash `Chewy.settings`
113
+ * via the configuration file `chewy.yml`
99
114
 
100
- You can create this file manually or run `rails g chewy:install`.
115
+ You can create `chewy.yml` manually or run `rails g chewy:install` to
116
+ generate it.
101
117
 
102
118
  ```ruby
103
119
  # config/initializers/chewy.rb
@@ -129,25 +145,29 @@ Chewy.logger = Logger.new(STDOUT)
129
145
 
130
146
  See [config.rb](lib/chewy/config.rb) for more details.
131
147
 
132
- #### Aws Elastic Search
133
- If you would like to use AWS's ElasticSearch using an IAM user policy, you will need to sign your requests for the `es:*` action by injecting the appropriate headers passing a proc to `transport_options`.
148
+ #### AWS Elasticsearch
149
+
150
+ If you would like to use AWS's Elasticsearch using an IAM user policy, you will need to sign your requests for the `es:*` action by injecting the appropriate headers passing a proc to `transport_options`.
151
+ You'll need an additional gem for Faraday middleware: add `gem 'faraday_middleware-aws-sigv4'` to your Gemfile.
134
152
 
135
153
  ```ruby
136
- Chewy.settings = {
137
- host: 'http://my-es-instance-on-aws.us-east-1.es.amazonaws.com:80',
138
- transport_options: {
139
- headers: { content_type: 'application/json' },
140
- proc: -> (f) do
141
- f.request :aws_signers_v4,
142
- service_name: 'es',
143
- region: 'us-east-1',
144
- credentials: Aws::Credentials.new(
145
- ENV['AWS_ACCESS_KEY'],
146
- ENV['AWS_SECRET_ACCESS_KEY'])
147
- end
148
- }
154
+ require 'faraday_middleware/aws_sigv4'
155
+
156
+ Chewy.settings = {
157
+ host: 'http://my-es-instance-on-aws.us-east-1.es.amazonaws.com:80',
158
+ port: 80, # 443 for https host
159
+ transport_options: {
160
+ headers: { content_type: 'application/json' },
161
+ proc: -> (f) do
162
+ f.request :aws_sigv4,
163
+ service: 'es',
164
+ region: 'us-east-1',
165
+ access_key_id: ENV['AWS_ACCESS_KEY'],
166
+ secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
167
+ end
149
168
  }
150
- ```
169
+ }
170
+ ```
151
171
 
152
172
  ### Index definition
153
173
 
@@ -180,7 +200,7 @@ If you would like to use AWS's ElasticSearch using an IAM user policy, you will
180
200
  field :badges, value: ->(user) { user.badges.map(&:name) } # passing array values to index
181
201
  field :projects do # the same block syntax for multi_field, if `:type` is specified
182
202
  field :title
183
- field :description # default data type is `string`
203
+ field :description # default data type is `text`
184
204
  # additional top-level objects passed to value proc:
185
205
  field :categories, value: ->(project, user) { project.categories.map(&:name) if user.active? }
186
206
  end
@@ -208,7 +228,7 @@ If you would like to use AWS's ElasticSearch using an IAM user policy, you will
208
228
 
209
229
  define_type User.active.includes(:country, :badges, :projects) do
210
230
  root date_detection: false do
211
- template 'about_translations.*', type: 'string', analyzer: 'standard'
231
+ template 'about_translations.*', type: 'text', analyzer: 'standard'
212
232
 
213
233
  field :first_name, :last_name
214
234
  field :email, analyzer: 'email'
@@ -228,7 +248,7 @@ If you would like to use AWS's ElasticSearch using an IAM user policy, you will
228
248
  ```
229
249
 
230
250
  [See index settings here](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html).
231
- [See root object settings here](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-root-object-type.html).
251
+ [See root object settings here](https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-field-mapping.html).
232
252
 
233
253
  See [mapping.rb](lib/chewy/type/mapping.rb) for more details.
234
254
 
@@ -335,9 +355,9 @@ This will automatically set the type or root field to `object`. You may also spe
335
355
  To define a multi field you have to specify any type except for `object` or `nested` in the root field:
336
356
 
337
357
  ```ruby
338
- field :full_name, type: 'string', value: ->{ full_name.strip } do
358
+ field :full_name, type: 'text', value: ->{ full_name.strip } do
339
359
  field :ordered, analyzer: 'ordered'
340
- field :untouched, index: 'not_analyzed'
360
+ field :untouched, type: 'keyword'
341
361
  end
342
362
  ```
343
363
 
@@ -357,7 +377,7 @@ end
357
377
  ```
358
378
  ### Geo Point fields
359
379
 
360
- You can use [Elasticsearch's geo mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-geo-point-type.html) with the `geo_point` field type, allowing you to query, filter and order by latitude and longitude. You can use the following hash format:
380
+ You can use [Elasticsearch's geo mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html) with the `geo_point` field type, allowing you to query, filter and order by latitude and longitude. You can use the following hash format:
361
381
 
362
382
  ```ruby
363
383
  field :coordinates, type: 'geo_point', value: ->{ {lat: latitude, lon: longitude} }
@@ -678,6 +698,11 @@ Chewy.strategy(:sidekiq) do
678
698
  end
679
699
  ```
680
700
 
701
+ The default queue name is `chewy`, you can customize it in settings: `sidekiq.queue_name`
702
+ ```
703
+ Chewy.settings[:sidekiq] = {queue: :low}
704
+ ```
705
+
681
706
  #### `:active_job`
682
707
 
683
708
  This does the same thing as `:atomic`, but using ActiveJob. This will inherit the ActiveJob configuration settings including the `active_job.queue_adapter` setting for the environment. Patch `Chewy::Strategy::ActiveJob::Worker` for index updates improving.
@@ -688,6 +713,11 @@ Chewy.strategy(:active_job) do
688
713
  end
689
714
  ```
690
715
 
716
+ The default queue name is `chewy`, you can customize it in settings: `active_job.queue_name`
717
+ ```
718
+ Chewy.settings[:active_job] = {queue: :low}
719
+ ```
720
+
691
721
  #### `:shoryuken`
692
722
 
693
723
  This does the same thing as `:atomic`, but asynchronously using shoryuken. Patch `Chewy::Strategy::Shoryuken::Worker` for index updates improving.
@@ -807,38 +837,84 @@ Chewy has notifying the following events:
807
837
  To integrate with NewRelic you may use the following example source (config/initializers/chewy.rb):
808
838
 
809
839
  ```ruby
810
- ActiveSupport::Notifications.subscribe('import_objects.chewy') do |name, start, finish, id, payload|
811
- metric_name = "Database/ElasticSearch/import"
812
- duration = (finish - start).to_f
813
- logged = "#{payload[:type]} #{payload[:import].to_a.map{ |i| i.join(':') }.join(', ')}"
814
-
815
- self.class.trace_execution_scoped([metric_name]) do
816
- NewRelic::Agent.instance.transaction_sampler.notice_sql(logged, nil, duration)
817
- NewRelic::Agent.instance.sql_sampler.notice_sql(logged, metric_name, nil, duration)
818
- NewRelic::Agent.record_metric(metric_name, duration)
840
+ require 'new_relic/agent/instrumentation/evented_subscriber'
841
+
842
+ class ChewySubscriber < NewRelic::Agent::Instrumentation::EventedSubscriber
843
+ def start(name, id, payload)
844
+ event = ChewyEvent.new(name, Time.current, nil, id, payload)
845
+ push_event(event)
846
+ end
847
+
848
+ def finish(_name, id, _payload)
849
+ pop_event(id).finish
819
850
  end
820
- end
821
851
 
822
- ActiveSupport::Notifications.subscribe('search_query.chewy') do |name, start, finish, id, payload|
823
- metric_name = "Database/ElasticSearch/search"
824
- duration = (finish - start).to_f
825
- logged = "#{payload[:type].presence || payload[:index]} #{payload[:request]}"
852
+ class ChewyEvent < NewRelic::Agent::Instrumentation::Event
853
+ OPERATIONS = {
854
+ 'import_objects.chewy' => 'import',
855
+ 'search_query.chewy' => 'search',
856
+ 'delete_query.chewy' => 'delete'
857
+ }.freeze
826
858
 
827
- self.class.trace_execution_scoped([metric_name]) do
828
- NewRelic::Agent.instance.transaction_sampler.notice_sql(logged, nil, duration)
829
- NewRelic::Agent.instance.sql_sampler.notice_sql(logged, metric_name, nil, duration)
830
- NewRelic::Agent.record_metric(metric_name, duration)
859
+ def initialize(*args)
860
+ super
861
+ @segment = start_segment
862
+ end
863
+
864
+ def start_segment
865
+ segment = NewRelic::Agent::Transaction::DatastoreSegment.new product, operation, collection, host, port
866
+ if (txn = state.current_transaction)
867
+ segment.transaction = txn
868
+ end
869
+ segment.notice_sql @payload[:request].to_s
870
+ segment.start
871
+ segment
872
+ end
873
+
874
+ def finish
875
+ if (txn = state.current_transaction)
876
+ txn.add_segment @segment
877
+ end
878
+ @segment.finish
879
+ end
880
+
881
+ private
882
+
883
+ def state
884
+ @state ||= NewRelic::Agent::TransactionState.tl_get
885
+ end
886
+
887
+ def product
888
+ 'Elasticsearch'
889
+ end
890
+
891
+ def operation
892
+ OPERATIONS[name]
893
+ end
894
+
895
+ def collection
896
+ payload.values_at(:type, :index)
897
+ .reject { |value| value.try(:empty?) }
898
+ .first
899
+ .to_s
900
+ end
901
+
902
+ def host
903
+ Chewy.client.transport.hosts.first[:host]
904
+ end
905
+
906
+ def port
907
+ Chewy.client.transport.hosts.first[:port]
908
+ end
831
909
  end
832
910
  end
911
+
912
+ ActiveSupport::Notifications.subscribe(/.chewy$/, ChewySubscriber.new)
833
913
  ```
834
914
 
835
915
  ### Search requests
836
916
 
837
- Long story short: there is a new DSL that supports ES2 and ES5, the previous DSL version (which supports ES1 and ES2) documentation was moved to [LEGACY_DSL.md](LEGACY_DSL.md).
838
-
839
- If you want to use it - simply do `Chewy.search_class = Chewy::Query` somewhere before indices are initialized.
840
-
841
- The new DSL is enabled by default, here is a quick introduction.
917
+ Quick introduction.
842
918
 
843
919
  #### Composing requests
844
920
 
@@ -906,17 +982,6 @@ scope.each do |wrapper|
906
982
  end
907
983
  ```
908
984
 
909
- #### Legacy DSL incompatibilities
910
-
911
- * Filters advanced block DSL is not supported anymore, `elasticsearch-dsl` is used instead.
912
- * Things like `query_mode` and `filter_mode` are in past, use advanced DSL to achieve similar behavior. See [Chewy::Search::QueryProxy](lib/chewy/search/query_proxy.rb) for details.
913
- * `preload` method is no more, the collection returned by scope doesn't depend on loading options, scope always returns `Chewy::Type` wrappers. To get ORM/ODM objects, use `#objects` method.
914
- * Some of the methods have changed their purpose: `only` was used to filter fields before, now it filters the scope. To filter fields use `source` or `stored_fields`.
915
- * `types!` method is no more, use `except(:types).types(...)`
916
- * Named aggregations are not supported, use named scopes instead.
917
- * A lot of query-level methods were not ported: everything that is related to boost and scoring. Use `query` manipulation to provide them.
918
- * `Chewy::Type#_object` returns nil always. Use `Chewy::Search::Response#object_hash` instead.
919
-
920
985
  ### Rake tasks
921
986
 
922
987
  For a Rails application, some index-maintaining rake tasks are defined.
@@ -936,9 +1001,9 @@ rake chewy:reset[-users,places] # resets every index in the application except s
936
1001
 
937
1002
  Performs reset exactly the same way as `chewy:reset` does, but only when the index specification (setting or mapping) was changed.
938
1003
 
939
- It works only when index specification is locked in `Chewy::Stash` index. The first run will reset all indexes and lock their specifications.
1004
+ It works only when index specification is locked in `Chewy::Stash::Specification` index. The first run will reset all indexes and lock their specifications.
940
1005
 
941
- See [Chewy::Stash](lib/chewy/stash.rb) and [Chewy::Index::Specification](lib/chewy/index/specification.rb) for more details.
1006
+ See [Chewy::Stash::Specification](lib/chewy/stash.rb) and [Chewy::Index::Specification](lib/chewy/index/specification.rb) for more details.
942
1007
 
943
1008
 
944
1009
  ```bash
@@ -1011,7 +1076,7 @@ rake chewy:journal:apply["$(date -v-1H -u +%FT%TZ)"] # apply journaled changes f
1011
1076
  rake chewy:journal:apply["$(date -v-1H -u +%FT%TZ)",users] # apply journaled changes for the past hour on UsersIndex only
1012
1077
  ```
1013
1078
 
1014
- ### Rspec integration
1079
+ ### RSpec integration
1015
1080
 
1016
1081
  Just add `require 'chewy/rspec'` to your spec_helper.rb and you will get additional features: See [update_index.rb](lib/chewy/rspec/update_index.rb) for more details.
1017
1082
 
@@ -1032,12 +1097,6 @@ If you use `DatabaseCleaner` in your tests with [the `transaction` strategy](htt
1032
1097
  Chewy.use_after_commit_callbacks = !Rails.env.test?
1033
1098
  ```
1034
1099
 
1035
- ## TODO a.k.a coming soon:
1036
-
1037
- * Typecasting support
1038
- * update_all support
1039
- * Maybe, closer ORM/ODM integration, creating index classes implicitly
1040
-
1041
1100
  ## Contributing
1042
1101
 
1043
1102
  1. Fork it (http://github.com/toptal/chewy/fork)
@@ -1053,3 +1112,8 @@ Use the following Rake tasks to control the Elasticsearch cluster while developi
1053
1112
  rake elasticsearch:start # start Elasticsearch cluster on 9250 port for tests
1054
1113
  rake elasticsearch:stop # stop Elasticsearch
1055
1114
  ```
1115
+
1116
+ ## Copyright
1117
+
1118
+ Copyright (c) 2013-2021 Toptal, LLC. See [LICENSE.txt](LICENSE.txt) for
1119
+ further details.
data/chewy.gemspec CHANGED
@@ -1,5 +1,3 @@
1
- # coding: utf-8
2
-
3
1
  lib = File.expand_path('../lib', __FILE__)
4
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
3
  require 'chewy/version'
@@ -7,8 +5,8 @@ require 'chewy/version'
7
5
  Gem::Specification.new do |spec| # rubocop:disable BlockLength
8
6
  spec.name = 'chewy'
9
7
  spec.version = Chewy::VERSION
10
- spec.authors = ['pyromaniac']
11
- spec.email = ['kinwizard@gmail.com']
8
+ spec.authors = ['Toptal, LLC', 'pyromaniac']
9
+ spec.email = ['open-source@toptal.com', 'kinwizard@gmail.com']
12
10
  spec.summary = 'Elasticsearch ODM client wrapper'
13
11
  spec.description = 'Chewy provides functionality for Elasticsearch index handling, documents import mappings and chainable query DSL'
14
12
  spec.homepage = 'https://github.com/toptal/chewy'
@@ -19,22 +17,22 @@ Gem::Specification.new do |spec| # rubocop:disable BlockLength
19
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
18
  spec.require_paths = ['lib']
21
19
 
22
- spec.add_development_dependency 'rake'
23
20
  spec.add_development_dependency 'appraisal'
24
- spec.add_development_dependency 'rspec'
25
- spec.add_development_dependency 'rspec-its'
26
- spec.add_development_dependency 'rspec-collection_matchers'
27
- spec.add_development_dependency 'sqlite3'
28
21
  spec.add_development_dependency 'database_cleaner'
29
22
  spec.add_development_dependency 'elasticsearch-extensions'
23
+ spec.add_development_dependency 'rake'
30
24
  spec.add_development_dependency 'resque_spec'
25
+ spec.add_development_dependency 'rspec', '>= 3.7.0'
26
+ spec.add_development_dependency 'rspec-collection_matchers'
27
+ spec.add_development_dependency 'rspec-its'
28
+ spec.add_development_dependency 'rubocop', '0.52.1'
29
+ spec.add_development_dependency 'sqlite3'
31
30
  spec.add_development_dependency 'timecop'
32
- spec.add_development_dependency 'rubocop', '0.48.1'
33
31
 
34
32
  spec.add_development_dependency 'method_source'
35
33
  spec.add_development_dependency 'unparser'
36
34
 
37
- spec.add_dependency 'activesupport', '>= 4.0'
38
- spec.add_dependency 'elasticsearch', '>= 2.0.0'
35
+ spec.add_dependency 'activesupport', '>= 5.2'
36
+ spec.add_dependency 'elasticsearch', '>= 6.3.0'
39
37
  spec.add_dependency 'elasticsearch-dsl'
40
38
  end