elasticsearch-dsl 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (228) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +16 -0
  3. data/LICENSE.txt +10 -19
  4. data/README.md +244 -15
  5. data/Rakefile +212 -0
  6. data/elasticsearch-dsl.gemspec +32 -15
  7. data/lib/elasticsearch-dsl.rb +1 -0
  8. data/lib/elasticsearch/dsl.rb +43 -3
  9. data/lib/elasticsearch/dsl/search.rb +241 -0
  10. data/lib/elasticsearch/dsl/search/aggregation.rb +79 -0
  11. data/lib/elasticsearch/dsl/search/aggregations/avg.rb +25 -0
  12. data/lib/elasticsearch/dsl/search/aggregations/cardinality.rb +32 -0
  13. data/lib/elasticsearch/dsl/search/aggregations/children.rb +39 -0
  14. data/lib/elasticsearch/dsl/search/aggregations/date_histogram.rb +40 -0
  15. data/lib/elasticsearch/dsl/search/aggregations/date_range.rb +34 -0
  16. data/lib/elasticsearch/dsl/search/aggregations/extended_stats.rb +25 -0
  17. data/lib/elasticsearch/dsl/search/aggregations/filter.rb +30 -0
  18. data/lib/elasticsearch/dsl/search/aggregations/filters.rb +35 -0
  19. data/lib/elasticsearch/dsl/search/aggregations/geo_bounds.rb +44 -0
  20. data/lib/elasticsearch/dsl/search/aggregations/geo_distance.rb +39 -0
  21. data/lib/elasticsearch/dsl/search/aggregations/geohash_grid.rb +35 -0
  22. data/lib/elasticsearch/dsl/search/aggregations/global.rb +25 -0
  23. data/lib/elasticsearch/dsl/search/aggregations/histogram.rb +35 -0
  24. data/lib/elasticsearch/dsl/search/aggregations/ip_range.rb +31 -0
  25. data/lib/elasticsearch/dsl/search/aggregations/max.rb +25 -0
  26. data/lib/elasticsearch/dsl/search/aggregations/min.rb +25 -0
  27. data/lib/elasticsearch/dsl/search/aggregations/nested.rb +34 -0
  28. data/lib/elasticsearch/dsl/search/aggregations/percentile_ranks.rb +34 -0
  29. data/lib/elasticsearch/dsl/search/aggregations/percentiles.rb +33 -0
  30. data/lib/elasticsearch/dsl/search/aggregations/range.rb +53 -0
  31. data/lib/elasticsearch/dsl/search/aggregations/reverse_nested.rb +37 -0
  32. data/lib/elasticsearch/dsl/search/aggregations/scripted_metric.rb +39 -0
  33. data/lib/elasticsearch/dsl/search/aggregations/significant_terms.rb +45 -0
  34. data/lib/elasticsearch/dsl/search/aggregations/stats.rb +25 -0
  35. data/lib/elasticsearch/dsl/search/aggregations/sum.rb +25 -0
  36. data/lib/elasticsearch/dsl/search/aggregations/terms.rb +33 -0
  37. data/lib/elasticsearch/dsl/search/aggregations/top_hits.rb +35 -0
  38. data/lib/elasticsearch/dsl/search/aggregations/value_count.rb +25 -0
  39. data/lib/elasticsearch/dsl/search/base_aggregation_component.rb +65 -0
  40. data/lib/elasticsearch/dsl/search/base_component.rb +162 -0
  41. data/lib/elasticsearch/dsl/search/base_compound_filter_component.rb +97 -0
  42. data/lib/elasticsearch/dsl/search/filter.rb +56 -0
  43. data/lib/elasticsearch/dsl/search/filters/and.rb +45 -0
  44. data/lib/elasticsearch/dsl/search/filters/bool.rb +80 -0
  45. data/lib/elasticsearch/dsl/search/filters/exists.rb +32 -0
  46. data/lib/elasticsearch/dsl/search/filters/geo_bounding_box.rb +45 -0
  47. data/lib/elasticsearch/dsl/search/filters/geo_distance.rb +57 -0
  48. data/lib/elasticsearch/dsl/search/filters/geo_distance_range.rb +32 -0
  49. data/lib/elasticsearch/dsl/search/filters/geo_polygon.rb +41 -0
  50. data/lib/elasticsearch/dsl/search/filters/geo_shape.rb +35 -0
  51. data/lib/elasticsearch/dsl/search/filters/geohash_cell.rb +46 -0
  52. data/lib/elasticsearch/dsl/search/filters/has_child.rb +73 -0
  53. data/lib/elasticsearch/dsl/search/filters/has_parent.rb +72 -0
  54. data/lib/elasticsearch/dsl/search/filters/ids.rb +32 -0
  55. data/lib/elasticsearch/dsl/search/filters/indices.rb +77 -0
  56. data/lib/elasticsearch/dsl/search/filters/limit.rb +31 -0
  57. data/lib/elasticsearch/dsl/search/filters/match_all.rb +29 -0
  58. data/lib/elasticsearch/dsl/search/filters/missing.rb +34 -0
  59. data/lib/elasticsearch/dsl/search/filters/nested.rb +58 -0
  60. data/lib/elasticsearch/dsl/search/filters/not.rb +73 -0
  61. data/lib/elasticsearch/dsl/search/filters/or.rb +45 -0
  62. data/lib/elasticsearch/dsl/search/filters/prefix.rb +29 -0
  63. data/lib/elasticsearch/dsl/search/filters/query.rb +54 -0
  64. data/lib/elasticsearch/dsl/search/filters/range.rb +39 -0
  65. data/lib/elasticsearch/dsl/search/filters/regexp.rb +34 -0
  66. data/lib/elasticsearch/dsl/search/filters/script.rb +32 -0
  67. data/lib/elasticsearch/dsl/search/filters/term.rb +32 -0
  68. data/lib/elasticsearch/dsl/search/filters/terms.rb +32 -0
  69. data/lib/elasticsearch/dsl/search/filters/type.rb +34 -0
  70. data/lib/elasticsearch/dsl/search/highlight.rb +92 -0
  71. data/lib/elasticsearch/dsl/search/options.rb +56 -0
  72. data/lib/elasticsearch/dsl/search/queries/bool.rb +75 -0
  73. data/lib/elasticsearch/dsl/search/queries/boosting.rb +33 -0
  74. data/lib/elasticsearch/dsl/search/queries/common.rb +38 -0
  75. data/lib/elasticsearch/dsl/search/queries/constant_score.rb +66 -0
  76. data/lib/elasticsearch/dsl/search/queries/dis_max.rb +35 -0
  77. data/lib/elasticsearch/dsl/search/queries/filtered.rb +77 -0
  78. data/lib/elasticsearch/dsl/search/queries/function_score.rb +102 -0
  79. data/lib/elasticsearch/dsl/search/queries/fuzzy.rb +45 -0
  80. data/lib/elasticsearch/dsl/search/queries/fuzzy_like_this.rb +37 -0
  81. data/lib/elasticsearch/dsl/search/queries/fuzzy_like_this_field.rb +36 -0
  82. data/lib/elasticsearch/dsl/search/queries/geo_shape.rb +31 -0
  83. data/lib/elasticsearch/dsl/search/queries/has_child.rb +57 -0
  84. data/lib/elasticsearch/dsl/search/queries/has_parent.rb +57 -0
  85. data/lib/elasticsearch/dsl/search/queries/ids.rb +28 -0
  86. data/lib/elasticsearch/dsl/search/queries/indices.rb +34 -0
  87. data/lib/elasticsearch/dsl/search/queries/match.rb +34 -0
  88. data/lib/elasticsearch/dsl/search/queries/match_all.rb +27 -0
  89. data/lib/elasticsearch/dsl/search/queries/more_like_this.rb +58 -0
  90. data/lib/elasticsearch/dsl/search/queries/multi_match.rb +35 -0
  91. data/lib/elasticsearch/dsl/search/queries/nested.rb +55 -0
  92. data/lib/elasticsearch/dsl/search/queries/prefix.rb +30 -0
  93. data/lib/elasticsearch/dsl/search/queries/query_string.rb +50 -0
  94. data/lib/elasticsearch/dsl/search/queries/range.rb +47 -0
  95. data/lib/elasticsearch/dsl/search/queries/regexp.rb +31 -0
  96. data/lib/elasticsearch/dsl/search/queries/simple_query_string.rb +38 -0
  97. data/lib/elasticsearch/dsl/search/queries/span_first.rb +28 -0
  98. data/lib/elasticsearch/dsl/search/queries/span_multi.rb +28 -0
  99. data/lib/elasticsearch/dsl/search/queries/span_near.rb +32 -0
  100. data/lib/elasticsearch/dsl/search/queries/span_not.rb +33 -0
  101. data/lib/elasticsearch/dsl/search/queries/span_or.rb +28 -0
  102. data/lib/elasticsearch/dsl/search/queries/span_term.rb +26 -0
  103. data/lib/elasticsearch/dsl/search/queries/template.rb +31 -0
  104. data/lib/elasticsearch/dsl/search/queries/term.rb +27 -0
  105. data/lib/elasticsearch/dsl/search/queries/terms.rb +27 -0
  106. data/lib/elasticsearch/dsl/search/queries/top_children.rb +58 -0
  107. data/lib/elasticsearch/dsl/search/queries/wildcard.rb +30 -0
  108. data/lib/elasticsearch/dsl/search/query.rb +55 -0
  109. data/lib/elasticsearch/dsl/search/sort.rb +50 -0
  110. data/lib/elasticsearch/dsl/search/suggest.rb +28 -0
  111. data/lib/elasticsearch/dsl/utils.rb +25 -0
  112. data/lib/elasticsearch/dsl/version.rb +2 -2
  113. data/test/integration/search_aggregation_children_test.rb +69 -0
  114. data/test/integration/search_aggregation_geo_test.rb +90 -0
  115. data/test/integration/search_aggregation_nested_test.rb +100 -0
  116. data/test/integration/search_aggregations_test.rb +232 -0
  117. data/test/integration/search_filters_test.rb +359 -0
  118. data/test/integration/search_options_test.rb +28 -0
  119. data/test/integration/search_query_test.rb +55 -0
  120. data/test/integration/search_size_from_test.rb +41 -0
  121. data/test/integration/search_sort_test.rb +36 -0
  122. data/test/integration/search_suggest_test.rb +62 -0
  123. data/test/test_helper.rb +62 -0
  124. data/test/unit/aggregations/avg_test.rb +24 -0
  125. data/test/unit/aggregations/cardinality_test.rb +40 -0
  126. data/test/unit/aggregations/children_test.rb +36 -0
  127. data/test/unit/aggregations/date_histogram_test.rb +49 -0
  128. data/test/unit/aggregations/date_range_test.rb +42 -0
  129. data/test/unit/aggregations/extended_stats_test.rb +24 -0
  130. data/test/unit/aggregations/filter_test.rb +31 -0
  131. data/test/unit/aggregations/filters_test.rb +52 -0
  132. data/test/unit/aggregations/geo_bounds_test.rb +37 -0
  133. data/test/unit/aggregations/geo_distance_test.rb +45 -0
  134. data/test/unit/aggregations/geohash_grid_test.rb +40 -0
  135. data/test/unit/aggregations/global_test.rb +20 -0
  136. data/test/unit/aggregations/histogram_test.rb +42 -0
  137. data/test/unit/aggregations/ip_range_test.rb +41 -0
  138. data/test/unit/aggregations/max_test.rb +24 -0
  139. data/test/unit/aggregations/min_test.rb +24 -0
  140. data/test/unit/aggregations/nested_test.rb +48 -0
  141. data/test/unit/aggregations/percentile_ranks_test.rb +41 -0
  142. data/test/unit/aggregations/percentiles_test.rb +40 -0
  143. data/test/unit/aggregations/range_test.rb +48 -0
  144. data/test/unit/aggregations/reverse_nested_test.rb +20 -0
  145. data/test/unit/aggregations/scripted_metric_test.rb +41 -0
  146. data/test/unit/aggregations/significant_terms_test.rb +46 -0
  147. data/test/unit/aggregations/stats_test.rb +24 -0
  148. data/test/unit/aggregations/sum_test.rb +24 -0
  149. data/test/unit/aggregations/terms_test.rb +51 -0
  150. data/test/unit/aggregations/top_hits_test.rb +38 -0
  151. data/test/unit/aggregations/value_count_test.rb +24 -0
  152. data/test/unit/dsl_test.rb +18 -0
  153. data/test/unit/filters/and_test.rb +69 -0
  154. data/test/unit/filters/bool_test.rb +98 -0
  155. data/test/unit/filters/exists_test.rb +36 -0
  156. data/test/unit/filters/geo_bounding_box_test.rb +45 -0
  157. data/test/unit/filters/geo_distance_range_test.rb +51 -0
  158. data/test/unit/filters/geo_distance_test.rb +58 -0
  159. data/test/unit/filters/geo_polygon_test.rb +36 -0
  160. data/test/unit/filters/geo_shape_test.rb +37 -0
  161. data/test/unit/filters/geohash_cell_test.rb +43 -0
  162. data/test/unit/filters/has_child_test.rb +52 -0
  163. data/test/unit/filters/has_parent_test.rb +52 -0
  164. data/test/unit/filters/ids_test.rb +38 -0
  165. data/test/unit/filters/indices_test.rb +52 -0
  166. data/test/unit/filters/limit_test.rb +36 -0
  167. data/test/unit/filters/match_all_test.rb +20 -0
  168. data/test/unit/filters/missing_test.rb +38 -0
  169. data/test/unit/filters/nested_test.rb +40 -0
  170. data/test/unit/filters/not_test.rb +35 -0
  171. data/test/unit/filters/or_test.rb +53 -0
  172. data/test/unit/filters/prefix_test.rb +25 -0
  173. data/test/unit/filters/query_test.rb +33 -0
  174. data/test/unit/filters/range_test.rb +47 -0
  175. data/test/unit/filters/regexp_test.rb +42 -0
  176. data/test/unit/filters/script_test.rb +38 -0
  177. data/test/unit/filters/term_test.rb +27 -0
  178. data/test/unit/filters/terms_test.rb +24 -0
  179. data/test/unit/filters/type_test.rb +36 -0
  180. data/test/unit/queries/bool_test.rb +107 -0
  181. data/test/unit/queries/boosting_test.rb +41 -0
  182. data/test/unit/queries/common_test.rb +42 -0
  183. data/test/unit/queries/constant_score_test.rb +47 -0
  184. data/test/unit/queries/dis_max_test.rb +38 -0
  185. data/test/unit/queries/filtered_test.rb +51 -0
  186. data/test/unit/queries/function_score_test.rb +70 -0
  187. data/test/unit/queries/fuzzy_like_this_field_test.rb +42 -0
  188. data/test/unit/queries/fuzzy_like_this_test.rb +44 -0
  189. data/test/unit/queries/fuzzy_test.rb +40 -0
  190. data/test/unit/queries/geo_shape_test.rb +37 -0
  191. data/test/unit/queries/has_child_test.rb +53 -0
  192. data/test/unit/queries/has_parent_test.rb +39 -0
  193. data/test/unit/queries/ids_test.rb +38 -0
  194. data/test/unit/queries/indices_test.rb +39 -0
  195. data/test/unit/queries/match_all_test.rb +36 -0
  196. data/test/unit/queries/match_test.rb +64 -0
  197. data/test/unit/queries/more_like_this_test.rb +53 -0
  198. data/test/unit/queries/multi_match_test.rb +42 -0
  199. data/test/unit/queries/nested_test.rb +50 -0
  200. data/test/unit/queries/prefix_test.rb +37 -0
  201. data/test/unit/queries/query_string_test.rb +55 -0
  202. data/test/unit/queries/range_test.rb +39 -0
  203. data/test/unit/queries/regexp_test.rb +43 -0
  204. data/test/unit/queries/simple_query_string_test.rb +43 -0
  205. data/test/unit/queries/span_first_test.rb +36 -0
  206. data/test/unit/queries/span_multi_test.rb +36 -0
  207. data/test/unit/queries/span_near_test.rb +39 -0
  208. data/test/unit/queries/span_not_test.rb +40 -0
  209. data/test/unit/queries/span_or_test.rb +36 -0
  210. data/test/unit/queries/span_term_test.rb +24 -0
  211. data/test/unit/queries/template_test.rb +43 -0
  212. data/test/unit/queries/term_test.rb +27 -0
  213. data/test/unit/queries/terms_test.rb +24 -0
  214. data/test/unit/queries/top_children_test.rb +53 -0
  215. data/test/unit/queries/wildcard_test.rb +43 -0
  216. data/test/unit/search_aggregation_test.rb +72 -0
  217. data/test/unit/search_base_aggregation_component_test.rb +41 -0
  218. data/test/unit/search_base_component_test.rb +176 -0
  219. data/test/unit/search_filter_test.rb +61 -0
  220. data/test/unit/search_highlight_test.rb +77 -0
  221. data/test/unit/search_options_test.rb +80 -0
  222. data/test/unit/search_query_test.rb +65 -0
  223. data/test/unit/search_size_from_test.rb +43 -0
  224. data/test/unit/search_sort_test.rb +40 -0
  225. data/test/unit/search_suggest_test.rb +20 -0
  226. data/test/unit/search_test.rb +201 -0
  227. data/test/unit/utils_test.rb +21 -0
  228. metadata +524 -25
@@ -0,0 +1,25 @@
1
+ module Elasticsearch
2
+ module DSL
3
+ module Search
4
+ module Aggregations
5
+
6
+ # A single-value metric aggregation which returns the number of values for the aggregation scope
7
+ #
8
+ # @example
9
+ #
10
+ # search do
11
+ # aggregation :value_count do
12
+ # value_count field: 'clicks'
13
+ # end
14
+ # end
15
+ #
16
+ # @see http://elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html
17
+ #
18
+ class ValueCount
19
+ include BaseComponent
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,65 @@
1
+ module Elasticsearch
2
+ module DSL
3
+ module Search
4
+
5
+ # Module containing common functionality for aggregation DSL classes
6
+ #
7
+ module BaseAggregationComponent
8
+
9
+ def self.included(base)
10
+ base.__send__ :include, BaseComponent
11
+ base.__send__ :include, InstanceMethods
12
+ end
13
+
14
+ module InstanceMethods
15
+
16
+ attr_reader :aggregations
17
+
18
+ # Looks up the corresponding class for a method being invoked, and initializes it
19
+ #
20
+ # @raise [NoMethodError] When the corresponding class cannot be found
21
+ #
22
+ def method_missing(name, *args, &block)
23
+ klass = Utils.__camelize(name)
24
+ if Aggregations.const_defined? klass
25
+ @value = Aggregations.const_get(klass).new *args, &block
26
+ else
27
+ raise NoMethodError, "undefined method '#{name}' for #{self}"
28
+ end
29
+ end
30
+
31
+ # Adds a nested aggregation into the aggregation definition
32
+ #
33
+ # @return [self]
34
+ #
35
+ def aggregation(*args, &block)
36
+ @aggregations ||= {}
37
+ @aggregations.update args.first => Aggregation.new(*args, &block)
38
+ self
39
+ end
40
+
41
+ # Convert the aggregations to a Hash
42
+ #
43
+ # A default implementation, DSL classes can overload it.
44
+ #
45
+ # @return [Hash]
46
+ #
47
+ def to_hash(options={})
48
+ call
49
+
50
+ @hash = { name => @args } unless @hash && @hash[name] && ! @hash[name].empty?
51
+
52
+ if @aggregations
53
+ @hash[:aggregations] = {}
54
+ @aggregations.map { |name, value| @hash[:aggregations][name] = value.to_hash }
55
+ end
56
+ @hash
57
+ end
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+ end
64
+ end
65
+
@@ -0,0 +1,162 @@
1
+ module Elasticsearch
2
+ module DSL
3
+ module Search
4
+
5
+ # Module containing common functionality for DSL classes
6
+ #
7
+ module BaseComponent
8
+ def self.included(base)
9
+ base.__send__ :extend, ClassMethods
10
+ base.__send__ :include, InstanceMethods
11
+
12
+ base.instance_eval do
13
+ # Defines an "inner" method for DSL classes
14
+ #
15
+ # @example Define a method `bar` for the MyQuery class which updates the query definition
16
+ #
17
+ # class MyQuery
18
+ # include BaseComponent
19
+ #
20
+ # option_method :bar
21
+ # end
22
+ #
23
+ # q = MyQuery.new :foo do
24
+ # bar 'TEST'
25
+ # end
26
+ #
27
+ # q.to_hash
28
+ # # => {:myquery=>{:foo=>{:bar=>"TEST"}}}
29
+ #
30
+ # @example Define a method `bar` with custom logic for updating the Hash with query definition
31
+ #
32
+ # class MyCustomQuery
33
+ # include BaseComponent
34
+ #
35
+ # option_method :bar, lambda { |*args| @hash[self.name.to_sym][@args].update custom: args.pop }
36
+ # end
37
+ #
38
+ # q = MyCustomQuery.new :foo do
39
+ # bar 'TEST'
40
+ # end
41
+ #
42
+ # q.to_hash
43
+ # # => {:mycustomquery=>{:foo=>{:custom=>"TEST"}}}
44
+ #
45
+ def option_method(name, block=nil)
46
+ if block
47
+ self.__send__ :define_method, name, &block
48
+ else
49
+ self.__send__ :define_method, name do |*args|
50
+ # 1. Component has empty @args (ie. no user supplied name or @hash value)
51
+ if @args && @args.respond_to?(:to_hash) && @args.empty?
52
+ @hash[self.name.to_sym].update name.to_sym => args.first
53
+ # 2. Component user-supplied name or @hash value passed in @args
54
+ else
55
+ @hash[self.name.to_sym] = { @args => {} } unless @hash[self.name.to_sym][@args]
56
+ @hash[self.name.to_sym][@args].update name.to_sym => args.first
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ def initialize(*args, &block)
65
+ @hash = { name => {} }
66
+ @args = args.first || {}
67
+ @options = args.size > 1 ? args.last : {}
68
+ @block = block
69
+ end
70
+
71
+ module ClassMethods
72
+
73
+ # Get or set the name for the DSL class
74
+ #
75
+ # @example Set the index name for the `Article` model and re-evaluate it on each call
76
+ #
77
+ # class MyQuery
78
+ # include BaseComponent
79
+ # name :my_special_query
80
+ # end
81
+ #
82
+ # MyQuery.name
83
+ # # => :my_special_query
84
+ #
85
+ def name(value=nil)
86
+ if value
87
+ @name = value.to_sym
88
+ else
89
+ @name ||= begin
90
+ value = self.to_s.split('::').last
91
+ value.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
92
+ value.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
93
+ value.tr!("-", "_")
94
+ value.downcase!
95
+ value.to_sym
96
+ end
97
+ end
98
+ end
99
+
100
+ # Set the name for the DSL class
101
+ #
102
+ def name=(value)
103
+ @name = value.to_sym
104
+ end
105
+ end
106
+
107
+ module InstanceMethods
108
+
109
+ # Return the name for instance of the DSL class
110
+ #
111
+ # @return [String]
112
+ #
113
+ def name
114
+ self.class.name
115
+ end
116
+
117
+ # Evaluates any block passed to the query
118
+ #
119
+ # @return [self]
120
+ #
121
+ def call
122
+ @block.arity < 1 ? self.instance_eval(&@block) : @block.call(self) if @block
123
+ self
124
+ end
125
+
126
+ # Return true when the component definition is empty
127
+ #
128
+ def empty?
129
+ to_hash[name].respond_to?(:empty?) && to_hash[name].empty?
130
+ end
131
+
132
+ # Convert the query definition to a Hash
133
+ #
134
+ # A default implementation, DSL classes can overload it.
135
+ #
136
+ # @return [Hash]
137
+ #
138
+ def to_hash(options={})
139
+ case
140
+ # 1. Create hash from the block
141
+ when @block
142
+ @hash = (@args && ! @args.empty?) ? { name => { @args => {} } } : { name => {} }
143
+ call
144
+ @hash[self.name.to_sym].update @options unless @options.empty?
145
+ @hash
146
+ # 2. Hash created with option methods
147
+ when @hash[self.name.to_sym] && ! @args.is_a?(Hash) && @hash[self.name.to_sym][@args]
148
+ @hash[self.name.to_sym].update @options unless @options.empty?
149
+ @hash
150
+ # 3. Hash passsed as @args
151
+ when @hash[self.name.to_sym] && @args.respond_to?(:to_hash) && ! @args.empty?
152
+ { name => @args.to_hash }
153
+ # 4. Hash already built
154
+ else
155
+ @hash
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,97 @@
1
+ module Elasticsearch
2
+ module DSL
3
+ module Search
4
+
5
+ # Module containing common functionality for a "compound" (wrapping) filters, such as `and`, `or`, `not`
6
+ #
7
+ module BaseCompoundFilterComponent
8
+ include Enumerable
9
+
10
+ def initialize(*args, &block)
11
+ super
12
+ @value = []
13
+ end
14
+
15
+ def self.included(base)
16
+ base.__send__ :include, InstanceMethods
17
+ base.__send__ :include, EnumerableMethods
18
+ base.__send__ :include, MethodDelegation
19
+ end
20
+
21
+ # Common functionality for the compound filter components
22
+ #
23
+ module InstanceMethods
24
+ # Evaluates the block passed to initializer, ensuring it is called just once
25
+ #
26
+ # @return [self]
27
+ #
28
+ # @api private
29
+ #
30
+ def call
31
+ @block.arity < 1 ? self.instance_eval(&@block) : @block.call(self) if @block && ! @_block_called
32
+ @_block_called = true
33
+ self
34
+ end
35
+
36
+ # Convert the component to a Hash
37
+ #
38
+ # A default implementation, DSL classes can overload it.
39
+ #
40
+ # @return [Hash]
41
+ #
42
+ def to_hash(options={})
43
+ case
44
+ when @value.empty? && ! @block
45
+ @hash = super
46
+ when @block
47
+ call
48
+ @hash = { name.to_sym => @value.map { |i| i.to_hash } }
49
+ else
50
+ @hash = { name.to_sym => @value }
51
+ end
52
+ @hash
53
+ end
54
+ end
55
+
56
+ # Implements the {Enumerable} methods
57
+ #
58
+ module EnumerableMethods
59
+ def each(&block)
60
+ @value.each(&block)
61
+ end
62
+
63
+ def slice(*args)
64
+ @value.slice(*args)
65
+ end; alias :[] :slice
66
+
67
+ def size
68
+ @value.size
69
+ end
70
+
71
+ def <<(value)
72
+ @value << value
73
+ end
74
+
75
+ def empty?
76
+ @value.empty?
77
+ end
78
+ end
79
+
80
+ module MethodDelegation
81
+ # Looks up the corresponding class for a method being invoked, and initializes it
82
+ #
83
+ # @raise [NoMethodError] When the corresponding class cannot be found
84
+ #
85
+ def method_missing(name, *args, &block)
86
+ klass = Utils.__camelize(name)
87
+ if Filters.const_defined? klass
88
+ @value << Filters.const_get(klass).new(*args, &block)
89
+ else
90
+ raise NoMethodError, "undefined method '#{name}' for #{self}"
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,56 @@
1
+ module Elasticsearch
2
+ module DSL
3
+ module Search
4
+
5
+ # Contains the classes for Elasticsearch filters
6
+ #
7
+ module Filters;end
8
+
9
+ # Wraps the `filter` part of a search definition, aggregation, etc
10
+ #
11
+ # @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filters.html
12
+ #
13
+ class Filter
14
+ def initialize(*args, &block)
15
+ @block = block
16
+ end
17
+
18
+ # Looks up the corresponding class for a method being invoked, and initializes it
19
+ #
20
+ # @raise [NoMethodError] When the corresponding class cannot be found
21
+ #
22
+ def method_missing(name, *args, &block)
23
+ klass = Utils.__camelize(name)
24
+ if Filters.const_defined? klass
25
+ @value = Filters.const_get(klass).new *args, &block
26
+ else
27
+ raise NoMethodError, "undefined method '#{name}' for #{self}"
28
+ end
29
+ end
30
+
31
+ # Evaluates any block passed to the query
32
+ #
33
+ # @return [self]
34
+ #
35
+ def call
36
+ @block.arity < 1 ? self.instance_eval(&@block) : @block.call(self) if @block
37
+ self
38
+ end
39
+
40
+ # Converts the query definition to a Hash
41
+ #
42
+ # @return [Hash]
43
+ #
44
+ def to_hash(options={})
45
+ call
46
+ if @value
47
+ @value.to_hash
48
+ else
49
+ {}
50
+ end
51
+ end
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,45 @@
1
+ module Elasticsearch
2
+ module DSL
3
+ module Search
4
+ module Filters
5
+
6
+ # A compound filter which matches documents by an intersection of individual filters.
7
+ #
8
+ # @note Since `and` is a keyword in Ruby, use the `_and` method in DSL definitions
9
+ #
10
+ # @example Pass the filters as a Hash
11
+ # search do
12
+ # query do
13
+ # filtered do
14
+ # filter do
15
+ # _and filters: [ {term: { color: 'red' }}, {term: { size: 'xxl' }} ]
16
+ # end
17
+ # end
18
+ # end
19
+ # end
20
+ #
21
+ # @example Define the filters with a block
22
+ #
23
+ # search do
24
+ # query do
25
+ # filtered do
26
+ # filter do
27
+ # _and do
28
+ # term color: 'red'
29
+ # term size: 'xxl'
30
+ # end
31
+ # end
32
+ # end
33
+ # end
34
+ # end
35
+ #
36
+ # @see http://elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-and-filter.html
37
+ #
38
+ class And
39
+ include BaseComponent
40
+ include BaseCompoundFilterComponent
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end