stretchy 0.5.1 → 0.5.2

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: 0c3818aa28e0d5d43fa9b1abc168571856081382
4
- data.tar.gz: 31e8a84723d588e7b5e0ffff48c205bf9a96a514
3
+ metadata.gz: 1a8263c1321815880f552cd0526e9bdd5df59b46
4
+ data.tar.gz: 276a949c2c902bf1f860acfebdc943531ee27620
5
5
  SHA512:
6
- metadata.gz: a43550179be297d7e581075223a43781cf24f23e9eca3f379256492186107154cdd76d398184efff9f3b1ac9c4ee11c37c4b576dd90c2c8e0aac0366172f06ec
7
- data.tar.gz: 9cd2d2c2344b52bcf20cb4e852cc20514f48cc1bc80aca17a35b51a59370b5c74dd13edbb08e60a996c0a1672e304627a846020d4968169c5e58153acf7e8810
6
+ metadata.gz: 686770d04b195057ecf51bca7c95e6c34f47b37c1ee5398dc5ceae20bf0a281d11f85a081372a10d6fda7e06f2d9003d16bcc0a05c6f6fa50c72df44dcc03ca9
7
+ data.tar.gz: 540bb5b13632defe17885490ce58fbd85941cbb774d1a58913f14bb253309bfd7d574aa3dc00deabbbae2eca243baec60d2b95575f2a5db5767d8c1e4f40febf
@@ -42,13 +42,23 @@ module Stretchy
42
42
  end
43
43
 
44
44
  def filter_node
45
- @filter_node ||= if query_nodes.any? || boost_nodes.any?
46
- Node.new({query: filtered_query_node.json}, context)
45
+ @filter_node ||= if query_nodes.any?
46
+ if boost_nodes.any?
47
+ Node.new({query: function_score_node.json}, context)
48
+ elsif filter_nodes.any?
49
+ Node.new({query: filtered_query_node.json}, context)
50
+ else
51
+ Node.new({query: single_query_node.json}, context)
52
+ end
47
53
  else
48
54
  Node.new(compile_nodes(filter_nodes).json, context)
49
55
  end
50
56
  end
51
57
 
58
+ def filter_json
59
+ filter_node.json
60
+ end
61
+
52
62
  def filter_nodes
53
63
  @filter_nodes ||= begin
54
64
  node_arr = collect_nodes nodes do |n|
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, :root, :context
12
+ attr_reader :collector, :opts, :root, :context
13
13
 
14
14
  delegate [
15
15
  :total,
@@ -26,10 +26,11 @@ module Stretchy
26
26
  :aggregations
27
27
  ] => :results_obj
28
28
 
29
- def initialize(options = {})
30
- @collector = AndCollector.new(options[:nodes] || [], query: true)
31
- @root = options[:root] || {}
32
- @context = options[:context] || {}
29
+ def initialize(opts = {})
30
+ @opts = opts
31
+ @collector = AndCollector.new(opts[:nodes] || [], query: true)
32
+ @root = opts[:root] || {}
33
+ @context = opts[:context] || {}
33
34
  end
34
35
 
35
36
  def context?(*args)
@@ -213,23 +214,27 @@ module Stretchy
213
214
  end
214
215
 
215
216
  def add_nodes(additional)
216
- self.class.new nodes: collector.nodes + Array(additional), root: root
217
+ self.class.new(opts.merge(
218
+ nodes: collector.nodes + Array(additional),
219
+ root: root,
220
+ context: {}
221
+ ))
217
222
  end
218
223
 
219
224
  def add_root(options = {})
220
- self.class.new(
225
+ self.class.new(opts.merge(
221
226
  nodes: collector.nodes,
222
227
  root: root.merge(options),
223
228
  context: context
224
- )
229
+ ))
225
230
  end
226
231
 
227
232
  def add_context(*args)
228
- self.class.new(
233
+ self.class.new(opts.merge(
229
234
  nodes: collector.nodes,
230
235
  root: root,
231
236
  context: context.merge(args_to_context(*args))
232
- )
237
+ ))
233
238
  end
234
239
 
235
240
  end
@@ -41,10 +41,12 @@ module Stretchy
41
41
  end
42
42
 
43
43
  def raw_boost_node(params, context)
44
+ boost_params = extract_boost_params!(params)
44
45
  context[:fn_score] = extract_function_score_options!(params)
45
46
  context[:boost] ||= true
46
47
  context[:filter] ||= true
47
- Node.new(params, context)
48
+ json = context[:query] ? {query: params} : params
49
+ Node.new(boost_params.merge(filter: json), context)
48
50
  end
49
51
 
50
52
  def context_nodes(params, context = default_context)
@@ -76,8 +78,17 @@ module Stretchy
76
78
 
77
79
  def params_to_queries(params, context = default_context)
78
80
  params.map do |field, val|
79
- val = val.join if val.is_a? Array
80
- Node.new({match: {field => val}}, context)
81
+ case val
82
+ when Array
83
+ Node.new({match: {
84
+ field => {query: val.join(' '),
85
+ :operator => :or
86
+ }}}, context)
87
+ when Range
88
+ Node.new({range: {field => {gte: val.min, lte: val.max}}}, context)
89
+ else
90
+ Node.new({match: {field => val}}, context)
91
+ end
81
92
  end
82
93
  end
83
94
 
@@ -85,20 +96,11 @@ module Stretchy
85
96
  params.map do |field, val|
86
97
  case val
87
98
  when Range
88
- Node.new(
89
- {range: {field => {gte: val.min, lte: val.max}}},
90
- context
91
- )
99
+ Node.new({range: {field => {gte: val.min, lte: val.max}}}, context)
92
100
  when nil
93
- Node.new(
94
- {missing: {field: field}},
95
- context
96
- )
101
+ Node.new({missing: {field: field}}, context)
97
102
  else
98
- Node.new(
99
- {terms: {field => Array(val)}},
100
- context
101
- )
103
+ Node.new({terms: {field => Array(val)}}, context)
102
104
  end
103
105
  end
104
106
  end
@@ -0,0 +1,30 @@
1
+ module Stretchy
2
+ module Scopes
3
+
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def stretchify(options = {})
10
+ @stretchy_options = options
11
+ end
12
+
13
+ def search(options = {})
14
+ stretchy_scope.new stretchy_options.merge(options)
15
+ end
16
+
17
+ def stretchy_options
18
+ Hash(@stretchy_options)
19
+ end
20
+
21
+ def stretchy_scope
22
+ @scopes_class ||= Class.new Stretchy::API
23
+ end
24
+
25
+ def stretch(name, block)
26
+ stretchy_scope.send(:define_method, name, &block)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module Stretchy
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
data/lib/stretchy.rb CHANGED
@@ -10,6 +10,7 @@ require 'stretchy/errors'
10
10
  require 'stretchy/factory'
11
11
  require 'stretchy/node'
12
12
  require 'stretchy/results'
13
+ require 'stretchy/scopes'
13
14
  require 'stretchy/utils'
14
15
  require 'stretchy/version'
15
16
 
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.1
4
+ version: 0.5.2
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-07 00:00:00.000000000 Z
11
+ date: 2015-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: elasticsearch
@@ -160,6 +160,7 @@ files:
160
160
  - lib/stretchy/factory.rb
161
161
  - lib/stretchy/node.rb
162
162
  - lib/stretchy/results.rb
163
+ - lib/stretchy/scopes.rb
163
164
  - lib/stretchy/utils.rb
164
165
  - lib/stretchy/version.rb
165
166
  - solano.yml