jay_api 27.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +786 -0
- data/README.md +61 -0
- data/jay_api.gemspec +38 -0
- data/lib/jay_api/abstract/connection.rb +50 -0
- data/lib/jay_api/abstract/constant_wait.rb +17 -0
- data/lib/jay_api/abstract/geometric_wait.rb +35 -0
- data/lib/jay_api/abstract/wait_strategy.rb +43 -0
- data/lib/jay_api/configuration.rb +115 -0
- data/lib/jay_api/elasticsearch/async.rb +72 -0
- data/lib/jay_api/elasticsearch/batch_counter.rb +76 -0
- data/lib/jay_api/elasticsearch/client.rb +96 -0
- data/lib/jay_api/elasticsearch/client_factory.rb +100 -0
- data/lib/jay_api/elasticsearch/errors/elasticsearch_error.rb +13 -0
- data/lib/jay_api/elasticsearch/errors/end_of_query_results_error.rb +22 -0
- data/lib/jay_api/elasticsearch/errors/query_execution_error.rb +15 -0
- data/lib/jay_api/elasticsearch/errors/query_execution_failure.rb +17 -0
- data/lib/jay_api/elasticsearch/errors/query_execution_timeout.rb +13 -0
- data/lib/jay_api/elasticsearch/errors/search_after_error.rb +13 -0
- data/lib/jay_api/elasticsearch/index.rb +223 -0
- data/lib/jay_api/elasticsearch/query_builder/aggregations/aggregation.rb +66 -0
- data/lib/jay_api/elasticsearch/query_builder/aggregations/avg.rb +56 -0
- data/lib/jay_api/elasticsearch/query_builder/aggregations/errors/aggregations_error.rb +17 -0
- data/lib/jay_api/elasticsearch/query_builder/aggregations/errors.rb +14 -0
- data/lib/jay_api/elasticsearch/query_builder/aggregations/filter.rb +67 -0
- data/lib/jay_api/elasticsearch/query_builder/aggregations/max.rb +51 -0
- data/lib/jay_api/elasticsearch/query_builder/aggregations/scripted_metric.rb +72 -0
- data/lib/jay_api/elasticsearch/query_builder/aggregations/sum.rb +57 -0
- data/lib/jay_api/elasticsearch/query_builder/aggregations/terms.rb +73 -0
- data/lib/jay_api/elasticsearch/query_builder/aggregations/top_hits.rb +49 -0
- data/lib/jay_api/elasticsearch/query_builder/aggregations/value_count.rb +50 -0
- data/lib/jay_api/elasticsearch/query_builder/aggregations.rb +168 -0
- data/lib/jay_api/elasticsearch/query_builder/errors/query_builder_error.rb +16 -0
- data/lib/jay_api/elasticsearch/query_builder/query_clauses/bool.rb +179 -0
- data/lib/jay_api/elasticsearch/query_builder/query_clauses/exists.rb +33 -0
- data/lib/jay_api/elasticsearch/query_builder/query_clauses/match_all.rb +22 -0
- data/lib/jay_api/elasticsearch/query_builder/query_clauses/match_clauses.rb +140 -0
- data/lib/jay_api/elasticsearch/query_builder/query_clauses/match_none.rb +22 -0
- data/lib/jay_api/elasticsearch/query_builder/query_clauses/match_phrase.rb +35 -0
- data/lib/jay_api/elasticsearch/query_builder/query_clauses/negator.rb +42 -0
- data/lib/jay_api/elasticsearch/query_builder/query_clauses/query_clause.rb +17 -0
- data/lib/jay_api/elasticsearch/query_builder/query_clauses/query_string.rb +50 -0
- data/lib/jay_api/elasticsearch/query_builder/query_clauses/range.rb +49 -0
- data/lib/jay_api/elasticsearch/query_builder/query_clauses/regexp.rb +39 -0
- data/lib/jay_api/elasticsearch/query_builder/query_clauses/term.rb +37 -0
- data/lib/jay_api/elasticsearch/query_builder/query_clauses/terms.rb +37 -0
- data/lib/jay_api/elasticsearch/query_builder/query_clauses/wildcard.rb +37 -0
- data/lib/jay_api/elasticsearch/query_builder/query_clauses.rb +163 -0
- data/lib/jay_api/elasticsearch/query_builder/script.rb +36 -0
- data/lib/jay_api/elasticsearch/query_builder.rb +196 -0
- data/lib/jay_api/elasticsearch/query_results.rb +111 -0
- data/lib/jay_api/elasticsearch/response.rb +43 -0
- data/lib/jay_api/elasticsearch/search_after_results.rb +58 -0
- data/lib/jay_api/elasticsearch/tasks.rb +36 -0
- data/lib/jay_api/elasticsearch/time.rb +18 -0
- data/lib/jay_api/errors/configuration_error.rb +22 -0
- data/lib/jay_api/errors/error.rb +8 -0
- data/lib/jay_api/git/errors/invalid_repository_error.rb +11 -0
- data/lib/jay_api/git/errors/missing_url_error.rb +13 -0
- data/lib/jay_api/git/gerrit/gitiles_helper.rb +58 -0
- data/lib/jay_api/git/repository.rb +356 -0
- data/lib/jay_api/id_builder.rb +52 -0
- data/lib/jay_api/mergeables/merge_selector/configuration.rb +29 -0
- data/lib/jay_api/mergeables/merge_selector/merger.rb +58 -0
- data/lib/jay_api/mergeables/merge_selector.rb +15 -0
- data/lib/jay_api/prior_version_fetcher_base.rb +66 -0
- data/lib/jay_api/properties_fetcher.rb +196 -0
- data/lib/jay_api/rspec/configuration.rb +46 -0
- data/lib/jay_api/rspec/git.rb +60 -0
- data/lib/jay_api/rspec/test_data_collector.rb +189 -0
- data/lib/jay_api/rspec.rb +9 -0
- data/lib/jay_api/version.rb +6 -0
- data/lib/jay_api.rb +9 -0
- metadata +215 -0
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'aggregation'
|
4
|
+
|
5
|
+
module JayAPI
|
6
|
+
module Elasticsearch
|
7
|
+
class QueryBuilder
|
8
|
+
class Aggregations
|
9
|
+
# Represents a +max+ aggregation in Elasticsearch.
|
10
|
+
# Information on this type of aggregation can be found here:
|
11
|
+
# https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-max-aggregation.html
|
12
|
+
class Max < ::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Aggregation
|
13
|
+
attr_reader :field, :missing
|
14
|
+
|
15
|
+
# @param [String] name The name used by Elasticsearch to identify each
|
16
|
+
# of the aggregations.
|
17
|
+
# @param [String] field The field whose values should be added-up
|
18
|
+
def initialize(name, field:)
|
19
|
+
super(name)
|
20
|
+
|
21
|
+
@field = field
|
22
|
+
@missing = missing
|
23
|
+
end
|
24
|
+
|
25
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Aggregations::Errors::AggregationsError]
|
26
|
+
# Is always raised. The Max aggregation cannot have nested aggregations.
|
27
|
+
def aggs
|
28
|
+
no_nested_aggregations('Max')
|
29
|
+
end
|
30
|
+
|
31
|
+
# @return [self] A copy of the receiver.
|
32
|
+
def clone
|
33
|
+
self.class.new(name, field: field)
|
34
|
+
end
|
35
|
+
|
36
|
+
# @return [Hash] The Hash representation of the +Aggregation+.
|
37
|
+
# Properly formatted for Elasticsearch.
|
38
|
+
def to_h
|
39
|
+
super do
|
40
|
+
{
|
41
|
+
max: {
|
42
|
+
field: field
|
43
|
+
}
|
44
|
+
}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'aggregation'
|
4
|
+
|
5
|
+
module JayAPI
|
6
|
+
module Elasticsearch
|
7
|
+
class QueryBuilder
|
8
|
+
class Aggregations
|
9
|
+
# Represents a +scripted_metric+ aggregation in Elasticsearch.
|
10
|
+
# Information about this type of aggregation can be found in:
|
11
|
+
# https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html
|
12
|
+
class ScriptedMetric < ::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Aggregation
|
13
|
+
attr_reader :init_script, :map_script, :combine_script, :reduce_script
|
14
|
+
|
15
|
+
# @param [String] name The name used by Elasticsearch to identify each
|
16
|
+
# of the aggregations.
|
17
|
+
# @param [String] init_script The script that gets executed prior to
|
18
|
+
# any collection of documents. Allows the aggregation to set up any
|
19
|
+
# initial state.
|
20
|
+
# @param [String] map_script The script that gets executed once per
|
21
|
+
# document collected. This is a required script.
|
22
|
+
# @param [String] combine_script The script that gets executed once on
|
23
|
+
# each shard after document collection is complete. Allows the
|
24
|
+
# aggregation to consolidate the state returned from each shard.
|
25
|
+
# @param [String] reduce_script The script that gets executed once on
|
26
|
+
# the coordinating node after all shards have returned their
|
27
|
+
# results. The script is provided with access to a variable +states+
|
28
|
+
# which is an array of the result of the combine_script on each
|
29
|
+
# shard.
|
30
|
+
def initialize(name, map_script:, combine_script:, reduce_script:, init_script: nil)
|
31
|
+
super(name)
|
32
|
+
|
33
|
+
@init_script = init_script
|
34
|
+
@map_script = map_script
|
35
|
+
@combine_script = combine_script
|
36
|
+
@reduce_script = reduce_script
|
37
|
+
end
|
38
|
+
|
39
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Aggregations::Errors::AggregationsError]
|
40
|
+
# Is always raised. The Scripted Metric aggregation cannot have
|
41
|
+
# nested aggregations.
|
42
|
+
def aggs
|
43
|
+
no_nested_aggregations('Scripted Metric')
|
44
|
+
end
|
45
|
+
|
46
|
+
# @return [self] A copy of the receiver.
|
47
|
+
def clone
|
48
|
+
self.class.new(
|
49
|
+
name, map_script: map_script, combine_script: combine_script,
|
50
|
+
reduce_script: reduce_script, init_script: init_script
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
54
|
+
# @return [Hash] The Hash representation of the +Aggregation+.
|
55
|
+
# Properly formatted for Elasticsearch.
|
56
|
+
def to_h
|
57
|
+
super do
|
58
|
+
{
|
59
|
+
scripted_metric: {
|
60
|
+
init_script: init_script,
|
61
|
+
map_script: map_script,
|
62
|
+
combine_script: combine_script,
|
63
|
+
reduce_script: reduce_script
|
64
|
+
}.compact
|
65
|
+
}
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'aggregation'
|
4
|
+
|
5
|
+
module JayAPI
|
6
|
+
module Elasticsearch
|
7
|
+
class QueryBuilder
|
8
|
+
class Aggregations
|
9
|
+
# Represents a +sum+ aggregation in Elasticsearch.
|
10
|
+
# Information on this type of aggregation can be found here:
|
11
|
+
# https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html
|
12
|
+
class Sum < ::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Aggregation
|
13
|
+
# TODO: Add script support to the aggregation
|
14
|
+
|
15
|
+
attr_reader :field, :missing
|
16
|
+
|
17
|
+
# @param [String] name The name used by Elasticsearch to identify each
|
18
|
+
# of the aggregations.
|
19
|
+
# @param [String] field The field whose values should be added-up
|
20
|
+
# @param [Numeric] missing The value to use when the field doesn't
|
21
|
+
# have a value. The type of the parameter depends on the type of the
|
22
|
+
# field in Elasticsearch.
|
23
|
+
def initialize(name, field:, missing: nil)
|
24
|
+
super(name)
|
25
|
+
|
26
|
+
@field = field
|
27
|
+
@missing = missing
|
28
|
+
end
|
29
|
+
|
30
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Aggregations::Errors::AggregationsError]
|
31
|
+
# Is always raised. The Sum aggregation cannot have nested aggregations.
|
32
|
+
def aggs
|
33
|
+
no_nested_aggregations('Sum')
|
34
|
+
end
|
35
|
+
|
36
|
+
# @return [self] A copy of the receiver.
|
37
|
+
def clone
|
38
|
+
self.class.new(name, field: field, missing: missing)
|
39
|
+
end
|
40
|
+
|
41
|
+
# @return [Hash] The Hash representation of the +Aggregation+.
|
42
|
+
# Properly formatted for Elasticsearch.
|
43
|
+
def to_h
|
44
|
+
super do
|
45
|
+
{
|
46
|
+
sum: {
|
47
|
+
field: field,
|
48
|
+
missing: missing
|
49
|
+
}.compact
|
50
|
+
}
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/core_ext/object/blank'
|
5
|
+
require 'active_support/core_ext/object/deep_dup'
|
6
|
+
|
7
|
+
require_relative 'aggregation'
|
8
|
+
|
9
|
+
module JayAPI
|
10
|
+
module Elasticsearch
|
11
|
+
class QueryBuilder
|
12
|
+
class Aggregations
|
13
|
+
# Represents a +terms+ aggregation in Elasticsearch.
|
14
|
+
# Information about this type of aggregation can be found in:
|
15
|
+
# https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html
|
16
|
+
class Terms < ::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Aggregation
|
17
|
+
attr_reader :field, :script, :size, :order
|
18
|
+
|
19
|
+
# @param [String] name The name used by Elasticsearch to identify each
|
20
|
+
# of the aggregations.
|
21
|
+
# @param [String] field The field whose unique values should be counted.
|
22
|
+
# @param [JayAPI::Elasticsearch::QueryBuilder::Script] script If a
|
23
|
+
# script is given the aggregation will count the unique values
|
24
|
+
# returned by the script instead of the unique values in a specific
|
25
|
+
# field.
|
26
|
+
# @param [Integer] size By default the aggregation returns the top 10
|
27
|
+
# unique values (the ones with the higher frequency). By specifying
|
28
|
+
# a size this can be changed.
|
29
|
+
# @param [Hash] order A custom order for the buckets produced by the
|
30
|
+
# aggregation. By default, the +terms+ aggregation orders terms by
|
31
|
+
# descending document +_count+. This can be changed by providing a
|
32
|
+
# custom +order+ hash.
|
33
|
+
# @raise [ArgumentError] If neither a +field+ nor a +script+ are given
|
34
|
+
# or if both of them are given. Only one should be present.
|
35
|
+
def initialize(name, field: nil, script: nil, size: nil, order: nil)
|
36
|
+
if (field.present? && script.present?) || (field.blank? && script.blank?)
|
37
|
+
raise ArgumentError, "Either 'field' or 'script' must be provided"
|
38
|
+
end
|
39
|
+
|
40
|
+
super(name)
|
41
|
+
|
42
|
+
@field = field
|
43
|
+
@script = script
|
44
|
+
@size = size
|
45
|
+
@order = order
|
46
|
+
end
|
47
|
+
|
48
|
+
# @return [self] A copy of the receiver.
|
49
|
+
def clone
|
50
|
+
self.class.new(name, field: field, script: script, size: size, order: order&.deep_dup).tap do |copy|
|
51
|
+
copy.aggregations = aggregations.clone
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# @return [Hash] The Hash representation of the +Aggregation+.
|
56
|
+
# Properly formatted for Elasticsearch.
|
57
|
+
def to_h
|
58
|
+
super do
|
59
|
+
{
|
60
|
+
terms: {
|
61
|
+
field: field,
|
62
|
+
size: size,
|
63
|
+
script: script&.to_h,
|
64
|
+
order: order
|
65
|
+
}.compact
|
66
|
+
}
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/core_ext/string/inflections'
|
5
|
+
|
6
|
+
require_relative '../query_clauses'
|
7
|
+
require_relative 'aggregation'
|
8
|
+
require_relative 'errors/aggregations_error'
|
9
|
+
|
10
|
+
module JayAPI
|
11
|
+
module Elasticsearch
|
12
|
+
class QueryBuilder
|
13
|
+
class Aggregations
|
14
|
+
# Represents a +filter+ aggregation in Elasticsearch.
|
15
|
+
# Information on this type of aggregation can be found here:
|
16
|
+
# https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html
|
17
|
+
class TopHits < ::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Aggregation
|
18
|
+
attr_reader :size
|
19
|
+
|
20
|
+
# @param [String] name The name used by Elasticsearch to identify each
|
21
|
+
# of the aggregations.
|
22
|
+
# @param [String] size The number of hits that will be returned.
|
23
|
+
def initialize(name, size:)
|
24
|
+
super(name)
|
25
|
+
|
26
|
+
@size = size
|
27
|
+
end
|
28
|
+
|
29
|
+
# @return [self] A copy of the receiver.
|
30
|
+
def clone
|
31
|
+
self.class.new(name, size: size)
|
32
|
+
end
|
33
|
+
|
34
|
+
# @return [Hash] The Hash representation of the +Aggregation+.
|
35
|
+
# Properly formatted for Elasticsearch.
|
36
|
+
def to_h
|
37
|
+
super do
|
38
|
+
{
|
39
|
+
top_hits: {
|
40
|
+
size: size
|
41
|
+
}
|
42
|
+
}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'aggregation'
|
4
|
+
|
5
|
+
module JayAPI
|
6
|
+
module Elasticsearch
|
7
|
+
class QueryBuilder
|
8
|
+
class Aggregations
|
9
|
+
# Represents a +value_count+ aggregation in Elasticsearch.
|
10
|
+
# Information about this type of aggregation can be found in:
|
11
|
+
# https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html
|
12
|
+
class ValueCount < ::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Aggregation
|
13
|
+
# TODO: Add script support to the aggregation
|
14
|
+
|
15
|
+
attr_reader :field
|
16
|
+
|
17
|
+
# @param [String] field The whose non-empty values should be counted.
|
18
|
+
def initialize(name, field:)
|
19
|
+
@field = field
|
20
|
+
super(name)
|
21
|
+
end
|
22
|
+
|
23
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Aggregations::Errors::AggregationsError]
|
24
|
+
# Is always raised. The Value Count aggregation cannot have nested
|
25
|
+
# aggregations.
|
26
|
+
def aggs
|
27
|
+
no_nested_aggregations('Value Count')
|
28
|
+
end
|
29
|
+
|
30
|
+
# @return [self] A copy of the receiver.
|
31
|
+
def clone
|
32
|
+
self.class.new(name, field: field)
|
33
|
+
end
|
34
|
+
|
35
|
+
# @return [Hash] The Hash representation of the +Aggregation+.
|
36
|
+
# Properly formatted for Elasticsearch.
|
37
|
+
def to_h
|
38
|
+
super do
|
39
|
+
{
|
40
|
+
value_count: {
|
41
|
+
field: field
|
42
|
+
}
|
43
|
+
}
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,168 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'forwardable'
|
4
|
+
|
5
|
+
require_relative 'aggregations/aggregation'
|
6
|
+
require_relative 'aggregations/avg'
|
7
|
+
require_relative 'aggregations/filter'
|
8
|
+
require_relative 'aggregations/scripted_metric'
|
9
|
+
require_relative 'aggregations/sum'
|
10
|
+
require_relative 'aggregations/max'
|
11
|
+
require_relative 'aggregations/terms'
|
12
|
+
require_relative 'aggregations/value_count'
|
13
|
+
require_relative 'aggregations/top_hits'
|
14
|
+
require_relative 'aggregations/errors'
|
15
|
+
|
16
|
+
module JayAPI
|
17
|
+
module Elasticsearch
|
18
|
+
class QueryBuilder
|
19
|
+
# The list of aggregations to be included in an Elasticsearch query.
|
20
|
+
class Aggregations
|
21
|
+
extend Forwardable
|
22
|
+
|
23
|
+
def_delegators :aggregations, :any?, :none?
|
24
|
+
|
25
|
+
def initialize
|
26
|
+
@aggregations = []
|
27
|
+
end
|
28
|
+
|
29
|
+
# Adds a +terms+ type aggregation. For information about the parameters
|
30
|
+
# @see JayAPI::Elasticsearch::QueryBuilder::Aggregations::Terms#initialize
|
31
|
+
def terms(name, field: nil, script: nil, size: nil, order: nil)
|
32
|
+
add(
|
33
|
+
::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Terms.new(
|
34
|
+
name, field: field, script: script, size: size, order: order
|
35
|
+
)
|
36
|
+
)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Adds an +avg+ type aggregation. For information about the parameters
|
40
|
+
# @see JayAPI::Elasticsearch::QueryBuilder::Aggregations::Avg#initialize
|
41
|
+
def avg(name, field:, missing: nil)
|
42
|
+
add(
|
43
|
+
::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Avg.new(
|
44
|
+
name, field: field, missing: missing
|
45
|
+
)
|
46
|
+
)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Adds a +sum+ type aggregation. For information about the parameters
|
50
|
+
# @see JayAPI::Elasticsearch::QueryBuilder::Aggregations::Sum#initialize
|
51
|
+
def sum(name, field:, missing: nil)
|
52
|
+
add(
|
53
|
+
::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Sum.new(
|
54
|
+
name, field: field, missing: missing
|
55
|
+
)
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Adds a +value_count+ type aggregation. For information about the parameters
|
60
|
+
# @see JayAPI::Elasticsearch::QueryBuilder::Aggregations::ValueCount#initialize
|
61
|
+
def value_count(name, field:)
|
62
|
+
add(
|
63
|
+
::JayAPI::Elasticsearch::QueryBuilder::Aggregations::ValueCount.new(
|
64
|
+
name, field: field
|
65
|
+
)
|
66
|
+
)
|
67
|
+
end
|
68
|
+
|
69
|
+
# Adds a +top_hits+ type aggregation. For more information about the parameters
|
70
|
+
# @see JayAPI::Elasticsearch::QueryBuilder::Aggregations::TopHits#initialize
|
71
|
+
def top_hits(name, size:)
|
72
|
+
add(::JayAPI::Elasticsearch::QueryBuilder::Aggregations::TopHits.new(name, size: size))
|
73
|
+
end
|
74
|
+
|
75
|
+
# Adds an +scripted_metric+ type aggregation. For information about the parameters
|
76
|
+
# @see JayAPI::Elasticsearch::QueryBuilder::Aggregations::ScriptedMetric#initialize
|
77
|
+
def scripted_metric(name, map_script:, combine_script:, reduce_script:, init_script: nil)
|
78
|
+
add(
|
79
|
+
::JayAPI::Elasticsearch::QueryBuilder::Aggregations::ScriptedMetric.new(
|
80
|
+
name, map_script: map_script, combine_script: combine_script,
|
81
|
+
reduce_script: reduce_script, init_script: init_script
|
82
|
+
)
|
83
|
+
)
|
84
|
+
end
|
85
|
+
|
86
|
+
# Adds a +max+ type aggregation. For information about the parameters
|
87
|
+
# @see JayAPI::Elasticsearch::QueryBuilder::Aggregations::Max#initialize
|
88
|
+
def max(name, field:)
|
89
|
+
add(
|
90
|
+
::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Max.new(
|
91
|
+
name, field: field
|
92
|
+
)
|
93
|
+
)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Adds a +filter+ type aggregation. For more information about the parameters
|
97
|
+
# @see JayAPI::Elasticsearch::QueryBuilder::Aggregations::Filter#initialize
|
98
|
+
def filter(name, &block)
|
99
|
+
add(::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Filter.new(name, &block))
|
100
|
+
end
|
101
|
+
|
102
|
+
# Returns a Hash with the correct format for the current list of
|
103
|
+
# aggregations. For example:
|
104
|
+
#
|
105
|
+
# {
|
106
|
+
# "aggs" => {
|
107
|
+
# "my-agg-name" => {
|
108
|
+
# "terms" => {
|
109
|
+
# "field" => "my_field"
|
110
|
+
# }
|
111
|
+
# },
|
112
|
+
# "my-average" => {
|
113
|
+
# "avg" => {
|
114
|
+
# "field" => "my_numeric_field"
|
115
|
+
# }
|
116
|
+
# }
|
117
|
+
# }
|
118
|
+
# }
|
119
|
+
#
|
120
|
+
# @return [Hash] A Hash with the list of aggregations
|
121
|
+
def to_h
|
122
|
+
return {} if none?
|
123
|
+
|
124
|
+
{
|
125
|
+
aggs: aggregations.inject({}) do |hash, aggregation|
|
126
|
+
hash.merge(aggregation.to_h)
|
127
|
+
end
|
128
|
+
}
|
129
|
+
end
|
130
|
+
|
131
|
+
# @param [self] other The object to merge with the receiver.
|
132
|
+
# @return [self] A new object, which represents the combination of the
|
133
|
+
# aggregations in the receiver and +other+.
|
134
|
+
# @raise [TypeError] If +other+ is not an instance of the same class
|
135
|
+
# (or a subclass of it).
|
136
|
+
def merge(other)
|
137
|
+
klass = self.class
|
138
|
+
raise TypeError, "Cannot merge #{klass} with #{other.class}" unless other.is_a?(klass)
|
139
|
+
|
140
|
+
klass.new.tap do |merged|
|
141
|
+
merged.aggregations = aggregations.map(&:clone) + other.aggregations.map(&:clone)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
# @return [self] A copy of the receiver.
|
146
|
+
def clone
|
147
|
+
self.class.new.tap do |clone|
|
148
|
+
clone.aggregations = aggregations.map(&:clone)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
protected
|
153
|
+
|
154
|
+
attr_accessor :aggregations
|
155
|
+
|
156
|
+
# Adds the given +aggregation+ to the +aggregations+ array.
|
157
|
+
# @return [JayAPI::Elasticsearch::QueryBuilder::Aggregations::Aggregation]
|
158
|
+
# The added aggregation. This has two reasons:
|
159
|
+
# * To keep the inner +aggregations+ array from leaking out.
|
160
|
+
# * To allow nesting of aggregations in the future.
|
161
|
+
def add(aggregation)
|
162
|
+
aggregations << aggregation
|
163
|
+
aggregation
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../../../errors/error'
|
4
|
+
|
5
|
+
module JayAPI
|
6
|
+
module Elasticsearch
|
7
|
+
class QueryBuilder
|
8
|
+
module Errors
|
9
|
+
# An error to be raised when the user tries to build a query which would
|
10
|
+
# result in an invalid query for Elasticsearch.
|
11
|
+
class QueryBuilderError < JayAPI::Errors::Error
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|