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.
- checksums.yaml +7 -0
- data/Gemfile +16 -0
- data/LICENSE.txt +10 -19
- data/README.md +244 -15
- data/Rakefile +212 -0
- data/elasticsearch-dsl.gemspec +32 -15
- data/lib/elasticsearch-dsl.rb +1 -0
- data/lib/elasticsearch/dsl.rb +43 -3
- data/lib/elasticsearch/dsl/search.rb +241 -0
- data/lib/elasticsearch/dsl/search/aggregation.rb +79 -0
- data/lib/elasticsearch/dsl/search/aggregations/avg.rb +25 -0
- data/lib/elasticsearch/dsl/search/aggregations/cardinality.rb +32 -0
- data/lib/elasticsearch/dsl/search/aggregations/children.rb +39 -0
- data/lib/elasticsearch/dsl/search/aggregations/date_histogram.rb +40 -0
- data/lib/elasticsearch/dsl/search/aggregations/date_range.rb +34 -0
- data/lib/elasticsearch/dsl/search/aggregations/extended_stats.rb +25 -0
- data/lib/elasticsearch/dsl/search/aggregations/filter.rb +30 -0
- data/lib/elasticsearch/dsl/search/aggregations/filters.rb +35 -0
- data/lib/elasticsearch/dsl/search/aggregations/geo_bounds.rb +44 -0
- data/lib/elasticsearch/dsl/search/aggregations/geo_distance.rb +39 -0
- data/lib/elasticsearch/dsl/search/aggregations/geohash_grid.rb +35 -0
- data/lib/elasticsearch/dsl/search/aggregations/global.rb +25 -0
- data/lib/elasticsearch/dsl/search/aggregations/histogram.rb +35 -0
- data/lib/elasticsearch/dsl/search/aggregations/ip_range.rb +31 -0
- data/lib/elasticsearch/dsl/search/aggregations/max.rb +25 -0
- data/lib/elasticsearch/dsl/search/aggregations/min.rb +25 -0
- data/lib/elasticsearch/dsl/search/aggregations/nested.rb +34 -0
- data/lib/elasticsearch/dsl/search/aggregations/percentile_ranks.rb +34 -0
- data/lib/elasticsearch/dsl/search/aggregations/percentiles.rb +33 -0
- data/lib/elasticsearch/dsl/search/aggregations/range.rb +53 -0
- data/lib/elasticsearch/dsl/search/aggregations/reverse_nested.rb +37 -0
- data/lib/elasticsearch/dsl/search/aggregations/scripted_metric.rb +39 -0
- data/lib/elasticsearch/dsl/search/aggregations/significant_terms.rb +45 -0
- data/lib/elasticsearch/dsl/search/aggregations/stats.rb +25 -0
- data/lib/elasticsearch/dsl/search/aggregations/sum.rb +25 -0
- data/lib/elasticsearch/dsl/search/aggregations/terms.rb +33 -0
- data/lib/elasticsearch/dsl/search/aggregations/top_hits.rb +35 -0
- data/lib/elasticsearch/dsl/search/aggregations/value_count.rb +25 -0
- data/lib/elasticsearch/dsl/search/base_aggregation_component.rb +65 -0
- data/lib/elasticsearch/dsl/search/base_component.rb +162 -0
- data/lib/elasticsearch/dsl/search/base_compound_filter_component.rb +97 -0
- data/lib/elasticsearch/dsl/search/filter.rb +56 -0
- data/lib/elasticsearch/dsl/search/filters/and.rb +45 -0
- data/lib/elasticsearch/dsl/search/filters/bool.rb +80 -0
- data/lib/elasticsearch/dsl/search/filters/exists.rb +32 -0
- data/lib/elasticsearch/dsl/search/filters/geo_bounding_box.rb +45 -0
- data/lib/elasticsearch/dsl/search/filters/geo_distance.rb +57 -0
- data/lib/elasticsearch/dsl/search/filters/geo_distance_range.rb +32 -0
- data/lib/elasticsearch/dsl/search/filters/geo_polygon.rb +41 -0
- data/lib/elasticsearch/dsl/search/filters/geo_shape.rb +35 -0
- data/lib/elasticsearch/dsl/search/filters/geohash_cell.rb +46 -0
- data/lib/elasticsearch/dsl/search/filters/has_child.rb +73 -0
- data/lib/elasticsearch/dsl/search/filters/has_parent.rb +72 -0
- data/lib/elasticsearch/dsl/search/filters/ids.rb +32 -0
- data/lib/elasticsearch/dsl/search/filters/indices.rb +77 -0
- data/lib/elasticsearch/dsl/search/filters/limit.rb +31 -0
- data/lib/elasticsearch/dsl/search/filters/match_all.rb +29 -0
- data/lib/elasticsearch/dsl/search/filters/missing.rb +34 -0
- data/lib/elasticsearch/dsl/search/filters/nested.rb +58 -0
- data/lib/elasticsearch/dsl/search/filters/not.rb +73 -0
- data/lib/elasticsearch/dsl/search/filters/or.rb +45 -0
- data/lib/elasticsearch/dsl/search/filters/prefix.rb +29 -0
- data/lib/elasticsearch/dsl/search/filters/query.rb +54 -0
- data/lib/elasticsearch/dsl/search/filters/range.rb +39 -0
- data/lib/elasticsearch/dsl/search/filters/regexp.rb +34 -0
- data/lib/elasticsearch/dsl/search/filters/script.rb +32 -0
- data/lib/elasticsearch/dsl/search/filters/term.rb +32 -0
- data/lib/elasticsearch/dsl/search/filters/terms.rb +32 -0
- data/lib/elasticsearch/dsl/search/filters/type.rb +34 -0
- data/lib/elasticsearch/dsl/search/highlight.rb +92 -0
- data/lib/elasticsearch/dsl/search/options.rb +56 -0
- data/lib/elasticsearch/dsl/search/queries/bool.rb +75 -0
- data/lib/elasticsearch/dsl/search/queries/boosting.rb +33 -0
- data/lib/elasticsearch/dsl/search/queries/common.rb +38 -0
- data/lib/elasticsearch/dsl/search/queries/constant_score.rb +66 -0
- data/lib/elasticsearch/dsl/search/queries/dis_max.rb +35 -0
- data/lib/elasticsearch/dsl/search/queries/filtered.rb +77 -0
- data/lib/elasticsearch/dsl/search/queries/function_score.rb +102 -0
- data/lib/elasticsearch/dsl/search/queries/fuzzy.rb +45 -0
- data/lib/elasticsearch/dsl/search/queries/fuzzy_like_this.rb +37 -0
- data/lib/elasticsearch/dsl/search/queries/fuzzy_like_this_field.rb +36 -0
- data/lib/elasticsearch/dsl/search/queries/geo_shape.rb +31 -0
- data/lib/elasticsearch/dsl/search/queries/has_child.rb +57 -0
- data/lib/elasticsearch/dsl/search/queries/has_parent.rb +57 -0
- data/lib/elasticsearch/dsl/search/queries/ids.rb +28 -0
- data/lib/elasticsearch/dsl/search/queries/indices.rb +34 -0
- data/lib/elasticsearch/dsl/search/queries/match.rb +34 -0
- data/lib/elasticsearch/dsl/search/queries/match_all.rb +27 -0
- data/lib/elasticsearch/dsl/search/queries/more_like_this.rb +58 -0
- data/lib/elasticsearch/dsl/search/queries/multi_match.rb +35 -0
- data/lib/elasticsearch/dsl/search/queries/nested.rb +55 -0
- data/lib/elasticsearch/dsl/search/queries/prefix.rb +30 -0
- data/lib/elasticsearch/dsl/search/queries/query_string.rb +50 -0
- data/lib/elasticsearch/dsl/search/queries/range.rb +47 -0
- data/lib/elasticsearch/dsl/search/queries/regexp.rb +31 -0
- data/lib/elasticsearch/dsl/search/queries/simple_query_string.rb +38 -0
- data/lib/elasticsearch/dsl/search/queries/span_first.rb +28 -0
- data/lib/elasticsearch/dsl/search/queries/span_multi.rb +28 -0
- data/lib/elasticsearch/dsl/search/queries/span_near.rb +32 -0
- data/lib/elasticsearch/dsl/search/queries/span_not.rb +33 -0
- data/lib/elasticsearch/dsl/search/queries/span_or.rb +28 -0
- data/lib/elasticsearch/dsl/search/queries/span_term.rb +26 -0
- data/lib/elasticsearch/dsl/search/queries/template.rb +31 -0
- data/lib/elasticsearch/dsl/search/queries/term.rb +27 -0
- data/lib/elasticsearch/dsl/search/queries/terms.rb +27 -0
- data/lib/elasticsearch/dsl/search/queries/top_children.rb +58 -0
- data/lib/elasticsearch/dsl/search/queries/wildcard.rb +30 -0
- data/lib/elasticsearch/dsl/search/query.rb +55 -0
- data/lib/elasticsearch/dsl/search/sort.rb +50 -0
- data/lib/elasticsearch/dsl/search/suggest.rb +28 -0
- data/lib/elasticsearch/dsl/utils.rb +25 -0
- data/lib/elasticsearch/dsl/version.rb +2 -2
- data/test/integration/search_aggregation_children_test.rb +69 -0
- data/test/integration/search_aggregation_geo_test.rb +90 -0
- data/test/integration/search_aggregation_nested_test.rb +100 -0
- data/test/integration/search_aggregations_test.rb +232 -0
- data/test/integration/search_filters_test.rb +359 -0
- data/test/integration/search_options_test.rb +28 -0
- data/test/integration/search_query_test.rb +55 -0
- data/test/integration/search_size_from_test.rb +41 -0
- data/test/integration/search_sort_test.rb +36 -0
- data/test/integration/search_suggest_test.rb +62 -0
- data/test/test_helper.rb +62 -0
- data/test/unit/aggregations/avg_test.rb +24 -0
- data/test/unit/aggregations/cardinality_test.rb +40 -0
- data/test/unit/aggregations/children_test.rb +36 -0
- data/test/unit/aggregations/date_histogram_test.rb +49 -0
- data/test/unit/aggregations/date_range_test.rb +42 -0
- data/test/unit/aggregations/extended_stats_test.rb +24 -0
- data/test/unit/aggregations/filter_test.rb +31 -0
- data/test/unit/aggregations/filters_test.rb +52 -0
- data/test/unit/aggregations/geo_bounds_test.rb +37 -0
- data/test/unit/aggregations/geo_distance_test.rb +45 -0
- data/test/unit/aggregations/geohash_grid_test.rb +40 -0
- data/test/unit/aggregations/global_test.rb +20 -0
- data/test/unit/aggregations/histogram_test.rb +42 -0
- data/test/unit/aggregations/ip_range_test.rb +41 -0
- data/test/unit/aggregations/max_test.rb +24 -0
- data/test/unit/aggregations/min_test.rb +24 -0
- data/test/unit/aggregations/nested_test.rb +48 -0
- data/test/unit/aggregations/percentile_ranks_test.rb +41 -0
- data/test/unit/aggregations/percentiles_test.rb +40 -0
- data/test/unit/aggregations/range_test.rb +48 -0
- data/test/unit/aggregations/reverse_nested_test.rb +20 -0
- data/test/unit/aggregations/scripted_metric_test.rb +41 -0
- data/test/unit/aggregations/significant_terms_test.rb +46 -0
- data/test/unit/aggregations/stats_test.rb +24 -0
- data/test/unit/aggregations/sum_test.rb +24 -0
- data/test/unit/aggregations/terms_test.rb +51 -0
- data/test/unit/aggregations/top_hits_test.rb +38 -0
- data/test/unit/aggregations/value_count_test.rb +24 -0
- data/test/unit/dsl_test.rb +18 -0
- data/test/unit/filters/and_test.rb +69 -0
- data/test/unit/filters/bool_test.rb +98 -0
- data/test/unit/filters/exists_test.rb +36 -0
- data/test/unit/filters/geo_bounding_box_test.rb +45 -0
- data/test/unit/filters/geo_distance_range_test.rb +51 -0
- data/test/unit/filters/geo_distance_test.rb +58 -0
- data/test/unit/filters/geo_polygon_test.rb +36 -0
- data/test/unit/filters/geo_shape_test.rb +37 -0
- data/test/unit/filters/geohash_cell_test.rb +43 -0
- data/test/unit/filters/has_child_test.rb +52 -0
- data/test/unit/filters/has_parent_test.rb +52 -0
- data/test/unit/filters/ids_test.rb +38 -0
- data/test/unit/filters/indices_test.rb +52 -0
- data/test/unit/filters/limit_test.rb +36 -0
- data/test/unit/filters/match_all_test.rb +20 -0
- data/test/unit/filters/missing_test.rb +38 -0
- data/test/unit/filters/nested_test.rb +40 -0
- data/test/unit/filters/not_test.rb +35 -0
- data/test/unit/filters/or_test.rb +53 -0
- data/test/unit/filters/prefix_test.rb +25 -0
- data/test/unit/filters/query_test.rb +33 -0
- data/test/unit/filters/range_test.rb +47 -0
- data/test/unit/filters/regexp_test.rb +42 -0
- data/test/unit/filters/script_test.rb +38 -0
- data/test/unit/filters/term_test.rb +27 -0
- data/test/unit/filters/terms_test.rb +24 -0
- data/test/unit/filters/type_test.rb +36 -0
- data/test/unit/queries/bool_test.rb +107 -0
- data/test/unit/queries/boosting_test.rb +41 -0
- data/test/unit/queries/common_test.rb +42 -0
- data/test/unit/queries/constant_score_test.rb +47 -0
- data/test/unit/queries/dis_max_test.rb +38 -0
- data/test/unit/queries/filtered_test.rb +51 -0
- data/test/unit/queries/function_score_test.rb +70 -0
- data/test/unit/queries/fuzzy_like_this_field_test.rb +42 -0
- data/test/unit/queries/fuzzy_like_this_test.rb +44 -0
- data/test/unit/queries/fuzzy_test.rb +40 -0
- data/test/unit/queries/geo_shape_test.rb +37 -0
- data/test/unit/queries/has_child_test.rb +53 -0
- data/test/unit/queries/has_parent_test.rb +39 -0
- data/test/unit/queries/ids_test.rb +38 -0
- data/test/unit/queries/indices_test.rb +39 -0
- data/test/unit/queries/match_all_test.rb +36 -0
- data/test/unit/queries/match_test.rb +64 -0
- data/test/unit/queries/more_like_this_test.rb +53 -0
- data/test/unit/queries/multi_match_test.rb +42 -0
- data/test/unit/queries/nested_test.rb +50 -0
- data/test/unit/queries/prefix_test.rb +37 -0
- data/test/unit/queries/query_string_test.rb +55 -0
- data/test/unit/queries/range_test.rb +39 -0
- data/test/unit/queries/regexp_test.rb +43 -0
- data/test/unit/queries/simple_query_string_test.rb +43 -0
- data/test/unit/queries/span_first_test.rb +36 -0
- data/test/unit/queries/span_multi_test.rb +36 -0
- data/test/unit/queries/span_near_test.rb +39 -0
- data/test/unit/queries/span_not_test.rb +40 -0
- data/test/unit/queries/span_or_test.rb +36 -0
- data/test/unit/queries/span_term_test.rb +24 -0
- data/test/unit/queries/template_test.rb +43 -0
- data/test/unit/queries/term_test.rb +27 -0
- data/test/unit/queries/terms_test.rb +24 -0
- data/test/unit/queries/top_children_test.rb +53 -0
- data/test/unit/queries/wildcard_test.rb +43 -0
- data/test/unit/search_aggregation_test.rb +72 -0
- data/test/unit/search_base_aggregation_component_test.rb +41 -0
- data/test/unit/search_base_component_test.rb +176 -0
- data/test/unit/search_filter_test.rb +61 -0
- data/test/unit/search_highlight_test.rb +77 -0
- data/test/unit/search_options_test.rb +80 -0
- data/test/unit/search_query_test.rb +65 -0
- data/test/unit/search_size_from_test.rb +43 -0
- data/test/unit/search_sort_test.rb +40 -0
- data/test/unit/search_suggest_test.rb +20 -0
- data/test/unit/search_test.rb +201 -0
- data/test/unit/utils_test.rb +21 -0
- metadata +524 -25
data/elasticsearch-dsl.gemspec
CHANGED
@@ -3,21 +3,38 @@ lib = File.expand_path('../lib', __FILE__)
|
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
require 'elasticsearch/dsl/version'
|
5
5
|
|
6
|
-
Gem::Specification.new do |
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "elasticsearch-dsl"
|
8
|
+
s.version = Elasticsearch::DSL::VERSION
|
9
|
+
s.authors = ["Karel Minarik"]
|
10
|
+
s.email = ["karel.minarik@elasticsearch.com"]
|
11
|
+
s.description = %q{A Ruby DSL builder for Elasticsearch}
|
12
|
+
s.summary = s.description
|
13
|
+
s.homepage = "https://github.com/elasticsearch/elasticsearch-ruby/tree/master/elasticsearch-dsl"
|
14
|
+
s.license = "Apache 2"
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
s.files = `git ls-files`.split($/)
|
17
|
+
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
19
|
+
s.require_paths = ["lib"]
|
20
20
|
|
21
|
-
|
22
|
-
|
21
|
+
s.extra_rdoc_files = [ "README.md", "LICENSE.txt" ]
|
22
|
+
s.rdoc_options = [ "--charset=UTF-8" ]
|
23
|
+
|
24
|
+
s.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
s.add_development_dependency "rake"
|
26
|
+
|
27
|
+
s.add_development_dependency "elasticsearch"
|
28
|
+
s.add_development_dependency "elasticsearch-extensions"
|
29
|
+
|
30
|
+
s.add_development_dependency 'shoulda-context'
|
31
|
+
s.add_development_dependency 'mocha'
|
32
|
+
s.add_development_dependency 'minitest', '~> 4.0'
|
33
|
+
s.add_development_dependency 'minitest-reporters'
|
34
|
+
s.add_development_dependency 'simplecov'
|
35
|
+
s.add_development_dependency 'simplecov-rcov'
|
36
|
+
s.add_development_dependency 'ci_reporter', '~> 1.9'
|
37
|
+
s.add_development_dependency 'yard'
|
38
|
+
s.add_development_dependency 'cane'
|
39
|
+
s.add_development_dependency 'pry'
|
23
40
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'elasticsearch/dsl'
|
data/lib/elasticsearch/dsl.rb
CHANGED
@@ -1,7 +1,47 @@
|
|
1
|
-
require
|
1
|
+
require 'elasticsearch/dsl/version'
|
2
|
+
|
3
|
+
require 'elasticsearch/dsl/utils'
|
4
|
+
require 'elasticsearch/dsl/search/base_component'
|
5
|
+
require 'elasticsearch/dsl/search/base_compound_filter_component'
|
6
|
+
require 'elasticsearch/dsl/search/base_aggregation_component'
|
7
|
+
require 'elasticsearch/dsl/search/query'
|
8
|
+
require 'elasticsearch/dsl/search/filter'
|
9
|
+
require 'elasticsearch/dsl/search/aggregation'
|
10
|
+
require 'elasticsearch/dsl/search/highlight'
|
11
|
+
require 'elasticsearch/dsl/search/sort'
|
12
|
+
require 'elasticsearch/dsl/search/options'
|
13
|
+
require 'elasticsearch/dsl/search/suggest'
|
14
|
+
|
15
|
+
Dir[ File.expand_path('../dsl/search/queries/**/*.rb', __FILE__) ].each { |f| require f }
|
16
|
+
Dir[ File.expand_path('../dsl/search/filters/**/*.rb', __FILE__) ].each { |f| require f }
|
17
|
+
Dir[ File.expand_path('../dsl/search/aggregations/**/*.rb', __FILE__) ].each { |f| require f }
|
18
|
+
|
19
|
+
require 'elasticsearch/dsl/search'
|
2
20
|
|
3
21
|
module Elasticsearch
|
4
|
-
|
5
|
-
|
22
|
+
|
23
|
+
# The main module, which can be included into your own class or namespace,
|
24
|
+
# to provide the DSL methods.
|
25
|
+
#
|
26
|
+
# @example
|
27
|
+
#
|
28
|
+
# include Elasticsearch::DSL
|
29
|
+
#
|
30
|
+
# definition = search do
|
31
|
+
# query do
|
32
|
+
# match title: 'test'
|
33
|
+
# end
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# definition.to_hash
|
37
|
+
# # => { query: { match: { title: "test"} } }
|
38
|
+
#
|
39
|
+
# @see Search
|
40
|
+
# @see http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/query-dsl-intro.html
|
41
|
+
#
|
42
|
+
module DSL
|
43
|
+
def self.included(base)
|
44
|
+
base.__send__ :include, Elasticsearch::DSL::Search
|
45
|
+
end
|
6
46
|
end
|
7
47
|
end
|
@@ -0,0 +1,241 @@
|
|
1
|
+
module Elasticsearch
|
2
|
+
module DSL
|
3
|
+
|
4
|
+
# Provides DSL methods for building the search definition
|
5
|
+
# (queries, filters, aggregations, sorting, etc)
|
6
|
+
#
|
7
|
+
module Search
|
8
|
+
|
9
|
+
# Initialize a new Search object
|
10
|
+
#
|
11
|
+
# @example Building a search definition declaratively
|
12
|
+
#
|
13
|
+
# definition = search do
|
14
|
+
# query do
|
15
|
+
# match title: 'test'
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# @example Using the class imperatively
|
20
|
+
#
|
21
|
+
# definition = Search.new
|
22
|
+
# query = Query.new
|
23
|
+
# definition.query query
|
24
|
+
# definition.to_hash
|
25
|
+
# # => => {:query=>{:match=>{:title=>"Test"}}}
|
26
|
+
#
|
27
|
+
# @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search.html
|
28
|
+
#
|
29
|
+
def search(*args, &block)
|
30
|
+
Search.new(*args, &block)
|
31
|
+
end
|
32
|
+
|
33
|
+
extend self
|
34
|
+
|
35
|
+
# Wraps the whole search definition (queries, filters, aggregations, sorting, etc)
|
36
|
+
#
|
37
|
+
class Search
|
38
|
+
attr_reader :aggregations
|
39
|
+
|
40
|
+
def initialize(*args, &block)
|
41
|
+
@options = Options.new
|
42
|
+
instance_eval(&block) if block
|
43
|
+
end
|
44
|
+
|
45
|
+
# DSL method for building or accessing the `query` part of a search definition
|
46
|
+
#
|
47
|
+
# @return [self, {Query}]
|
48
|
+
#
|
49
|
+
def query(*args, &block)
|
50
|
+
case
|
51
|
+
when block
|
52
|
+
@query = Query.new(*args, &block)
|
53
|
+
self
|
54
|
+
when !args.empty?
|
55
|
+
@query = args.first
|
56
|
+
self
|
57
|
+
else
|
58
|
+
@query
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Set the query part of a search definition
|
63
|
+
#
|
64
|
+
def query=(value)
|
65
|
+
query value
|
66
|
+
end
|
67
|
+
|
68
|
+
# DSL method for building the `filter` part of a search definition
|
69
|
+
#
|
70
|
+
# @return [self]
|
71
|
+
#
|
72
|
+
def filter(*args, &block)
|
73
|
+
case
|
74
|
+
when block
|
75
|
+
@filter = Filter.new(*args, &block)
|
76
|
+
self
|
77
|
+
when !args.empty?
|
78
|
+
@filter = args.first
|
79
|
+
self
|
80
|
+
else
|
81
|
+
@filter
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# Set the filter part of a search definition
|
86
|
+
#
|
87
|
+
def filter=(value)
|
88
|
+
filter value
|
89
|
+
end
|
90
|
+
|
91
|
+
# DSL method for building the `post_filter` part of a search definition
|
92
|
+
#
|
93
|
+
# @return [self]
|
94
|
+
#
|
95
|
+
def post_filter(*args, &block)
|
96
|
+
case
|
97
|
+
when block
|
98
|
+
@post_filter = Filter.new(*args, &block)
|
99
|
+
self
|
100
|
+
when !args.empty?
|
101
|
+
@post_filter = args.first
|
102
|
+
self
|
103
|
+
else
|
104
|
+
@post_filter
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
# Set the post_filter part of a search definition
|
109
|
+
#
|
110
|
+
def post_filter=(value)
|
111
|
+
post_filter value
|
112
|
+
end
|
113
|
+
|
114
|
+
# DSL method for building the `aggregations` part of a search definition
|
115
|
+
#
|
116
|
+
# @return [self]
|
117
|
+
#
|
118
|
+
def aggregation(*args, &block)
|
119
|
+
@aggregations ||= {}
|
120
|
+
|
121
|
+
if block
|
122
|
+
@aggregations.update args.first => Aggregation.new(*args, &block)
|
123
|
+
else
|
124
|
+
name = args.shift
|
125
|
+
@aggregations.update name => args.shift
|
126
|
+
end
|
127
|
+
self
|
128
|
+
end
|
129
|
+
|
130
|
+
# Set the aggregations part of a search definition
|
131
|
+
#
|
132
|
+
def aggregations=(value)
|
133
|
+
@aggregations = value
|
134
|
+
end
|
135
|
+
|
136
|
+
# DSL method for building the `highlight` part of a search definition
|
137
|
+
#
|
138
|
+
# @return [self]
|
139
|
+
#
|
140
|
+
def highlight(*args, &block)
|
141
|
+
if !args.empty? || block
|
142
|
+
@highlight = Highlight.new(*args, &block)
|
143
|
+
self
|
144
|
+
else
|
145
|
+
@highlight
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
# DSL method for building the `sort` part of a search definition
|
150
|
+
#
|
151
|
+
# @return [self]
|
152
|
+
#
|
153
|
+
def sort(*args, &block)
|
154
|
+
if !args.empty? || block
|
155
|
+
@sort = Sort.new(*args, &block)
|
156
|
+
self
|
157
|
+
else
|
158
|
+
@sort
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
# DSL method for building the `size` part of a search definition
|
163
|
+
#
|
164
|
+
# @return [self]
|
165
|
+
#
|
166
|
+
def size(value=nil)
|
167
|
+
if value
|
168
|
+
@size = value
|
169
|
+
self
|
170
|
+
else
|
171
|
+
@size
|
172
|
+
end
|
173
|
+
end; alias_method :size=, :size
|
174
|
+
|
175
|
+
# DSL method for building the `from` part of a search definition
|
176
|
+
#
|
177
|
+
# @return [self]
|
178
|
+
#
|
179
|
+
def from(value=nil)
|
180
|
+
if value
|
181
|
+
@from = value
|
182
|
+
self
|
183
|
+
else
|
184
|
+
@from
|
185
|
+
end
|
186
|
+
end; alias_method :from=, :from
|
187
|
+
|
188
|
+
# DSL method for building the `suggest` part of a search definition
|
189
|
+
#
|
190
|
+
# @return [self]
|
191
|
+
#
|
192
|
+
def suggest(*args, &block)
|
193
|
+
if !args.empty? || block
|
194
|
+
@suggest ||= {}
|
195
|
+
key, options = args
|
196
|
+
@suggest.update key => Suggest.new(key, options, &block)
|
197
|
+
self
|
198
|
+
else
|
199
|
+
@suggest
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
# Set the suggest part of a search definition
|
204
|
+
#
|
205
|
+
def suggest=(value)
|
206
|
+
@suggest = value
|
207
|
+
end
|
208
|
+
|
209
|
+
# Delegates to the methods provided by the {Options} class
|
210
|
+
#
|
211
|
+
def method_missing(name, *args, &block)
|
212
|
+
if @options.respond_to? name
|
213
|
+
@options.__send__ name, *args, &block
|
214
|
+
self
|
215
|
+
else
|
216
|
+
super
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
# Converts the search definition to a Hash
|
221
|
+
#
|
222
|
+
# @return [Hash]
|
223
|
+
#
|
224
|
+
def to_hash
|
225
|
+
hash = {}
|
226
|
+
hash.update(query: @query.to_hash) if @query
|
227
|
+
hash.update(filter: @filter.to_hash) if @filter
|
228
|
+
hash.update(post_filter: @post_filter.to_hash) if @post_filter
|
229
|
+
hash.update(aggregations: @aggregations.reduce({}) { |sum,item| sum.update item.first => item.last.to_hash }) if @aggregations
|
230
|
+
hash.update(sort: @sort.to_hash) if @sort
|
231
|
+
hash.update(size: @size) if @size
|
232
|
+
hash.update(from: @from) if @from
|
233
|
+
hash.update(suggest: @suggest.reduce({}) { |sum,item| sum.update item.last.to_hash }) if @suggest
|
234
|
+
hash.update(highlight: @highlight.to_hash) if @highlight
|
235
|
+
hash.update(@options) unless @options.empty?
|
236
|
+
hash
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Elasticsearch
|
2
|
+
module DSL
|
3
|
+
module Search
|
4
|
+
|
5
|
+
# Contains the classes for Elasticsearch aggregations
|
6
|
+
#
|
7
|
+
module Aggregations;end
|
8
|
+
|
9
|
+
# Wraps the `aggregations` part of a search definition
|
10
|
+
#
|
11
|
+
# @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations.html
|
12
|
+
#
|
13
|
+
class Aggregation
|
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 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
|
+
# Defines a nested aggregation
|
32
|
+
#
|
33
|
+
def aggregation(*args, &block)
|
34
|
+
call
|
35
|
+
@value.__send__ :aggregation, *args, &block
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns the aggregations
|
39
|
+
#
|
40
|
+
def aggregations
|
41
|
+
call
|
42
|
+
@value.__send__ :aggregations
|
43
|
+
end
|
44
|
+
|
45
|
+
# Evaluates the block passed to initializer, ensuring it is called just once
|
46
|
+
#
|
47
|
+
# @return [self]
|
48
|
+
#
|
49
|
+
# @api private
|
50
|
+
#
|
51
|
+
def call
|
52
|
+
@block.arity < 1 ? self.instance_eval(&@block) : @block.call(self) if @block && ! @_block_called
|
53
|
+
@_block_called = true
|
54
|
+
self
|
55
|
+
end
|
56
|
+
|
57
|
+
# Converts the object to a Hash
|
58
|
+
#
|
59
|
+
# @return [Hash]
|
60
|
+
#
|
61
|
+
def to_hash(options={})
|
62
|
+
call
|
63
|
+
|
64
|
+
if @value
|
65
|
+
case
|
66
|
+
when @value.respond_to?(:to_hash)
|
67
|
+
@value.to_hash
|
68
|
+
else
|
69
|
+
@value
|
70
|
+
end
|
71
|
+
else
|
72
|
+
{}
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -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 average of numeric values
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
#
|
10
|
+
# search do
|
11
|
+
# aggregation :avg_clicks do
|
12
|
+
# avg field: 'clicks'
|
13
|
+
# end
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# @see http://elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-avg-aggregation.html
|
17
|
+
#
|
18
|
+
class Avg
|
19
|
+
include BaseComponent
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|