elasticband 0.1.7 → 0.1.8
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 +8 -8
- data/lib/elasticband/filter/range.rb +31 -0
- data/lib/elasticband/filter.rb +13 -1
- data/lib/elasticband/version.rb +1 -1
- data/spec/filter/range_spec.rb +53 -0
- data/spec/filter_spec.rb +14 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZGRjZjQxZjYzZWIxYmM2MTNjODZkOTE5OTM4OGJhZjUyYmZlY2Q4Yw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MjIzZjRkY2YyMjU2ZGFkNWFlNDg2MWZmOGMwNTcyYzRkMTBhNWVhYg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZGE5ZmU3YjkyYWM4NWY5MTBhNTVkMzE2NDI2ZWVmNWJlNDEyYmIyMjI1ZjMx
|
10
|
+
ZDFhZjQ2YTdkMGZjN2NkY2RmYzlhOTVmODcyYzJiNGUzYTc0MTE5MWI0OTVh
|
11
|
+
ZmFkZmZmYzk4NjBiNjBkMGU1ZWRhYmY4MzEyZmI3MWIwYjRjYjU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
Y2EyNWVjNDM1MDVlYzRiZGEwZjM2Y2EyNGUxNjliZDgyNDc2YzNhYWE1MThl
|
14
|
+
YTNlMGI5Y2JmNDVjNDA4OWVmODk5ZGUxZWM4ZDQ0ZWQzZjJlM2JiNDZmNTlm
|
15
|
+
N2NmMDFkOWFkMTgwMzEwZjE4ODBjOWQzNTc4NzZmZGY0ZjJjMTk=
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Elasticband
|
2
|
+
class Filter
|
3
|
+
class Range < Filter
|
4
|
+
attr_accessor :field, :ranges
|
5
|
+
|
6
|
+
RANGES = %i(gt gteq lt lteq)
|
7
|
+
|
8
|
+
def initialize(field, ranges)
|
9
|
+
self.field = field.to_sym
|
10
|
+
self.ranges = permitted_ranges(ranges)
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_h
|
14
|
+
{ range: { field => parsed_ranges } }
|
15
|
+
end
|
16
|
+
|
17
|
+
def permitted_ranges(ranges)
|
18
|
+
ranges.keep_if { |key, _| RANGES.include?(key) }
|
19
|
+
end
|
20
|
+
|
21
|
+
def parsed_ranges
|
22
|
+
translated_range = ranges.dup
|
23
|
+
|
24
|
+
translated_range[:gte] = translated_range.delete(:gteq) if translated_range.key?(:gteq)
|
25
|
+
translated_range[:lte] = translated_range.delete(:lteq) if translated_range.key?(:lteq)
|
26
|
+
|
27
|
+
translated_range
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/elasticband/filter.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
require 'elasticband/filter/and'
|
2
2
|
require 'elasticband/filter/not'
|
3
3
|
require 'elasticband/filter/query'
|
4
|
+
require 'elasticband/filter/range'
|
4
5
|
require 'elasticband/filter/term'
|
5
6
|
require 'elasticband/filter/terms'
|
6
7
|
|
7
8
|
module Elasticband
|
8
9
|
class Filter
|
9
|
-
PARSE_FILTERS = %i(only except includes)
|
10
|
+
PARSE_FILTERS = %i(only except includes range)
|
10
11
|
|
11
12
|
def to_h
|
12
13
|
{ match_all: {} }
|
@@ -20,6 +21,7 @@ module Elasticband
|
|
20
21
|
# * `only:` Filter the search results where the condition is `true`
|
21
22
|
# * `except`: Filter the search results where the condition is `false`.
|
22
23
|
# * `includes:` Filter the search results with a `Match` query.
|
24
|
+
# * `range:` Filter the search results where the condition is on the given range.
|
23
25
|
#
|
24
26
|
# #### Examples
|
25
27
|
# ```
|
@@ -31,6 +33,9 @@ module Elasticband
|
|
31
33
|
#
|
32
34
|
# Filter.parse(includes: ['company', on: :description])
|
33
35
|
# => { query: { match: { description: 'company' } } }
|
36
|
+
#
|
37
|
+
# Filter.parse(range: { companies_count: { gt: 1, gteq: 1, lt: 1, lteq: 1 } })
|
38
|
+
# => { range: { companies_count: { gt: 1, gte: 1, lt: 1, lte: 1 } } }
|
34
39
|
# ```
|
35
40
|
def parse(options = {})
|
36
41
|
return {} if options.blank?
|
@@ -38,6 +43,7 @@ module Elasticband
|
|
38
43
|
filter = parse_filters(options[:only])
|
39
44
|
filter += parse_filters(options[:except]).map { |f| Filter::Not.new(f) }
|
40
45
|
filter += parse_includes_filter(options[:includes])
|
46
|
+
filter += parse_range_filter(options[:range])
|
41
47
|
join_filters(filter).to_h
|
42
48
|
end
|
43
49
|
|
@@ -53,6 +59,12 @@ module Elasticband
|
|
53
59
|
[Filter::Query.new(Elasticband::Query.parse(*includes_options))]
|
54
60
|
end
|
55
61
|
|
62
|
+
def parse_range_filter(range_options)
|
63
|
+
return [] if range_options.blank?
|
64
|
+
|
65
|
+
[Filter::Range.new(range_options.keys.first, range_options.values.first)]
|
66
|
+
end
|
67
|
+
|
56
68
|
def parse_filters(options)
|
57
69
|
return [] if options.blank?
|
58
70
|
|
data/lib/elasticband/version.rb
CHANGED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Elasticband::Filter::Range do
|
4
|
+
describe '#to_h' do
|
5
|
+
subject { described_class.new(:field_name, options).to_h }
|
6
|
+
|
7
|
+
context 'with `:gt` option' do
|
8
|
+
let(:options) { { gt: 1 } }
|
9
|
+
|
10
|
+
it { is_expected.to eq(range: { field_name: { gt: 1 } }) }
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'with `:gteq` option' do
|
14
|
+
let(:options) { { gteq: 1 } }
|
15
|
+
|
16
|
+
it { is_expected.to eq(range: { field_name: { gte: 1 } }) }
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'with `:lt` option' do
|
20
|
+
let(:options) { { lt: 1 } }
|
21
|
+
|
22
|
+
it { is_expected.to eq(range: { field_name: { lt: 1 } }) }
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'with `:lteq` option' do
|
26
|
+
let(:options) { { lteq: 1 } }
|
27
|
+
|
28
|
+
it { is_expected.to eq(range: { field_name: { lte: 1 } }) }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#parsed_ranges' do
|
33
|
+
subject { described_class.new(:field_name, options).parsed_ranges }
|
34
|
+
|
35
|
+
context 'with `:gteq` option' do
|
36
|
+
let(:options) { { gteq: 1 } }
|
37
|
+
|
38
|
+
it { is_expected.to eq(gte: 1) }
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'with `:lteq` option' do
|
42
|
+
let(:options) { { lteq: 1 } }
|
43
|
+
|
44
|
+
it { is_expected.to eq(lte: 1) }
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'with an unpermitted option' do
|
48
|
+
let(:options) { { unpermitted: 1 } }
|
49
|
+
|
50
|
+
it { is_expected.to eq({}) }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/spec/filter_spec.rb
CHANGED
@@ -83,5 +83,19 @@ RSpec.describe Elasticband::Filter do
|
|
83
83
|
|
84
84
|
it { is_expected.to eq(query: { name: 'query' }) }
|
85
85
|
end
|
86
|
+
|
87
|
+
context 'with `:range` option' do
|
88
|
+
context 'with one range' do
|
89
|
+
let(:options) { { range: { foo: { gt: 1 } } } }
|
90
|
+
|
91
|
+
it { is_expected.to eq(range: { foo: { gt: 1 } }) }
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'with more than one range' do
|
95
|
+
let(:options) { { range: { foo: { gt: 1, lt: 2 } } } }
|
96
|
+
|
97
|
+
it { is_expected.to eq(range: { foo: { gt: 1, lt: 2 } }) }
|
98
|
+
end
|
99
|
+
end
|
86
100
|
end
|
87
101
|
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.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Glauber Campinho
|
@@ -125,6 +125,7 @@ files:
|
|
125
125
|
- lib/elasticband/filter/and.rb
|
126
126
|
- lib/elasticband/filter/not.rb
|
127
127
|
- lib/elasticband/filter/query.rb
|
128
|
+
- lib/elasticband/filter/range.rb
|
128
129
|
- lib/elasticband/filter/term.rb
|
129
130
|
- lib/elasticband/filter/terms.rb
|
130
131
|
- lib/elasticband/query.rb
|
@@ -150,6 +151,7 @@ files:
|
|
150
151
|
- spec/filter/and_spec.rb
|
151
152
|
- spec/filter/not_spec.rb
|
152
153
|
- spec/filter/query_spec.rb
|
154
|
+
- spec/filter/range_spec.rb
|
153
155
|
- spec/filter/term_spec.rb
|
154
156
|
- spec/filter/terms_spec.rb
|
155
157
|
- spec/filter_spec.rb
|