chewy 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile +0 -3
- data/lib/chewy.rb +0 -1
- data/lib/chewy/errors.rb +3 -0
- data/lib/chewy/index.rb +2 -2
- data/lib/chewy/query.rb +34 -11
- data/lib/chewy/query/criteria.rb +13 -19
- data/lib/chewy/search.rb +35 -0
- data/lib/chewy/type.rb +2 -2
- data/lib/chewy/version.rb +1 -1
- data/spec/chewy/index/search_spec.rb +1 -1
- data/spec/chewy/query_spec.rb +25 -0
- metadata +3 -3
- data/lib/chewy/index/search.rb +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 751edf8eb80e8e5a9f2eaf9dc2d1a1e639427a91
|
4
|
+
data.tar.gz: 985d1dd1398d00df7f922cf9147cae1a006f9ca0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf632b85c2b0849ef88b549a170deca5d2ac39f2ec6822eabc7f5bd2d9d3c86ccbca2199bdae3ae72990e59f1de8945aee4547b0efe6b84ae789435895af9c10
|
7
|
+
data.tar.gz: 787052a2a10499f4b0dba7b68292a7a300616cc170c34e264d821e862fe69553bf61ef1fe9c5d9c6419d6b720fd3673b1cbd564c0011119261ed8e80beff600a
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/lib/chewy.rb
CHANGED
data/lib/chewy/errors.rb
CHANGED
data/lib/chewy/index.rb
CHANGED
@@ -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
|
|
data/lib/chewy/query.rb
CHANGED
@@ -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
|
-
|
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)
|
data/lib/chewy/query/criteria.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
121
|
-
|
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
|
data/lib/chewy/search.rb
ADDED
@@ -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
|
data/lib/chewy/type.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'chewy/
|
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
|
13
|
+
include Search
|
14
14
|
include Mapping
|
15
15
|
include Wrapper
|
16
16
|
include Observe
|
data/lib/chewy/version.rb
CHANGED
data/spec/chewy/query_spec.rb
CHANGED
@@ -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.
|
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-
|
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
|
data/lib/chewy/index/search.rb
DELETED
@@ -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
|