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,179 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../errors/query_builder_error'
|
4
|
+
require_relative 'query_clause'
|
5
|
+
require_relative 'match_clauses'
|
6
|
+
|
7
|
+
module JayAPI
|
8
|
+
module Elasticsearch
|
9
|
+
class QueryBuilder
|
10
|
+
class QueryClauses
|
11
|
+
# Represents an Elasticsearch boolean query clause. For more information
|
12
|
+
# about this type of clause check: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
|
13
|
+
class Bool < JayAPI::Elasticsearch::QueryBuilder::QueryClauses::QueryClause
|
14
|
+
include ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::MatchClauses
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
@query_clauses = {}
|
18
|
+
end
|
19
|
+
|
20
|
+
# Adds a +must+ clause to the +bool+ clause.
|
21
|
+
# @yield [JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool]
|
22
|
+
# Yields itself if a block is given.
|
23
|
+
# @return [JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool]
|
24
|
+
# Returns itself so that other methods can be chained.
|
25
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError]
|
26
|
+
# If no boolean clause currently exists.
|
27
|
+
def must(&block)
|
28
|
+
add_boolean_clause(:must, &block)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Adds a +filter+ clause to an already existing +bool+ clause.
|
32
|
+
# @yield [JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool]
|
33
|
+
# Yields itself if a block is given.
|
34
|
+
# @return [JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool]
|
35
|
+
# Returns itself so that other methods can be chained.
|
36
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError]
|
37
|
+
# If no boolean clause currently exists.
|
38
|
+
def filter(&block)
|
39
|
+
add_boolean_clause(:filter, &block)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Adds a +should+ clause to an already existing +bool+ clause.
|
43
|
+
# @yield [JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool]
|
44
|
+
# Yields itself if a block is given.
|
45
|
+
# @return [JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool]
|
46
|
+
# Returns itself so that other methods can be chained.
|
47
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError]
|
48
|
+
# If no boolean clause currently exists.
|
49
|
+
def should(&block)
|
50
|
+
add_boolean_clause(:should, &block)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Adds a +must_not+ clause to an already existing +bool+ clause.
|
54
|
+
# @yield [JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool]
|
55
|
+
# Yields itself if a block is given.
|
56
|
+
# @return [JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool]
|
57
|
+
# Returns itself so that other methods can be chained.
|
58
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError]
|
59
|
+
# If no boolean clause currently exists.
|
60
|
+
def must_not(&block)
|
61
|
+
add_boolean_clause(:must_not, &block)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Adds a clause to the current sub-clause of the boolean clause.
|
65
|
+
# @param [JayAPI::Elasticsearch::QueryBuilder::QueryClauses::QueryClause]
|
66
|
+
# query_clause The query clause to add.
|
67
|
+
# @return [JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool]
|
68
|
+
# Returns itself so that other methods can be chained.
|
69
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError]
|
70
|
+
# If no boolean sub-clause has been defined.
|
71
|
+
def <<(query_clause)
|
72
|
+
unless current_bool
|
73
|
+
raise ::JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError,
|
74
|
+
'Please call #must, #filter, #should or #must_not in order ' \
|
75
|
+
'to add query clauses inside a boolean clause'
|
76
|
+
end
|
77
|
+
|
78
|
+
current_bool << query_clause
|
79
|
+
self
|
80
|
+
end
|
81
|
+
|
82
|
+
# @return [Hash] The Hash representation of the Query Clause
|
83
|
+
def to_h
|
84
|
+
unless query_clauses.any?
|
85
|
+
raise ::JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError,
|
86
|
+
'A boolean clause has been defined but no boolean sub-clauses were added'
|
87
|
+
end
|
88
|
+
|
89
|
+
unless query_clauses.values.all?(&:any?)
|
90
|
+
raise ::JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError,
|
91
|
+
'A boolean clause and a sub-clause were defined but no match clauses were added'
|
92
|
+
end
|
93
|
+
|
94
|
+
{
|
95
|
+
bool: query_clauses.transform_values { |value| value.map(&:to_h) }
|
96
|
+
}
|
97
|
+
end
|
98
|
+
|
99
|
+
# @return [self] A copy of the receiver and all match clauses attached
|
100
|
+
# to it.
|
101
|
+
def clone
|
102
|
+
self.class.new.tap do |copy|
|
103
|
+
copy.query_clauses = query_clauses.transform_values do |array|
|
104
|
+
array.map(&:clone)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# @param [Other] other Another +QueryClause+ object.
|
110
|
+
# @return [self] The receiver, after having merged the given
|
111
|
+
# +QueryClause+ into itself.
|
112
|
+
def merge!(other)
|
113
|
+
if other.is_a?(self.class)
|
114
|
+
merge_clauses(other)
|
115
|
+
elsif other.is_a?(self.class.superclass)
|
116
|
+
must << other.clone
|
117
|
+
else
|
118
|
+
raise_cannot_merge_error(other)
|
119
|
+
end
|
120
|
+
|
121
|
+
self
|
122
|
+
end
|
123
|
+
|
124
|
+
# @param [Other] other Another +QueryClause+ object.
|
125
|
+
# @return [self] A new instance of the class with the merging of the
|
126
|
+
# receiver abd the given +QueryClause+ object.
|
127
|
+
def merge(other)
|
128
|
+
# This +if+ prevents the creation of a clone (which can be costly)
|
129
|
+
# only for an +ArgumentError+ to be raised when +#merge!+ is called.
|
130
|
+
if [self.class, self.class.superclass].any? { |klass| other.is_a?(klass) }
|
131
|
+
clone.merge!(other)
|
132
|
+
else
|
133
|
+
raise_cannot_merge_error(other)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
protected
|
138
|
+
|
139
|
+
attr_accessor :query_clauses # Used by #clone
|
140
|
+
|
141
|
+
private
|
142
|
+
|
143
|
+
attr_reader :current_bool
|
144
|
+
|
145
|
+
# Adds the given +bool+ sub-clause
|
146
|
+
# @param [Symbol] clause_type The type of clause to add (one of +must+,
|
147
|
+
# +filter+, +should+ or +must_not+)
|
148
|
+
# @yield [JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool]
|
149
|
+
# Yields itself if a block is given.
|
150
|
+
# @return [JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool]
|
151
|
+
# Returns itself so that other methods can be chained.
|
152
|
+
def add_boolean_clause(clause_type, &block)
|
153
|
+
@current_bool = query_clauses[clause_type] ||= []
|
154
|
+
yield self if block
|
155
|
+
self
|
156
|
+
end
|
157
|
+
|
158
|
+
# Merges the boolean clauses of the given +Bool+ into the query
|
159
|
+
# clauses of the receiver. Producing a union of both.
|
160
|
+
# @param [self] other Another instance of the class.
|
161
|
+
def merge_clauses(other)
|
162
|
+
all_clauses = query_clauses.keys.union(other.query_clauses.keys)
|
163
|
+
all_clauses.each do |clause|
|
164
|
+
others_clauses = other.query_clauses.fetch(clause, [])
|
165
|
+
|
166
|
+
query_clauses[clause] ||= []
|
167
|
+
query_clauses[clause] += others_clauses.map(&:clone)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
# @raise [ArgumentError] Is always raised with the appropriate error message.
|
172
|
+
def raise_cannot_merge_error(other)
|
173
|
+
raise ArgumentError, "Cannot merge #{self.class} with #{other.class}"
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'query_clause'
|
4
|
+
|
5
|
+
module JayAPI
|
6
|
+
module Elasticsearch
|
7
|
+
class QueryBuilder
|
8
|
+
class QueryClauses
|
9
|
+
# Represents an +Exists+ query
|
10
|
+
# Documentation for this type of query can be found here:
|
11
|
+
# https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html
|
12
|
+
class Exists < ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::QueryClause
|
13
|
+
attr_reader :field
|
14
|
+
|
15
|
+
# @param [String, Symbol] field The name of the field.
|
16
|
+
def initialize(field:)
|
17
|
+
@field = field
|
18
|
+
end
|
19
|
+
|
20
|
+
# @return [Hash] The Hash representation of the Query Clause (in
|
21
|
+
# Elasticsearch's format)
|
22
|
+
def to_h
|
23
|
+
{
|
24
|
+
exists: {
|
25
|
+
field: field
|
26
|
+
}
|
27
|
+
}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'query_clause'
|
4
|
+
|
5
|
+
module JayAPI
|
6
|
+
module Elasticsearch
|
7
|
+
class QueryBuilder
|
8
|
+
class QueryClauses
|
9
|
+
# Represents a +match_all+ query
|
10
|
+
# Documentation for this type of query can be found here:
|
11
|
+
# https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html
|
12
|
+
class MatchAll < QueryClause
|
13
|
+
# @return [Hash] The Hash representation of the Query Clause (in
|
14
|
+
# Elasticsearch's format)
|
15
|
+
def to_h
|
16
|
+
{ match_all: {} }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'exists'
|
4
|
+
require_relative 'match_all'
|
5
|
+
require_relative 'match_none'
|
6
|
+
require_relative 'match_phrase'
|
7
|
+
require_relative 'query_string'
|
8
|
+
require_relative 'range'
|
9
|
+
require_relative 'regexp'
|
10
|
+
require_relative 'term'
|
11
|
+
require_relative 'terms'
|
12
|
+
require_relative 'wildcard'
|
13
|
+
|
14
|
+
module JayAPI
|
15
|
+
module Elasticsearch
|
16
|
+
class QueryBuilder
|
17
|
+
class QueryClauses
|
18
|
+
# Provides an easy interface to add query clauses to objects that behave
|
19
|
+
# like query clauses sets.
|
20
|
+
# @see JayAPI::Elasticsearch::QueryBuilder::QueryClauses
|
21
|
+
# @see JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool
|
22
|
+
module MatchClauses
|
23
|
+
# Adds a +JayAPI::Elasticsearch::QueryBuilder::QueryClauses::MatchPhrase+
|
24
|
+
# clause to the Query Clauses set.
|
25
|
+
# @param [Hash] params The parameters for the +MatchPhrase+ class.
|
26
|
+
# @return [self] Returns itself so that other methods can be chained.
|
27
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError]
|
28
|
+
# If an error occurs when trying to add the query clause to the set.
|
29
|
+
def match_phrase(**params)
|
30
|
+
self << ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::MatchPhrase.new(**params)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Adds a +JayAPI::Elasticsearch::QueryBuilder::QueryClauses::QueryString+
|
34
|
+
# clause to the Query Clauses set.
|
35
|
+
# @param [Hash] params The parameters for the +QueryString+ class
|
36
|
+
# constructor.
|
37
|
+
# @see JayAPI::Elasticsearch::QueryBuilder::QueryClauses::QueryString#initialize
|
38
|
+
# @return [self] Returns itself so that other methods can be chained.
|
39
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError]
|
40
|
+
# If an error occurs when trying to add the query clause to the set.
|
41
|
+
def query_string(**params)
|
42
|
+
self << ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::QueryString.new(**params)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Adds a +JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Wildcard+
|
46
|
+
# clause to the Query Clauses set.
|
47
|
+
# @param [Hash] params The parameters for the +Wildcard+ class
|
48
|
+
# constructor.
|
49
|
+
# @see JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Wildcard#initialize
|
50
|
+
# @return [self] Returns itself so that other methods can be chained.
|
51
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError]
|
52
|
+
# If an error occurs when trying to add the query clause to the set.
|
53
|
+
def wildcard(**params)
|
54
|
+
self << ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Wildcard.new(**params)
|
55
|
+
end
|
56
|
+
|
57
|
+
# Adds a +JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Exists+
|
58
|
+
# clause to the Query Clauses set.
|
59
|
+
# @param [Hash] params The parameters for the +Exists+ class'
|
60
|
+
# constructor.
|
61
|
+
# @see JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Exists#initialize
|
62
|
+
# @return [self] Returns itself so that other methods can be chained.
|
63
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError]
|
64
|
+
# If an error occurs when trying to add the query clause to the set.
|
65
|
+
def exists(**params)
|
66
|
+
self << ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Exists.new(**params)
|
67
|
+
end
|
68
|
+
|
69
|
+
# Adds a +JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Term+
|
70
|
+
# clause to the Query Clauses set.
|
71
|
+
# @param [Hash] params The parameters for the +Term+ class'
|
72
|
+
# constructor.
|
73
|
+
# @see JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Term#initialize
|
74
|
+
# @return [self] Returns itself so that other methods can be chained.
|
75
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError]
|
76
|
+
# If an error occurs when trying to add the query clause to the set.
|
77
|
+
def term(**params)
|
78
|
+
self << ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Term.new(**params)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Adds a +JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Range+
|
82
|
+
# clause to the Query Clauses set.
|
83
|
+
# @param [Hash] params The parameters for the +Range+ class'
|
84
|
+
# constructor.
|
85
|
+
# @see JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Rang#initialize
|
86
|
+
# @return [self] Returns itself so that other methods can be chained.
|
87
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError]
|
88
|
+
# If an error occurs when trying to add the query clause to the set.
|
89
|
+
# @raise [ArgumentError] If there are any issues with the parameters
|
90
|
+
# passed down to the +Range+ class's constructor.
|
91
|
+
def range(**params)
|
92
|
+
self << ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Range.new(**params)
|
93
|
+
end
|
94
|
+
|
95
|
+
# Adds a +JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Terms+
|
96
|
+
# clause to the Query Clauses set.
|
97
|
+
# @param [Hash] params The parameters for the +Terms+ class'
|
98
|
+
# constructor.
|
99
|
+
# @see JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Terms#initialize
|
100
|
+
# @return [self] Returns itself, so that other methods can be chained.
|
101
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError]
|
102
|
+
# If an error occurs when trying to add the query clause to the set.
|
103
|
+
def terms(**params)
|
104
|
+
self << ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Terms.new(**params)
|
105
|
+
end
|
106
|
+
|
107
|
+
# Adds a +JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Regexp+
|
108
|
+
# clause to the Query Clauses set.
|
109
|
+
# @param [Hash] params The parameters for the +Regexp+ class'
|
110
|
+
# constructor.
|
111
|
+
# @see JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Regexp#initialize
|
112
|
+
# @return [self] Returns itself, so that other methods can be chained.
|
113
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError]
|
114
|
+
# If an error occurs when trying to add the query clause to the set.
|
115
|
+
def regexp(**params)
|
116
|
+
self << ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Regexp.new(**params)
|
117
|
+
end
|
118
|
+
|
119
|
+
# Adds a +JayAPI::Elasticsearch::QueryBuilder::QueryClauses::MatchAll+
|
120
|
+
# clause to the Query Clauses set.
|
121
|
+
# @return [self] Returns itself, so that other methods can be chained.
|
122
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError]
|
123
|
+
# If an error occurs when trying to add the query clause to the set.
|
124
|
+
def match_all
|
125
|
+
self << ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::MatchAll.new
|
126
|
+
end
|
127
|
+
|
128
|
+
# Adds a +JayAPI::Elasticsearch::QueryBuilder::QueryClauses::MatchNone+
|
129
|
+
# clause to the Query Clauses set.
|
130
|
+
# @return [self] Returns itself, so that other methods can be chained.
|
131
|
+
# @raise [JayAPI::Elasticsearch::QueryBuilder::Errors::QueryBuilderError]
|
132
|
+
# If an error occurs when trying to add the query clause to the set.
|
133
|
+
def match_none
|
134
|
+
self << ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::MatchNone.new
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'query_clause'
|
4
|
+
|
5
|
+
module JayAPI
|
6
|
+
module Elasticsearch
|
7
|
+
class QueryBuilder
|
8
|
+
class QueryClauses
|
9
|
+
# Represents a +match_none+ query
|
10
|
+
# Documentation for this type of query can be found here:
|
11
|
+
# https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html#query-dsl-match-none-query
|
12
|
+
class MatchNone < QueryClause
|
13
|
+
# @return [Hash] The Hash representation of the Query Clause (in
|
14
|
+
# Elasticsearch's format)
|
15
|
+
def to_h
|
16
|
+
{ match_none: {} }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'query_clause'
|
4
|
+
|
5
|
+
module JayAPI
|
6
|
+
module Elasticsearch
|
7
|
+
class QueryBuilder
|
8
|
+
class QueryClauses
|
9
|
+
# Represents a Match Phrase query in Elasticsearch
|
10
|
+
# Documentation for this type of query can be found here:
|
11
|
+
# https://www.elastic.co/guide/en/elasticsearch/reference/7.9/query-dsl-match-query-phrase.html#query-dsl-match-query-phrase
|
12
|
+
class MatchPhrase < ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::QueryClause
|
13
|
+
attr_reader :field, :phrase
|
14
|
+
|
15
|
+
# @param [String] field The name of the field to match against
|
16
|
+
# @param [String] phrase The phrase to match
|
17
|
+
def initialize(field:, phrase:)
|
18
|
+
@field = field
|
19
|
+
@phrase = phrase
|
20
|
+
end
|
21
|
+
|
22
|
+
# @return [Hash] The Hash representation of the Query Clause (in
|
23
|
+
# Elasticsearch's format)
|
24
|
+
def to_h
|
25
|
+
{
|
26
|
+
match_phrase: {
|
27
|
+
field => phrase
|
28
|
+
}
|
29
|
+
}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'bool'
|
4
|
+
require_relative 'match_all'
|
5
|
+
require_relative 'match_none'
|
6
|
+
|
7
|
+
module JayAPI
|
8
|
+
module Elasticsearch
|
9
|
+
class QueryBuilder
|
10
|
+
class QueryClauses
|
11
|
+
# A class capable of negating any QueryClause object
|
12
|
+
class Negator
|
13
|
+
# A mapping between QueryClause classes and their corresponding inverse
|
14
|
+
# clauses. Only some of the clauses can be directly inverted.
|
15
|
+
INVERSE_CLAUSES = {
|
16
|
+
::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::MatchAll =>
|
17
|
+
::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::MatchNone,
|
18
|
+
::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::MatchNone =>
|
19
|
+
::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::MatchAll
|
20
|
+
}.freeze
|
21
|
+
|
22
|
+
attr_reader :query_clause
|
23
|
+
|
24
|
+
# @param [JayAPI::Elasticsearch::QueryBuilder::QueryClauses::QueryClause] query_clause
|
25
|
+
# The +QueryClause+ to negate.
|
26
|
+
def initialize(query_clause)
|
27
|
+
@query_clause = query_clause
|
28
|
+
end
|
29
|
+
|
30
|
+
# @return [JayAPI::Elasticsearch::QueryBuilder::QueryClauses::QueryClause]
|
31
|
+
# The negated version of the given +QueryClause+
|
32
|
+
def negate
|
33
|
+
@negate ||= INVERSE_CLAUSES[query_clause.class]&.new ||
|
34
|
+
::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Bool.new.must_not do |bool_query|
|
35
|
+
bool_query << query_clause
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JayAPI
|
4
|
+
module Elasticsearch
|
5
|
+
class QueryBuilder
|
6
|
+
class QueryClauses
|
7
|
+
# This is the base class for all the query types that Elasticsearch can
|
8
|
+
# handle. See the subclasses for info on each of the query types.
|
9
|
+
class QueryClause
|
10
|
+
def to_h
|
11
|
+
raise NotImplementedError, "Please implement the #to_h method in the #{self.class} class"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/core_ext/array'
|
5
|
+
|
6
|
+
require_relative 'query_clause'
|
7
|
+
|
8
|
+
module JayAPI
|
9
|
+
module Elasticsearch
|
10
|
+
class QueryBuilder
|
11
|
+
class QueryClauses
|
12
|
+
# Represents a Query String query in Elasticsearch
|
13
|
+
# Documentation for this type of query can be found here:
|
14
|
+
# https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
|
15
|
+
class QueryString < ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::QueryClause
|
16
|
+
attr_reader :fields, :query
|
17
|
+
|
18
|
+
# @param [String, Array<String>] fields The field or fields to search
|
19
|
+
# into.
|
20
|
+
# @param [String] query The query string
|
21
|
+
def initialize(query:, fields: nil)
|
22
|
+
@fields = fields ? Array.wrap(fields) : nil
|
23
|
+
@query = query
|
24
|
+
end
|
25
|
+
|
26
|
+
# @return [Hash] The Hash that represents this query (in
|
27
|
+
# Elasticsearch's format)
|
28
|
+
def to_h
|
29
|
+
{
|
30
|
+
query_string: {
|
31
|
+
query: query
|
32
|
+
}.merge(fields_attribute)
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
# @return [Hash] A Hash with the +fields+ attribute for the
|
39
|
+
# +query_string+ clause or an empty array if no +fields+ were
|
40
|
+
# specified during initialization.
|
41
|
+
def fields_attribute
|
42
|
+
return {} unless fields
|
43
|
+
|
44
|
+
{ fields: fields }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'query_clause'
|
4
|
+
|
5
|
+
module JayAPI
|
6
|
+
module Elasticsearch
|
7
|
+
class QueryBuilder
|
8
|
+
class QueryClauses
|
9
|
+
# Represents a +Range+ query in Elasticsearch
|
10
|
+
# More information about this type of query can be found here:
|
11
|
+
# https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html
|
12
|
+
class Range < ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::QueryClause
|
13
|
+
VALID_PARAMS = %i[gt gte lt lte].freeze
|
14
|
+
|
15
|
+
attr_reader :field, :params
|
16
|
+
|
17
|
+
# @param [Hash] params A hash with parameters for the class.
|
18
|
+
# @option params [String, Symbol] :field The field where the range
|
19
|
+
# query should be applied.
|
20
|
+
# @option params [String, Numeric] :gt Greater than
|
21
|
+
# @option params [String, Numeric] :gte Greater than or equal
|
22
|
+
# @option params [String, Numeric] :lt Less than
|
23
|
+
# @option params [String, Numeric] :lte Less than or equal
|
24
|
+
def initialize(params)
|
25
|
+
@field = params.delete(:field) || raise(ArgumentError, "Missing required key 'field'")
|
26
|
+
|
27
|
+
invalid_keys = params.keys - VALID_PARAMS
|
28
|
+
raise ArgumentError, "Invalid keys: #{invalid_keys.join(', ')}" if invalid_keys.any?
|
29
|
+
|
30
|
+
params = params.compact
|
31
|
+
raise ArgumentError, "At least one of #{VALID_PARAMS.join(', ')} should be given" unless params.any?
|
32
|
+
|
33
|
+
@params = params
|
34
|
+
end
|
35
|
+
|
36
|
+
# @return [Hash] The Hash that represents this query (in
|
37
|
+
# Elasticsearch's format)
|
38
|
+
def to_h
|
39
|
+
{
|
40
|
+
range: {
|
41
|
+
field => params
|
42
|
+
}
|
43
|
+
}
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'query_clause'
|
4
|
+
|
5
|
+
module JayAPI
|
6
|
+
module Elasticsearch
|
7
|
+
class QueryBuilder
|
8
|
+
class QueryClauses
|
9
|
+
# Represents a +Regexp+ query in Elasticsearch
|
10
|
+
# More information about this type of query can be found here:
|
11
|
+
# https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html
|
12
|
+
class Regexp < ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::QueryClause
|
13
|
+
attr_reader :field, :value
|
14
|
+
|
15
|
+
# @param [String, Symbol] field The field where the regexp query
|
16
|
+
# should be applied.
|
17
|
+
# @param [String, Regexp] value Terms to be found in +field+
|
18
|
+
def initialize(field:, value:)
|
19
|
+
super()
|
20
|
+
@field = field
|
21
|
+
@value = value
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [Hash] The Hash that represents this query (in
|
25
|
+
# Elasticsearch's format)
|
26
|
+
def to_h
|
27
|
+
{
|
28
|
+
regexp: {
|
29
|
+
field => {
|
30
|
+
value: value
|
31
|
+
}
|
32
|
+
}
|
33
|
+
}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'query_clause'
|
4
|
+
|
5
|
+
module JayAPI
|
6
|
+
module Elasticsearch
|
7
|
+
class QueryBuilder
|
8
|
+
class QueryClauses
|
9
|
+
# Represents a Term query in Elasticsearch
|
10
|
+
# Information about this type of query can be found here:
|
11
|
+
# https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
|
12
|
+
class Term < ::JayAPI::Elasticsearch::QueryBuilder::QueryClauses::QueryClause
|
13
|
+
attr_reader :field, :value
|
14
|
+
|
15
|
+
# @param [String, Symbol] field The name of the field to search
|
16
|
+
# @param [String] value The value to search for
|
17
|
+
def initialize(field:, value:)
|
18
|
+
@field = field
|
19
|
+
@value = value
|
20
|
+
end
|
21
|
+
|
22
|
+
# @return [Hash] The Hash that represents this query (in
|
23
|
+
# Elasticsearch's format)
|
24
|
+
def to_h
|
25
|
+
{
|
26
|
+
term: {
|
27
|
+
field => {
|
28
|
+
value: value
|
29
|
+
}
|
30
|
+
}
|
31
|
+
}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|