daedal 0.0.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 +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +82 -0
- data/Guardfile +4 -0
- data/LICENSE +20 -0
- data/README.md +4 -0
- data/daedal.gemspec +14 -0
- data/lib/daedal.rb +8 -0
- data/lib/daedal/attributes.rb +94 -0
- data/lib/daedal/filters.rb +7 -0
- data/lib/daedal/filters/base_filter.rb +17 -0
- data/lib/daedal/filters/term_filter.rb +18 -0
- data/lib/daedal/queries.rb +13 -0
- data/lib/daedal/queries/base_query.rb +13 -0
- data/lib/daedal/queries/bool_query.rb +48 -0
- data/lib/daedal/queries/constant_score_query.rb +35 -0
- data/lib/daedal/queries/dis_max_query.rb +37 -0
- data/lib/daedal/queries/filtered_query.rb +20 -0
- data/lib/daedal/queries/match_all_query.rb +12 -0
- data/lib/daedal/queries/match_query.rb +33 -0
- data/lib/daedal/queries/multi_match_query.rb +43 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/unit/daedal/filters/term_filter_spec.rb +55 -0
- data/spec/unit/daedal/queries/bool_query_spec.rb +214 -0
- data/spec/unit/daedal/queries/constant_score_query_spec.rb +72 -0
- data/spec/unit/daedal/queries/dis_max_query_spec.rb +151 -0
- data/spec/unit/daedal/queries/filtered_query_spec.rb +60 -0
- data/spec/unit/daedal/queries/match_all_query_spec.rb +19 -0
- data/spec/unit/daedal/queries/match_query_spec.rb +245 -0
- data/spec/unit/daedal/queries/multi_match_query_spec.rb +253 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ee41dfa3c33abb94b45a246a59dd12b9ed45a278
|
4
|
+
data.tar.gz: 4ec21b922bb6b1e9fcd5cb94f6effad2159d8d70
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0676d06f03db74a4cf9fe0fa016f0d7197d75fc1a1c2acb21780e5b22191ff9a487f9022580a412943b62f968b8d19f9264b3a3ece0cc719475e5a70a74c47d2
|
7
|
+
data.tar.gz: c7a4f7ece2fa6d1aebc00fdad3b095ff22b529a84aba9440124b6d5b607da38ce62c117cad8905a1b476fb8cfdee085e94dc27a3e9b86f09c446224f7279c0b8
|
data/.gitignore
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
2
|
+
#
|
3
|
+
# If you find yourself ignoring temporary files generated by your text editor
|
4
|
+
# or operating system, you probably want to add a global ignore instead:
|
5
|
+
# git config --global core.excludesfile '~/.gitignore_global'
|
6
|
+
|
7
|
+
# Ignore bundler config.
|
8
|
+
/.bundle
|
9
|
+
.rvmrc
|
10
|
+
|
11
|
+
# Ignore all logfiles and tempfiles.
|
12
|
+
/log/*.log
|
13
|
+
/tmp
|
14
|
+
*.swp
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
axiom-types (0.0.5)
|
5
|
+
descendants_tracker (~> 0.0.1)
|
6
|
+
ice_nine (~> 0.9)
|
7
|
+
backports (3.3.5)
|
8
|
+
coderay (1.0.9)
|
9
|
+
coercible (0.2.0)
|
10
|
+
backports (~> 3.0, >= 3.1.0)
|
11
|
+
descendants_tracker (~> 0.0.1)
|
12
|
+
columnize (0.3.6)
|
13
|
+
debugger (1.6.2)
|
14
|
+
columnize (>= 0.3.1)
|
15
|
+
debugger-linecache (~> 1.2.0)
|
16
|
+
debugger-ruby_core_source (~> 1.2.3)
|
17
|
+
debugger-linecache (1.2.0)
|
18
|
+
debugger-ruby_core_source (1.2.3)
|
19
|
+
descendants_tracker (0.0.3)
|
20
|
+
diff-lcs (1.2.4)
|
21
|
+
equalizer (0.0.8)
|
22
|
+
ffi (1.9.0)
|
23
|
+
formatador (0.2.4)
|
24
|
+
fuubar (1.2.1)
|
25
|
+
rspec (~> 2.0)
|
26
|
+
rspec-instafail (~> 0.2.0)
|
27
|
+
ruby-progressbar (~> 1.0)
|
28
|
+
guard (1.8.3)
|
29
|
+
formatador (>= 0.2.4)
|
30
|
+
listen (~> 1.3)
|
31
|
+
lumberjack (>= 1.0.2)
|
32
|
+
pry (>= 0.9.10)
|
33
|
+
thor (>= 0.14.6)
|
34
|
+
guard-rspec (3.0.2)
|
35
|
+
guard (>= 1.8)
|
36
|
+
rspec (~> 2.13)
|
37
|
+
ice_nine (0.10.0)
|
38
|
+
listen (1.3.1)
|
39
|
+
rb-fsevent (>= 0.9.3)
|
40
|
+
rb-inotify (>= 0.9)
|
41
|
+
rb-kqueue (>= 0.2)
|
42
|
+
lumberjack (1.0.4)
|
43
|
+
method_source (0.8.2)
|
44
|
+
pry (0.9.12.2)
|
45
|
+
coderay (~> 1.0.5)
|
46
|
+
method_source (~> 0.8)
|
47
|
+
slop (~> 3.4)
|
48
|
+
rb-fsevent (0.9.3)
|
49
|
+
rb-inotify (0.9.2)
|
50
|
+
ffi (>= 0.5.0)
|
51
|
+
rb-kqueue (0.2.0)
|
52
|
+
ffi (>= 0.5.0)
|
53
|
+
require_all (1.3.1)
|
54
|
+
rspec (2.14.1)
|
55
|
+
rspec-core (~> 2.14.0)
|
56
|
+
rspec-expectations (~> 2.14.0)
|
57
|
+
rspec-mocks (~> 2.14.0)
|
58
|
+
rspec-core (2.14.5)
|
59
|
+
rspec-expectations (2.14.2)
|
60
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
61
|
+
rspec-instafail (0.2.4)
|
62
|
+
rspec-mocks (2.14.3)
|
63
|
+
ruby-progressbar (1.2.0)
|
64
|
+
slop (3.4.6)
|
65
|
+
thor (0.18.1)
|
66
|
+
virtus (1.0.0)
|
67
|
+
axiom-types (~> 0.0.5)
|
68
|
+
coercible (~> 0.2)
|
69
|
+
descendants_tracker (~> 0.0.1)
|
70
|
+
equalizer (~> 0.0.7)
|
71
|
+
|
72
|
+
PLATFORMS
|
73
|
+
ruby
|
74
|
+
|
75
|
+
DEPENDENCIES
|
76
|
+
debugger
|
77
|
+
fuubar
|
78
|
+
guard
|
79
|
+
guard-rspec
|
80
|
+
require_all
|
81
|
+
rspec
|
82
|
+
virtus (= 1.0.0)
|
data/Guardfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 cschuch
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
data/daedal.gemspec
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'daedal'
|
3
|
+
s.version = '0.0.2'
|
4
|
+
s.date = '2013-12-06'
|
5
|
+
s.summary = "ElasticSearch Query DSL Builders"
|
6
|
+
s.description = "Classes for easier ElasticSearch query creation"
|
7
|
+
s.authors = ["Christopher Schuch"]
|
8
|
+
s.email = 'cas13091@gmail.com'
|
9
|
+
s.files = `git ls-files`.split("\n")
|
10
|
+
s.require_paths = ['lib']
|
11
|
+
s.license = 'MIT'
|
12
|
+
s.homepage =
|
13
|
+
'https://github.com/cschuch/daedal'
|
14
|
+
end
|
data/lib/daedal.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'virtus'
|
2
|
+
require 'daedal/queries/base_query'
|
3
|
+
require 'daedal/filters/base_filter'
|
4
|
+
|
5
|
+
module Daedal
|
6
|
+
module Attributes
|
7
|
+
|
8
|
+
"""Custom coercer for a query, allowing nil"""
|
9
|
+
class QueryOrNil < Virtus::Attribute
|
10
|
+
def coerce(value)
|
11
|
+
unless value.is_a? Daedal::Queries::BaseQuery or value.nil?
|
12
|
+
raise "Invalid query"
|
13
|
+
end
|
14
|
+
value
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
"""Custom coercer for a filter, allowing nil"""
|
19
|
+
class FilterOrNil < Virtus::Attribute
|
20
|
+
def coerce(value)
|
21
|
+
unless value.is_a? Daedal::Filters::BaseFilter or value.nil?
|
22
|
+
raise "Invalid filter"
|
23
|
+
end
|
24
|
+
value
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
"""Custom coercer for a query"""
|
29
|
+
class Query < Virtus::Attribute
|
30
|
+
def coerce(value)
|
31
|
+
unless value.is_a? Daedal::Queries::BaseQuery
|
32
|
+
raise "Must give a query"
|
33
|
+
end
|
34
|
+
value
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
"""Custom coercer for a filter"""
|
39
|
+
class Filter < Virtus::Attribute
|
40
|
+
def coerce(value)
|
41
|
+
unless value.is_a? Daedal::Filters::BaseFilter
|
42
|
+
raise "Must give a filter"
|
43
|
+
end
|
44
|
+
value
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
"""Custom coercer for an array of queries"""
|
49
|
+
class QueryArray < Virtus::Attribute
|
50
|
+
def coerce(value)
|
51
|
+
unless value.is_a? Array
|
52
|
+
value = [value]
|
53
|
+
end
|
54
|
+
|
55
|
+
value.each do |q|
|
56
|
+
unless q.is_a? Daedal::Queries::BaseQuery
|
57
|
+
raise "Must give an array of queries"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
value
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
"""Custom coercer for the operator attribute"""
|
66
|
+
class Operator < Virtus::Attribute
|
67
|
+
ALLOWED_MATCH_OPERATORS = [:or, :and]
|
68
|
+
def coerce(value)
|
69
|
+
unless value.nil?
|
70
|
+
value = value.to_sym
|
71
|
+
unless ALLOWED_MATCH_OPERATORS.include? value
|
72
|
+
raise "#{value} is not a valid operator. Allowed values are #{ALLOWED_MATCH_OPERATORS}."
|
73
|
+
end
|
74
|
+
end
|
75
|
+
value
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
"""Custom coercer for the type attribute"""
|
80
|
+
class MatchType < Virtus::Attribute
|
81
|
+
ALLOWED_MATCH_TYPES = [:phrase, :phrase_prefix]
|
82
|
+
def coerce(value)
|
83
|
+
unless value.nil?
|
84
|
+
value = value.to_sym
|
85
|
+
unless ALLOWED_MATCH_TYPES.include? value
|
86
|
+
raise "#{value} is not a valid type. Allowed values are #{ALLOWED_MATCH_TYPES}."
|
87
|
+
end
|
88
|
+
end
|
89
|
+
value
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'daedal/filters/base_filter'
|
2
|
+
require 'daedal/attributes'
|
3
|
+
|
4
|
+
module Daedal
|
5
|
+
module Filters
|
6
|
+
"""Class for the basic match query"""
|
7
|
+
class TermFilter < BaseFilter
|
8
|
+
|
9
|
+
# required attributes
|
10
|
+
attribute :field, Symbol
|
11
|
+
attribute :term, Symbol
|
12
|
+
|
13
|
+
def to_hash
|
14
|
+
{term: {field => term}}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'daedal/queries/base_query'
|
2
|
+
require 'daedal/queries/bool_query'
|
3
|
+
require 'daedal/queries/constant_score_query'
|
4
|
+
require 'daedal/queries/dis_max_query'
|
5
|
+
require 'daedal/queries/filtered_query'
|
6
|
+
require 'daedal/queries/match_all_query'
|
7
|
+
require 'daedal/queries/match_query'
|
8
|
+
require 'daedal/queries/multi_match_query'
|
9
|
+
|
10
|
+
module Daedal
|
11
|
+
module Queries
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'daedal/queries/base_query'
|
2
|
+
require 'daedal/attributes'
|
3
|
+
|
4
|
+
module Daedal
|
5
|
+
module Queries
|
6
|
+
"""Class for the basic match query"""
|
7
|
+
class BoolQuery < BaseQuery
|
8
|
+
|
9
|
+
# required attributes
|
10
|
+
attribute :should, Attributes::QueryArray, default: Array.new
|
11
|
+
attribute :must, Attributes::QueryArray, default: Array.new
|
12
|
+
attribute :must_not, Attributes::QueryArray, default: Array.new
|
13
|
+
|
14
|
+
# non required attributes
|
15
|
+
attribute :minimum_should_match, Integer, required: false
|
16
|
+
attribute :boost, Integer, required: false
|
17
|
+
|
18
|
+
def verify_query(q)
|
19
|
+
unless q.is_a? Daedal::Queries::BaseQuery
|
20
|
+
raise "Must give a valid query"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_should_query(q)
|
25
|
+
verify_query(q)
|
26
|
+
should << q
|
27
|
+
end
|
28
|
+
|
29
|
+
def add_must_query(q)
|
30
|
+
verify_query(q)
|
31
|
+
must << q
|
32
|
+
end
|
33
|
+
|
34
|
+
def add_must_not_query(q)
|
35
|
+
verify_query(q)
|
36
|
+
must_not << q
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_hash
|
40
|
+
result = {bool: {should: should.map {|q| q.to_hash}, must: must.map {|q| q.to_hash}, must_not: must_not.map {|q| q.to_hash}}}
|
41
|
+
options = {minimum_should_match: minimum_should_match, boost: boost}
|
42
|
+
result[:bool].merge!(options.select { |k,v| !v.nil? })
|
43
|
+
|
44
|
+
result
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'daedal/queries/base_query'
|
2
|
+
require 'daedal/attributes'
|
3
|
+
|
4
|
+
module Daedal
|
5
|
+
module Queries
|
6
|
+
"""Class for the basic match query"""
|
7
|
+
class ConstantScoreQuery < BaseQuery
|
8
|
+
|
9
|
+
# required attributes
|
10
|
+
attribute :boost, Float
|
11
|
+
|
12
|
+
# non required attributes, but one must be required of the two
|
13
|
+
attribute :query, Attributes::QueryOrNil
|
14
|
+
attribute :filter, Attributes::FilterOrNil
|
15
|
+
|
16
|
+
def initialize(options={})
|
17
|
+
super options
|
18
|
+
if query.nil? && filter.nil?
|
19
|
+
raise "Must give a query or a filter"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_hash
|
24
|
+
result = {constant_score: {boost: boost}}
|
25
|
+
if !query.nil?
|
26
|
+
result[:constant_score][:query] = query.to_hash
|
27
|
+
elsif !filter.nil?
|
28
|
+
result[:constant_score][:filter] = filter.to_hash
|
29
|
+
end
|
30
|
+
|
31
|
+
result
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'daedal/queries/base_query'
|
2
|
+
require 'daedal/attributes'
|
3
|
+
|
4
|
+
module Daedal
|
5
|
+
module Queries
|
6
|
+
|
7
|
+
"""Class for the basic match query"""
|
8
|
+
class DisMaxQuery < BaseQuery
|
9
|
+
|
10
|
+
# required attributes
|
11
|
+
attribute :queries, Attributes::QueryArray, default: Array.new
|
12
|
+
|
13
|
+
# non required attributes
|
14
|
+
attribute :tie_breaker, Float, required: false
|
15
|
+
attribute :boost, Integer, required: false
|
16
|
+
|
17
|
+
def verify_query(q)
|
18
|
+
unless q.is_a? Daedal::Queries::BaseQuery
|
19
|
+
raise "Must give a valid query"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_query(q)
|
24
|
+
verify_query(q)
|
25
|
+
queries << q
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_hash
|
29
|
+
result = {dis_max: {queries: queries.map {|q| q.to_hash }}}
|
30
|
+
options = {tie_breaker: tie_breaker, boost: boost}
|
31
|
+
result[:dis_max].merge!(options.select { |k,v| !v.nil? })
|
32
|
+
|
33
|
+
result
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|