elasticband 0.1.10 → 0.2.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 +8 -8
- data/lib/elasticband/query.rb +27 -4
- data/lib/elasticband/query/function_score.rb +7 -7
- data/lib/elasticband/query/score_function.rb +2 -0
- data/lib/elasticband/query/score_function/gauss.rb +19 -0
- data/lib/elasticband/query/score_function/geo_location.rb +18 -0
- data/lib/elasticband/version.rb +1 -1
- data/spec/query/score_function/gauss_spec.rb +11 -0
- data/spec/query/score_function/geo_location_spec.rb +33 -0
- data/spec/query_spec.rb +27 -0
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YjQyMGZiM2RiNGJhODg2M2Y2NzViZjNlOGQ4NmNlNWU3YzE2NGEyNA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
M2JmMzRkMmZiMDM0NWI5ZWFmNDgwOTVlOTNjN2VmY2IzNTQ5MmVkNg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZTQwYTA5ZDA0ZmJiMmEwYzkwNGVhZmQyYmRkZmUwY2MxODJjYzU5ZjBkOTFl
|
10
|
+
NWMxMmJjZDI1NjQ5ZDUyZWMzNDU1NjUzY2VkYjgyNTBmNzk2ZGFlNWI0MGJh
|
11
|
+
N2YwY2MwN2JmNDRkZDliYWZkZjY1NjRjNjM1NmUyNjU1ZTc5OGU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OGUwMmE0Y2RiMDJiNzdmY2UxMWYzZWYyNTMwNzA4NjkyMzI3YjQzZTQxNmMy
|
14
|
+
ZGNkYWRmN2Q4NTZlMTMzM2ZhNmJjMGE0OTZlNDY4NTgzNmQ1YmViNTBiODU3
|
15
|
+
MTU1YTFhMDBkMGRjNzA3MzRhMGE5YTgxZGMxMDBjNGYwMTA4N2I=
|
data/lib/elasticband/query.rb
CHANGED
@@ -24,6 +24,7 @@ module Elasticband
|
|
24
24
|
# * `boost_function:` Boosts using the function passed.
|
25
25
|
# * `boost_mode:` Defines how the function_score will be used.
|
26
26
|
# * `score_mode:` Defines how the query score will be used.
|
27
|
+
# * `geo_location:` Defines query by geo location.
|
27
28
|
#
|
28
29
|
# #### Examples
|
29
30
|
# ```
|
@@ -66,12 +67,27 @@ module Elasticband
|
|
66
67
|
# ]
|
67
68
|
# }
|
68
69
|
# }
|
70
|
+
#
|
71
|
+
# Query.parse(
|
72
|
+
# 'foo',
|
73
|
+
# geo_location: {
|
74
|
+
# on: :location,
|
75
|
+
# latitude: 12,
|
76
|
+
# longitude: 34,
|
77
|
+
# distance: { same_score: '5km', half_score: '10km' }
|
78
|
+
# }
|
79
|
+
# )
|
80
|
+
# => {
|
81
|
+
# function_score: {
|
82
|
+
# query: ...,
|
83
|
+
# gauss: { location: { origin: { lat: 12, lon: 34 }, offset: '5km', scale: '10km' } } } }
|
69
84
|
# ```
|
70
85
|
def parse(query_text, options = {})
|
71
86
|
query = parse_on(query_text, options[:on])
|
72
87
|
query = parse_query_filters(query, options)
|
73
88
|
query = parse_boost(
|
74
89
|
query,
|
90
|
+
options[:geo_location],
|
75
91
|
options.slice(:boost_by, :boost_where, :boost_function),
|
76
92
|
options[:score_mode],
|
77
93
|
options[:boost_mode]
|
@@ -97,14 +113,21 @@ module Elasticband
|
|
97
113
|
filter.blank? ? query : Query::Filtered.new(filter, query)
|
98
114
|
end
|
99
115
|
|
100
|
-
def parse_boost(query, boost_options, score_mode_option, boost_mode_option)
|
101
|
-
return query if boost_options.blank?
|
116
|
+
def parse_boost(query, geo_location_options, boost_options, score_mode_option, boost_mode_option)
|
117
|
+
return query if boost_options.blank? && geo_location_options.blank?
|
102
118
|
|
103
|
-
|
119
|
+
functions = score_functions(geo_location_options, boost_options)
|
104
120
|
score_mode = Query::ScoreFunction::ScoreMode.new(score_mode_option)
|
105
121
|
boost_mode = Query::ScoreFunction::BoostMode.new(boost_mode_option)
|
106
122
|
|
107
|
-
Query::FunctionScore.new(query,
|
123
|
+
Query::FunctionScore.new(query, functions, score_mode, boost_mode)
|
124
|
+
end
|
125
|
+
|
126
|
+
def score_functions(geo_location_options, boost_options)
|
127
|
+
functions = []
|
128
|
+
functions << Query::ScoreFunction::GeoLocation.new(geo_location_options) if geo_location_options
|
129
|
+
functions << parse_boost_function(boost_options) if boost_options.present?
|
130
|
+
functions
|
108
131
|
end
|
109
132
|
|
110
133
|
def parse_boost_function(boost_options)
|
@@ -30,16 +30,16 @@ module Elasticband
|
|
30
30
|
|
31
31
|
def function_hash
|
32
32
|
if function.is_a?(Enumerable)
|
33
|
-
{ functions: function.map(&:to_h) }
|
33
|
+
function.size > 1 ? { functions: function.map(&:to_h) } : single_function_hash(function.first)
|
34
34
|
else
|
35
|
-
|
36
|
-
if function_hash.key?(:filter)
|
37
|
-
{ functions: [function_hash] }
|
38
|
-
else
|
39
|
-
function_hash
|
40
|
-
end
|
35
|
+
single_function_hash(function)
|
41
36
|
end
|
42
37
|
end
|
38
|
+
|
39
|
+
def single_function_hash(function)
|
40
|
+
function_hash = function.to_h
|
41
|
+
function_hash.key?(:filter) ? { functions: [function_hash] } : function_hash
|
42
|
+
end
|
43
43
|
end
|
44
44
|
end
|
45
45
|
end
|
@@ -4,6 +4,8 @@ require 'elasticband/query/score_function/filtered'
|
|
4
4
|
require 'elasticband/query/score_function/script_score'
|
5
5
|
require 'elasticband/query/score_function/boost_mode'
|
6
6
|
require 'elasticband/query/score_function/score_mode'
|
7
|
+
require 'elasticband/query/score_function/gauss'
|
8
|
+
require 'elasticband/query/score_function/geo_location'
|
7
9
|
|
8
10
|
module Elasticband
|
9
11
|
class Query
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Elasticband
|
2
|
+
class Query
|
3
|
+
class ScoreFunction
|
4
|
+
class Gauss < ScoreFunction
|
5
|
+
attr_accessor :options
|
6
|
+
|
7
|
+
def initialize(options)
|
8
|
+
self.options = options
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_h
|
12
|
+
return {} unless options
|
13
|
+
|
14
|
+
{ gauss: options }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Elasticband
|
2
|
+
class Query
|
3
|
+
class ScoreFunction
|
4
|
+
class GeoLocation < Gauss
|
5
|
+
def initialize(options)
|
6
|
+
return unless options.present?
|
7
|
+
|
8
|
+
origin = { lat: options[:latitude], lon: options[:longitude] }
|
9
|
+
distance = options[:distance]
|
10
|
+
location = { origin: origin, offset: distance[:same_score], scale: distance[:half_score] }
|
11
|
+
field = options[:on] || :location
|
12
|
+
|
13
|
+
super(field => location)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/elasticband/version.rb
CHANGED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Elasticband::Query::ScoreFunction::Gauss do
|
4
|
+
describe '#to_h' do
|
5
|
+
subject { described_class.new(options).to_h }
|
6
|
+
|
7
|
+
let(:options) { { origin: { lat: 5, lon: 2 } } }
|
8
|
+
|
9
|
+
it { is_expected.to eq(gauss: { origin: { lat: 5, lon: 2 } }) }
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Elasticband::Query::ScoreFunction::GeoLocation do
|
4
|
+
describe '#to_h' do
|
5
|
+
subject { described_class.new(options).to_h }
|
6
|
+
|
7
|
+
let(:options) { { latitude: 5.1, longitude: -2.3, distance: { same_score: '10km', half_score: '15km' } } }
|
8
|
+
let(:origin) { { lat: 5.1, lon: -2.3 } }
|
9
|
+
|
10
|
+
it { is_expected.to eq(gauss: { location: { origin: origin, offset: '10km', scale: '15km' } }) }
|
11
|
+
|
12
|
+
context 'with `on` option' do
|
13
|
+
let(:options) do
|
14
|
+
{ on: :loc, latitude: 5.1, longitude: -2.3, distance: { same_score: '10km', half_score: '15km' } }
|
15
|
+
end
|
16
|
+
let(:origin) { { lat: 5.1, lon: -2.3 } }
|
17
|
+
|
18
|
+
it { is_expected.to eq(gauss: { loc: { origin: origin, offset: '10km', scale: '15km' } }) }
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'with nil options' do
|
22
|
+
let(:options) { nil }
|
23
|
+
|
24
|
+
it { is_expected.to eq({}) }
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with empty options' do
|
28
|
+
let(:options) { {} }
|
29
|
+
|
30
|
+
it { is_expected.to eq({}) }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/spec/query_spec.rb
CHANGED
@@ -233,6 +233,33 @@ RSpec.describe Elasticband::Query do
|
|
233
233
|
end
|
234
234
|
end
|
235
235
|
end
|
236
|
+
|
237
|
+
context 'with `:geo_location` options' do
|
238
|
+
let(:options) do
|
239
|
+
{
|
240
|
+
geo_location: {
|
241
|
+
latitude: 12.5,
|
242
|
+
longitude: -34.9,
|
243
|
+
distance: { same_score: '5km', half_score: '10km' }
|
244
|
+
}
|
245
|
+
}
|
246
|
+
end
|
247
|
+
|
248
|
+
it 'returns a function score query with the score_mode' do
|
249
|
+
is_expected.to eq(
|
250
|
+
function_score: {
|
251
|
+
query: { match: { _all: 'foo' } },
|
252
|
+
gauss: {
|
253
|
+
location: {
|
254
|
+
origin: { lat: 12.5, lon: -34.9 },
|
255
|
+
offset: '5km',
|
256
|
+
scale: '10km'
|
257
|
+
}
|
258
|
+
}
|
259
|
+
}
|
260
|
+
)
|
261
|
+
end
|
262
|
+
end
|
236
263
|
end
|
237
264
|
end
|
238
265
|
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.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Glauber Campinho
|
@@ -138,6 +138,8 @@ files:
|
|
138
138
|
- lib/elasticband/query/score_function/boost_mode.rb
|
139
139
|
- lib/elasticband/query/score_function/field_value_factor.rb
|
140
140
|
- lib/elasticband/query/score_function/filtered.rb
|
141
|
+
- lib/elasticband/query/score_function/gauss.rb
|
142
|
+
- lib/elasticband/query/score_function/geo_location.rb
|
141
143
|
- lib/elasticband/query/score_function/score_mode.rb
|
142
144
|
- lib/elasticband/query/score_function/script_score.rb
|
143
145
|
- lib/elasticband/search.rb
|
@@ -165,6 +167,8 @@ files:
|
|
165
167
|
- spec/query/score_function/boost_mode_spec.rb
|
166
168
|
- spec/query/score_function/field_value_factor_spec.rb
|
167
169
|
- spec/query/score_function/filtered_spec.rb
|
170
|
+
- spec/query/score_function/gauss_spec.rb
|
171
|
+
- spec/query/score_function/geo_location_spec.rb
|
168
172
|
- spec/query/score_function/score_mode_spec.rb
|
169
173
|
- spec/query/score_function/script_score_spec.rb
|
170
174
|
- spec/query/score_function_spec.rb
|