daedal 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +8 -1
- data/.travis.yml +7 -0
- data/Gemfile +9 -7
- data/Gemfile.lock +47 -32
- data/README.md +115 -56
- data/daedal.gemspec +0 -1
- data/lib/daedal.rb +41 -5
- data/lib/daedal/attributes/boost.rb +7 -0
- data/lib/daedal/attributes/facet.rb +13 -0
- data/lib/daedal/attributes/field.rb +7 -0
- data/lib/daedal/attributes/filter.rb +2 -2
- data/lib/daedal/attributes/filter_array.rb +4 -4
- data/lib/daedal/attributes/query.rb +2 -2
- data/lib/daedal/attributes/query_array.rb +2 -2
- data/lib/daedal/attributes/query_value.rb +19 -0
- data/lib/daedal/facets/facet.rb +15 -0
- data/lib/daedal/filters/and_filter.rb +2 -5
- data/lib/daedal/filters/bool_filter.rb +15 -0
- data/lib/daedal/filters/{base_filter.rb → filter.rb} +1 -3
- data/lib/daedal/filters/geo_distance_filter.rb +6 -9
- data/lib/daedal/filters/or_filter.rb +18 -0
- data/lib/daedal/filters/range_filter.rb +8 -8
- data/lib/daedal/filters/term_filter.rb +3 -6
- data/lib/daedal/filters/terms_filter.rb +3 -6
- data/lib/daedal/queries/bool_query.rb +8 -11
- data/lib/daedal/queries/constant_score_query.rb +4 -8
- data/lib/daedal/queries/dis_max_query.rb +5 -9
- data/lib/daedal/queries/filtered_query.rb +3 -8
- data/lib/daedal/queries/fuzzy_query.rb +24 -0
- data/lib/daedal/queries/match_all_query.rb +1 -3
- data/lib/daedal/queries/match_query.rb +11 -15
- data/lib/daedal/queries/multi_match_query.rb +12 -16
- data/lib/daedal/queries/nested_query.rb +7 -10
- data/lib/daedal/queries/prefix_query.rb +7 -9
- data/lib/daedal/queries/{base_query.rb → query.rb} +1 -4
- data/lib/daedal/queries/query_string_query.rb +32 -0
- data/lib/daedal/version.rb +1 -1
- data/spec/spec_helper.rb +4 -2
- data/spec/unit/daedal/filters/and_filter_spec.rb +0 -1
- data/spec/unit/daedal/filters/bool_filter_spec.rb +89 -0
- data/spec/unit/daedal/filters/geo_distance_filter_spec.rb +0 -1
- data/spec/unit/daedal/filters/or_filter_spec.rb +74 -0
- data/spec/unit/daedal/filters/range_filter_spec.rb +0 -1
- data/spec/unit/daedal/filters/term_filter_spec.rb +0 -1
- data/spec/unit/daedal/filters/terms_filter_spec.rb +4 -2
- data/spec/unit/daedal/queries/bool_query_spec.rb +1 -3
- data/spec/unit/daedal/queries/constant_score_query_spec.rb +0 -2
- data/spec/unit/daedal/queries/dis_max_query_spec.rb +1 -2
- data/spec/unit/daedal/queries/filtered_query_spec.rb +1 -3
- data/spec/unit/daedal/queries/fuzzy_query_spec.rb +89 -0
- data/spec/unit/daedal/queries/match_all_query_spec.rb +0 -1
- data/spec/unit/daedal/queries/match_query_spec.rb +2 -3
- data/spec/unit/daedal/queries/multi_match_query_spec.rb +2 -3
- data/spec/unit/daedal/queries/nested_query_spec.rb +0 -2
- data/spec/unit/daedal/queries/prefix_query_spec.rb +0 -1
- data/spec/unit/daedal/queries/query_string_spec.rb +296 -0
- metadata +18 -7
- data/lib/daedal/attributes.rb +0 -15
- data/lib/daedal/filters.rb +0 -11
- data/lib/daedal/queries.rb +0 -15
data/daedal.gemspec
CHANGED
@@ -3,7 +3,6 @@ require File.expand_path('../lib/daedal/version', __FILE__)
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = 'daedal'
|
5
5
|
s.version = Daedal::VERSION
|
6
|
-
s.date = '2013-12-06'
|
7
6
|
s.summary = "ElasticSearch Query DSL Builders"
|
8
7
|
s.description = "Classes for easier ElasticSearch query creation"
|
9
8
|
s.authors = ["Christopher Schuch"]
|
data/lib/daedal.rb
CHANGED
@@ -1,9 +1,45 @@
|
|
1
1
|
require 'json'
|
2
|
+
require 'virtus'
|
2
3
|
|
3
|
-
|
4
|
-
require 'daedal/
|
5
|
-
require 'daedal/
|
4
|
+
# base query and filter classes needed by the attributes
|
5
|
+
require 'daedal/queries/query'
|
6
|
+
require 'daedal/filters/filter'
|
7
|
+
require 'daedal/facets/facet'
|
6
8
|
|
7
|
-
|
9
|
+
# attributes
|
10
|
+
require 'daedal/attributes/operator'
|
11
|
+
require 'daedal/attributes/match_type'
|
12
|
+
require 'daedal/attributes/query_array'
|
13
|
+
require 'daedal/attributes/filter_array'
|
14
|
+
require 'daedal/attributes/score_mode'
|
15
|
+
require 'daedal/attributes/lower_case_string'
|
16
|
+
require 'daedal/attributes/distance_unit'
|
17
|
+
require 'daedal/attributes/query'
|
18
|
+
require 'daedal/attributes/filter'
|
19
|
+
require 'daedal/attributes/field'
|
20
|
+
require 'daedal/attributes/query_value'
|
21
|
+
require 'daedal/attributes/boost'
|
8
22
|
|
9
|
-
|
23
|
+
# filters
|
24
|
+
require 'daedal/filters/term_filter'
|
25
|
+
require 'daedal/filters/terms_filter'
|
26
|
+
require 'daedal/filters/range_filter'
|
27
|
+
require 'daedal/filters/geo_distance_filter'
|
28
|
+
require 'daedal/filters/and_filter'
|
29
|
+
require 'daedal/filters/or_filter'
|
30
|
+
require 'daedal/filters/bool_filter'
|
31
|
+
|
32
|
+
# queries
|
33
|
+
require 'daedal/queries/match_all_query'
|
34
|
+
require 'daedal/queries/bool_query'
|
35
|
+
require 'daedal/queries/constant_score_query'
|
36
|
+
require 'daedal/queries/dis_max_query'
|
37
|
+
require 'daedal/queries/filtered_query'
|
38
|
+
require 'daedal/queries/match_query'
|
39
|
+
require 'daedal/queries/multi_match_query'
|
40
|
+
require 'daedal/queries/nested_query'
|
41
|
+
require 'daedal/queries/prefix_query'
|
42
|
+
require 'daedal/queries/fuzzy_query'
|
43
|
+
require 'daedal/queries/query_string_query'
|
44
|
+
|
45
|
+
# facets
|
@@ -2,8 +2,8 @@ module Daedal
|
|
2
2
|
module Attributes
|
3
3
|
class Filter < Virtus::Attribute
|
4
4
|
def coerce(f)
|
5
|
-
unless f.is_a? Daedal::Filters::
|
6
|
-
raise Virtus::CoercionError.new(f, 'Daedal::Filters::
|
5
|
+
unless f.is_a? Daedal::Filters::Filter or !required? && f.nil?
|
6
|
+
raise Virtus::CoercionError.new(f, 'Daedal::Filters::Filter')
|
7
7
|
end
|
8
8
|
|
9
9
|
f
|
@@ -5,18 +5,18 @@ module Daedal
|
|
5
5
|
# override the << method so that you throw
|
6
6
|
# an error if you don't try to append a query
|
7
7
|
def <<(f)
|
8
|
-
if f.is_a? Daedal::Filters::
|
8
|
+
if f.is_a? Daedal::Filters::Filter
|
9
9
|
super f
|
10
10
|
else
|
11
|
-
raise Virtus::CoercionError.new(f, 'Daedal::Filters::
|
11
|
+
raise Virtus::CoercionError.new(f, 'Daedal::Filters::Filter')
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
def unshift(f)
|
16
|
-
if f.is_a? Daedal::Filters::
|
16
|
+
if f.is_a? Daedal::Filters::Filter
|
17
17
|
super f
|
18
18
|
else
|
19
|
-
raise Virtus::CoercionError.new(f, 'Daedal::Filters::
|
19
|
+
raise Virtus::CoercionError.new(f, 'Daedal::Filters::Filter')
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
@@ -2,8 +2,8 @@ module Daedal
|
|
2
2
|
module Attributes
|
3
3
|
class Query < Virtus::Attribute
|
4
4
|
def coerce(q)
|
5
|
-
unless q.is_a? Daedal::Queries::
|
6
|
-
raise Virtus::CoercionError.new(q, 'Daedal::Queries::
|
5
|
+
unless q.is_a? Daedal::Queries::Query or !required? && q.nil?
|
6
|
+
raise Virtus::CoercionError.new(q, 'Daedal::Queries::Query')
|
7
7
|
end
|
8
8
|
|
9
9
|
q
|
@@ -7,10 +7,10 @@ module Daedal
|
|
7
7
|
# an error if you don't try to append a query
|
8
8
|
|
9
9
|
def <<(q)
|
10
|
-
if q.is_a? Daedal::Queries::
|
10
|
+
if q.is_a? Daedal::Queries::Query
|
11
11
|
super q
|
12
12
|
else
|
13
|
-
raise Virtus::CoercionError.new(q, 'Daedal::Queries::
|
13
|
+
raise Virtus::CoercionError.new(q, 'Daedal::Queries::Query')
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Daedal
|
2
|
+
module Attributes
|
3
|
+
"""Custom coercer for the value of any query - can be
|
4
|
+
a string, a symbol, a float, or an integer. If it's none
|
5
|
+
of those, raises a coercion error."""
|
6
|
+
class QueryValue < Virtus::Attribute
|
7
|
+
ALLOWED_QUERY_VALUE_CLASSES = [String, Symbol, Float, Fixnum]
|
8
|
+
def coerce(q)
|
9
|
+
if !required? and q.nil?
|
10
|
+
return q
|
11
|
+
elsif ALLOWED_QUERY_VALUE_CLASSES.include? q.class
|
12
|
+
return q
|
13
|
+
else
|
14
|
+
raise Virtus::CoercionError.new(q, self.class)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Daedal
|
2
|
+
module Facets
|
3
|
+
class Facet
|
4
|
+
# Virtus coercion is set to strict so that errors
|
5
|
+
# are returned when supplied fields cannot be properly
|
6
|
+
# coerced.
|
7
|
+
include Virtus.model strict: true
|
8
|
+
|
9
|
+
# requires the subclasses to define #to_hash
|
10
|
+
def to_json
|
11
|
+
to_hash.to_json
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,13 +1,10 @@
|
|
1
|
-
require 'daedal/filters/base_filter'
|
2
|
-
require 'daedal/attributes'
|
3
|
-
|
4
1
|
module Daedal
|
5
2
|
module Filters
|
6
3
|
"""Class for the basic term filter"""
|
7
|
-
class AndFilter <
|
4
|
+
class AndFilter < Filter
|
8
5
|
|
9
6
|
# required attributes
|
10
|
-
attribute :filters, Attributes::FilterArray
|
7
|
+
attribute :filters, Daedal::Attributes::FilterArray
|
11
8
|
|
12
9
|
def to_hash
|
13
10
|
unless filters.empty?
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Daedal
|
2
|
+
module Filters
|
3
|
+
"""Class for the basic term filter"""
|
4
|
+
class BoolFilter < Filter
|
5
|
+
|
6
|
+
attribute :should, Daedal::Attributes::FilterArray
|
7
|
+
attribute :must, Daedal::Attributes::FilterArray
|
8
|
+
attribute :must_not, Daedal::Attributes::FilterArray
|
9
|
+
|
10
|
+
def to_hash
|
11
|
+
{bool: {should: should.map {|f| f.to_hash}, must: must.map {|f| f.to_hash}, must_not: must_not.map {|f| f.to_hash}}}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,19 +1,16 @@
|
|
1
|
-
require 'daedal/filters/base_filter'
|
2
|
-
require 'daedal/attributes'
|
3
|
-
|
4
1
|
module Daedal
|
5
2
|
module Filters
|
6
3
|
"""Class for the basic term filter"""
|
7
|
-
class GeoDistanceFilter <
|
4
|
+
class GeoDistanceFilter < Filter
|
8
5
|
|
9
6
|
# required attributes
|
10
|
-
attribute :field,
|
11
|
-
attribute :lat,
|
12
|
-
attribute :lon,
|
13
|
-
attribute :distance,
|
7
|
+
attribute :field, Daedal::Attributes::Field
|
8
|
+
attribute :lat, Float
|
9
|
+
attribute :lon, Float
|
10
|
+
attribute :distance, Float
|
14
11
|
|
15
12
|
# non required attributes
|
16
|
-
attribute :unit,
|
13
|
+
attribute :unit, Daedal::Attributes::DistanceUnit, default: 'km'
|
17
14
|
|
18
15
|
def full_distance
|
19
16
|
"#{distance}#{unit}"
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Daedal
|
2
|
+
module Filters
|
3
|
+
"""Class for the basic term filter"""
|
4
|
+
class OrFilter < Filter
|
5
|
+
|
6
|
+
# required attributes
|
7
|
+
attribute :filters, Daedal::Attributes::FilterArray
|
8
|
+
|
9
|
+
def to_hash
|
10
|
+
unless filters.empty?
|
11
|
+
{:or => filters.map {|f| f.to_hash}}
|
12
|
+
else
|
13
|
+
super
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -1,16 +1,16 @@
|
|
1
|
-
require 'daedal/filters/base_filter'
|
2
|
-
|
3
1
|
module Daedal
|
4
2
|
module Filters
|
5
3
|
"""Class for the basic term filter"""
|
6
|
-
class RangeFilter <
|
4
|
+
class RangeFilter < Filter
|
7
5
|
|
8
6
|
# required attributes
|
9
|
-
attribute :field,
|
10
|
-
|
11
|
-
|
12
|
-
attribute :
|
13
|
-
attribute :
|
7
|
+
attribute :field, Daedal::Attributes::Field
|
8
|
+
|
9
|
+
# non required attributes
|
10
|
+
attribute :gte, Daedal::Attributes::QueryValue, required: false
|
11
|
+
attribute :lte, Daedal::Attributes::QueryValue, required: false
|
12
|
+
attribute :gt, Daedal::Attributes::QueryValue, required: false
|
13
|
+
attribute :lt, Daedal::Attributes::QueryValue, required: false
|
14
14
|
|
15
15
|
def initialize(options={})
|
16
16
|
super options
|
@@ -1,14 +1,11 @@
|
|
1
|
-
require 'daedal/filters/base_filter'
|
2
|
-
require 'daedal/attributes'
|
3
|
-
|
4
1
|
module Daedal
|
5
2
|
module Filters
|
6
3
|
"""Class for the basic term filter"""
|
7
|
-
class TermFilter <
|
4
|
+
class TermFilter < Filter
|
8
5
|
|
9
6
|
# required attributes
|
10
|
-
attribute :field,
|
11
|
-
attribute :term,
|
7
|
+
attribute :field, Daedal::Attributes::Field
|
8
|
+
attribute :term, Daedal::Attributes::QueryValue
|
12
9
|
|
13
10
|
def to_hash
|
14
11
|
{term: {field => term}}
|
@@ -1,14 +1,11 @@
|
|
1
|
-
require 'daedal/filters/base_filter'
|
2
|
-
require 'daedal/attributes'
|
3
|
-
|
4
1
|
module Daedal
|
5
2
|
module Filters
|
6
3
|
"""Class for the basic term filter"""
|
7
|
-
class TermsFilter <
|
4
|
+
class TermsFilter < Filter
|
8
5
|
|
9
6
|
# required attributes
|
10
|
-
attribute :field,
|
11
|
-
attribute :terms,
|
7
|
+
attribute :field, Daedal::Attributes::Field
|
8
|
+
attribute :terms, Array[Daedal::Attributes::QueryValue]
|
12
9
|
|
13
10
|
def to_hash
|
14
11
|
{terms: {field => terms}}
|
@@ -1,24 +1,21 @@
|
|
1
|
-
require 'daedal/queries/base_query'
|
2
|
-
require 'daedal/attributes'
|
3
|
-
|
4
1
|
module Daedal
|
5
2
|
module Queries
|
6
3
|
"""Class for the bool query"""
|
7
|
-
class BoolQuery <
|
4
|
+
class BoolQuery < Query
|
8
5
|
|
9
6
|
# required attributes
|
10
7
|
|
11
8
|
# should, must, and must_not must be an array of queries
|
12
9
|
# these queries must inherit from the BaseQuery class
|
13
|
-
attribute :should,
|
14
|
-
attribute :must,
|
15
|
-
attribute :must_not,
|
10
|
+
attribute :should, Daedal::Attributes::QueryArray
|
11
|
+
attribute :must, Daedal::Attributes::QueryArray
|
12
|
+
attribute :must_not, Daedal::Attributes::QueryArray
|
16
13
|
|
17
14
|
# non required attributes
|
18
|
-
attribute :minimum_should_match,
|
19
|
-
attribute :boost,
|
20
|
-
attribute :name,
|
21
|
-
attribute :disable_coord,
|
15
|
+
attribute :minimum_should_match, Integer, required: false
|
16
|
+
attribute :boost, Daedal::Attributes::Boost, required: false
|
17
|
+
attribute :name, Symbol, required: false
|
18
|
+
attribute :disable_coord, Boolean, required: false
|
22
19
|
|
23
20
|
def to_hash
|
24
21
|
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}}}
|
@@ -1,18 +1,14 @@
|
|
1
|
-
require 'daedal/queries/base_query'
|
2
|
-
require 'daedal/filters/base_filter'
|
3
|
-
require 'daedal/attributes'
|
4
|
-
|
5
1
|
module Daedal
|
6
2
|
module Queries
|
7
3
|
"""Class for the constant score query"""
|
8
|
-
class ConstantScoreQuery <
|
4
|
+
class ConstantScoreQuery < Query
|
9
5
|
|
10
6
|
# required attributes
|
11
|
-
attribute :boost,
|
7
|
+
attribute :boost, Daedal::Attributes::Boost
|
12
8
|
|
13
9
|
# non required attributes, but one must be required of the two
|
14
|
-
attribute :query,
|
15
|
-
attribute :filter,
|
10
|
+
attribute :query, Daedal::Attributes::Query, required: false
|
11
|
+
attribute :filter, Daedal::Attributes::Filter, required: false
|
16
12
|
|
17
13
|
# you must require *one of* query or filter in order for this to be valid
|
18
14
|
def initialize(options={})
|
@@ -1,19 +1,15 @@
|
|
1
|
-
require 'daedal/queries/base_query'
|
2
|
-
require 'daedal/attributes'
|
3
|
-
|
4
1
|
module Daedal
|
5
2
|
module Queries
|
6
|
-
|
7
3
|
"""Class for the dis max query"""
|
8
|
-
class DisMaxQuery <
|
4
|
+
class DisMaxQuery < Query
|
9
5
|
|
10
6
|
# required attributes
|
11
|
-
attribute :queries,
|
7
|
+
attribute :queries, Daedal::Attributes::QueryArray
|
12
8
|
|
13
9
|
# non required attributes
|
14
|
-
attribute :tie_breaker,
|
15
|
-
attribute :boost,
|
16
|
-
attribute :name,
|
10
|
+
attribute :tie_breaker, Float, required: false
|
11
|
+
attribute :boost, Daedal::Attributes::Boost, required: false
|
12
|
+
attribute :name, Symbol, required: false
|
17
13
|
|
18
14
|
def to_hash
|
19
15
|
result = {dis_max: {queries: queries.map {|q| q.to_hash }}}
|
@@ -1,16 +1,11 @@
|
|
1
|
-
require 'daedal/queries/match_all_query'
|
2
|
-
require 'daedal/filters/base_filter'
|
3
|
-
require 'daedal/queries/base_query'
|
4
|
-
require 'daedal/attributes'
|
5
|
-
|
6
1
|
module Daedal
|
7
2
|
module Queries
|
8
3
|
"""Class for the filtered query"""
|
9
|
-
class FilteredQuery <
|
4
|
+
class FilteredQuery < Query
|
10
5
|
|
11
6
|
# required attributes
|
12
|
-
attribute :query,
|
13
|
-
attribute :filter,
|
7
|
+
attribute :query, Daedal::Attributes::Query, default: Daedal::Queries::MatchAllQuery.new
|
8
|
+
attribute :filter, Daedal::Attributes::Filter, default: Daedal::Filters::Filter.new
|
14
9
|
|
15
10
|
def to_hash
|
16
11
|
{filtered: {query: query.to_hash, filter: filter.to_hash}}
|