elasticquerybuilder 0.1.5 → 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 +4 -4
- data/Gemfile.lock +1 -1
- data/elasticquerybuilder-0.0.1.gem +0 -0
- data/elasticquerybuilder-0.1.5.gem +0 -0
- data/lib/elasticquerybuilder.rb +59 -116
- data/lib/elasticquerybuilder/modals.rb +14 -0
- data/lib/elasticquerybuilder/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ced73c82a31289251a18b233d55a2368fc975e97425d6d7b64ffe177e943dd40
|
4
|
+
data.tar.gz: 2c824aca2bf7afa316cd7eb8b3b59144013e8a8faab0ff32bf7a60fa3fa8c7c8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f25d3d51c86afd77c1e9f45fa631c7d5d5973c91437423f2ed8835f8ae555600a9b44a060ef56a1ad5e5b1bfe7d733f87773adfa23bb0abf8ab9b5be9b627397
|
7
|
+
data.tar.gz: 791a543fa6b589e539ba584c3abd3fffb9e567b4487d0936c8eed2f755955d10df137c8c738061b5490065bec19da2cfbc4558ecb17e8c64dc54bc5a09ccaa69
|
data/Gemfile.lock
CHANGED
Binary file
|
Binary file
|
data/lib/elasticquerybuilder.rb
CHANGED
@@ -1,137 +1,42 @@
|
|
1
1
|
require 'elasticquerybuilder/version'
|
2
|
+
require 'elasticquerybuilder/modals'
|
2
3
|
|
3
4
|
module ElasticQueryBuilder
|
5
|
+
# main class that generates elastic criteria
|
4
6
|
class Engine
|
5
7
|
attr_reader :criteria
|
6
8
|
def initialize
|
7
9
|
@criteria = { query: {} }
|
8
|
-
@
|
10
|
+
@modal = ''
|
9
11
|
end
|
10
12
|
|
11
13
|
# opts = [{field,value,operation},{field,value,operation}]
|
12
14
|
def must
|
13
|
-
@
|
15
|
+
@modal = ElasticQueryBuilder::Modals.must
|
14
16
|
self
|
15
17
|
end
|
16
18
|
|
19
|
+
# used for matching strings
|
17
20
|
def term(opts)
|
18
|
-
term
|
19
|
-
prev = @keyword.to_sym
|
20
|
-
if @criteria[:query].include? :bool
|
21
|
-
@criteria[:query][:bool][prev] = [] unless @criteria[:query][:bool].include? prev
|
22
|
-
bool_query = true
|
23
|
-
else
|
24
|
-
bool_query = false
|
25
|
-
@criteria[:query] = []
|
26
|
-
end
|
27
|
-
opts.each do |option, _value|
|
28
|
-
val = option[:value]
|
29
|
-
term[option[:field]] = val
|
30
|
-
end
|
31
|
-
if bool_query
|
32
|
-
@criteria[:query][:bool][prev].push(term: term)
|
33
|
-
else
|
34
|
-
if opts.count > 1
|
35
|
-
@criteria[:query].push(term: term)
|
36
|
-
else
|
37
|
-
@criteria[:query] = { term: {} }
|
38
|
-
@criteria[:query][:term] = term
|
39
|
-
end
|
40
|
-
end
|
41
|
-
self
|
21
|
+
setup_query('term', opts)
|
42
22
|
end
|
43
23
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
if @criteria[:query].include? :bool
|
48
|
-
@criteria[:query][:bool][prev] = [] unless @criteria[:query][:bool].include? prev
|
49
|
-
bool_query = true
|
50
|
-
else
|
51
|
-
bool_query = false
|
52
|
-
@criteria[:query] = []
|
53
|
-
end
|
54
|
-
opts.each do |option, _value|
|
55
|
-
val = handle_operation('in', option[:value])
|
56
|
-
term[option[:field]] = val
|
57
|
-
end
|
58
|
-
if bool_query
|
59
|
-
@criteria[:query][:bool][prev].push(terms: term)
|
60
|
-
else
|
61
|
-
if opts.count > 1
|
62
|
-
@criteria[:query].push(terms: term)
|
63
|
-
else
|
64
|
-
@criteria[:query] = { terms: {} }
|
65
|
-
@criteria[:query][:terms] = term
|
66
|
-
end
|
67
|
-
end
|
68
|
-
self
|
24
|
+
# used for matching a value within an array
|
25
|
+
def terms(opts, default_operator = 'in')
|
26
|
+
setup_query('terms', opts, default_operator)
|
69
27
|
end
|
70
28
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
if @criteria[:query].include? :bool
|
75
|
-
@criteria[:query][:bool][prev] = [] unless @criteria[:query][:bool].include? prev
|
76
|
-
bool_query = true
|
77
|
-
else
|
78
|
-
bool_query = false
|
79
|
-
@criteria[:query] = []
|
80
|
-
end
|
81
|
-
opts.each do |option, _value|
|
82
|
-
val = if !option.include? :operation
|
83
|
-
option[:value]
|
84
|
-
else
|
85
|
-
handle_operation(option[:operation], option[:value])
|
86
|
-
end
|
87
|
-
wildcard[option[:field]] = val
|
88
|
-
end
|
89
|
-
if bool_query
|
90
|
-
@criteria[:query][:bool][prev].push(wildcard: wildcard)
|
91
|
-
else
|
92
|
-
if opts.count > 1
|
93
|
-
@criteria[:query].push(wildcard: wildcard)
|
94
|
-
else
|
95
|
-
@criteria[:query] = { wildcard: {} }
|
96
|
-
@criteria[:query][:wildcard] = wildcard
|
97
|
-
end
|
98
|
-
end
|
99
|
-
self
|
29
|
+
# used for wildcards. supports whole wildcard, left and right matching
|
30
|
+
def wildcard(opts, default_operator = 'wildcard')
|
31
|
+
setup_query('wildcard', opts, default_operator)
|
100
32
|
end
|
101
33
|
|
102
|
-
def
|
103
|
-
match
|
104
|
-
prev = @keyword.to_sym
|
105
|
-
if @criteria[:query].include? :bool
|
106
|
-
@criteria[:query][:bool][prev] = [] unless @criteria[:query][:bool].include? prev
|
107
|
-
bool_query = true
|
108
|
-
else
|
109
|
-
bool_query = false
|
110
|
-
@criteria[:query] = []
|
111
|
-
end
|
112
|
-
opts.each do |option, _value|
|
113
|
-
val = if !option.include? :operation
|
114
|
-
option[:value]
|
115
|
-
else
|
116
|
-
handle_operation(option[:operation], option[:value])
|
117
|
-
end
|
118
|
-
match[option[:field]] = val
|
119
|
-
end
|
120
|
-
if bool_query
|
121
|
-
@criteria[:query][:bool][prev].push(match: match)
|
122
|
-
else
|
123
|
-
if opts.count > 1
|
124
|
-
@criteria[:query].push(match: match)
|
125
|
-
else
|
126
|
-
@criteria[:query] = { match: {} }
|
127
|
-
@criteria[:query][:match] = match
|
128
|
-
end
|
129
|
-
end
|
130
|
-
self
|
34
|
+
def single_match(opts)
|
35
|
+
setup_query('match', opts)
|
131
36
|
end
|
132
37
|
|
133
38
|
def should
|
134
|
-
@
|
39
|
+
@modal = ElasticQueryBuilder::Modals.should
|
135
40
|
self
|
136
41
|
end
|
137
42
|
|
@@ -140,21 +45,59 @@ module ElasticQueryBuilder
|
|
140
45
|
self
|
141
46
|
end
|
142
47
|
|
143
|
-
# TODO: single wildcard or match not in bool clause
|
144
|
-
# commit when should and must combination working
|
145
|
-
# provide to_searchkick method which allows a formatted criteria to be sent through a relevant model
|
146
|
-
# push version 0.05 to rubygems
|
147
|
-
|
148
48
|
private
|
149
49
|
|
150
50
|
def handle_operation(operation, value)
|
151
51
|
case operation
|
152
|
-
when 'eq' then
|
52
|
+
when 'eq' then value
|
153
53
|
when 'in' then value.split(',').map(&:to_i)
|
154
54
|
when 'wildcard-left' then '*' + value
|
155
55
|
when 'wildcard-right' then value + '*'
|
156
56
|
when 'wildcard' then '*' + value + '*'
|
157
57
|
end
|
158
58
|
end
|
59
|
+
|
60
|
+
def populate_data(opts, default_operator)
|
61
|
+
data = {}
|
62
|
+
opts.each do |option, _value|
|
63
|
+
val = if !option.include? :operation
|
64
|
+
handle_operation(default_operator, option[:value])
|
65
|
+
else
|
66
|
+
handle_operation(option[:operation], option[:value])
|
67
|
+
end
|
68
|
+
data[option[:field]] = val
|
69
|
+
end
|
70
|
+
data
|
71
|
+
end
|
72
|
+
|
73
|
+
def setup_query(key, opts, default_operator = 'eq')
|
74
|
+
prev = @modal.to_sym
|
75
|
+
formulate_query_structure(prev, key, opts, default_operator)
|
76
|
+
self
|
77
|
+
end
|
78
|
+
|
79
|
+
def formulate_query_structure(prev, key, opts, default_operator)
|
80
|
+
if @criteria[:query].include? :bool
|
81
|
+
@criteria[:query][:bool][prev] = [] unless @criteria[:query][:bool].include? prev
|
82
|
+
bool_query = true
|
83
|
+
else
|
84
|
+
bool_query = false
|
85
|
+
@criteria[:query] = []
|
86
|
+
end
|
87
|
+
after_formulate_query_structure(prev, bool_query, key, opts, default_operator)
|
88
|
+
end
|
89
|
+
|
90
|
+
def after_formulate_query_structure(prev, bool_query, key, opts, default_operator)
|
91
|
+
data = populate_data(opts, default_operator)
|
92
|
+
if bool_query
|
93
|
+
@criteria[:query][:bool][prev].push(key.to_sym => data)
|
94
|
+
elsif opts.count > 1
|
95
|
+
@criteria[:query].push(key.to_sym => data)
|
96
|
+
else
|
97
|
+
@criteria[:query] = { key.to_sym => {} }
|
98
|
+
@criteria[:query][key.to_sym] = data
|
99
|
+
end
|
100
|
+
bool_query
|
101
|
+
end
|
159
102
|
end
|
160
103
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elasticquerybuilder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Comms365
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,8 +71,11 @@ files:
|
|
71
71
|
- Rakefile
|
72
72
|
- bin/console
|
73
73
|
- bin/setup
|
74
|
+
- elasticquerybuilder-0.0.1.gem
|
75
|
+
- elasticquerybuilder-0.1.5.gem
|
74
76
|
- elasticquerybuilder.gemspec
|
75
77
|
- lib/elasticquerybuilder.rb
|
78
|
+
- lib/elasticquerybuilder/modals.rb
|
76
79
|
- lib/elasticquerybuilder/version.rb
|
77
80
|
homepage: https://github.com/comms365/elasticquerybuilder/
|
78
81
|
licenses:
|