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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6ae2d23e5c3212e4f375a1ca277f81de55a2fc60cb766090d986610659683561
4
- data.tar.gz: f34f8c97cb587d2ee3c20fc4837d11b503f991ece3799ae98ac9e9cbec5f6c19
3
+ metadata.gz: b04560361a0f7240549b8803c1b3bc611f18ed6eddbdabd83a4f8e7c72a61438
4
+ data.tar.gz: c8cf3274aba46e51720430b4af10ed42d4d8c0d6bbe3218831781b1320c8ab7b
5
5
  SHA512:
6
- metadata.gz: 7bff78ac052e96a7b5740ecb69adb4ef9c5e570808ead6dd4fd29cf4aa3d42f32126ba67c59cb83923c819ffed147b573ad78e774ccbeeac4c2d52ddf6069959
7
- data.tar.gz: e5c6c1a48f88c8ee874bfdeca48a1f772db9f1218087bb57c3db74e8dde3c1269a6dbae253d81a7edddac6fc32a85f7fe5c8da1b1ac93458d2bb1441cb60a43b
6
+ metadata.gz: f0cf96c3b0036b32ceb0d9c97a2d8d808f019a4794271faef0f171aca85eb9bd1e416b6e259fb23b4efd5e18cbe8da8d0745f8987a48cee348e2e6dd0823b1b2
7
+ data.tar.gz: 0ca2a8eef82e9dda91b3dcfa2955fbf9f295e638f2387f2751d9501fc24139ee7dd1d80c4b07738591e8f510a71a5a190921250caa84e5c118384e7574431898
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
- source "https://rubygems.org"
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
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
@@ -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 "elasticquerybuilder/version"
3
+ require 'elasticquerybuilder/version'
5
4
 
6
5
  Gem::Specification.new do |spec|
7
- spec.name = "elasticquerybuilder"
6
+ spec.name = 'elasticquerybuilder'
8
7
  spec.version = ElasticQueryBuilder::VERSION
9
- spec.authors = ["Comms365"]
10
- spec.email = ["ben.hassan@comms365.com"]
8
+ spec.authors = ['Comms365']
9
+ spec.email = ['ben.hassan@comms365.com']
11
10
 
12
- spec.summary = %q{Query Criteria Builder For Elasticsearch}
13
- spec.description = %q{Allows for an easier way to construct Elasticsearch queries. Tries to minimise the verbosity.}
14
- spec.homepage = "https://github.com/comms365/elasticquerybuilder/"
15
- spec.license = "MIT"
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('..', __FILE__)) do
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 = "exe"
30
+ spec.bindir = 'exe'
32
31
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
- spec.require_paths = ["lib"]
32
+ spec.require_paths = ['lib']
34
33
 
35
- spec.add_development_dependency "bundler", "~> 1.16"
36
- spec.add_development_dependency "rake", "~> 10.0"
37
- spec.add_development_dependency "rspec", "~> 3.0"
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
@@ -1,5 +1,160 @@
1
- require "elasticquerybuilder/version"
1
+ require 'elasticquerybuilder/version'
2
2
 
3
3
  module ElasticQueryBuilder
4
- # Your code goes here...
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
@@ -1,3 +1,3 @@
1
1
  module ElasticQueryBuilder
2
- VERSION = "0.0.1"
2
+ VERSION = '0.1.5'.freeze
3
3
  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.0.1
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 00:00:00.000000000 Z
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