stretchy 0.5.2 → 0.5.3

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1a8263c1321815880f552cd0526e9bdd5df59b46
4
- data.tar.gz: 276a949c2c902bf1f860acfebdc943531ee27620
3
+ metadata.gz: fc1f47df9e733b26cbf7a600076991ef6c2fd740
4
+ data.tar.gz: f62cabd7fc16980276d70de66c90dba0d25fb03e
5
5
  SHA512:
6
- metadata.gz: 686770d04b195057ecf51bca7c95e6c34f47b37c1ee5398dc5ceae20bf0a281d11f85a081372a10d6fda7e06f2d9003d16bcc0a05c6f6fa50c72df44dcc03ca9
7
- data.tar.gz: 540bb5b13632defe17885490ce58fbd85941cbb774d1a58913f14bb253309bfd7d574aa3dc00deabbbae2eca243baec60d2b95575f2a5db5767d8c1e4f40febf
6
+ metadata.gz: 0642e6ceab6cb04e30aef51cfedefbc1ae4b49f49b387528f71425d7a72bd9bf21a0763f6e1968e95c4568cc9d9148a66f5565cc42b0e5802adfbf08d6c7d6e7
7
+ data.tar.gz: 33e7884e2d19d7dbc269258b21b3cb448b8174e055c0f88338ef786caad83efc38886be1386ec3d094b96b4fed584ab7754bb3f2d7c239fd7d8a838f13be9b35
data/lib/stretchy/api.rb CHANGED
@@ -9,7 +9,7 @@ module Stretchy
9
9
  include Enumerable
10
10
  include Utils::Methods
11
11
 
12
- attr_reader :collector, :opts, :root, :context
12
+ attr_reader :collector, :opts, :root, :body, :context
13
13
 
14
14
  delegate [
15
15
  :total,
@@ -30,6 +30,7 @@ module Stretchy
30
30
  @opts = opts
31
31
  @collector = AndCollector.new(opts[:nodes] || [], query: true)
32
32
  @root = opts[:root] || {}
33
+ @body = opts[:body] || {}
33
34
  @context = opts[:context] || {}
34
35
  end
35
36
 
@@ -77,16 +78,24 @@ module Stretchy
77
78
  end
78
79
 
79
80
  def aggs(params = {})
80
- add_root aggs: params
81
+ add_body aggs: params
82
+ end
83
+
84
+ def highlight(params = {})
85
+ add_body highlight: params
81
86
  end
82
87
 
83
88
  def where(params = {})
84
- add_params params, :filter, :context_nodes
89
+ subcontext = {filter: true}
90
+ subcontext[:nested] = params.delete(:nested) if params[:nested]
91
+ add_params params, subcontext, :context_nodes
85
92
  end
86
93
 
87
94
  def match(params = {})
88
95
  if params.is_a? Hash
89
- add_params params, :query, :context_nodes
96
+ subcontext = {query: true}
97
+ subcontext[:nested] = true if params.delete(:nested)
98
+ add_params params, subcontext, :context_nodes
90
99
  else
91
100
  add_params Hash[_all: params], :query, :context_nodes
92
101
  end
@@ -159,12 +168,7 @@ module Stretchy
159
168
 
160
169
  def request
161
170
  @request ||= begin
162
- base = root.dup
163
- sub = {query: collector.as_json}
164
- agg = base.delete(:aggs) || {}
165
- sub[:aggs] = agg if agg.any?
166
-
167
- base.merge(body: sub)
171
+ root.merge(body: body.merge(query: collector.as_json))
168
172
  end
169
173
  end
170
174
 
@@ -217,6 +221,7 @@ module Stretchy
217
221
  self.class.new(opts.merge(
218
222
  nodes: collector.nodes + Array(additional),
219
223
  root: root,
224
+ body: body,
220
225
  context: {}
221
226
  ))
222
227
  end
@@ -225,6 +230,16 @@ module Stretchy
225
230
  self.class.new(opts.merge(
226
231
  nodes: collector.nodes,
227
232
  root: root.merge(options),
233
+ body: body,
234
+ context: context
235
+ ))
236
+ end
237
+
238
+ def add_body(options = {})
239
+ self.class.new(opts.merge(
240
+ nodes: collector.nodes,
241
+ root: root,
242
+ body: body.merge(options),
228
243
  context: context
229
244
  ))
230
245
  end
@@ -233,6 +248,7 @@ module Stretchy
233
248
  self.class.new(opts.merge(
234
249
  nodes: collector.nodes,
235
250
  root: root,
251
+ body: body,
236
252
  context: context.merge(args_to_context(*args))
237
253
  ))
238
254
  end
@@ -1,4 +1,6 @@
1
1
  module Stretchy
2
- class InvalidParamsError < StandardError; end
3
- class IndexDoesNotExistError < StandardError ; end
2
+ module Errors
3
+ class InvalidParamsError < StandardError; end
4
+ class IndexDoesNotExistError < StandardError ; end
5
+ end
4
6
  end
@@ -32,6 +32,14 @@ module Stretchy
32
32
  Utils.extract_options!(params, FUNCTION_SCORE_OPTIONS)
33
33
  end
34
34
 
35
+ def dotify_params(params, context)
36
+ if context[:nested]
37
+ Utils.nestify(params)
38
+ else
39
+ Utils.dotify(params)
40
+ end
41
+ end
42
+
35
43
  def raw_node(params, context)
36
44
  if context[:boost]
37
45
  raw_boost_node(params, context)
@@ -43,19 +51,21 @@ module Stretchy
43
51
  def raw_boost_node(params, context)
44
52
  boost_params = extract_boost_params!(params)
45
53
  context[:fn_score] = extract_function_score_options!(params)
46
- context[:boost] ||= true
47
- context[:filter] ||= true
54
+ context[:boost] = true
55
+ context[:filter] = true
48
56
  json = context[:query] ? {query: params} : params
49
57
  Node.new(boost_params.merge(filter: json), context)
50
58
  end
51
59
 
52
60
  def context_nodes(params, context = default_context)
61
+ subparams = dotify_params(params, context)
62
+
53
63
  if context[:boost]
54
- params_to_boost(params, context)
64
+ params_to_boost(subparams, context)
55
65
  elsif context[:query]
56
- params_to_queries(params, context)
66
+ params_to_queries(subparams, context)
57
67
  else
58
- params_to_filters(params, context)
68
+ params_to_filters(subparams, context)
59
69
  end
60
70
  end
61
71
 
@@ -81,11 +91,14 @@ module Stretchy
81
91
  case val
82
92
  when Array
83
93
  Node.new({match: {
84
- field => {query: val.join(' '),
85
- :operator => :or
86
- }}}, context)
94
+ field => {query: val.join(' '), :operator => :or}
95
+ }}, context)
87
96
  when Range
88
- Node.new({range: {field => {gte: val.min, lte: val.max}}}, context)
97
+ Node.new({range: {
98
+ field => {gte: val.min, lte: val.max}
99
+ }}, context)
100
+ when Hash
101
+ nested(val, field, context)
89
102
  else
90
103
  Node.new({match: {field => val}}, context)
91
104
  end
@@ -99,12 +112,31 @@ module Stretchy
99
112
  Node.new({range: {field => {gte: val.min, lte: val.max}}}, context)
100
113
  when nil
101
114
  Node.new({missing: {field: field}}, context)
115
+ when Hash
116
+ nested(val, field, context)
102
117
  else
103
118
  Node.new({terms: {field => Array(val)}}, context)
104
119
  end
105
120
  end
106
121
  end
107
122
 
123
+ def nested(params, path, context = default_context)
124
+ type, json = if context[:query]
125
+ nodes = params_to_queries(params, context)
126
+ json = AndCollector.new(nodes, context).json
127
+ [:query, json]
128
+ else
129
+ nodes = params_to_filters(params, context)
130
+ json = AndCollector.new(nodes, context).filter_json
131
+ [:filter, json]
132
+ end
133
+
134
+ Node.new({nested: {
135
+ path: path,
136
+ type => json
137
+ }}, context)
138
+ end
139
+
108
140
  # https://www.elastic.co/guide/en/elasticsearch/guide/current/proximity-relevance.html
109
141
  def fulltext_nodes_from_string(params, context = default_context)
110
142
  subcontext = context.merge(query: true)
@@ -17,8 +17,8 @@ module Stretchy
17
17
  # raises error if required parameters are missing
18
18
  def require_params!(method, params, *fields)
19
19
  raise Errors::InvalidParamsError.new(
20
- "#{method} requires at least #{fields.join(' and ')} params",
21
- "params found: #{params}"
20
+ "#{method} requires at least #{fields.join(' and ')} params, but " +
21
+ "found: #{params}"
22
22
  ) if fields.any? {|f| is_empty? params[f] }
23
23
 
24
24
  true
@@ -27,7 +27,7 @@ module Stretchy
27
27
  # generates a hash of specified options,
28
28
  # removing them from the original hash
29
29
  def extract_options!(params, list)
30
- boost_params = Hash[list.map do |opt|
30
+ output = Hash[list.map do |opt|
31
31
  [opt, params.delete(opt)]
32
32
  end].keep_if {|k,v| !is_empty?(v)}
33
33
  end
@@ -39,7 +39,33 @@ module Stretchy
39
39
 
40
40
  # coerces ids to integers, unless they contain non-integers
41
41
  def coerce_id(id)
42
- id =~ /\d+/ ? id.to_i : id
42
+ id =~ /^\d+$/ ? id.to_i : id
43
+ end
44
+
45
+ def dotify(hash, prefixes = [])
46
+ hash.reduce({}) do |memo, kv|
47
+ key, val = kv
48
+ subprefixes = (prefixes + [key])
49
+ prefix = subprefixes.join('.')
50
+ if val.is_a? Hash
51
+ memo.merge(dotify(val, subprefixes))
52
+ else
53
+ memo.merge(prefix => val)
54
+ end
55
+ end
56
+ end
57
+
58
+ def nestify(hash, prefixes = [])
59
+ hash.reduce({}) do |memo, kv|
60
+ key, val = kv
61
+ subprefixes = (prefixes + [key])
62
+ prefix = subprefixes.join('.')
63
+ if val.is_a? Hash
64
+ memo.merge(prefix => nestify(val, subprefixes))
65
+ else
66
+ memo.merge(prefix => val)
67
+ end
68
+ end
43
69
  end
44
70
  end
45
71
 
@@ -1,3 +1,3 @@
1
1
  module Stretchy
2
- VERSION = "0.5.2"
2
+ VERSION = "0.5.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stretchy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - agius
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-15 00:00:00.000000000 Z
11
+ date: 2015-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: elasticsearch