chewy 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11ba7489e6c513a9a8e81cc220a64718808fcde0
4
- data.tar.gz: 9a3336d2e062b027ab1ca03396dc6eac549d964a
3
+ metadata.gz: 751edf8eb80e8e5a9f2eaf9dc2d1a1e639427a91
4
+ data.tar.gz: 985d1dd1398d00df7f922cf9147cae1a006f9ca0
5
5
  SHA512:
6
- metadata.gz: 17dfc426d7a92c958d5d1548aa7420e380b984297c650e8b9f29658306a8ef30b9a75c3a6d86e4149af6d3d6008228fd709a98797f881fc28600fbcb511cee26
7
- data.tar.gz: 55899323c6be7796cd3640a3386c9f39ccfd785a01c0594e74a53fd9022b508e0372644dc92a739c997b9f3ba094e021ed81b2c6b4fc125910a2bec2b15aa515
6
+ metadata.gz: bf632b85c2b0849ef88b549a170deca5d2ac39f2ec6822eabc7f5bd2d9d3c86ccbca2199bdae3ae72990e59f1de8945aee4547b0efe6b84ae789435895af9c10
7
+ data.tar.gz: 787052a2a10499f4b0dba7b68292a7a300616cc170c34e264d821e862fe69553bf61ef1fe9c5d9c6419d6b720fd3673b1cbd564c0011119261ed8e80beff600a
@@ -1,5 +1,13 @@
1
1
  # master
2
2
 
3
+ # Version 0.6.1
4
+
5
+ ## Changes
6
+
7
+ * `min_score` query option support (@jshirley)
8
+
9
+ * `Chewy::Query#find` method for finding records by id
10
+
3
11
  # Version 0.6.0
4
12
 
5
13
  ## Changes
data/Gemfile CHANGED
@@ -11,7 +11,4 @@ gem 'kaminari', require: false
11
11
  group :test do
12
12
  gem 'guard'
13
13
  gem 'guard-rspec'
14
- gem 'rb-inotify', require: false
15
- gem 'rb-fsevent', require: false
16
- gem 'rb-fchange', require: false
17
14
  end
@@ -15,7 +15,6 @@ require 'chewy/config'
15
15
  require 'chewy/runtime'
16
16
  require 'chewy/index'
17
17
  require 'chewy/type'
18
- require 'chewy/query'
19
18
  require 'chewy/fields/base'
20
19
  require 'chewy/fields/root'
21
20
 
@@ -11,6 +11,9 @@ module Chewy
11
11
  class UnderivableType < Error
12
12
  end
13
13
 
14
+ class DocumentNotFound < Error
15
+ end
16
+
14
17
  class ImportFailed < Error
15
18
  def initialize type, errors
16
19
  output = "Import failed for `#{type}` with:\n"
@@ -1,13 +1,13 @@
1
+ require 'chewy/search'
1
2
  require 'chewy/index/actions'
2
3
  require 'chewy/index/aliases'
3
- require 'chewy/index/search'
4
4
  require 'chewy/index/settings'
5
5
 
6
6
  module Chewy
7
7
  class Index
8
+ include Search
8
9
  include Actions
9
10
  include Aliases
10
- include Search
11
11
 
12
12
  singleton_class.delegate :client, to: 'Chewy'
13
13
 
@@ -314,6 +314,14 @@ module Chewy
314
314
  chain { criteria.update_request_options rescore: value }
315
315
  end
316
316
 
317
+ # Elasticsearch minscore option support
318
+ #
319
+ # UsersIndex.query(...).min_score(0.5)
320
+ #
321
+ def min_score value
322
+ chain { criteria.update_request_options min_score: value }
323
+ end
324
+
317
325
  # Adds facets section to the search request.
318
326
  # All the chained facets a merged and added to the
319
327
  # search request
@@ -530,6 +538,7 @@ module Chewy
530
538
  # # => []
531
539
  # UsersIndex.none.query(text: {name: 'Johny'}).to_a
532
540
  # # => []
541
+ #
533
542
  def none
534
543
  chain { criteria.update_options none: true }
535
544
  end
@@ -836,7 +845,31 @@ module Chewy
836
845
  # UsersIndex::User.filter{ age <= 42 }.delete_all
837
846
  #
838
847
  def delete_all
839
- _delete_all_response
848
+ request = chain { criteria.update_options simple: true }.send(:_request)
849
+ ActiveSupport::Notifications.instrument 'delete_query.chewy', request: request, index: index do
850
+ index.client.delete_by_query(request)
851
+ end
852
+ end
853
+
854
+ # Deletes all records matching a query.
855
+ #
856
+ # UsersIndex.find(42)
857
+ # UsersIndex.filter{ age <= 42 }.find(42)
858
+ # UsersIndex::User.find(42)
859
+ # UsersIndex::User.filter{ age <= 42 }.find(42)
860
+ #
861
+ # In all the previous examples find will return a single object.
862
+ # To get a collection - pass an array of ids.
863
+ #
864
+ # UsersIndex::User.find(42, 7, 3) # array of objects with ids in [42, 7, 3]
865
+ # UsersIndex::User.find([8, 13]) # array of objects with ids in [8, 13]
866
+ # UsersIndex::User.find([42]) # array of the object with id == 42
867
+ #
868
+ def find *ids
869
+ results = chain { criteria.update_options simple: true }.filter { _id == ids.flatten }.to_a
870
+
871
+ raise Chewy::DocumentNotFound.new("Could not find documents for ids #{ids.flatten}") if results.empty?
872
+ ids.one? && !ids.first.is_a?(Array) ? results.first : results
840
873
  end
841
874
 
842
875
  # Returns request total time elapsed as reported by elasticsearch
@@ -882,10 +915,6 @@ module Chewy
882
915
  @_request ||= criteria.request_body.merge(index: index.index_name, type: types)
883
916
  end
884
917
 
885
- def _delete_all_request
886
- @_delete_all_request ||= criteria.delete_all_request_body.merge(index: index.index_name, type: types)
887
- end
888
-
889
918
  def _response
890
919
  @_response ||= ActiveSupport::Notifications.instrument 'search_query.chewy', request: _request, index: index do
891
920
  begin
@@ -897,12 +926,6 @@ module Chewy
897
926
  end
898
927
  end
899
928
 
900
- def _delete_all_response
901
- @_delete_all_response ||= ActiveSupport::Notifications.instrument 'delete_query.chewy', request: _delete_all_request, index: index do
902
- index.client.delete_by_query(_delete_all_request)
903
- end
904
- end
905
-
906
929
  def _results
907
930
  @_results ||= (criteria.none? || _response == {} ? [] : _response['hits']['hits']).map do |hit|
908
931
  attributes = (hit['_source'] || {}).merge(hit['highlight'] || {}, &RESULT_MERGER)
@@ -101,24 +101,22 @@ module Chewy
101
101
  end
102
102
 
103
103
  def request_body
104
- body = {}
104
+ body = _filtered_query(_request_query, _request_filter, options.slice(:strategy))
105
105
 
106
+ if options[:simple]
107
+ { body: body.presence || { query: { match_all: {} } } }
108
+ else
109
+ body.merge!(post_filter: _request_post_filter) if post_filters?
110
+ body.merge!(facets: facets) if facets?
111
+ body.merge!(aggregations: aggregations) if aggregations?
112
+ body.merge!(suggest: suggest) if suggest?
113
+ body.merge!(sort: sort) if sort?
114
+ body.merge!(_source: fields) if fields?
106
115
 
107
- body.merge!(_filtered_query(_request_query, _request_filter, options.slice(:strategy)))
108
- body.merge!(post_filter: _request_post_filter) if post_filters?
109
- body.merge!(facets: facets) if facets?
110
- body.merge!(aggregations: aggregations) if aggregations?
111
- body.merge!(suggest: suggest) if suggest?
112
- body.merge!(sort: sort) if sort?
113
- body.merge!(_source: fields) if fields?
114
-
115
- body = _boost_query(body)
116
-
117
- { body: body.merge!(request_options) }
118
- end
116
+ body = _boost_query(body)
119
117
 
120
- def delete_all_request_body
121
- { body: _filtered_query(_request_query, _request_filter, options.slice(:strategy)).presence || { query: { match_all: {} } } }
118
+ { body: body.merge!(request_options) }
119
+ end
122
120
  end
123
121
 
124
122
  protected
@@ -151,10 +149,6 @@ module Chewy
151
149
  body.tap { |b| b[:query] = { function_score: score } }
152
150
  end
153
151
 
154
- def _request_options
155
- options.slice(:size, :from, :explain, :highlight, :rescore)
156
- end
157
-
158
152
  def _request_query
159
153
  _queries_join(queries, options[:query_mode])
160
154
  end
@@ -0,0 +1,35 @@
1
+ require 'chewy/query'
2
+
3
+ module Chewy
4
+ module Search
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ singleton_class.delegate :explain, :query_mode, :filter_mode, :post_filter_mode,
9
+ :timeout, :limit, :offset, :highlight, :min_score, :rescore, :facets, :script_score,
10
+ :boost_factor, :random_score, :field_value_factor, :decay, :aggregations,
11
+ :suggest, :none, :strategy, :query, :filter, :post_filter, :boost_mode,
12
+ :score_mode, :order, :reorder, :only, :types, :delete_all, :find, :total,
13
+ :total_count, :total_entries, to: :all
14
+ end
15
+
16
+ module ClassMethods
17
+ def all
18
+ Chewy::Query.new(search_index, types: search_type)
19
+ end
20
+
21
+ def search_string query, options = {}
22
+ options = options.merge(index: search_index.index_name, type: search_type, q: query)
23
+ client.search(options)
24
+ end
25
+
26
+ def search_index
27
+ raise NotImplementedError
28
+ end
29
+
30
+ def search_type
31
+ raise NotImplementedError
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,4 +1,4 @@
1
- require 'chewy/index/search'
1
+ require 'chewy/search'
2
2
  require 'chewy/type/mapping'
3
3
  require 'chewy/type/wrapper'
4
4
  require 'chewy/type/observe'
@@ -10,7 +10,7 @@ require 'chewy/type/adapter/mongoid'
10
10
 
11
11
  module Chewy
12
12
  class Type
13
- include Chewy::Index::Search
13
+ include Search
14
14
  include Mapping
15
15
  include Wrapper
16
16
  include Observe
@@ -1,3 +1,3 @@
1
1
  module Chewy
2
- VERSION = '0.6.0'
2
+ VERSION = '0.6.1'
3
3
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Chewy::Index::Search do
3
+ describe Chewy::Search do
4
4
  before do
5
5
  stub_index(:products) do
6
6
  define_type :product
@@ -291,6 +291,31 @@ describe Chewy::Query do
291
291
  specify { expect { ProductsIndex::City.delete_all }.to change { ProductsIndex.total }.from(9).to(6) }
292
292
  end
293
293
 
294
+ describe '#find' do
295
+ let(:products) { 3.times.map { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
296
+ let(:cities) { 1.times.map { |i| {id: '4'}.stringify_keys! } }
297
+ let(:countries) { 1.times.map { |i| {id: '4'}.stringify_keys! } }
298
+
299
+ before do
300
+ ProductsIndex::Product.import!(products.map { |h| double(h) })
301
+ ProductsIndex::City.import!(cities.map { |h| double(h) })
302
+ ProductsIndex::Country.import!(countries.map { |h| double(h) })
303
+ end
304
+
305
+ specify { expect(subject.find(1)).to be_a(ProductsIndex::Product) }
306
+ specify { expect(subject.find(1).id).to eq('1') }
307
+ specify { expect(subject.find(4).id).to eq('4') }
308
+ specify { expect(subject.find([1]).map(&:id)).to match_array(%w(1)) }
309
+ specify { expect(subject.find([4]).map(&:id)).to match_array(%w(4 4)) }
310
+ specify { expect(subject.find([1, 3]).map(&:id)).to match_array(%w(1 3)) }
311
+ specify { expect(subject.find(1, 3).map(&:id)).to match_array(%w(1 3)) }
312
+ specify { expect(subject.find(1, 10).map(&:id)).to match_array(%w(1)) }
313
+
314
+ specify { expect { subject.find(10) }.to raise_error Chewy::DocumentNotFound }
315
+ specify { expect { subject.find([10]) }.to raise_error Chewy::DocumentNotFound }
316
+ specify { expect { subject.find([10, 20]) }.to raise_error Chewy::DocumentNotFound }
317
+ end
318
+
294
319
  describe '#none' do
295
320
  specify { expect(subject.none).to be_a described_class }
296
321
  specify { expect(subject.none).not_to eq(subject) }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chewy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - pyromaniac
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-29 00:00:00.000000000 Z
11
+ date: 2014-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -186,7 +186,6 @@ files:
186
186
  - lib/chewy/index.rb
187
187
  - lib/chewy/index/actions.rb
188
188
  - lib/chewy/index/aliases.rb
189
- - lib/chewy/index/search.rb
190
189
  - lib/chewy/index/settings.rb
191
190
  - lib/chewy/query.rb
192
191
  - lib/chewy/query/compose.rb
@@ -221,6 +220,7 @@ files:
221
220
  - lib/chewy/rspec/update_index.rb
222
221
  - lib/chewy/runtime.rb
223
222
  - lib/chewy/runtime/version.rb
223
+ - lib/chewy/search.rb
224
224
  - lib/chewy/type.rb
225
225
  - lib/chewy/type/actions.rb
226
226
  - lib/chewy/type/adapter/active_record.rb
@@ -1,35 +0,0 @@
1
- module Chewy
2
- class Index
3
- module Search
4
- extend ActiveSupport::Concern
5
-
6
- included do
7
- singleton_class.delegate :explain, :query_mode, :filter_mode, :post_filter_mode,
8
- :timeout, :limit, :offset, :highlight, :rescore, :facets, :script_score,
9
- :boost_factor, :random_score, :field_value_factor, :decay, :aggregations,
10
- :suggest, :none, :strategy, :query, :filter, :post_filter, :boost_mode,
11
- :score_mode, :order, :reorder, :only, :types, :delete_all, :total,
12
- :total_count, :total_entries, to: :all
13
- end
14
-
15
- module ClassMethods
16
- def all
17
- Chewy::Query.new(search_index, types: search_type)
18
- end
19
-
20
- def search_string query, options = {}
21
- options = options.merge(index: search_index.index_name, type: search_type, q: query)
22
- client.search(options)
23
- end
24
-
25
- def search_index
26
- raise NotImplementedError
27
- end
28
-
29
- def search_type
30
- raise NotImplementedError
31
- end
32
- end
33
- end
34
- end
35
- end