elasticband 0.3.0 → 0.4.0

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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YmE0MWFkZjBhOWEzNjY1ODgzZTkwZGIwMWE2YTc2OWVlZjUwZDc3NQ==
4
+ YTJlMTE0ZjI5Mjc0ODE5MjNkODU0YjgwNzlhYTI5M2JhOTM5ZGQzMQ==
5
5
  data.tar.gz: !binary |-
6
- NDNlMGI1Y2VjNjA3ZGYyN2ViZDg2ODhkNzY5YjQ5OTFlMDRkODEyYw==
6
+ NzNmZTY1MzY0ZmZmNzFmM2Q3NGQwYjE1MjkwMzBhMDNiMjU3ZDQ1Ng==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- Yjg1MzQ3M2U4ZmY0YWZhNTVhZDVjOThiOGE3NTEwMGY2NWQ0ZDAxNTllODc3
10
- ZThjZjQ0NDlmMzMwMDg0OWI4NjZhM2MxNjQ4ODQ1OTFiMTgyMmI5NmE0YmQ4
11
- YTVlZGM1NGQzNDBhNTc4NDMzMGNlNThiMDE3YzJkNTRjY2Y0NDc=
9
+ MDdmYjljMDZmYWI3YTE3NmE2ODhmZGQyY2ZjOTAxMGUwOTQ0ZTYyOWY5M2Jl
10
+ MTdkNDYzYWQxZDA0ZDNmZmQ5ZWQ3OThjZjk0OWU3MWUxZmI2ZDA0OGM4MzU5
11
+ Yjk1NzIwOTRiMzM0ZGY4ODFmODgwMTUxODc1ZDIzNDc1YjVmMzQ=
12
12
  data.tar.gz: !binary |-
13
- YWYyMmEzMzBhOWQ3Y2ZjZWY4YmVkMmFmODlhNTA0MWI2YWJmNjg4YmNjMGUx
14
- OWQ2Njk4NTRkMGFlOTQzMmYyNGY1ZmQ4YzkyNGM3YjAzMmQyMzI0MTJkOWY2
15
- YTViNWEwNTNlMWMzNmIzYWM5MTg3MTU4M2RiMjA5MjM4OGIwMWQ=
13
+ OTcyZGNkY2UyYTNhMWUxODk4N2EwMWJlZDg2YWUxMmI1NDdiZDU1NzkyY2Y4
14
+ MzY3YWJkNjBjOGYzYTdmOTkxODBmYjIwY2Q3YjQ3OTA0MDJlM2NhMjMxOWRk
15
+ MzhmYWViYjEyODhlOWFkYjE0YWQzMTE2MGRmZGVlODdiNTYyZTk=
@@ -0,0 +1,26 @@
1
+ module Elasticband
2
+ class Filter
3
+ class Script < Filter
4
+ attr_accessor :script, :params
5
+
6
+ def initialize(script, params = {})
7
+ self.script = script
8
+ self.params = params
9
+ end
10
+
11
+ def to_h
12
+ return {} unless script
13
+
14
+ { script: { script: script }.merge(params_hash) }
15
+ end
16
+
17
+ private
18
+
19
+ def params_hash
20
+ return {} unless params.any?
21
+
22
+ { params: params }
23
+ end
24
+ end
25
+ end
26
+ end
@@ -5,6 +5,7 @@ require 'elasticband/filter/range'
5
5
  require 'elasticband/filter/near'
6
6
  require 'elasticband/filter/term'
7
7
  require 'elasticband/filter/terms'
8
+ require 'elasticband/filter/script'
8
9
 
9
10
  module Elasticband
10
11
  class Filter
@@ -24,6 +25,7 @@ module Elasticband
24
25
  # * `includes:` Filter the search results with a `Match` query.
25
26
  # * `range:` Filter the search results where the condition is on the given range.
26
27
  # * `near:` Filter the search results where the results are near a geo point.
28
+ # * `script:` Filter the search results where the results match the script.
27
29
  #
28
30
  # #### Examples
29
31
  # ```
@@ -41,19 +43,30 @@ module Elasticband
41
43
  #
42
44
  # Filter.parse(near: { latitude: 12, longitude: 34, distance: '5km' })
43
45
  # => { geo_distance: { location: { lat: 12, lon: 34 } }, distance: '5km' }
46
+ #
47
+ # Filter.parse(script: ['(param1 + param2) > 0', param1: 1, param2: 1])
48
+ # => { script: { script: '(param1 + param2) > 0', params: { param1: 1, param2: 1 } } }
44
49
  # ```
45
50
  def parse(options = {})
46
51
  return {} if options.blank?
47
52
 
48
- filter = only_and_except_filters(options[:only], options[:except])
49
- filter += includes_filter(options[:includes])
50
- filter += range_filter(options[:range])
51
- filter += near_filter(options[:near])
52
- join_filters(filter).to_h
53
+ filters = map_filters(options)
54
+
55
+ join_filters(filters).to_h
53
56
  end
54
57
 
55
58
  private
56
59
 
60
+ def map_filters(options)
61
+ filters = only_and_except_filters(options[:only], options[:except])
62
+ filters += includes_filter(options[:includes])
63
+ filters += range_filter(options[:range])
64
+ filters += near_filter(options[:near])
65
+ filters += script_filter(options[:script])
66
+
67
+ filters
68
+ end
69
+
57
70
  def only_and_except_filters(only_options, except_options)
58
71
  parse_filters(only_options) + parse_filters(except_options).map { |f| Filter::Not.new(f) }
59
72
  end
@@ -80,6 +93,12 @@ module Elasticband
80
93
  [Filter::Near.new(options)]
81
94
  end
82
95
 
96
+ def script_filter(script_options)
97
+ return [] if script_options.blank?
98
+
99
+ [Filter::Script.new(*script_options)]
100
+ end
101
+
83
102
  def parse_filters(options)
84
103
  return [] if options.blank?
85
104
 
@@ -1,3 +1,3 @@
1
1
  module Elasticband
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Elasticband::Filter::Script do
4
+ describe '#to_h' do
5
+ context 'without params' do
6
+ subject { described_class.new('(param1 + param2) > 0').to_h }
7
+
8
+ it { is_expected.to eq(script: { script: '(param1 + param2) > 0' }) }
9
+ end
10
+
11
+ context 'with params' do
12
+ subject { described_class.new('(param1 + param2) > 0', param1: 1, param2: 1).to_h }
13
+
14
+ it { is_expected.to eq(script: { script: '(param1 + param2) > 0', params: { param1: 1, param2: 1 } }) }
15
+ end
16
+ end
17
+ end
data/spec/filter_spec.rb CHANGED
@@ -104,5 +104,12 @@ RSpec.describe Elasticband::Filter do
104
104
 
105
105
  it { is_expected.to eq(filter) }
106
106
  end
107
+
108
+ context 'with `:script` option' do
109
+ let(:options) { { script: ['(param1 + param2) > 0', param1: 1, param2: 1] } }
110
+ let(:filter) { { script: { script: '(param1 + param2) > 0', params: { param1: 1, param2: 1 } } } }
111
+
112
+ it { is_expected.to eq(filter) }
113
+ end
107
114
  end
108
115
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticband
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Glauber Campinho
@@ -127,6 +127,7 @@ files:
127
127
  - lib/elasticband/filter/not.rb
128
128
  - lib/elasticband/filter/query.rb
129
129
  - lib/elasticband/filter/range.rb
130
+ - lib/elasticband/filter/script.rb
130
131
  - lib/elasticband/filter/term.rb
131
132
  - lib/elasticband/filter/terms.rb
132
133
  - lib/elasticband/query.rb
@@ -158,6 +159,7 @@ files:
158
159
  - spec/filter/not_spec.rb
159
160
  - spec/filter/query_spec.rb
160
161
  - spec/filter/range_spec.rb
162
+ - spec/filter/script_spec.rb
161
163
  - spec/filter/term_spec.rb
162
164
  - spec/filter/terms_spec.rb
163
165
  - spec/filter_spec.rb