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
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Elasticsearch
|
4
|
+
module Test
|
5
|
+
class BaseAggregationComponentTest < ::Test::Unit::TestCase
|
6
|
+
context "BaseAggregationComponent" do
|
7
|
+
|
8
|
+
class DummyAggregationComponent
|
9
|
+
include ::Elasticsearch::DSL::Search::BaseAggregationComponent
|
10
|
+
end
|
11
|
+
|
12
|
+
class ::Elasticsearch::DSL::Search::Aggregations::Dummy
|
13
|
+
include ::Elasticsearch::DSL::Search::BaseAggregationComponent
|
14
|
+
end
|
15
|
+
|
16
|
+
subject { DummyAggregationComponent.new }
|
17
|
+
|
18
|
+
should "return an instance of the aggregation by name" do
|
19
|
+
assert_instance_of ::Elasticsearch::DSL::Search::Aggregations::Dummy, subject.dummy
|
20
|
+
end
|
21
|
+
|
22
|
+
should "raise an exception when unknown aggregation is called" do
|
23
|
+
assert_raise(NoMethodError) { subject.foobar }
|
24
|
+
end
|
25
|
+
|
26
|
+
should "add a nested aggregation" do
|
27
|
+
subject.aggregation :inner do
|
28
|
+
dummy field: 'foo'
|
29
|
+
end
|
30
|
+
|
31
|
+
assert ! subject.aggregations.empty?, "#{subject.aggregations.inspect} is empty"
|
32
|
+
|
33
|
+
assert_instance_of Elasticsearch::DSL::Search::Aggregation, subject.aggregations[:inner]
|
34
|
+
assert_equal( {:dummy=>{:field=>"foo"}}, subject.aggregations[:inner].to_hash )
|
35
|
+
|
36
|
+
assert_equal 'foo', subject.to_hash[:aggregations][:inner][:dummy][:field]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,176 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Elasticsearch
|
4
|
+
module Test
|
5
|
+
class BaseComponentTest < ::Test::Unit::TestCase
|
6
|
+
context "BaseComponent" do
|
7
|
+
|
8
|
+
class DummyComponent
|
9
|
+
include Elasticsearch::DSL::Search::BaseComponent
|
10
|
+
end
|
11
|
+
|
12
|
+
class DummyComponentWithAName
|
13
|
+
include Elasticsearch::DSL::Search::BaseComponent
|
14
|
+
name :foo
|
15
|
+
end
|
16
|
+
|
17
|
+
class DummyComponentWithNewName
|
18
|
+
include Elasticsearch::DSL::Search::BaseComponent
|
19
|
+
end
|
20
|
+
|
21
|
+
class DummyCompoundFilter
|
22
|
+
include Elasticsearch::DSL::Search::BaseCompoundFilterComponent
|
23
|
+
end
|
24
|
+
|
25
|
+
subject { DummyComponent.new :foo }
|
26
|
+
|
27
|
+
should "have a name" do
|
28
|
+
assert_equal :dummy_component, DummyComponent.new.name
|
29
|
+
end
|
30
|
+
|
31
|
+
should "have a custom name" do
|
32
|
+
assert_equal :foo, DummyComponentWithAName.new.name
|
33
|
+
end
|
34
|
+
|
35
|
+
should "allow to set a name" do
|
36
|
+
DummyComponentWithNewName.name :foo
|
37
|
+
assert_equal :foo, DummyComponentWithNewName.new.name
|
38
|
+
assert_equal :foo, DummyComponentWithNewName.name
|
39
|
+
|
40
|
+
DummyComponentWithNewName.name = :bar
|
41
|
+
assert_equal :bar, DummyComponentWithNewName.name
|
42
|
+
assert_equal :bar, DummyComponentWithNewName.new.name
|
43
|
+
end
|
44
|
+
|
45
|
+
should "initialize the hash" do
|
46
|
+
assert_instance_of Hash, subject.to_hash
|
47
|
+
end
|
48
|
+
|
49
|
+
should "have an empty Hash as args by default" do
|
50
|
+
subject = DummyComponentWithNewName.new
|
51
|
+
assert_equal({}, subject.instance_variable_get(:@args))
|
52
|
+
end
|
53
|
+
|
54
|
+
should "have an option method with args" do
|
55
|
+
class DummyComponentWithOptionMethod
|
56
|
+
include Elasticsearch::DSL::Search::BaseComponent
|
57
|
+
option_method :bar
|
58
|
+
end
|
59
|
+
|
60
|
+
subject = DummyComponentWithOptionMethod.new :foo
|
61
|
+
assert_respond_to subject, :bar
|
62
|
+
|
63
|
+
subject.bar 'BAM'
|
64
|
+
assert_equal({ dummy_component_with_option_method: { foo: { bar: 'BAM' } } }, subject.to_hash)
|
65
|
+
end
|
66
|
+
|
67
|
+
should "have an option method without args" do
|
68
|
+
class DummyComponentWithOptionMethod
|
69
|
+
include Elasticsearch::DSL::Search::BaseComponent
|
70
|
+
option_method :bar
|
71
|
+
end
|
72
|
+
|
73
|
+
subject = DummyComponentWithOptionMethod.new
|
74
|
+
assert_respond_to subject, :bar
|
75
|
+
|
76
|
+
subject.bar 'BAM'
|
77
|
+
assert_equal({ dummy_component_with_option_method: { bar: 'BAM' } }, subject.to_hash)
|
78
|
+
end
|
79
|
+
|
80
|
+
should "define a custom option method" do
|
81
|
+
class DummyComponentWithCustomOptionMethod
|
82
|
+
include Elasticsearch::DSL::Search::BaseComponent
|
83
|
+
option_method :bar, lambda { |*args| @hash = { :foo => 'bar' } }
|
84
|
+
end
|
85
|
+
|
86
|
+
subject = DummyComponentWithCustomOptionMethod.new
|
87
|
+
subject.bar
|
88
|
+
|
89
|
+
assert_equal 'bar', subject.instance_variable_get(:@hash)[:foo]
|
90
|
+
end
|
91
|
+
|
92
|
+
should "execute the passed block" do
|
93
|
+
subject = DummyComponent.new(:foo) { @foo = 'BAR' }
|
94
|
+
|
95
|
+
assert_respond_to subject, :call
|
96
|
+
assert_instance_of DummyComponent, subject.call
|
97
|
+
assert_equal 'BAR', subject.instance_variable_get(:@foo)
|
98
|
+
end
|
99
|
+
|
100
|
+
should "respond to empty?" do
|
101
|
+
assert DummyComponent.new.empty?
|
102
|
+
assert DummyComponent.new(:foo).empty?
|
103
|
+
|
104
|
+
subject = DummyComponent.new(:foo) { @hash = { foo: 'bar' } }
|
105
|
+
assert ! subject.empty?
|
106
|
+
end
|
107
|
+
|
108
|
+
context "to_hash conversion" do
|
109
|
+
|
110
|
+
should "build the hash with the block with args" do
|
111
|
+
subject = DummyComponent.new :foo do
|
112
|
+
@hash[:dummy_component][:foo].update moo: 'xoo'
|
113
|
+
end
|
114
|
+
|
115
|
+
assert_equal({dummy_component: { foo: { moo: 'xoo' } } }, subject.to_hash )
|
116
|
+
end
|
117
|
+
|
118
|
+
should "build the hash with the block without args" do
|
119
|
+
subject = DummyComponent.new do
|
120
|
+
@hash[:dummy_component].update moo: 'xoo'
|
121
|
+
end
|
122
|
+
|
123
|
+
assert_equal({dummy_component: { moo: 'xoo' } }, subject.to_hash )
|
124
|
+
end
|
125
|
+
|
126
|
+
should "build the hash with the option method" do
|
127
|
+
class DummyComponentWithOptionMethod
|
128
|
+
include Elasticsearch::DSL::Search::BaseComponent
|
129
|
+
option_method :foo
|
130
|
+
end
|
131
|
+
|
132
|
+
subject = DummyComponentWithOptionMethod.new do
|
133
|
+
foo 'bar'
|
134
|
+
end
|
135
|
+
|
136
|
+
assert_equal({ dummy_component_with_option_method: { foo: 'bar' } }, subject.to_hash)
|
137
|
+
end
|
138
|
+
|
139
|
+
should "build the hash with the passed args" do
|
140
|
+
subject = DummyComponent.new foo: 'bar'
|
141
|
+
|
142
|
+
assert_equal({ dummy_component: { foo: 'bar' } }, subject.to_hash)
|
143
|
+
end
|
144
|
+
|
145
|
+
should "merge the top-level options to the hash" do
|
146
|
+
class DummyComponentWithOptionMethod
|
147
|
+
include Elasticsearch::DSL::Search::BaseComponent
|
148
|
+
option_method :bar
|
149
|
+
end
|
150
|
+
|
151
|
+
subject = DummyComponentWithOptionMethod.new :foo, xoo: 'X' do
|
152
|
+
bar 'B'
|
153
|
+
end
|
154
|
+
|
155
|
+
assert_equal({ dummy_component_with_option_method: { xoo: 'X', foo: { bar: 'B' } } }, subject.to_hash)
|
156
|
+
end
|
157
|
+
|
158
|
+
should "return the already built hash" do
|
159
|
+
subject = DummyComponent.new
|
160
|
+
subject.instance_variable_set(:@hash, { foo: 'bar' })
|
161
|
+
|
162
|
+
assert_equal({ foo: 'bar' }, subject.to_hash)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
context "compound filter" do
|
167
|
+
subject { DummyCompoundFilter.new }
|
168
|
+
|
169
|
+
should "raise an exception for unknown DSL method" do
|
170
|
+
assert_raise(NoMethodError) { subject.foofoo }
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Elasticsearch
|
4
|
+
module Test
|
5
|
+
class SearchFilterTest < ::Test::Unit::TestCase
|
6
|
+
subject { Elasticsearch::DSL::Search::Filter.new }
|
7
|
+
|
8
|
+
context "Search Filter" do
|
9
|
+
|
10
|
+
should "be serializable to a Hash" do
|
11
|
+
assert_equal( {}, subject.to_hash )
|
12
|
+
|
13
|
+
subject = Elasticsearch::DSL::Search::Filter.new
|
14
|
+
subject.instance_variable_set(:@value, { foo: 'bar' })
|
15
|
+
assert_equal( { foo: 'bar' }, subject.to_hash )
|
16
|
+
end
|
17
|
+
|
18
|
+
should "evaluate the block and return itself" do
|
19
|
+
block = Proc.new { 1+1 }
|
20
|
+
subject = Elasticsearch::DSL::Search::Filter.new &block
|
21
|
+
|
22
|
+
subject.expects(:instance_eval)
|
23
|
+
assert_instance_of Elasticsearch::DSL::Search::Filter, subject.call
|
24
|
+
end
|
25
|
+
|
26
|
+
should "call the block and return itself" do
|
27
|
+
block = Proc.new { |s| 1+1 }
|
28
|
+
subject = Elasticsearch::DSL::Search::Filter.new &block
|
29
|
+
|
30
|
+
block.expects(:call)
|
31
|
+
assert_instance_of Elasticsearch::DSL::Search::Filter, subject.call
|
32
|
+
end
|
33
|
+
|
34
|
+
should "define the value with filter methods" do
|
35
|
+
assert_nothing_raised do
|
36
|
+
subject.term foo: 'bar'
|
37
|
+
assert_instance_of Hash, subject.to_hash
|
38
|
+
assert_equal( { term: { foo: 'bar' } }, subject.to_hash )
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
should "redefine the value with filter methods" do
|
43
|
+
assert_nothing_raised do
|
44
|
+
subject.term foo: 'bar'
|
45
|
+
subject.term foo: 'bam'
|
46
|
+
subject.to_hash
|
47
|
+
subject.to_hash
|
48
|
+
assert_instance_of Hash, subject.to_hash
|
49
|
+
assert_equal({ term: { foo: 'bam' } }, subject.to_hash)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
should "raise an exception for unknown filter" do
|
54
|
+
assert_raise(NoMethodError) { subject.foofoo }
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Elasticsearch
|
4
|
+
module Test
|
5
|
+
class HighlightTest < ::Test::Unit::TestCase
|
6
|
+
context "Search highlight" do
|
7
|
+
subject { Elasticsearch::DSL::Search::Highlight.new }
|
8
|
+
|
9
|
+
should "take a Hash" do
|
10
|
+
subject = Elasticsearch::DSL::Search::Highlight.new fields: { 'foo' => {} }, pre_tags: ['*'], post_tags: ['*']
|
11
|
+
|
12
|
+
assert_equal({ fields: { 'foo' => {} }, pre_tags: ['*'], post_tags: ['*'] }, subject.to_hash)
|
13
|
+
end
|
14
|
+
|
15
|
+
should "encode fields as an array" do
|
16
|
+
subject.fields ['foo', 'bar']
|
17
|
+
assert_equal({ fields: { foo: {}, bar: {} } }, subject.to_hash)
|
18
|
+
end
|
19
|
+
|
20
|
+
should "encode fields as a Hash" do
|
21
|
+
subject.fields foo: { bar: 1 }, xoo: { bar: 2 }
|
22
|
+
assert_equal({ fields: { foo: { bar: 1 }, xoo: { bar: 2 } } }, subject.to_hash)
|
23
|
+
end
|
24
|
+
|
25
|
+
should "encode a field" do
|
26
|
+
subject.field 'foo'
|
27
|
+
assert_equal({ fields: { foo: {} } }, subject.to_hash)
|
28
|
+
end
|
29
|
+
|
30
|
+
should "be additive on multiple calls" do
|
31
|
+
subject.fields ['foo', 'bar']
|
32
|
+
subject.field 'bam'
|
33
|
+
subject.field 'baz', { xoo: 10 }
|
34
|
+
assert_equal({ fields: { foo: {}, bar: {}, bam: {}, baz: { xoo: 10 } } }, subject.to_hash)
|
35
|
+
end
|
36
|
+
|
37
|
+
should "encode pre_tags" do
|
38
|
+
subject.pre_tags '*'
|
39
|
+
assert_equal({ pre_tags: ['*'] }, subject.to_hash)
|
40
|
+
end
|
41
|
+
|
42
|
+
should "encode post_tags" do
|
43
|
+
subject.post_tags '*'
|
44
|
+
assert_equal({ post_tags: ['*'] }, subject.to_hash)
|
45
|
+
end
|
46
|
+
|
47
|
+
should "encode pre_tags as an array" do
|
48
|
+
subject.pre_tags ['*', '**']
|
49
|
+
assert_equal({ pre_tags: ['*', '**'] }, subject.to_hash)
|
50
|
+
end
|
51
|
+
|
52
|
+
should "encode post_tags as an array" do
|
53
|
+
subject.post_tags ['*', '**']
|
54
|
+
assert_equal({ post_tags: ['*', '**'] }, subject.to_hash)
|
55
|
+
end
|
56
|
+
|
57
|
+
should "encode the encoder option" do
|
58
|
+
subject.encoder 'foo'
|
59
|
+
assert_equal({ encoder: 'foo' }, subject.to_hash)
|
60
|
+
end
|
61
|
+
|
62
|
+
should "encode the tags_schema option" do
|
63
|
+
subject.tags_schema 'foo'
|
64
|
+
assert_equal({ tags_schema: 'foo' }, subject.to_hash)
|
65
|
+
end
|
66
|
+
|
67
|
+
should "combine the options" do
|
68
|
+
subject.fields ['foo', 'bar']
|
69
|
+
subject.field 'bam'
|
70
|
+
subject.pre_tags '*'
|
71
|
+
subject.post_tags '*'
|
72
|
+
assert_equal({ fields: { foo: {}, bar: {}, bam: {} }, pre_tags: ['*'], post_tags: ['*'] }, subject.to_hash)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Elasticsearch
|
4
|
+
module Test
|
5
|
+
class SearchOptionsTest < ::Test::Unit::TestCase
|
6
|
+
subject { Elasticsearch::DSL::Search::Options.new }
|
7
|
+
|
8
|
+
context "Search options" do
|
9
|
+
should "combine different options" do
|
10
|
+
subject.version true
|
11
|
+
subject.min_score 0.5
|
12
|
+
|
13
|
+
assert_equal({version: true, min_score: 0.5}, subject.to_hash)
|
14
|
+
end
|
15
|
+
|
16
|
+
should "encode _source" do
|
17
|
+
subject._source false
|
18
|
+
assert_equal( { _source: false }, subject.to_hash )
|
19
|
+
|
20
|
+
subject._source 'foo.*'
|
21
|
+
assert_equal( { _source: 'foo.*' }, subject.to_hash )
|
22
|
+
|
23
|
+
subject._source ['foo', 'bar']
|
24
|
+
assert_equal( { _source: ['foo', 'bar'] }, subject.to_hash )
|
25
|
+
|
26
|
+
subject._source include: ['foo.*'], exclude: ['bar.*']
|
27
|
+
assert_equal( { _source: { include: ['foo.*'], exclude: ['bar.*'] } }, subject.to_hash )
|
28
|
+
|
29
|
+
subject.source false
|
30
|
+
assert_equal( { _source: false }, subject.to_hash )
|
31
|
+
end
|
32
|
+
|
33
|
+
should "encode fields" do
|
34
|
+
subject.fields ['foo']
|
35
|
+
assert_equal( { fields: ['foo'] }, subject.to_hash )
|
36
|
+
end
|
37
|
+
|
38
|
+
should "encode script_fields" do
|
39
|
+
subject.script_fields ['foo']
|
40
|
+
assert_equal( { script_fields: ['foo'] }, subject.to_hash )
|
41
|
+
end
|
42
|
+
|
43
|
+
should "encode fielddata_fields" do
|
44
|
+
subject.fielddata_fields ['foo']
|
45
|
+
assert_equal( { fielddata_fields: ['foo'] }, subject.to_hash )
|
46
|
+
end
|
47
|
+
|
48
|
+
should "encode rescore" do
|
49
|
+
subject.rescore foo: 'bar'
|
50
|
+
assert_equal( { rescore: { foo: 'bar' } }, subject.to_hash )
|
51
|
+
end
|
52
|
+
|
53
|
+
should "encode explain" do
|
54
|
+
subject.explain true
|
55
|
+
assert_equal( { explain: true }, subject.to_hash )
|
56
|
+
end
|
57
|
+
|
58
|
+
should "encode version" do
|
59
|
+
subject.version true
|
60
|
+
assert_equal( { version: true }, subject.to_hash )
|
61
|
+
end
|
62
|
+
|
63
|
+
should "encode indices_boost" do
|
64
|
+
subject.indices_boost foo: 'bar'
|
65
|
+
assert_equal( { indices_boost: { foo: 'bar' } }, subject.to_hash )
|
66
|
+
end
|
67
|
+
|
68
|
+
should "encode track_scores" do
|
69
|
+
subject.track_scores true
|
70
|
+
assert_equal( { track_scores: true }, subject.to_hash )
|
71
|
+
end
|
72
|
+
|
73
|
+
should "encode min_score" do
|
74
|
+
subject.min_score 0.5
|
75
|
+
assert_equal( { min_score: 0.5 }, subject.to_hash )
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Elasticsearch
|
4
|
+
module Test
|
5
|
+
class SearchQueryTest < ::Test::Unit::TestCase
|
6
|
+
subject { Elasticsearch::DSL::Search::Query.new }
|
7
|
+
|
8
|
+
context "Search Query" do
|
9
|
+
|
10
|
+
should "be serializable to a Hash" do
|
11
|
+
assert_equal( {}, subject.to_hash )
|
12
|
+
|
13
|
+
subject = Elasticsearch::DSL::Search::Query.new
|
14
|
+
subject.instance_variable_set(:@value, {})
|
15
|
+
assert_equal( {}, subject.to_hash )
|
16
|
+
end
|
17
|
+
|
18
|
+
should "evaluate the block and return itself" do
|
19
|
+
block = Proc.new { 1+1 }
|
20
|
+
subject = Elasticsearch::DSL::Search::Query.new &block
|
21
|
+
|
22
|
+
subject.expects(:instance_eval)
|
23
|
+
assert_instance_of Elasticsearch::DSL::Search::Query, subject.call
|
24
|
+
end
|
25
|
+
|
26
|
+
should "call the block and return itself" do
|
27
|
+
block = Proc.new { |s| 1+1 }
|
28
|
+
subject = Elasticsearch::DSL::Search::Query.new &block
|
29
|
+
|
30
|
+
block.expects(:call)
|
31
|
+
assert_instance_of Elasticsearch::DSL::Search::Query, subject.call
|
32
|
+
end
|
33
|
+
|
34
|
+
should "define the value with query methods" do
|
35
|
+
assert_nothing_raised do
|
36
|
+
subject.match foo: 'bar'
|
37
|
+
assert_instance_of Hash, subject.to_hash
|
38
|
+
assert_equal( { match: { foo: 'bar' } }, subject.to_hash )
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
should "redefine the value with query methods" do
|
43
|
+
assert_nothing_raised do
|
44
|
+
subject.match foo: 'bar'
|
45
|
+
subject.match foo: 'bam'
|
46
|
+
subject.to_hash
|
47
|
+
subject.to_hash
|
48
|
+
assert_instance_of Hash, subject.to_hash
|
49
|
+
assert_equal({ match: { foo: 'bam' } }, subject.to_hash)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
should "have the query methods" do
|
54
|
+
assert_nothing_raised { subject.match foo: 'bar' }
|
55
|
+
end
|
56
|
+
|
57
|
+
should "raise an exception for unknown query" do
|
58
|
+
assert_raise(NoMethodError) { subject.foofoo }
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|