daedal 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +8 -1
  3. data/.travis.yml +7 -0
  4. data/Gemfile +9 -7
  5. data/Gemfile.lock +47 -32
  6. data/README.md +115 -56
  7. data/daedal.gemspec +0 -1
  8. data/lib/daedal.rb +41 -5
  9. data/lib/daedal/attributes/boost.rb +7 -0
  10. data/lib/daedal/attributes/facet.rb +13 -0
  11. data/lib/daedal/attributes/field.rb +7 -0
  12. data/lib/daedal/attributes/filter.rb +2 -2
  13. data/lib/daedal/attributes/filter_array.rb +4 -4
  14. data/lib/daedal/attributes/query.rb +2 -2
  15. data/lib/daedal/attributes/query_array.rb +2 -2
  16. data/lib/daedal/attributes/query_value.rb +19 -0
  17. data/lib/daedal/facets/facet.rb +15 -0
  18. data/lib/daedal/filters/and_filter.rb +2 -5
  19. data/lib/daedal/filters/bool_filter.rb +15 -0
  20. data/lib/daedal/filters/{base_filter.rb → filter.rb} +1 -3
  21. data/lib/daedal/filters/geo_distance_filter.rb +6 -9
  22. data/lib/daedal/filters/or_filter.rb +18 -0
  23. data/lib/daedal/filters/range_filter.rb +8 -8
  24. data/lib/daedal/filters/term_filter.rb +3 -6
  25. data/lib/daedal/filters/terms_filter.rb +3 -6
  26. data/lib/daedal/queries/bool_query.rb +8 -11
  27. data/lib/daedal/queries/constant_score_query.rb +4 -8
  28. data/lib/daedal/queries/dis_max_query.rb +5 -9
  29. data/lib/daedal/queries/filtered_query.rb +3 -8
  30. data/lib/daedal/queries/fuzzy_query.rb +24 -0
  31. data/lib/daedal/queries/match_all_query.rb +1 -3
  32. data/lib/daedal/queries/match_query.rb +11 -15
  33. data/lib/daedal/queries/multi_match_query.rb +12 -16
  34. data/lib/daedal/queries/nested_query.rb +7 -10
  35. data/lib/daedal/queries/prefix_query.rb +7 -9
  36. data/lib/daedal/queries/{base_query.rb → query.rb} +1 -4
  37. data/lib/daedal/queries/query_string_query.rb +32 -0
  38. data/lib/daedal/version.rb +1 -1
  39. data/spec/spec_helper.rb +4 -2
  40. data/spec/unit/daedal/filters/and_filter_spec.rb +0 -1
  41. data/spec/unit/daedal/filters/bool_filter_spec.rb +89 -0
  42. data/spec/unit/daedal/filters/geo_distance_filter_spec.rb +0 -1
  43. data/spec/unit/daedal/filters/or_filter_spec.rb +74 -0
  44. data/spec/unit/daedal/filters/range_filter_spec.rb +0 -1
  45. data/spec/unit/daedal/filters/term_filter_spec.rb +0 -1
  46. data/spec/unit/daedal/filters/terms_filter_spec.rb +4 -2
  47. data/spec/unit/daedal/queries/bool_query_spec.rb +1 -3
  48. data/spec/unit/daedal/queries/constant_score_query_spec.rb +0 -2
  49. data/spec/unit/daedal/queries/dis_max_query_spec.rb +1 -2
  50. data/spec/unit/daedal/queries/filtered_query_spec.rb +1 -3
  51. data/spec/unit/daedal/queries/fuzzy_query_spec.rb +89 -0
  52. data/spec/unit/daedal/queries/match_all_query_spec.rb +0 -1
  53. data/spec/unit/daedal/queries/match_query_spec.rb +2 -3
  54. data/spec/unit/daedal/queries/multi_match_query_spec.rb +2 -3
  55. data/spec/unit/daedal/queries/nested_query_spec.rb +0 -2
  56. data/spec/unit/daedal/queries/prefix_query_spec.rb +0 -1
  57. data/spec/unit/daedal/queries/query_string_spec.rb +296 -0
  58. metadata +18 -7
  59. data/lib/daedal/attributes.rb +0 -15
  60. data/lib/daedal/filters.rb +0 -11
  61. data/lib/daedal/queries.rb +0 -15
@@ -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"]
@@ -1,9 +1,45 @@
1
1
  require 'json'
2
+ require 'virtus'
2
3
 
3
- require 'daedal/queries'
4
- require 'daedal/filters'
5
- require 'daedal/attributes'
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
- module Daedal
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
- end
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
@@ -0,0 +1,7 @@
1
+ module Daedal
2
+ module Attributes
3
+ """Attribute for the boost parameter"""
4
+ class Boost < Float
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ module Daedal
2
+ module Attributes
3
+ class Facet < Virtus::Attribute
4
+ def coerce(f)
5
+ unless f.is_a? Daedal::Facets::Facet or !required? && f.nil?
6
+ raise Virtus::CoercionError.new(f, 'Daedal::Facets::Facet')
7
+ end
8
+
9
+ f
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ module Daedal
2
+ module Attributes
3
+ """Attribute for any ElasticSearch document field, currently just a Symbol"""
4
+ class Field < Symbol
5
+ end
6
+ end
7
+ end
@@ -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::BaseFilter or !required? && f.nil?
6
- raise Virtus::CoercionError.new(f, 'Daedal::Filters::BaseFilter')
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::BaseFilter
8
+ if f.is_a? Daedal::Filters::Filter
9
9
  super f
10
10
  else
11
- raise Virtus::CoercionError.new(f, 'Daedal::Filters::BaseFilter')
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::BaseFilter
16
+ if f.is_a? Daedal::Filters::Filter
17
17
  super f
18
18
  else
19
- raise Virtus::CoercionError.new(f, 'Daedal::Filters::BaseFilter')
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::BaseQuery or !required? && q.nil?
6
- raise Virtus::CoercionError.new(q, 'Daedal::Queries::BaseQuery')
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::BaseQuery
10
+ if q.is_a? Daedal::Queries::Query
11
11
  super q
12
12
  else
13
- raise Virtus::CoercionError.new(q, 'Daedal::Queries::BaseQuery')
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 < BaseFilter
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,8 +1,6 @@
1
- require 'virtus'
2
-
3
1
  module Daedal
4
2
  module Filters
5
- class BaseFilter
3
+ class Filter
6
4
  include Virtus.model strict: true
7
5
 
8
6
  def to_hash
@@ -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 < BaseFilter
4
+ class GeoDistanceFilter < Filter
8
5
 
9
6
  # required attributes
10
- attribute :field, Symbol
11
- attribute :lat, Float
12
- attribute :lon, Float
13
- attribute :distance, Float
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, Attributes::DistanceUnit, default: 'km'
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 < BaseFilter
4
+ class RangeFilter < Filter
7
5
 
8
6
  # required attributes
9
- attribute :field, Symbol
10
- attribute :gte
11
- attribute :lte
12
- attribute :gt
13
- attribute :lt
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 < BaseFilter
4
+ class TermFilter < Filter
8
5
 
9
6
  # required attributes
10
- attribute :field, Symbol
11
- attribute :term, Symbol
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 < BaseFilter
4
+ class TermsFilter < Filter
8
5
 
9
6
  # required attributes
10
- attribute :field, Symbol
11
- attribute :terms, Array[Symbol]
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 < BaseQuery
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, Attributes::QueryArray, default: Array.new
14
- attribute :must, Attributes::QueryArray, default: Array.new
15
- attribute :must_not, Attributes::QueryArray, default: Array.new
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, Integer, required: false
19
- attribute :boost, Integer, required: false
20
- attribute :name, Symbol, required: false
21
- attribute :disable_coord, Boolean, required: false
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 < BaseQuery
4
+ class ConstantScoreQuery < Query
9
5
 
10
6
  # required attributes
11
- attribute :boost, Float
7
+ attribute :boost, Daedal::Attributes::Boost
12
8
 
13
9
  # non required attributes, but one must be required of the two
14
- attribute :query, Attributes::Query, required: false
15
- attribute :filter, Attributes::Filter, required: false
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 < BaseQuery
4
+ class DisMaxQuery < Query
9
5
 
10
6
  # required attributes
11
- attribute :queries, Attributes::QueryArray, default: Array.new
7
+ attribute :queries, Daedal::Attributes::QueryArray
12
8
 
13
9
  # non required attributes
14
- attribute :tie_breaker, Float, required: false
15
- attribute :boost, Integer, required: false
16
- attribute :name, Symbol, required: false
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 < BaseQuery
4
+ class FilteredQuery < Query
10
5
 
11
6
  # required attributes
12
- attribute :query, Attributes::Query, default: Daedal::Queries::MatchAllQuery.new
13
- attribute :filter, Attributes::Filter, default: Daedal::Filters::BaseFilter.new
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}}