elasticband 0.1.4 → 0.1.5

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
- ZDIyZmVkODQxNTU1MzJkODExOTU3YjQ1MGNhNGVhNDc2YmU4MWZmNw==
4
+ MWFmN2E3YzkzYjMxY2Y0NjFlMGFiODRhZjM4MmFlMzE2NGE5ODNjZA==
5
5
  data.tar.gz: !binary |-
6
- NmRkZWU0ZWQwOGQxYWZlNzEwZmZlZjU1YjRhZWZiYTVlNWJjYTllNA==
6
+ NzcyMzlmZTg2YmU3ZmZjNTAxNDMxMTQ3OTFhZmY1ZDA5NWY4ODQwYQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZDE5ZjIzNTFkMTM0MWNiOTE3NzJjYmVmYzJiNTQ5OWU4YWEwZjI2YTZjMzM3
10
- ODVmNzdmODU2YmVhYzk3N2ZjMzNhNTY5MTAxNTg4ODg4Mjg0NDNmYmI3NzJl
11
- MDc3ZGMwMGMxMDZmYWQ4NzIzMjYwMTk3Y2JmZjg4MWFhY2E0Zjg=
9
+ NGRmZDJlYzQ2NzliMzM0MWE0ZDY0MTgwY2NkYjEwYjAxNzk4YjVhNzA1ZTA3
10
+ YWMxYzAwZDJlZDRkMTgyOWQyOGQwNjdmNWQ2MWI4N2QyODgwZDI4MDhkYjI1
11
+ YWUyYTcyMmY1NjAzMTA3ZWI1NTU3ZjIxZGJjMGFlZDkzZTE2Yjk=
12
12
  data.tar.gz: !binary |-
13
- N2Q4NjNmOTJhODc4MTZiYTc2ZTMzZWJhYjBiODllNjg1OGQ4MmVlMDVjZTky
14
- NmNmMzMyM2JhOWUxMWFjNWFiMGQ3YjI4ZWU1N2IxMzY5NWUxODA1ZmM3MDg4
15
- ZWQzNTBiNDZiMjRiOWZhMTAzOThjNDIzMTU4NDAxMWI0NTkxYzE=
13
+ OGQyOWY1OGIxOWU2ZWQ3NzgxNzdlOTYxYTM3ODRkYTE3ZmUzMGVjMDU0YTQx
14
+ ZjZhNGI2MThkMDFlODVhYTU0MWUwZjFiYzA1YWVjZWM3NTliMDVhZTE3NGNm
15
+ ZWJkZmNkZGI1ZDYwOThiMTQwNjc0ZjgyMzZjNTQyZDdiNTBkMTI=
data/.gitignore CHANGED
@@ -4,7 +4,6 @@
4
4
 
5
5
  /.bundle/
6
6
  /.yardoc
7
- /Gemfile.lock
8
7
  /_yardoc/
9
8
  /coverage/
10
9
  /doc/
data/Gemfile.lock ADDED
@@ -0,0 +1,73 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ elasticband (0.1.5)
5
+ activesupport
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (4.2.4)
11
+ i18n (~> 0.7)
12
+ json (~> 1.7, >= 1.7.7)
13
+ minitest (~> 5.1)
14
+ thread_safe (~> 0.3, >= 0.3.4)
15
+ tzinfo (~> 1.1)
16
+ ast (2.0.0)
17
+ astrolabe (1.3.0)
18
+ parser (>= 2.2.0.pre.3, < 3.0)
19
+ codeclimate-test-reporter (0.4.7)
20
+ simplecov (>= 0.7.1, < 1.0.0)
21
+ diff-lcs (1.2.5)
22
+ docile (1.1.5)
23
+ dotenv (2.0.2)
24
+ i18n (0.7.0)
25
+ json (1.8.3)
26
+ minitest (5.8.0)
27
+ parser (2.2.2.6)
28
+ ast (>= 1.1, < 3.0)
29
+ powerpack (0.1.1)
30
+ rainbow (2.0.0)
31
+ rake (10.4.2)
32
+ rspec (3.3.0)
33
+ rspec-core (~> 3.3.0)
34
+ rspec-expectations (~> 3.3.0)
35
+ rspec-mocks (~> 3.3.0)
36
+ rspec-core (3.3.1)
37
+ rspec-support (~> 3.3.0)
38
+ rspec-expectations (3.3.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.3.0)
41
+ rspec-mocks (3.3.1)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.3.0)
44
+ rspec-support (3.3.0)
45
+ rubocop (0.32.1)
46
+ astrolabe (~> 1.3)
47
+ parser (>= 2.2.2.5, < 3.0)
48
+ powerpack (~> 0.1)
49
+ rainbow (>= 1.99.1, < 3.0)
50
+ ruby-progressbar (~> 1.4)
51
+ ruby-progressbar (1.7.5)
52
+ simplecov (0.10.0)
53
+ docile (~> 1.1.0)
54
+ json (~> 1.8)
55
+ simplecov-html (~> 0.10.0)
56
+ simplecov-html (0.10.0)
57
+ thread_safe (0.3.5)
58
+ tzinfo (1.2.2)
59
+ thread_safe (~> 0.1)
60
+
61
+ PLATFORMS
62
+ ruby
63
+
64
+ DEPENDENCIES
65
+ codeclimate-test-reporter
66
+ dotenv
67
+ elasticband!
68
+ rake
69
+ rspec (~> 3.3.0)
70
+ rubocop
71
+
72
+ BUNDLED WITH
73
+ 1.10.6
data/lib/elasticband.rb CHANGED
@@ -7,6 +7,7 @@ require 'elasticband/aggregation'
7
7
  require 'elasticband/filter'
8
8
  require 'elasticband/query'
9
9
  require 'elasticband/search'
10
+ require 'elasticband/sort'
10
11
  require 'elasticband/version'
11
12
 
12
13
  module Elasticband
@@ -93,7 +93,7 @@ module Elasticband
93
93
 
94
94
  def parse_boost_function(boost_options)
95
95
  if boost_options[:boost_by].present?
96
- ScoreFunction::FieldValueFactor.new(boost_options[:boost_by], modifier: :ln2p)
96
+ ScoreFunction::FieldValueFactor.new(boost_options[:boost_by], modifier: :ln2p, missing: 1)
97
97
  elsif boost_options[:boost_where].present?
98
98
  filter = Filter.parse(only: boost_options[:boost_where])
99
99
  ScoreFunction::Filtered.new(filter, ScoreFunction::BoostFactor.new(1_000))
@@ -7,11 +7,18 @@ module Elasticband
7
7
  # #### Examples
8
8
  # ```
9
9
  # Search.parse('foo', on: :name, group_by: :status)
10
- # => { query: { match: { name: 'foo' } }, aggs: { status: { terms: { field: :status } } } }
10
+ # => {
11
+ # query: { match: { name: 'foo' } },
12
+ # aggs: { status: { terms: { field: :status } } },
13
+ # sort: [{name: 'desc'}, '+created_at']
14
+ # }
11
15
  # ```
12
16
  def parse(query_text, options)
13
- hash = { query: Query.parse(query_text, options), aggs: Aggregation.parse(options) }
14
- hash.reject { |_, value| value.blank? }
17
+ {
18
+ sort: Sort.parse(options),
19
+ query: Query.parse(query_text, options),
20
+ aggs: Aggregation.parse(options)
21
+ }.reject { |_, value| value.blank? }
15
22
  end
16
23
  end
17
24
  end
@@ -0,0 +1,40 @@
1
+ module Elasticband
2
+ class Sort
3
+ class << self
4
+ # Parses sort option to a Elasticsearch syntax.
5
+ #
6
+ # #### Options
7
+ #
8
+ # * `sort:` List of sort params.
9
+ #
10
+ # #### Examples
11
+ # ```
12
+ # Sort.parse(sort: [{ name: :desc }])
13
+ # => { sort: [{ name: :desc }] }
14
+ #
15
+ # Sort.parse(sort: [{ name: :asc }])
16
+ # => { sort: [{ name: :asc }] }
17
+ #
18
+ # Sort.parse(sort: ['+name'])
19
+ # => { sort: [{ name: :asc }] }
20
+ #
21
+ # Sort.parse(sort: ['-name'])
22
+ # => { sort: [{ name: :desc }] }
23
+ # ```
24
+ def parse(options)
25
+ options[:sort].map { |param| parse_param(param) }
26
+ end
27
+
28
+ private
29
+
30
+ def parse_param(param)
31
+ return param unless param.is_a?(String)
32
+
33
+ direction = param.start_with?('-') ? :desc : :asc
34
+ field = param.start_with?('-') || param.start_with?('+') ? param[1..-1] : param
35
+
36
+ { field.to_sym => direction }
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module Elasticband
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
data/spec/query_spec.rb CHANGED
@@ -62,7 +62,8 @@ RSpec.describe Elasticband::Query do
62
62
  query: { match: { _all: 'foo' } },
63
63
  field_value_factor: {
64
64
  field: :contents_count,
65
- modifier: :ln2p
65
+ modifier: :ln2p,
66
+ missing: 1
66
67
  }
67
68
  }
68
69
  )
data/spec/search_spec.rb CHANGED
@@ -2,16 +2,19 @@ require 'spec_helper'
2
2
 
3
3
  RSpec.describe Elasticband::Search do
4
4
  describe '.parse' do
5
- subject { described_class.parse('foo', on: :name, group_by: :status) }
5
+ subject { described_class.parse('foo', options) }
6
+ let(:options) { { on: :name, group_by: :status, sort: [{ name: :desc }] } }
6
7
 
7
8
  before do
8
- allow(Elasticband::Query).to receive(:parse).with('foo', on: :name, group_by: :status) { query }
9
- allow(Elasticband::Aggregation).to receive(:parse).with(on: :name, group_by: :status) { aggregation }
9
+ allow(Elasticband::Sort).to receive(:parse).with(options) { sort }
10
+ allow(Elasticband::Query).to receive(:parse).with('foo', options) { query }
11
+ allow(Elasticband::Aggregation).to receive(:parse).with(options) { aggregation }
10
12
  end
11
13
 
12
14
  context 'with only query' do
13
15
  let(:query) { 'query' }
14
16
  let(:aggregation) { {} }
17
+ let(:sort) { [] }
15
18
 
16
19
  it { is_expected.to eq(query: 'query') }
17
20
  end
@@ -19,15 +22,25 @@ RSpec.describe Elasticband::Search do
19
22
  context 'with only aggregation' do
20
23
  let(:query) { {} }
21
24
  let(:aggregation) { 'aggregation' }
25
+ let(:sort) { [] }
22
26
 
23
27
  it { is_expected.to eq(aggs: 'aggregation') }
24
28
  end
25
29
 
26
- context 'with query and aggregation' do
30
+ context 'with only sort' do
31
+ let(:query) { {} }
32
+ let(:aggregation) { {} }
33
+ let(:sort) { [{ name: :desc }] }
34
+
35
+ it { is_expected.to eq(sort: [{ name: :desc }]) }
36
+ end
37
+
38
+ context 'with query and aggregation and sort' do
27
39
  let(:query) { 'query' }
28
40
  let(:aggregation) { 'aggregation' }
41
+ let(:sort) { [{ name: :desc }] }
29
42
 
30
- it { is_expected.to eq(query: 'query', aggs: 'aggregation') }
43
+ it { is_expected.to eq(query: 'query', aggs: 'aggregation', sort: [{ name: :desc }]) }
31
44
  end
32
45
  end
33
46
  end
data/spec/sort_spec.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Elasticband::Sort do
4
+ describe '.parse' do
5
+ subject { described_class.parse(sort: sort_params) }
6
+
7
+ context 'with name desc' do
8
+ let(:sort_params) { [{ name: :desc }] }
9
+
10
+ it { is_expected.to eq([{ name: :desc }]) }
11
+ end
12
+
13
+ context 'with name asc short way' do
14
+ let(:sort_params) { ['+name'] }
15
+
16
+ it { is_expected.to eq([{ name: :asc }]) }
17
+ end
18
+
19
+ context 'with name desc short way' do
20
+ let(:sort_params) { ['-name'] }
21
+
22
+ it { is_expected.to eq([{ name: :desc }]) }
23
+ end
24
+
25
+ context 'with name as string' do
26
+ let(:sort_params) { ['name'] }
27
+
28
+ it { is_expected.to eq([{ name: :asc }]) }
29
+ end
30
+ end
31
+ 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
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Glauber Campinho
@@ -109,6 +109,7 @@ files:
109
109
  - .travis.yml
110
110
  - CODE_OF_CONDUCT.md
111
111
  - Gemfile
112
+ - Gemfile.lock
112
113
  - LICENSE
113
114
  - README.md
114
115
  - Rakefile
@@ -138,6 +139,7 @@ files:
138
139
  - lib/elasticband/query/score_function/filtered.rb
139
140
  - lib/elasticband/query/score_function/script_score.rb
140
141
  - lib/elasticband/search.rb
142
+ - lib/elasticband/sort.rb
141
143
  - lib/elasticband/version.rb
142
144
  - spec/aggregation/field_based_spec.rb
143
145
  - spec/aggregation/filter_spec.rb
@@ -163,6 +165,7 @@ files:
163
165
  - spec/query/score_function_spec.rb
164
166
  - spec/query_spec.rb
165
167
  - spec/search_spec.rb
168
+ - spec/sort_spec.rb
166
169
  - spec/spec_helper.rb
167
170
  homepage: https://github.com/LoveMondays/elasticband
168
171
  licenses: