elasticsearch-dsl 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -3
- data/elasticsearch-dsl.gemspec +5 -1
- data/lib/elasticsearch/dsl/search.rb +1 -1
- data/lib/elasticsearch/dsl/search/aggregations/missing.rb +36 -0
- data/lib/elasticsearch/dsl/search/aggregations/pipeline/avg_bucket.rb +34 -0
- data/lib/elasticsearch/dsl/search/aggregations/pipeline/bucket_script.rb +36 -0
- data/lib/elasticsearch/dsl/search/aggregations/pipeline/bucket_selector.rb +35 -0
- data/lib/elasticsearch/dsl/search/aggregations/pipeline/cumulative_sum.rb +33 -0
- data/lib/elasticsearch/dsl/search/aggregations/pipeline/derivative.rb +34 -0
- data/lib/elasticsearch/dsl/search/aggregations/pipeline/extended_stats_bucket.rb +34 -0
- data/lib/elasticsearch/dsl/search/aggregations/pipeline/max_bucket.rb +34 -0
- data/lib/elasticsearch/dsl/search/aggregations/pipeline/min_bucket.rb +34 -0
- data/lib/elasticsearch/dsl/search/aggregations/pipeline/moving_avg.rb +42 -0
- data/lib/elasticsearch/dsl/search/aggregations/pipeline/percentiles_bucket.rb +36 -0
- data/lib/elasticsearch/dsl/search/aggregations/pipeline/serial_diff.rb +36 -0
- data/lib/elasticsearch/dsl/search/aggregations/pipeline/stats_bucket.rb +34 -0
- data/lib/elasticsearch/dsl/search/aggregations/pipeline/sum_bucket.rb +34 -0
- data/lib/elasticsearch/dsl/search/queries/bool.rb +10 -0
- data/lib/elasticsearch/dsl/search/sort.rb +11 -2
- data/lib/elasticsearch/dsl/version.rb +1 -1
- data/test/integration/search_test.rb +60 -0
- data/test/unit/aggregations/missing_test.rb +39 -0
- data/test/unit/aggregations/pipeline/avg_bucket_test.rb +39 -0
- data/test/unit/aggregations/pipeline/bucket_script_test.rb +39 -0
- data/test/unit/aggregations/pipeline/bucket_selector_test.rb +38 -0
- data/test/unit/aggregations/pipeline/cumulative_sum_test.rb +37 -0
- data/test/unit/aggregations/pipeline/derivative_test.rb +39 -0
- data/test/unit/aggregations/pipeline/extended_stats_bucket_test.rb +38 -0
- data/test/unit/aggregations/pipeline/max_bucket_test.rb +38 -0
- data/test/unit/aggregations/pipeline/min_bucket_test.rb +38 -0
- data/test/unit/aggregations/pipeline/moving_avg_test.rb +41 -0
- data/test/unit/aggregations/pipeline/percentiles_bucket_test.rb +39 -0
- data/test/unit/aggregations/pipeline/serial_diff_test.rb +39 -0
- data/test/unit/aggregations/pipeline/stats_bucket_test.rb +38 -0
- data/test/unit/aggregations/pipeline/sum_bucket_test.rb +38 -0
- data/test/unit/queries/bool_test.rb +9 -0
- data/test/unit/search_sort_test.rb +25 -0
- data/test/unit/search_test.rb +34 -0
- metadata +50 -6
@@ -0,0 +1,36 @@
|
|
1
|
+
module Elasticsearch
|
2
|
+
module DSL
|
3
|
+
module Search
|
4
|
+
module Aggregations
|
5
|
+
|
6
|
+
# Serial differencing is a technique where values in a time series are subtracted from itself at different time lags or periods.
|
7
|
+
#
|
8
|
+
# @example Passing the options as a Hash
|
9
|
+
#
|
10
|
+
# aggregation :thirtieth_difference do
|
11
|
+
# serial_diff buckets_path: 'the_sum'
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# @example Passing the options as a block
|
15
|
+
#
|
16
|
+
# aggregation :thirtieth_difference do
|
17
|
+
# serial_diff do
|
18
|
+
# buckets_path 'the_sum'
|
19
|
+
# lag 30
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-serialdiff-aggregation.html
|
24
|
+
#
|
25
|
+
class SerialDiff
|
26
|
+
include BaseAggregationComponent
|
27
|
+
|
28
|
+
option_method :buckets_path
|
29
|
+
option_method :lag
|
30
|
+
option_method :gap_policy
|
31
|
+
option_method :format
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Elasticsearch
|
2
|
+
module DSL
|
3
|
+
module Search
|
4
|
+
module Aggregations
|
5
|
+
|
6
|
+
# A sibling pipeline aggregation which calculates a variety of stats across all bucket of a specified metric in a sibling aggregation.
|
7
|
+
#
|
8
|
+
# @example Passing the options as a Hash
|
9
|
+
#
|
10
|
+
# aggregation :stats_monthly_sales do
|
11
|
+
# stats_bucket buckets_path: 'sales_per_month>sales'
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# @example Passing the options as a block
|
15
|
+
#
|
16
|
+
# aggregation :stats_monthly_sales do
|
17
|
+
# stats_bucket do
|
18
|
+
# buckets_path 'sales_per_month>sales'
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-stats-bucket-aggregation.html
|
23
|
+
#
|
24
|
+
class StatsBucket
|
25
|
+
include BaseAggregationComponent
|
26
|
+
|
27
|
+
option_method :buckets_path
|
28
|
+
option_method :gap_policy
|
29
|
+
option_method :format
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Elasticsearch
|
2
|
+
module DSL
|
3
|
+
module Search
|
4
|
+
module Aggregations
|
5
|
+
|
6
|
+
# A sibling pipeline aggregation which calculates the sum across all bucket of a specified metric in a sibling aggregation.
|
7
|
+
#
|
8
|
+
# @example Passing the options as a Hash
|
9
|
+
#
|
10
|
+
# aggregation :sum_monthly_sales do
|
11
|
+
# sum_bucket buckets_path: 'sales_per_month>sales'
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# @example Passing the options as a block
|
15
|
+
#
|
16
|
+
# aggregation :sum_monthly_sales do
|
17
|
+
# sum_bucket do
|
18
|
+
# buckets_path 'sales_per_month>sales'
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-sum-bucket-aggregation.html
|
23
|
+
#
|
24
|
+
class SumBucket
|
25
|
+
include BaseAggregationComponent
|
26
|
+
|
27
|
+
option_method :buckets_path
|
28
|
+
option_method :gap_policy
|
29
|
+
option_method :format
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -57,6 +57,11 @@ module Elasticsearch
|
|
57
57
|
self
|
58
58
|
end
|
59
59
|
|
60
|
+
def filter(*args, &block)
|
61
|
+
@filter = block ? Filter.new(*args, &block) : args.first
|
62
|
+
self
|
63
|
+
end
|
64
|
+
|
60
65
|
def to_hash
|
61
66
|
@hash[name].update(@args.to_hash) if @args.respond_to?(:to_hash)
|
62
67
|
|
@@ -66,6 +71,11 @@ module Elasticsearch
|
|
66
71
|
@hash[name] = @args unless @args.nil? || @args.empty?
|
67
72
|
end
|
68
73
|
|
74
|
+
if @filter
|
75
|
+
_filter = @filter.respond_to?(:to_hash) ? @filter.to_hash : @filter
|
76
|
+
@hash[name].update(filter: _filter)
|
77
|
+
end
|
78
|
+
|
69
79
|
@hash
|
70
80
|
end
|
71
81
|
end
|
@@ -36,14 +36,23 @@ module Elasticsearch
|
|
36
36
|
#
|
37
37
|
def to_hash
|
38
38
|
if @block
|
39
|
-
call
|
39
|
+
call unless @block_called
|
40
|
+
@block_called = true
|
40
41
|
else
|
41
|
-
@value << @args if @args
|
42
|
+
@value << @args if @args && !@args.empty? && ! @value.include?(@args)
|
42
43
|
end
|
43
44
|
|
44
45
|
@hash = @value.flatten
|
45
46
|
@hash
|
46
47
|
end
|
48
|
+
|
49
|
+
# Return whether the definition is empty
|
50
|
+
#
|
51
|
+
# @return [Boolean]
|
52
|
+
#
|
53
|
+
def empty?
|
54
|
+
to_hash.empty?
|
55
|
+
end
|
47
56
|
end
|
48
57
|
end
|
49
58
|
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Elasticsearch
|
4
|
+
module Test
|
5
|
+
class SearchIntegrationTest < ::Elasticsearch::Test::IntegrationTestCase
|
6
|
+
include Elasticsearch::DSL::Search
|
7
|
+
|
8
|
+
class MySearch
|
9
|
+
include Elasticsearch::DSL::Search
|
10
|
+
|
11
|
+
def initialize(q)
|
12
|
+
@q = q
|
13
|
+
end
|
14
|
+
|
15
|
+
def tags
|
16
|
+
%w[ one two ]
|
17
|
+
end
|
18
|
+
|
19
|
+
def search_definition
|
20
|
+
search do |q|
|
21
|
+
q.query do |q|
|
22
|
+
q.bool do |q|
|
23
|
+
q.must do |q|
|
24
|
+
q.match title: @q
|
25
|
+
end
|
26
|
+
q.must do |q|
|
27
|
+
q.terms tags: tags
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "The Search class" do
|
36
|
+
startup do
|
37
|
+
Elasticsearch::Extensions::Test::Cluster.start(nodes: 1) if ENV['SERVER'] and not Elasticsearch::Extensions::Test::Cluster.running?
|
38
|
+
end
|
39
|
+
|
40
|
+
setup do
|
41
|
+
@client.indices.create index: 'test'
|
42
|
+
@client.index index: 'test', type: 'd', id: '1', body: { title: 'Test', tags: ['one'] }
|
43
|
+
@client.index index: 'test', type: 'd', id: '2', body: { title: 'Test', tags: ['one', 'two'] }
|
44
|
+
@client.index index: 'test', type: 'd', id: '3', body: { title: 'Test', tags: ['three'] }
|
45
|
+
@client.indices.refresh index: 'test'
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
should "have access to the calling context" do
|
50
|
+
s = MySearch.new('test')
|
51
|
+
response = @client.search index: 'test', body: s.search_definition.to_hash
|
52
|
+
|
53
|
+
assert_equal 2, response['hits']['total']
|
54
|
+
assert_equal 'Test', response['hits']['hits'][0]['_source']['title']
|
55
|
+
assert_same_elements ['1', '2'], response['hits']['hits'].map { |d| d['_id'] }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Elasticsearch
|
4
|
+
module Test
|
5
|
+
module Aggregations
|
6
|
+
class MissingTest < ::Test::Unit::TestCase
|
7
|
+
include Elasticsearch::DSL::Search::Aggregations
|
8
|
+
|
9
|
+
context "Missing aggregation" do
|
10
|
+
subject { Missing.new }
|
11
|
+
|
12
|
+
should "be converted to a Hash" do
|
13
|
+
assert_equal({ missing: {} }, subject.to_hash)
|
14
|
+
end
|
15
|
+
|
16
|
+
should "take a Hash" do
|
17
|
+
subject = Missing.new( { field: 'foo' } )
|
18
|
+
assert_equal({ missing: { field: "foo" } }, subject.to_hash)
|
19
|
+
end
|
20
|
+
|
21
|
+
should "have option methods" do
|
22
|
+
subject.field 'foo'
|
23
|
+
|
24
|
+
assert_equal %w[ field ], subject.to_hash[:missing].keys.map(&:to_s).sort
|
25
|
+
assert_equal 'foo', subject.to_hash[:missing][:field]
|
26
|
+
end
|
27
|
+
|
28
|
+
should "take a block" do
|
29
|
+
subject = Missing.new do
|
30
|
+
field 'bar'
|
31
|
+
end
|
32
|
+
|
33
|
+
assert_equal({missing: { field: 'bar' } }, subject.to_hash)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Elasticsearch
|
4
|
+
module Test
|
5
|
+
module Aggregations
|
6
|
+
class AvgBucketTest < ::Test::Unit::TestCase
|
7
|
+
include Elasticsearch::DSL::Search::Aggregations
|
8
|
+
|
9
|
+
context "Avg Bucket agg" do
|
10
|
+
subject { AvgBucket.new }
|
11
|
+
|
12
|
+
should "be converted to a hash" do
|
13
|
+
assert_equal({ avg_bucket: {} }, subject.to_hash)
|
14
|
+
end
|
15
|
+
|
16
|
+
should "have option methods" do
|
17
|
+
subject = AvgBucket.new :foo
|
18
|
+
|
19
|
+
subject.buckets_path 'bar'
|
20
|
+
subject.gap_policy 'bar'
|
21
|
+
subject.format 'bar'
|
22
|
+
|
23
|
+
assert_equal %w[ buckets_path format gap_policy ],
|
24
|
+
subject.to_hash[:avg_bucket][:foo].keys.map(&:to_s).sort
|
25
|
+
assert_equal 'bar', subject.to_hash[:avg_bucket][:foo][:buckets_path]
|
26
|
+
end
|
27
|
+
|
28
|
+
should "take a block" do
|
29
|
+
subject = AvgBucket.new :foo do
|
30
|
+
format 'bar'
|
31
|
+
end
|
32
|
+
assert_equal({avg_bucket: { foo: { format: 'bar' } } }, subject.to_hash)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Elasticsearch
|
4
|
+
module Test
|
5
|
+
module Aggregations
|
6
|
+
class BucketScriptTest < ::Test::Unit::TestCase
|
7
|
+
include Elasticsearch::DSL::Search::Aggregations
|
8
|
+
|
9
|
+
context "Bucket Script agg" do
|
10
|
+
subject { BucketScript.new }
|
11
|
+
|
12
|
+
should "be converted to a hash" do
|
13
|
+
assert_equal({ bucket_script: {} }, subject.to_hash)
|
14
|
+
end
|
15
|
+
|
16
|
+
should "have option methods" do
|
17
|
+
subject = BucketScript.new :foo
|
18
|
+
|
19
|
+
subject.buckets_path foo: 'foo', bar: 'bar'
|
20
|
+
subject.script 'bar'
|
21
|
+
subject.gap_policy 'skip'
|
22
|
+
subject.format 'bar'
|
23
|
+
|
24
|
+
assert_equal %w[ buckets_path format gap_policy script ],
|
25
|
+
subject.to_hash[:bucket_script][:foo].keys.map(&:to_s).sort
|
26
|
+
assert_equal 'bar', subject.to_hash[:bucket_script][:foo][:buckets_path][:bar]
|
27
|
+
end
|
28
|
+
|
29
|
+
should "take a block" do
|
30
|
+
subject = BucketScript.new :foo do
|
31
|
+
format 'bar'
|
32
|
+
end
|
33
|
+
assert_equal({bucket_script: { foo: { format: 'bar' } } }, subject.to_hash)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Elasticsearch
|
4
|
+
module Test
|
5
|
+
module Aggregations
|
6
|
+
class BucketSelectorTest < ::Test::Unit::TestCase
|
7
|
+
include Elasticsearch::DSL::Search::Aggregations
|
8
|
+
|
9
|
+
context "Bucket Selector agg" do
|
10
|
+
subject { BucketSelector.new }
|
11
|
+
|
12
|
+
should "be converted to a hash" do
|
13
|
+
assert_equal({ bucket_selector: {} }, subject.to_hash)
|
14
|
+
end
|
15
|
+
|
16
|
+
should "have option methods" do
|
17
|
+
subject = BucketSelector.new :foo
|
18
|
+
|
19
|
+
subject.buckets_path foo: 'foo', bar: 'bar'
|
20
|
+
subject.script 'bar'
|
21
|
+
subject.gap_policy 'skip'
|
22
|
+
|
23
|
+
assert_equal %w[ buckets_path gap_policy script ],
|
24
|
+
subject.to_hash[:bucket_selector][:foo].keys.map(&:to_s).sort
|
25
|
+
assert_equal 'bar', subject.to_hash[:bucket_selector][:foo][:buckets_path][:bar]
|
26
|
+
end
|
27
|
+
|
28
|
+
should "take a block" do
|
29
|
+
subject = BucketSelector.new :foo do
|
30
|
+
gap_policy 'skip'
|
31
|
+
end
|
32
|
+
assert_equal({bucket_selector: { foo: { gap_policy: 'skip' } } }, subject.to_hash)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Elasticsearch
|
4
|
+
module Test
|
5
|
+
module Aggregations
|
6
|
+
class CumulativeSumTest < ::Test::Unit::TestCase
|
7
|
+
include Elasticsearch::DSL::Search::Aggregations
|
8
|
+
|
9
|
+
context "Cumulative Sum Bucket agg" do
|
10
|
+
subject { CumulativeSum.new }
|
11
|
+
|
12
|
+
should "be converted to a hash" do
|
13
|
+
assert_equal({ cumulative_sum: {} }, subject.to_hash)
|
14
|
+
end
|
15
|
+
|
16
|
+
should "have option methods" do
|
17
|
+
subject = CumulativeSum.new :foo
|
18
|
+
|
19
|
+
subject.buckets_path 'bar'
|
20
|
+
subject.format 'bar'
|
21
|
+
|
22
|
+
assert_equal %w[ buckets_path format ],
|
23
|
+
subject.to_hash[:cumulative_sum][:foo].keys.map(&:to_s).sort
|
24
|
+
assert_equal 'bar', subject.to_hash[:cumulative_sum][:foo][:buckets_path]
|
25
|
+
end
|
26
|
+
|
27
|
+
should "take a block" do
|
28
|
+
subject = CumulativeSum.new :foo do
|
29
|
+
format 'bar'
|
30
|
+
end
|
31
|
+
assert_equal({cumulative_sum: { foo: { format: 'bar' } } }, subject.to_hash)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Elasticsearch
|
4
|
+
module Test
|
5
|
+
module Aggregations
|
6
|
+
class DerivativeTest < ::Test::Unit::TestCase
|
7
|
+
include Elasticsearch::DSL::Search::Aggregations
|
8
|
+
|
9
|
+
context "Derivative agg" do
|
10
|
+
subject { Derivative.new }
|
11
|
+
|
12
|
+
should "be converted to a hash" do
|
13
|
+
assert_equal({ derivative: {} }, subject.to_hash)
|
14
|
+
end
|
15
|
+
|
16
|
+
should "have option methods" do
|
17
|
+
subject = Derivative.new :foo
|
18
|
+
|
19
|
+
subject.buckets_path 'bar'
|
20
|
+
subject.gap_policy 'bar'
|
21
|
+
subject.format 'bar'
|
22
|
+
|
23
|
+
assert_equal %w[ buckets_path format gap_policy ],
|
24
|
+
subject.to_hash[:derivative][:foo].keys.map(&:to_s).sort
|
25
|
+
assert_equal 'bar', subject.to_hash[:derivative][:foo][:buckets_path]
|
26
|
+
end
|
27
|
+
|
28
|
+
should "take a block" do
|
29
|
+
subject = Derivative.new :foo do
|
30
|
+
format 'bar'
|
31
|
+
end
|
32
|
+
assert_equal({derivative: { foo: { format: 'bar' } } }, subject.to_hash)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|