elastic_query 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b32205036e3213b2463a73ed622673113f5f58c6
4
+ data.tar.gz: 299718483f5eb901b6e15dd815c6bac3877a7d6d
5
+ SHA512:
6
+ metadata.gz: 7e614212fc89732d869cb1848ed83c5d0039f4a15bc697883feb79116a5dc2c4031e3359517dc0715e1704998de1eeed026ef811671b5cd77a6ecd46da8a1b57
7
+ data.tar.gz: 3b74350272a7cc5d0741f00fd6dec85262e74762cb325a3fb8e58af5854dc5846e48ee4c4c730ce9d79d85ccb86a9c30acadcea711d804d669d9d61607646303
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ inherit_from: .ruby.yml
2
+
3
+ AllCops:
4
+ Exclude:
5
+ - spec/**/*
6
+ - Gemfile
7
+ - Rakefile
8
+ - elastic_adapter.gemspec
data/.ruby.yml ADDED
@@ -0,0 +1,261 @@
1
+ # Taken from the thoughtbot/hound repository
2
+ # See https://github.com/thoughtbot/hound/blob/master/config/style_guides/ruby.yml
3
+ AllCops:
4
+ Exclude:
5
+ - db/schema.rb
6
+
7
+ AccessorMethodName:
8
+ Enabled: false
9
+
10
+ ActionFilter:
11
+ Enabled: false
12
+
13
+ Alias:
14
+ Enabled: false
15
+
16
+ ArrayJoin:
17
+ Enabled: false
18
+
19
+ AsciiComments:
20
+ Enabled: false
21
+
22
+ AsciiIdentifiers:
23
+ Enabled: false
24
+
25
+ Attr:
26
+ Enabled: false
27
+
28
+ BlockNesting:
29
+ Enabled: false
30
+
31
+ CaseEquality:
32
+ Enabled: false
33
+
34
+ CharacterLiteral:
35
+ Enabled: false
36
+
37
+ ClassAndModuleChildren:
38
+ Enabled: false
39
+
40
+ ClassLength:
41
+ Enabled: false
42
+
43
+ ClassVars:
44
+ Enabled: false
45
+
46
+ CollectionMethods:
47
+ PreferredMethods:
48
+ find: detect
49
+ reduce: inject
50
+ collect: map
51
+ find_all: select
52
+
53
+ ColonMethodCall:
54
+ Enabled: false
55
+
56
+ CommentAnnotation:
57
+ Enabled: false
58
+
59
+ CyclomaticComplexity:
60
+ Enabled: false
61
+
62
+ Delegate:
63
+ Enabled: false
64
+
65
+ DeprecatedHashMethods:
66
+ Enabled: false
67
+
68
+ Documentation:
69
+ Enabled: false
70
+
71
+ DotPosition:
72
+ EnforcedStyle: trailing
73
+
74
+ DoubleNegation:
75
+ Enabled: false
76
+
77
+ EachWithObject:
78
+ Enabled: false
79
+
80
+ EmptyLiteral:
81
+ Enabled: false
82
+
83
+ Encoding:
84
+ Enabled: false
85
+
86
+ EvenOdd:
87
+ Enabled: false
88
+
89
+ FileName:
90
+ Enabled: false
91
+
92
+ FlipFlop:
93
+ Enabled: false
94
+
95
+ FormatString:
96
+ Enabled: false
97
+
98
+ GlobalVars:
99
+ Enabled: false
100
+
101
+ GuardClause:
102
+ Enabled: false
103
+
104
+ IfUnlessModifier:
105
+ Enabled: false
106
+
107
+ IfWithSemicolon:
108
+ Enabled: false
109
+
110
+ InlineComment:
111
+ Enabled: false
112
+
113
+ Lambda:
114
+ Enabled: false
115
+
116
+ LambdaCall:
117
+ Enabled: false
118
+
119
+ LineEndConcatenation:
120
+ Enabled: false
121
+
122
+ LineLength:
123
+ Max: 80
124
+
125
+ MethodLength:
126
+ Enabled: false
127
+
128
+ ModuleFunction:
129
+ Enabled: false
130
+
131
+ NegatedIf:
132
+ Enabled: false
133
+
134
+ NegatedWhile:
135
+ Enabled: false
136
+
137
+ Next:
138
+ Enabled: false
139
+
140
+ NilComparison:
141
+ Enabled: false
142
+
143
+ Not:
144
+ Enabled: false
145
+
146
+ NumericLiterals:
147
+ Enabled: false
148
+
149
+ OneLineConditional:
150
+ Enabled: false
151
+
152
+ OpMethod:
153
+ Enabled: false
154
+
155
+ ParameterLists:
156
+ Enabled: false
157
+
158
+ PercentLiteralDelimiters:
159
+ Enabled: false
160
+
161
+ PerlBackrefs:
162
+ Enabled: false
163
+
164
+ PredicateName:
165
+ NamePrefixBlacklist:
166
+ - is_
167
+
168
+ Proc:
169
+ Enabled: false
170
+
171
+ RaiseArgs:
172
+ Enabled: false
173
+
174
+ RegexpLiteral:
175
+ Enabled: false
176
+
177
+ SelfAssignment:
178
+ Enabled: false
179
+
180
+ SingleLineBlockParams:
181
+ Enabled: false
182
+
183
+ SingleLineMethods:
184
+ Enabled: false
185
+
186
+ SignalException:
187
+ Enabled: false
188
+
189
+ SpecialGlobalVars:
190
+ Enabled: false
191
+
192
+ StringLiterals:
193
+ EnforcedStyle: double_quotes
194
+
195
+ VariableInterpolation:
196
+ Enabled: false
197
+
198
+ TrailingComma:
199
+ Enabled: false
200
+
201
+ TrivialAccessors:
202
+ Enabled: false
203
+
204
+ VariableInterpolation:
205
+ Enabled: false
206
+
207
+ WhenThen:
208
+ Enabled: false
209
+
210
+ WhileUntilModifier:
211
+ Enabled: false
212
+
213
+ WordArray:
214
+ Enabled: false
215
+
216
+ # Lint
217
+
218
+ AmbiguousOperator:
219
+ Enabled: false
220
+
221
+ AmbiguousRegexpLiteral:
222
+ Enabled: false
223
+
224
+ AssignmentInCondition:
225
+ Enabled: false
226
+
227
+ ConditionPosition:
228
+ Enabled: false
229
+
230
+ DeprecatedClassMethods:
231
+ Enabled: false
232
+
233
+ ElseLayout:
234
+ Enabled: false
235
+
236
+ HandleExceptions:
237
+ Enabled: false
238
+
239
+ InvalidCharacterLiteral:
240
+ Enabled: false
241
+
242
+ LiteralInCondition:
243
+ Enabled: false
244
+
245
+ LiteralInInterpolation:
246
+ Enabled: false
247
+
248
+ Loop:
249
+ Enabled: false
250
+
251
+ ParenthesesAsGroupedExpression:
252
+ Enabled: false
253
+
254
+ RequireParentheses:
255
+ Enabled: false
256
+
257
+ UnderscorePrefixedVariableName:
258
+ Enabled: false
259
+
260
+ Void:
261
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in elastic_query.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # ElasticQuery
2
+
3
+ ## ToDo
4
+
5
+ * Change FilteredQuery to take options optional
6
+
7
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/elastic_query`. To experiment with that code, run `bin/console` for an interactive prompt.
8
+
9
+ TODO: Delete this and the text above, and describe your gem
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'elastic_query'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install elastic_query
26
+
27
+ ## Usage
28
+
29
+ TODO: Write usage instructions here
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
34
+
35
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it ( https://github.com/[my-github-username]/elastic_query/fork )
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "elastic_query"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'elastic_query/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "elastic_query"
8
+ spec.version = ElasticQuery::VERSION
9
+ spec.authors = ["Kristopher Bredemeier"]
10
+ spec.email = ["k.bredemeier@googlemail.com"]
11
+
12
+ spec.summary = %q{Provides a DSL to create elasticsearch queries}
13
+ spec.description = %q{Provides a DSL to create elasticsearch queries}
14
+ spec.homepage = "https://github.com/kbredemeier/elastic_query"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.8"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "pry-byebug", "~> 3.0.1"
24
+ end
@@ -0,0 +1,7 @@
1
+ module ElasticQuery
2
+ class BaseQuery
3
+ def to_hash
4
+ raise NotImplementedError
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ module ElasticQuery
2
+ class Builder
3
+ attr_reader :query
4
+
5
+ def initialize(query)
6
+ @query = Query.new(query)
7
+ end
8
+
9
+ def filter(filter_query)
10
+ @query = FilteredQuery.new(filter_query, query)
11
+ self
12
+ end
13
+
14
+ def rescore(rescore_query, rescore_options = {})
15
+ @query = RescoreQuery.new(query, rescore_query, rescore_options)
16
+ self
17
+ end
18
+
19
+ def to_hash
20
+ {
21
+ query: query.to_hash
22
+ }
23
+ end
24
+
25
+ def filter_nested(object_name, query, options = {})
26
+ filter(NestedQuery.new(object_name, query, options))
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ module ElasticQuery
2
+ # This class represents a filtered query.
3
+ #
4
+ # @attr_reader [Query] filter the filter
5
+ # @attr_reader [Query] query the query
6
+ class FilteredQuery < Query
7
+ attr_reader :filter
8
+
9
+ # @param [Hash] filter
10
+ # @param [Hash] query
11
+ def initialize(filter, query)
12
+ @filter = filter
13
+ super(query)
14
+ end
15
+
16
+ def to_hash
17
+ {
18
+ filtered: {
19
+ query: query.to_hash,
20
+ filter: filter.to_hash
21
+ }
22
+ }
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ module ElasticQuery
2
+ class NestedQuery < Query
3
+ attr_reader :object, :options
4
+
5
+ def initialize(object, query, options = {})
6
+ @object = object
7
+ @options = options
8
+ super(query)
9
+ end
10
+
11
+ def to_hash
12
+ {
13
+ nested: {
14
+ path: object.to_s,
15
+ query: query.to_hash,
16
+ }.merge(options)
17
+ }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module ElasticQuery
2
+ # This class represents a basic query like a term or match query
3
+ # @example
4
+ # Quey.new(match_all: {})
5
+ class Query < BaseQuery
6
+ attr_reader :query
7
+
8
+ def initialize(query)
9
+ if query.class < BaseQuery
10
+ @query = query.to_hash
11
+ else
12
+ @query = query
13
+ end
14
+ end
15
+
16
+ def to_hash
17
+ query
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,37 @@
1
+ module ElasticQuery
2
+ # This class represents Arrays of contitions like
3
+ #
4
+ # @example
5
+ # {
6
+ # must: [
7
+ # { match: { name: "mike" } }
8
+ # { term: { gender: "male" } }
9
+ # ]
10
+ # }
11
+ #
12
+ # Which woult be euqivalent to
13
+ #
14
+ # @example
15
+ # QueryCollection.new(:must,
16
+ # [
17
+ # Query.new(match: { name: "mike" }),
18
+ # Query.new(term: { gender: "male" })
19
+ # ]
20
+ # )
21
+ class QueryCollection < BaseQuery
22
+ attr_accessor :key, :queries
23
+
24
+ # @param [Symbol] key the key
25
+ # @param [Array<Query>] the queries
26
+ def initialize(key, queries)
27
+ @key = key
28
+ @queries = queries
29
+ end
30
+
31
+ def to_hash
32
+ {
33
+ key => queries.map(&:to_hash)
34
+ }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,20 @@
1
+ module ElasticQuery
2
+ class RescoreQuery < Query
3
+ attr_reader :options, :rescore_query
4
+
5
+ def initialize(query, rescore_query, options = {})
6
+ @options = options
7
+ @rescore_query = rescore_query
8
+ super(query)
9
+ end
10
+
11
+ def to_hash
12
+ {
13
+ query: query.to_hash,
14
+ rescore: {
15
+ rescore_query: rescore_query.to_hash,
16
+ }.merge(options)
17
+ }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module ElasticQuery
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,17 @@
1
+ begin
2
+ require "pry"
3
+ rescue LoadError
4
+ end
5
+
6
+ require "elastic_query/version"
7
+ require "elastic_query/base_query"
8
+ require "elastic_query/query"
9
+ require "elastic_query/nested_query"
10
+ require "elastic_query/query_collection"
11
+ require "elastic_query/filtered_query"
12
+ require "elastic_query/rescore_query"
13
+ require "elastic_query/builder"
14
+
15
+ module ElasticQuery
16
+ # Your code goes here...
17
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elastic_query
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kristopher Bredemeier
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.1
55
+ description: Provides a DSL to create elasticsearch queries
56
+ email:
57
+ - k.bredemeier@googlemail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".rubocop.yml"
65
+ - ".ruby.yml"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - elastic_query.gemspec
73
+ - lib/elastic_query.rb
74
+ - lib/elastic_query/base_query.rb
75
+ - lib/elastic_query/builder.rb
76
+ - lib/elastic_query/filtered_query.rb
77
+ - lib/elastic_query/nested_query.rb
78
+ - lib/elastic_query/query.rb
79
+ - lib/elastic_query/query_collection.rb
80
+ - lib/elastic_query/rescore_query.rb
81
+ - lib/elastic_query/version.rb
82
+ homepage: https://github.com/kbredemeier/elastic_query
83
+ licenses: []
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.4.6
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Provides a DSL to create elasticsearch queries
105
+ test_files: []
106
+ has_rdoc: