elasticquerybuilder 0.0.1 → 0.1.5
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 +2 -2
- data/Gemfile.lock +35 -0
- data/Rakefile +3 -3
- data/elasticquerybuilder.gemspec +15 -16
- data/lib/elasticquerybuilder.rb +157 -2
- data/lib/elasticquerybuilder/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b04560361a0f7240549b8803c1b3bc611f18ed6eddbdabd83a4f8e7c72a61438
|
4
|
+
data.tar.gz: c8cf3274aba46e51720430b4af10ed42d4d8c0d6bbe3218831781b1320c8ab7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0cf96c3b0036b32ceb0d9c97a2d8d808f019a4794271faef0f171aca85eb9bd1e416b6e259fb23b4efd5e18cbe8da8d0745f8987a48cee348e2e6dd0823b1b2
|
7
|
+
data.tar.gz: 0ca2a8eef82e9dda91b3dcfa2955fbf9f295e638f2387f2751d9501fc24139ee7dd1d80c4b07738591e8f510a71a5a190921250caa84e5c118384e7574431898
|
data/Gemfile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
3
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
4
4
|
|
5
5
|
# Specify your gem's dependencies in elasticquerybuilder.gemspec
|
6
6
|
gemspec
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
elasticquerybuilder (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.4.2)
|
11
|
+
rspec (3.8.0)
|
12
|
+
rspec-core (~> 3.8.0)
|
13
|
+
rspec-expectations (~> 3.8.0)
|
14
|
+
rspec-mocks (~> 3.8.0)
|
15
|
+
rspec-core (3.8.0)
|
16
|
+
rspec-support (~> 3.8.0)
|
17
|
+
rspec-expectations (3.8.1)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.8.0)
|
20
|
+
rspec-mocks (3.8.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.8.0)
|
23
|
+
rspec-support (3.8.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.16)
|
30
|
+
elasticquerybuilder!
|
31
|
+
rake (~> 10.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.16.5
|
data/Rakefile
CHANGED
data/elasticquerybuilder.gemspec
CHANGED
@@ -1,18 +1,17 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path("../lib", __FILE__)
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
3
|
+
require 'elasticquerybuilder/version'
|
5
4
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
6
|
+
spec.name = 'elasticquerybuilder'
|
8
7
|
spec.version = ElasticQueryBuilder::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
8
|
+
spec.authors = ['Comms365']
|
9
|
+
spec.email = ['ben.hassan@comms365.com']
|
11
10
|
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
14
|
-
spec.homepage =
|
15
|
-
spec.license =
|
11
|
+
spec.summary = 'Query Criteria Builder For Elasticsearch'
|
12
|
+
spec.description = 'Allows for an easier way to construct Elasticsearch queries. Tries to minimise the verbosity.'
|
13
|
+
spec.homepage = 'https://github.com/comms365/elasticquerybuilder/'
|
14
|
+
spec.license = 'MIT'
|
16
15
|
|
17
16
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
17
|
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
@@ -25,14 +24,14 @@ Gem::Specification.new do |spec|
|
|
25
24
|
|
26
25
|
# Specify which files should be added to the gem when it is released.
|
27
26
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
28
|
-
spec.files = Dir.chdir(File.expand_path(
|
27
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
29
28
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
30
29
|
end
|
31
|
-
spec.bindir =
|
30
|
+
spec.bindir = 'exe'
|
32
31
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
|
-
spec.require_paths = [
|
32
|
+
spec.require_paths = ['lib']
|
34
33
|
|
35
|
-
spec.add_development_dependency
|
36
|
-
spec.add_development_dependency
|
37
|
-
spec.add_development_dependency
|
34
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
35
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
36
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
38
37
|
end
|
data/lib/elasticquerybuilder.rb
CHANGED
@@ -1,5 +1,160 @@
|
|
1
|
-
require
|
1
|
+
require 'elasticquerybuilder/version'
|
2
2
|
|
3
3
|
module ElasticQueryBuilder
|
4
|
-
|
4
|
+
class Engine
|
5
|
+
attr_reader :criteria
|
6
|
+
def initialize
|
7
|
+
@criteria = { query: {} }
|
8
|
+
@keyword = ''
|
9
|
+
end
|
10
|
+
|
11
|
+
# opts = [{field,value,operation},{field,value,operation}]
|
12
|
+
def must
|
13
|
+
@keyword = 'must'
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
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
|
42
|
+
end
|
43
|
+
|
44
|
+
def terms(opts)
|
45
|
+
term = {}
|
46
|
+
prev = @keyword.to_sym
|
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
|
69
|
+
end
|
70
|
+
|
71
|
+
def wildcard(opts)
|
72
|
+
wildcard = {}
|
73
|
+
prev = @keyword.to_sym
|
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
|
100
|
+
end
|
101
|
+
|
102
|
+
def match_on_single_field(opts)
|
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
|
131
|
+
end
|
132
|
+
|
133
|
+
def should
|
134
|
+
@keyword = 'should'
|
135
|
+
self
|
136
|
+
end
|
137
|
+
|
138
|
+
def bool
|
139
|
+
@criteria[:query] = { bool: {} }
|
140
|
+
self
|
141
|
+
end
|
142
|
+
|
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
|
+
private
|
149
|
+
|
150
|
+
def handle_operation(operation, value)
|
151
|
+
case operation
|
152
|
+
when 'eq' then ''
|
153
|
+
when 'in' then value.split(',').map(&:to_i)
|
154
|
+
when 'wildcard-left' then '*' + value
|
155
|
+
when 'wildcard-right' then value + '*'
|
156
|
+
when 'wildcard' then '*' + value + '*'
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
5
160
|
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.1.5
|
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-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -65,6 +65,7 @@ files:
|
|
65
65
|
- ".travis.yml"
|
66
66
|
- CODE_OF_CONDUCT.md
|
67
67
|
- Gemfile
|
68
|
+
- Gemfile.lock
|
68
69
|
- LICENSE.txt
|
69
70
|
- README.md
|
70
71
|
- Rakefile
|