elasticband 0.1.8 → 0.1.9

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
- ZGRjZjQxZjYzZWIxYmM2MTNjODZkOTE5OTM4OGJhZjUyYmZlY2Q4Yw==
4
+ MGNmMTIxZjU0NmI4MjA2MGQxMmU3Y2UwZWYyZDlkMmU4MDJlMmFiYg==
5
5
  data.tar.gz: !binary |-
6
- MjIzZjRkY2YyMjU2ZGFkNWFlNDg2MWZmOGMwNTcyYzRkMTBhNWVhYg==
6
+ ZGM2YmVjZTc4MmQzNjJhNmE4OTBiMzJkNWE3MjVmNzBjMjI5MTJiZg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZGE5ZmU3YjkyYWM4NWY5MTBhNTVkMzE2NDI2ZWVmNWJlNDEyYmIyMjI1ZjMx
10
- ZDFhZjQ2YTdkMGZjN2NkY2RmYzlhOTVmODcyYzJiNGUzYTc0MTE5MWI0OTVh
11
- ZmFkZmZmYzk4NjBiNjBkMGU1ZWRhYmY4MzEyZmI3MWIwYjRjYjU=
9
+ ZDc2Yzg4M2VkYzIyZDI5ZDMyOTQ3OTNmNWI2YTYyMTcxOTg1MDZjM2Y4YTBk
10
+ OWU3Yzk5MzFlNGVmOWNjMDkzNWIzNDkwMzllNjAwZDRhYzNmYTUwMDE4NWEw
11
+ OTNlMjkzYTI4NWM0NWVjYmUyYjM0NGM2YWJkNzgwNmEwOTYxMDE=
12
12
  data.tar.gz: !binary |-
13
- Y2EyNWVjNDM1MDVlYzRiZGEwZjM2Y2EyNGUxNjliZDgyNDc2YzNhYWE1MThl
14
- YTNlMGI5Y2JmNDVjNDA4OWVmODk5ZGUxZWM4ZDQ0ZWQzZjJlM2JiNDZmNTlm
15
- N2NmMDFkOWFkMTgwMzEwZjE4ODBjOWQzNTc4NzZmZGY0ZjJjMTk=
13
+ MGM5OTc3Y2NkMmY5MWY3OGVhNTZlZTM3OTlhYjlhYjFiNDA0MmEyMmJlNzhh
14
+ ZDE1Njg5NzNmZmNjMWIzZTg1ZDMxMzM1NzFlZDFjYTg4OTdlYWQ5ZDgwZDc2
15
+ NDFkMTIxMzc0Yzc5ODE3YjcxMDJkMTNiNWIwNTk1YWQzNmMzMzQ=
@@ -1,12 +1,13 @@
1
1
  module Elasticband
2
2
  class Query
3
3
  class FunctionScore < Query
4
- attr_accessor :query_or_filter, :function, :options
4
+ attr_accessor :query_or_filter, :function, :score_mode, :boost_mode
5
5
 
6
- def initialize(query_or_filter, function, options = {})
6
+ def initialize(query_or_filter, function, score_mode, boost_mode)
7
7
  self.query_or_filter = query_or_filter
8
8
  self.function = function
9
- self.options = options
9
+ self.score_mode = score_mode
10
+ self.boost_mode = boost_mode
10
11
  end
11
12
 
12
13
  def to_h
@@ -16,7 +17,7 @@ module Elasticband
16
17
  private
17
18
 
18
19
  def function_score_hash
19
- query_or_filter_hash.merge!(function_hash).merge!(options)
20
+ query_or_filter_hash.merge!(function_hash).merge(score_mode.to_h).merge(boost_mode.to_h)
20
21
  end
21
22
 
22
23
  def query_or_filter_hash
@@ -0,0 +1,21 @@
1
+ module Elasticband
2
+ class Query
3
+ class ScoreFunction
4
+ class BoostMode < ScoreFunction
5
+ attr_accessor :mode
6
+
7
+ MODES = %i(multiply replace sum avg max min)
8
+
9
+ def initialize(mode = nil)
10
+ self.mode = mode
11
+ end
12
+
13
+ def to_h
14
+ return {} unless mode && MODES.include?(mode)
15
+
16
+ { boost_mode: mode }
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Elasticband
2
+ class Query
3
+ class ScoreFunction
4
+ class ScoreMode < ScoreFunction
5
+ attr_accessor :mode
6
+
7
+ MODES = %i(multiply mult sum avg first max min)
8
+
9
+ def initialize(mode = nil)
10
+ self.mode = mode
11
+ end
12
+
13
+ def to_h
14
+ return {} unless mode && MODES.include?(mode)
15
+
16
+ { score_mode: mode }
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -2,6 +2,8 @@ require 'elasticband/query/score_function/boost_factor'
2
2
  require 'elasticband/query/score_function/field_value_factor'
3
3
  require 'elasticband/query/score_function/filtered'
4
4
  require 'elasticband/query/score_function/script_score'
5
+ require 'elasticband/query/score_function/boost_mode'
6
+ require 'elasticband/query/score_function/score_mode'
5
7
 
6
8
  module Elasticband
7
9
  class Query
@@ -22,6 +22,8 @@ module Elasticband
22
22
  # * `boost_where:` Boosts the score of a query result where some condition is `true`.
23
23
  # This score will be multiplied by 1000 (arbitrary, based on gem `searchkick`)
24
24
  # * `boost_function:` Boosts using the function passed.
25
+ # * `boost_mode:` Defines how the function_score will be used.
26
+ # * `score_mode:` Defines how the query score will be used.
25
27
  #
26
28
  # #### Examples
27
29
  # ```
@@ -49,6 +51,12 @@ module Elasticband
49
51
  # Query.parse('foo', boost_function: "_score * doc['users_count'].value")
50
52
  # => { function_score: { query: ..., script_score: { script: '_score * doc['users_count'].value' } } }
51
53
  #
54
+ # Query.parse('foo', boost_function: ..., boost_mode: :multiply)
55
+ # => { function_score: { query: ..., boost_mode: :multiply, script_score: { script: ... } } }
56
+ #
57
+ # Query.parse('foo', boost_function: ..., score_mode: :multiply)
58
+ # => { function_score: { query: ..., score_mode: :multiply, script_score: { script: ... } } }
59
+ #
52
60
  # Query.parse('foo', boost_where: { company: { id: 1 } })
53
61
  # => {
54
62
  # function_score: {
@@ -62,7 +70,12 @@ module Elasticband
62
70
  def parse(query_text, options = {})
63
71
  query = parse_on(query_text, options[:on])
64
72
  query = parse_query_filters(query, options)
65
- query = parse_boost(query, options.slice(:boost_by, :boost_where, :boost_function))
73
+ query = parse_boost(
74
+ query,
75
+ options.slice(:boost_by, :boost_where, :boost_function),
76
+ options.slice(:score_mode),
77
+ options.slice(:boost_mode)
78
+ )
66
79
  query.to_h
67
80
  end
68
81
 
@@ -84,11 +97,14 @@ module Elasticband
84
97
  filter.blank? ? query : Query::Filtered.new(filter, query)
85
98
  end
86
99
 
87
- def parse_boost(query, boost_options)
100
+ def parse_boost(query, boost_options, score_mode_option, boost_mode_option)
88
101
  return query if boost_options.blank?
89
102
 
90
103
  function = parse_boost_function(boost_options)
91
- Query::FunctionScore.new(query, function)
104
+ score_mode = Query::ScoreFunction::ScoreMode.new(score_mode_option)
105
+ boost_mode = Query::ScoreFunction::BoostMode.new(boost_mode_option)
106
+
107
+ Query::FunctionScore.new(query, function, score_mode, boost_mode)
92
108
  end
93
109
 
94
110
  def parse_boost_function(boost_options)
@@ -1,3 +1,3 @@
1
1
  module Elasticband
2
- VERSION = '0.1.8'
2
+ VERSION = '0.1.9'
3
3
  end
@@ -6,6 +6,8 @@ RSpec.describe Elasticband::Query::FunctionScore do
6
6
  let(:filter) { Elasticband::Filter.new }
7
7
  let(:score_function_1) { Elasticband::Query::ScoreFunction.new }
8
8
  let(:score_function_2) { Elasticband::Query::ScoreFunction.new }
9
+ let(:boost_mode) { Elasticband::Query::ScoreFunction::BoostMode.new }
10
+ let(:score_mode) { Elasticband::Query::ScoreFunction::ScoreMode.new }
9
11
 
10
12
  before do
11
13
  allow(other_query).to receive(:to_h) { 'query' }
@@ -16,20 +18,22 @@ RSpec.describe Elasticband::Query::FunctionScore do
16
18
 
17
19
  context 'with a single function' do
18
20
  context 'with a query' do
19
- subject { described_class.new(other_query, score_function_1).to_h }
21
+ subject { described_class.new(other_query, score_function_1, boost_mode, score_mode).to_h }
20
22
 
21
23
  it { is_expected.to eq(function_score: { query: 'query', score_function_1: 'score_function_1' }) }
22
24
  end
23
25
 
24
26
  context 'with filter' do
25
- subject { described_class.new(filter, score_function_1).to_h }
27
+ subject { described_class.new(filter, score_function_1, boost_mode, score_mode).to_h }
26
28
 
27
29
  it { is_expected.to eq(function_score: { filter: 'filter', score_function_1: 'score_function_1' }) }
28
30
  end
29
31
  end
30
32
 
31
33
  context 'with multiple functions' do
32
- subject { described_class.new(other_query, [score_function_1, score_function_2]).to_h }
34
+ subject do
35
+ described_class.new(other_query, [score_function_1, score_function_2], boost_mode, score_mode).to_h
36
+ end
33
37
 
34
38
  it 'returns a hash with the query/filter and an array with the functions' do
35
39
  is_expected.to eq(
@@ -44,30 +48,48 @@ RSpec.describe Elasticband::Query::FunctionScore do
44
48
  end
45
49
  end
46
50
 
47
- context 'with options' do
48
- subject { described_class.new(other_query, score_function_1, boost: 1.2).to_h }
51
+ context 'with a filtered function' do
52
+ let(:filtered_function) { Elasticband::Query::ScoreFunction::Filtered.new(filter, score_function_1) }
53
+
54
+ subject { described_class.new(other_query, filtered_function, boost_mode, score_mode).to_h }
49
55
 
50
- it 'returns a hash with the query, function and options' do
56
+ it 'returns a hash with the query/filter and an array with the function' do
57
+ is_expected.to eq(
58
+ function_score: {
59
+ query: 'query',
60
+ functions: [{ filter: 'filter', score_function_1: 'score_function_1' }]
61
+ }
62
+ )
63
+ end
64
+ end
65
+
66
+ context 'with a boost mode' do
67
+ let(:boost_mode) { Elasticband::Query::ScoreFunction::BoostMode.new(:multiply) }
68
+
69
+ subject { described_class.new(other_query, score_function_1, boost_mode, score_mode).to_h }
70
+
71
+ it 'returns a hash with the query/filter and the given boost_mode' do
51
72
  is_expected.to eq(
52
73
  function_score: {
53
74
  query: 'query',
54
75
  score_function_1: 'score_function_1',
55
- boost: 1.2
76
+ boost_mode: :multiply
56
77
  }
57
78
  )
58
79
  end
59
80
  end
60
81
 
61
- context 'with a filtered function' do
62
- let(:filtered_function) { Elasticband::Query::ScoreFunction::Filtered.new(filter, score_function_1) }
82
+ context 'with a score mode' do
83
+ let(:score_mode) { Elasticband::Query::ScoreFunction::ScoreMode.new(:multiply) }
63
84
 
64
- subject { described_class.new(other_query, filtered_function).to_h }
85
+ subject { described_class.new(other_query, score_function_1, boost_mode, score_mode).to_h }
65
86
 
66
- it 'returns a hash with the query/filter and an array with the function' do
87
+ it 'returns a hash with the query/filter and the given score_mode' do
67
88
  is_expected.to eq(
68
89
  function_score: {
69
90
  query: 'query',
70
- functions: [{ filter: 'filter', score_function_1: 'score_function_1' }]
91
+ score_function_1: 'score_function_1',
92
+ score_mode: :multiply
71
93
  }
72
94
  )
73
95
  end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Elasticband::Query::ScoreFunction::BoostMode do
4
+ describe '#to_h' do
5
+ subject { described_class.new(mode).to_h }
6
+
7
+ let(:mode) { :multiply }
8
+
9
+ it { is_expected.to eq(boost_mode: :multiply) }
10
+
11
+ context 'with an unpermitted mode' do
12
+ let(:mode) { :foo }
13
+
14
+ it { is_expected.to be_empty }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Elasticband::Query::ScoreFunction::ScoreMode do
4
+ describe '#to_h' do
5
+ subject { described_class.new(mode).to_h }
6
+
7
+ let(:mode) { :multiply }
8
+
9
+ it { is_expected.to eq(score_mode: :multiply) }
10
+
11
+ context 'with an unpermitted mode' do
12
+ let(:mode) { :foo }
13
+
14
+ it { is_expected.to be_empty }
15
+ end
16
+ end
17
+ 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.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Glauber Campinho
@@ -135,8 +135,10 @@ files:
135
135
  - lib/elasticband/query/multi_match.rb
136
136
  - lib/elasticband/query/score_function.rb
137
137
  - lib/elasticband/query/score_function/boost_factor.rb
138
+ - lib/elasticband/query/score_function/boost_mode.rb
138
139
  - lib/elasticband/query/score_function/field_value_factor.rb
139
140
  - lib/elasticband/query/score_function/filtered.rb
141
+ - lib/elasticband/query/score_function/score_mode.rb
140
142
  - lib/elasticband/query/score_function/script_score.rb
141
143
  - lib/elasticband/search.rb
142
144
  - lib/elasticband/sort.rb
@@ -160,8 +162,10 @@ files:
160
162
  - spec/query/match_spec.rb
161
163
  - spec/query/multi_match_spec.rb
162
164
  - spec/query/score_function/boost_factor_spec.rb
165
+ - spec/query/score_function/boost_mode_spec.rb
163
166
  - spec/query/score_function/field_value_factor_spec.rb
164
167
  - spec/query/score_function/filtered_spec.rb
168
+ - spec/query/score_function/score_mode_spec.rb
165
169
  - spec/query/score_function/script_score_spec.rb
166
170
  - spec/query/score_function_spec.rb
167
171
  - spec/query_spec.rb