eson-dsl 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/MethodReference.md +53 -0
  2. data/README.md +59 -113
  3. data/Rakefile +2 -2
  4. data/eson-dsl.gemspec +2 -1
  5. data/examples/queries/custom_boost_factor.rb +9 -0
  6. data/examples/queries/custom_filters_score.rb +15 -0
  7. data/examples/queries/custom_filters_score_with_script.rb +15 -0
  8. data/examples/queries/filtered.rb +8 -0
  9. data/examples/queries/indices.rb +12 -0
  10. data/examples/queries/match.rb +7 -0
  11. data/examples/queries/multi_match.rb +5 -0
  12. data/examples/queries/text.rb +2 -0
  13. data/lib/compat.rb +15 -0
  14. data/lib/eson-dsl.rb +35 -0
  15. data/lib/eson/modules/query_plugin.rb +4 -2
  16. data/lib/eson/search/and.rb +1 -5
  17. data/lib/eson/search/base_query.rb +5 -8
  18. data/lib/eson/search/bool.rb +2 -5
  19. data/lib/eson/search/boosting.rb +1 -4
  20. data/lib/eson/search/custom_boost_factor.rb +19 -0
  21. data/lib/eson/search/custom_filters_score.rb +42 -0
  22. data/lib/eson/search/date_histogram.rb +0 -2
  23. data/lib/eson/search/facet.rb +10 -5
  24. data/lib/eson/search/facets.rb +2 -1
  25. data/lib/eson/search/field_based.rb +1 -1
  26. data/lib/eson/search/filter.rb +1 -0
  27. data/lib/eson/search/filtered.rb +15 -0
  28. data/lib/eson/search/geo_distance.rb +2 -5
  29. data/lib/eson/search/has_child.rb +3 -5
  30. data/lib/eson/search/ids.rb +2 -4
  31. data/lib/eson/search/indices.rb +48 -0
  32. data/lib/eson/search/match.rb +10 -0
  33. data/lib/eson/search/multi_match.rb +21 -0
  34. data/lib/eson/search/prefix.rb +2 -4
  35. data/lib/eson/search/query.rb +1 -0
  36. data/lib/eson/search/term.rb +2 -4
  37. data/lib/eson/search/terms.rb +3 -5
  38. data/lib/eson/search/top_children.rb +2 -4
  39. data/test/search/facets/scoped.json +1 -1
  40. data/test/search/filters/and.rb +7 -11
  41. data/test/search/filters/bool.json +1 -1
  42. data/test/search/filters/bool.rb +17 -22
  43. data/test/search/filters/geo_bounding_box_hash.json +1 -1
  44. data/test/search/filters/geo_distance_hash.json +1 -1
  45. data/test/search/filters/geo_distance_range_hash.json +1 -1
  46. data/test/search/filters/geo_polygon_hash.json +1 -1
  47. data/test/search/filters/has_child.rb +7 -11
  48. data/test/search/filters/ids.rb +7 -11
  49. data/test/search/filters/not.rb +5 -9
  50. data/test/search/filters/or.rb +7 -11
  51. data/test/search/filters/range.rb +6 -11
  52. data/test/search/filters/type.json +1 -1
  53. data/test/search/filters/type.rb +1 -1
  54. data/test/search/filters_test.rb +2 -2
  55. data/test/search/queries/custom_boost_factor.json +10 -0
  56. data/test/search/queries/custom_filters_score.json +21 -0
  57. data/test/search/queries/filtered.rb +1 -0
  58. data/test/search/queries/filters_and_facets.json +1 -1
  59. data/test/search/queries/indices.json +13 -0
  60. data/test/search/queries/match.json +1 -0
  61. data/test/search/queries/multi_match.json +8 -0
  62. data/test/search/query_test.rb +70 -15
  63. metadata +29 -9
  64. data/TODO.md +0 -1
@@ -11,7 +11,7 @@ module Eson
11
11
 
12
12
  def call(*args)
13
13
  if query.respond_to?(:to_query_hash)
14
- self.params = query.to_query_hash
14
+ self.parameters = query.to_query_hash
15
15
  end
16
16
 
17
17
  super(*args)
@@ -29,7 +29,9 @@ module Eson
29
29
  [protocol::Search,
30
30
  protocol::Count,
31
31
  protocol::DeleteByQuery,
32
- protocol::Percolate]
32
+ protocol::Percolate,
33
+ protocol::Explain,
34
+ protocol::Validate]
33
35
  end
34
36
 
35
37
  def handle_block(&block)
@@ -5,11 +5,7 @@ module Eson
5
5
  include FilterMethods
6
6
  include Filter
7
7
 
8
- ##
9
- # @macro and
10
- # @yield the block describing all subfilters
11
- # Generates a `$1` filter.
12
- # {include:file:eson-dsl/examples/filters/$1.rb}
8
+ # @!macro eson.filter
13
9
  short_name 'and'
14
10
 
15
11
  attr_accessor :options
@@ -18,14 +18,11 @@ module Eson
18
18
  end
19
19
 
20
20
  if filters && !filters.empty?
21
- hash = {
22
- :query => {
23
- :filtered => {
24
- :query => hash[:query],
25
- :filter => filters.to_query_hash
26
- }
27
- }
28
- }
21
+ if filters.length > 1
22
+ hash[:filter] = {:and => filter.to_query_hash}
23
+ else
24
+ hash[:filter] = filter.to_query_hash
25
+ end
29
26
  end
30
27
 
31
28
  if facets.length > 0
@@ -4,11 +4,8 @@ module Eson
4
4
  include Filter
5
5
  include Query
6
6
 
7
- # @macro bool
8
- # @yield the block containing all subqueries
9
- # Generates a `$1` filter or query, depending on context.
10
- # {include:file:eson-dsl/examples/filters/$1.rb}
11
- # {include:file:eson-dsl/examples/queries/$1.rb}
7
+ # @!macro eson.filter
8
+ # @!macro eson.query
12
9
  short_name :bool
13
10
 
14
11
  attr_accessor :options, :conditions
@@ -4,10 +4,7 @@ module Eson
4
4
  include Query
5
5
 
6
6
  ##
7
- # @macro eson.query
8
- # @yield the block describing all subqueries
9
- # Generates a `$1` query.
10
- # {include:file:eson-dsl/examples/queries/$1.rb}
7
+ # @!macro eson.query
11
8
  short_name :boosting
12
9
 
13
10
  attr_accessor :options, :conditions
@@ -0,0 +1,19 @@
1
+ module Eson
2
+ module Search
3
+ class CustomBoostFactor
4
+
5
+ include Queries
6
+ include Query
7
+
8
+ # @macro eson.query
9
+ short_name :custom_boost_factor
10
+
11
+ attr_accessor :options
12
+
13
+ def initialize(options = {})
14
+ self.options = options
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,42 @@
1
+ module Eson
2
+ module Search
3
+ class CustomFiltersScore
4
+ include Queries
5
+ include Query
6
+
7
+ # @!macro eson.query
8
+ short_name :custom_filters_score
9
+
10
+ attr_accessor :options, :filters
11
+
12
+ def initialize(options = {})
13
+ self.options = options
14
+ self.filters = []
15
+ end
16
+
17
+ def filters(&block)
18
+ instance_exec(self, &block)
19
+ end
20
+
21
+ def filter(options, &block)
22
+ sub = SubQuery.new(args)
23
+
24
+ @filters << [options, sub]
25
+
26
+ sub.send(context, &block)
27
+ end
28
+
29
+ def to_query_hash
30
+ result = {}
31
+
32
+ result[:query] = queries.to_query_hash
33
+ result[:filters] = @filters.map do |options, filter|
34
+ {:filter => filter.to_query_hash}.merge!(options)
35
+ end
36
+
37
+ {name => result.merge(options)}
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -4,8 +4,6 @@ module Eson
4
4
 
5
5
  ##
6
6
  # @macro eson.facet
7
- # Generates a `$1` facet.
8
- # {include:file:eson-dsl/examples/facets/$1.rb}
9
7
  short_name :date_histogram
10
8
 
11
9
  end
@@ -3,6 +3,7 @@ module Eson
3
3
  module Facet
4
4
  module ClassMethods
5
5
  def short_name(name)
6
+ name = name.to_sym
6
7
  define_method :name do
7
8
  name
8
9
  end
@@ -18,16 +19,20 @@ module Eson
18
19
  end
19
20
  end
20
21
 
21
- attr_accessor :args
22
+ attr_accessor :args, :scope_name
22
23
 
23
24
  def param(name)
24
25
  args[name] || (raise "Parameter #{name} not given")
25
26
  end
26
27
 
27
28
  def scope(query, name)
28
- raise "Cannot scope unless Query is a NestedQuery" unless Nested === query
29
- query.options[:_scope] = name
30
- self.options[:scope] = name
29
+ case query
30
+ when Nested, HasChild
31
+ query.options[:_scope] = name
32
+ self.scope_name = name
33
+ else
34
+ raise "Cannot scope unless Query is a Nested or HasChild Query"
35
+ end
31
36
  end
32
37
 
33
38
  def method_missing(name, arg)
@@ -36,4 +41,4 @@ module Eson
36
41
  end
37
42
  end
38
43
  end
39
- end
44
+ end
@@ -13,6 +13,7 @@ module Eson
13
13
  h = {}
14
14
  self.each do |k,v|
15
15
  h[k] = v.to_query_hash
16
+ h[k][:scope] = v.scope_name if v.scope_name
16
17
  end
17
18
  h
18
19
  end
@@ -34,4 +35,4 @@ module Eson
34
35
  alias :facet :facets
35
36
  end
36
37
  end
37
- end
38
+ end
@@ -14,7 +14,7 @@ module Eson
14
14
 
15
15
  def to_query_hash
16
16
  if field
17
- {name => field.to_query_hash}
17
+ field.to_query_hash
18
18
  else
19
19
  {name => options}
20
20
  end
@@ -3,6 +3,7 @@ module Eson
3
3
  module Filter
4
4
  module ClassMethods
5
5
  def short_name(name)
6
+ name = name.to_sym
6
7
  define_method :name do
7
8
  name
8
9
  end
@@ -0,0 +1,15 @@
1
+ module Eson
2
+ module Search
3
+ class Filtered
4
+ include Filters
5
+ include Queries
6
+
7
+ include Query
8
+
9
+ # @macro eson.query
10
+ short_name :filtered
11
+
12
+ attr_accessor :type
13
+ end
14
+ end
15
+ end
@@ -4,11 +4,8 @@ module Eson
4
4
  include Filter
5
5
  include Facet
6
6
 
7
- # @macro geo_distance
8
- # @yield a block defining options
9
- # Generates a `$1` filter or facet, depending on context.
10
- # {include:file:eson-dsl/examples/filters/$1.rb}
11
- # {include:file:eson-dsl/examples/facets/$1.rb}
7
+ # @macro eson.filter
8
+ # @macro eson.facet
12
9
  short_name :geo_distance
13
10
 
14
11
  attr_accessor :field, :options
@@ -6,10 +6,8 @@ module Eson
6
6
 
7
7
  include QueryGroup
8
8
 
9
- # @macro has_child
10
- # Generates a `$1` filter or query, depending on context.
11
- # {include:file:eson-dsl/examples/filters/$1.rb}
12
- # {include:file:eson-dsl/examples/queries/$1.rb}
9
+ # @macro eson.filter
10
+ # @macro eson.facet
13
11
  short_name :has_child
14
12
 
15
13
  attr_accessor :options
@@ -26,4 +24,4 @@ module Eson
26
24
 
27
25
  end
28
26
  end
29
- end
27
+ end
@@ -4,10 +4,8 @@ module Eson
4
4
  include Filter
5
5
  include Query
6
6
 
7
- # @macro ids
8
- # Generates a `$1` filter or query, depending on context.
9
- # {include:file:eson-dsl/examples/filters/$1.rb}
10
- # {include:file:eson-dsl/examples/queries/$1.rb}
7
+ # @macro eson.filter
8
+ # @macro eson.query
11
9
  short_name :ids
12
10
 
13
11
  attr_accessor :options
@@ -0,0 +1,48 @@
1
+ module Eson
2
+ module Search
3
+ class Indices
4
+ include Query
5
+
6
+ # @macro eson.query
7
+ short_name :indices
8
+
9
+ attr_accessor :indices, :no_match_val, :query_val
10
+
11
+ def initialize(*indices)
12
+ self.indices = indices
13
+ end
14
+
15
+ def query(&block)
16
+ self.query_val ||= SubQuery.new(args)
17
+ self.query_val.send(context, &Proc.new)
18
+ end
19
+
20
+ def no_match_query(no_match_val = nil)
21
+ if no_match_val
22
+ self.no_match_val = no_match_val
23
+ elsif block_given?
24
+ self.no_match_val ||= SubQuery.new(args)
25
+ self.no_match_val.send(context, &Proc.new)
26
+ end
27
+ end
28
+
29
+ def to_query_hash
30
+ options = {
31
+ :indices => indices,
32
+ :query => self.query_val.to_query_hash,
33
+ }
34
+
35
+ if no_match_val
36
+ if no_match_val.respond_to? :to_query_hash
37
+ no_match = no_match_val.to_query_hash
38
+ else
39
+ no_match = no_match_val
40
+ end
41
+ options.merge!(:no_match_query => no_match)
42
+ end
43
+
44
+ { name => options }
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,10 @@
1
+ module Eson
2
+ module Search
3
+ class Match < FieldBased
4
+ include Query
5
+
6
+ # @macro eson.query
7
+ short_name :match
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,21 @@
1
+ module Eson
2
+ module Search
3
+ class MultiMatch
4
+ include Query
5
+
6
+ # @macro eson.query
7
+ short_name :multi_match
8
+
9
+ attr_accessor :options, :query
10
+
11
+ def initialize(query, options = {})
12
+ self.options = options
13
+ self.query = query
14
+ end
15
+
16
+ def to_query_hash
17
+ {:multi_match => {:query => query}.merge!(options)}
18
+ end
19
+ end
20
+ end
21
+ end
@@ -4,10 +4,8 @@ module Eson
4
4
  include Query
5
5
  include Filter
6
6
 
7
- # @macro prefix
8
- # Generates a `$1` filter or query, depending on context.
9
- # {include:file:eson-dsl/examples/filters/$1.rb}
10
- # {include:file:eson-dsl/examples/queries/$1.rb}
7
+ # @macro eson.filter
8
+ # @macro eson.facet
11
9
  short_name :prefix
12
10
 
13
11
  def to_query_hash
@@ -3,6 +3,7 @@ module Eson
3
3
  module Query
4
4
  module ClassMethods
5
5
  def short_name(name)
6
+ name = name.to_sym
6
7
  define_method :name do
7
8
  name
8
9
  end
@@ -4,10 +4,8 @@ module Eson
4
4
  include Query
5
5
  include Filter
6
6
 
7
- # @macro term
8
- # Generates a `$1` filter, query, depending on context.
9
- # {include:file:eson-dsl/examples/filters/$1.rb}
10
- # {include:file:eson-dsl/examples/queries/$1.rb}
7
+ # @macro eson.filter
8
+ # @macro eson.facet
11
9
  short_name :term
12
10
 
13
11
  def to_query_hash
@@ -5,11 +5,9 @@ module Eson
5
5
  include Filter
6
6
  include Facet
7
7
 
8
- # @macro terms
9
- # Generates a `$1` filter, facet or query, depending on context.
10
- # {include:file:eson-dsl/examples/filters/$1.rb}
11
- # {include:file:eson-dsl/examples/facet/$1.rb}
12
- # {include:file:eson-dsl/examples/queries/$1.rb}
8
+ # @macro eson.filter
9
+ # @macro eson.query
10
+ # @macro eson.facet
13
11
  short_name :terms
14
12
 
15
13
  attr_accessor :options, :field
@@ -5,10 +5,8 @@ module Eson
5
5
  include Filter
6
6
  include QueryGroup
7
7
 
8
- # @macro top_children
9
- # Generates a `$1` filter, query, depending on context.
10
- # {include:file:eson-dsl/examples/filters/$1.rb}
11
- # {include:file:eson-dsl/examples/queries/$1.rb}# @macro eson.query
8
+ # @macro eson.filter
9
+ # @macro eson.query
12
10
  short_name :top_children
13
11
 
14
12
  attr_accessor :options
@@ -1 +1 @@
1
- {"query":{"nested":{"query":{"filtered":{"query":{"match_all":{}},"filter":{"range":{"age":{"from":10,"to":20}}}}},"path":"obj1","score_mode":"avg","_scope":"my_scope"}},"facets":{"hist1":{"histogram":{"field":"age","interval":2,"scope":"my_scope"}}}}
1
+ {"query":{"nested":{"query":{"filtered":{"query":{"match_all":{}},"filter":{"range":{"age":{"from":10,"to":20}}}}},"path":"obj1","score_mode":"avg","_scope":"my_scope"}},"facets":{"hist1":{"histogram":{"field":"age","interval":2},"scope":"my_scope"}}}