es_query_builder 2.0.0 → 2.0.1
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 +4 -4
- data/lib/aggregations/buckets/date_histogram_aggregation_builder.rb +64 -0
- data/lib/aggregations/buckets/date_range_aggregation_builder.rb +25 -0
- data/lib/aggregations/buckets/filter_aggregation_builder.rb +30 -0
- data/lib/aggregations/buckets/filters_aggregation_builder.rb +32 -0
- data/lib/aggregations/buckets/geo_grid_aggregation_builder.rb +50 -0
- data/lib/aggregations/buckets/histogram_aggregation_builder.rb +63 -0
- data/lib/aggregations/buckets/nested_aggregation_builder.rb +33 -0
- data/lib/aggregations/buckets/range_aggregation_builder.rb +25 -0
- data/lib/aggregations/buckets/reverse_nested_aggregation_builder.rb +23 -0
- data/lib/aggregations/buckets/terms_aggregation_builder.rb +87 -0
- data/lib/aggregations/buckets/top_hits_aggregation_builder.rb +78 -0
- data/lib/aggregations/helpers/abstract_aggregation_helper.rb +37 -0
- data/lib/aggregations/helpers/abstract_range_helper.rb +40 -0
- data/lib/aggregations/helpers/aggregation_query_builder_helper.rb +61 -0
- data/lib/aggregations/helpers/values_source_aggregation_helper.rb +53 -0
- data/lib/aggregations/metrics/avg_aggregation_builder.rb +24 -0
- data/lib/aggregations/metrics/max_aggregation_builder.rb +24 -0
- data/lib/aggregations/metrics/min_aggregation_builder.rb +24 -0
- data/lib/enums/combine_functions.rb +54 -0
- data/lib/enums/distance_types.rb +36 -0
- data/lib/enums/distance_units.rb +71 -0
- data/lib/enums/fuzziness.rb +56 -0
- data/lib/enums/orders.rb +17 -0
- data/lib/enums/score_modes.rb +54 -0
- data/lib/enums/script_sort_types.rb +32 -0
- data/lib/enums/script_types.rb +19 -0
- data/lib/enums/shape_relations.rb +47 -0
- data/lib/enums/sort_modes.rb +59 -0
- data/lib/enums/sort_orders.rb +33 -0
- data/lib/enums/validation_methods.rb +44 -0
- data/lib/enums/zero_terms_query.rb +37 -0
- data/lib/function_scores/random_score_function_builder.rb +53 -0
- data/lib/function_scores/score_function_builder.rb +19 -0
- data/lib/function_scores/script_score_function_builder.rb +30 -0
- data/lib/function_scores/weighted_score_function_builder.rb +38 -0
- data/lib/misc/bucket_order.rb +33 -0
- data/lib/misc/geo_point.rb +64 -0
- data/lib/misc/range.rb +44 -0
- data/lib/misc/script.rb +62 -0
- data/lib/misc/terms_lookup.rb +45 -0
- data/lib/queries/abstract_query_builder.rb +31 -0
- data/lib/queries/base_query_builder.rb +27 -0
- data/lib/queries/bool_query_builder.rb +148 -0
- data/lib/queries/constant_score_query_builder.rb +32 -0
- data/lib/queries/dis_max_query_builder.rb +70 -0
- data/lib/queries/exists_query_builder.rb +32 -0
- data/lib/queries/function_score_query_builder.rb +170 -0
- data/lib/queries/geo_distance_query_builder.rb +126 -0
- data/lib/queries/match_all_query_builder.rb +16 -0
- data/lib/queries/match_query_builder.rb +181 -0
- data/lib/queries/nested_query_builder.rb +55 -0
- data/lib/queries/nested_sort_query_builder.rb +44 -0
- data/lib/queries/query_builder.rb +33 -0
- data/lib/queries/range_query_builder.rb +153 -0
- data/lib/queries/term_query_builder.rb +44 -0
- data/lib/queries/terms_query_builder.rb +54 -0
- data/lib/sort/abstract_sort_builder.rb +15 -0
- data/lib/sort/field_sort_builder.rb +70 -0
- data/lib/sort/geo_distance_sort_builder.rb +120 -0
- data/lib/sort/nested_sort_builder.rb +68 -0
- data/lib/sort/score_sort_builder.rb +16 -0
- data/lib/sort/script_sort_builder.rb +83 -0
- data/lib/sort/sort_builder.rb +28 -0
- metadata +64 -1
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'query_builder'
|
4
|
+
module Queries
|
5
|
+
# Wraps a query keyword around the query of the given object
|
6
|
+
class BaseQueryBuilder < QueryBuilder
|
7
|
+
NAME = 'query'
|
8
|
+
|
9
|
+
# @params [QueryBuilder] inner_query
|
10
|
+
# query object whose query is to be wrapped around query keyword
|
11
|
+
def initialize(inner_query:)
|
12
|
+
@inner_query = inner_query
|
13
|
+
end
|
14
|
+
|
15
|
+
# @return [Hash] serialized query hash for the object
|
16
|
+
def query
|
17
|
+
query = { name.intern => inner_query.query }
|
18
|
+
query
|
19
|
+
end
|
20
|
+
|
21
|
+
# @!visibility protected
|
22
|
+
# Returns inner_query
|
23
|
+
def inner_query_expr
|
24
|
+
@inner_query
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,148 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'query_builder'
|
4
|
+
module Queries
|
5
|
+
# A query that matches documents matching boolean combinations of
|
6
|
+
# other queries. The bool query maps to Lucene BooleanQuery.
|
7
|
+
# It is built using one or more boolean clauses,
|
8
|
+
# each clause with a typed occurrence
|
9
|
+
# QueryComponents
|
10
|
+
# should: queries that may appear in the matching documents and
|
11
|
+
# will contribute to scoring
|
12
|
+
# must: queries that must appear in the matching documents and
|
13
|
+
# will contribute to scoring
|
14
|
+
# must_not: queries that must not appear in the matching documents and
|
15
|
+
# will contribute to scoring
|
16
|
+
# filter: queries that must appear in the matching documents but
|
17
|
+
# don't contribute to scoring
|
18
|
+
# minimum_should_match: minimum should match as interger or percentage
|
19
|
+
class BoolQueryBuilder < QueryBuilder
|
20
|
+
NAME = 'bool'
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
@should_queries = []
|
24
|
+
@must_queries = []
|
25
|
+
@mustnot_queries = []
|
26
|
+
@filter_queries = []
|
27
|
+
@minimum_should_match = nil
|
28
|
+
end
|
29
|
+
|
30
|
+
# @return [Hash] returns serialized query hash for this object
|
31
|
+
def query
|
32
|
+
query = {}
|
33
|
+
bool_query = common_query
|
34
|
+
bool_query[:should] = @should_queries.map(&:query)
|
35
|
+
bool_query[:must] = @must_queries.map(&:query)
|
36
|
+
bool_query[:must_not] = @mustnot_queries.map(&:query)
|
37
|
+
bool_query[:filter] = @filter_queries.map(&:query)
|
38
|
+
bool_query[:minimum_should_match] = @minimum_should_match
|
39
|
+
bool_query[:boost] = @boost
|
40
|
+
query[name.intern] = bool_query.compact
|
41
|
+
query
|
42
|
+
end
|
43
|
+
|
44
|
+
# @!visibility protected
|
45
|
+
# @return [Array<QueryBuilder>] the list of filter queries
|
46
|
+
def filter_expr
|
47
|
+
@filter_queries
|
48
|
+
end
|
49
|
+
|
50
|
+
# @param [QueryBuilder] query_builder
|
51
|
+
# filter query to be added
|
52
|
+
# @return [BoolQueryBuilder] modified self
|
53
|
+
# adds a filter query
|
54
|
+
def filter(query_builder)
|
55
|
+
if query_builder.is_a?(Array)
|
56
|
+
@filter_queries += query_builder
|
57
|
+
else
|
58
|
+
@filter_queries.append(query_builder)
|
59
|
+
end
|
60
|
+
self
|
61
|
+
end
|
62
|
+
|
63
|
+
# @!visibility protected
|
64
|
+
# @return [Array<QueryBuilder>] the list of should queries
|
65
|
+
def should_expr
|
66
|
+
@should_queries
|
67
|
+
end
|
68
|
+
|
69
|
+
# @param [QueryBuilder] query_builder
|
70
|
+
# should query to be added
|
71
|
+
# @return [BoolQueryBuilder] modified self
|
72
|
+
# adds a should query
|
73
|
+
def should(query_builder)
|
74
|
+
if query_builder.is_a?(Array)
|
75
|
+
@should_queries += query_builder
|
76
|
+
else
|
77
|
+
@should_queries.append(query_builder)
|
78
|
+
end
|
79
|
+
self
|
80
|
+
end
|
81
|
+
|
82
|
+
# @!visibility protected
|
83
|
+
# @return [Array<QueryBuilder>] the list of must queries
|
84
|
+
def must_expr
|
85
|
+
@must_queries
|
86
|
+
end
|
87
|
+
|
88
|
+
# @param [QueryBuilder] query_builder
|
89
|
+
# must query to be added
|
90
|
+
# @return [BoolQueryBuilder] modified self
|
91
|
+
# adds a must query
|
92
|
+
def must(query_builder)
|
93
|
+
if query_builder.is_a?(Array)
|
94
|
+
@must_queries += query_builder
|
95
|
+
else
|
96
|
+
@must_queries.append(query_builder)
|
97
|
+
end
|
98
|
+
self
|
99
|
+
end
|
100
|
+
|
101
|
+
# @!visibility protected
|
102
|
+
# @return [Array<QueryBuilder>] the list of must_not queries
|
103
|
+
def must_not_expr
|
104
|
+
@mustnot_queries
|
105
|
+
end
|
106
|
+
|
107
|
+
# @param [QueryBuilder] query_builder
|
108
|
+
# must_not query to be added
|
109
|
+
# @return [BoolQueryBuilder] modified self
|
110
|
+
# adds a must_not query
|
111
|
+
def must_not(query_builder)
|
112
|
+
if query_builder.is_a?(Array)
|
113
|
+
@mustnot_queries += query_builder
|
114
|
+
else
|
115
|
+
@mustnot_queries.append(query_builder)
|
116
|
+
end
|
117
|
+
self
|
118
|
+
end
|
119
|
+
|
120
|
+
# @!visibility protected
|
121
|
+
# @return [Integer] minimum should match value for this query
|
122
|
+
def minimum_should_match_expr
|
123
|
+
@minimum_should_match
|
124
|
+
end
|
125
|
+
|
126
|
+
# @param [Integer] value
|
127
|
+
# minimum number of should conditions to fulfill
|
128
|
+
# @return [BoolQueryBuilder] modified self
|
129
|
+
# sets the minimum should match value
|
130
|
+
# for the given query object
|
131
|
+
def minimum_should_match(value)
|
132
|
+
raise 'Minimum Should match cannot be nil' if value.nil?
|
133
|
+
|
134
|
+
@minimum_should_match = value
|
135
|
+
self
|
136
|
+
end
|
137
|
+
|
138
|
+
# @return [Boolean] returns true iff this query builder has at least one
|
139
|
+
# should, must, must not or filter clause
|
140
|
+
def clauses?
|
141
|
+
shuld_clause = @should_queries.present?
|
142
|
+
must_clause = @must_queries.present?
|
143
|
+
mustnot_clause = @mustnot_queries.present?
|
144
|
+
filter_clause = @filter_queries.present?
|
145
|
+
shuld_clause && must_clause && mustnot_clause && filter_clause
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'query_builder'
|
4
|
+
module Queries
|
5
|
+
# A query that wraps a filter and simply returns a constant score
|
6
|
+
# equal to the query boost for every document in the filter.
|
7
|
+
class ConstantScoreQueryBuilder < QueryBuilder
|
8
|
+
NAME = 'constant_score'
|
9
|
+
|
10
|
+
# @params [QueryBuilder] inner_query query for whose
|
11
|
+
# matching documents constant score is to be set
|
12
|
+
# @params [Numeric] boost boosting value
|
13
|
+
def initialize(inner_query:)
|
14
|
+
@inner_query = inner_query
|
15
|
+
end
|
16
|
+
|
17
|
+
# @return [Hash] serialized json query for the object
|
18
|
+
def query
|
19
|
+
query = {}
|
20
|
+
cs_query = common_query
|
21
|
+
cs_query[:filter] = @inner_query.query if @inner_query.present?
|
22
|
+
query[name.intern] = cs_query
|
23
|
+
query
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns inner_query
|
27
|
+
# @!visibility protected
|
28
|
+
def inner_query_expr
|
29
|
+
@inner_query
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'query_builder'
|
4
|
+
module Queries
|
5
|
+
# A query that generates the union of documents produced
|
6
|
+
# by its sub-queries, and that scores each document
|
7
|
+
# with the maximum score for that document as produced by any sub-query,
|
8
|
+
# plus a tie breaking increment for any additional matching sub-queries.
|
9
|
+
class DisMaxQueryBuilder < QueryBuilder
|
10
|
+
# @!visibility protected
|
11
|
+
DEFAULT_TIE_BREAKER = 0.0
|
12
|
+
# @!visibility protected
|
13
|
+
NAME = 'dis_max'
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@queries = []
|
17
|
+
@tie_breaker = DEFAULT_TIE_BREAKER
|
18
|
+
end
|
19
|
+
|
20
|
+
def query
|
21
|
+
query = {}
|
22
|
+
dm_query = common_query
|
23
|
+
dm_query[:queries] = @queries.map(&:query) if @queries.present?
|
24
|
+
dm_query[:tie_breaker] = @tie_breaker
|
25
|
+
query[name.intern] = dm_query
|
26
|
+
query
|
27
|
+
end
|
28
|
+
|
29
|
+
# an immutable list copy of the current sub-queries of this disjunction
|
30
|
+
# @!visibilty protected
|
31
|
+
def inner_queries_expr
|
32
|
+
@queries
|
33
|
+
end
|
34
|
+
|
35
|
+
# returns the tie breaker value
|
36
|
+
# @!visibility protected
|
37
|
+
def tie_breaker_expr
|
38
|
+
@tie_breaker
|
39
|
+
end
|
40
|
+
|
41
|
+
# @params [Numeric] value tie_breaker value
|
42
|
+
# tie_breaker:
|
43
|
+
# the score of each non-maximum disjunct for a document is multiplied
|
44
|
+
# by this weight and added into the final score. If non-zero,
|
45
|
+
# the value should be small, on the order of 0.1, which says that
|
46
|
+
# 10 occurrences of word in a lower-scored field that is also in a
|
47
|
+
# higher scored field is just as good as a unique word in the lower
|
48
|
+
# scored field (i.e., one that is not in any higher scored field).
|
49
|
+
# adds the tie_breaker value
|
50
|
+
# @return [DisMaxQueryBuilder]
|
51
|
+
# modified self
|
52
|
+
def tie_breaker(value)
|
53
|
+
raise 'Tie Breaker value is nil' if value.nil?
|
54
|
+
|
55
|
+
@tie_breaker = value
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
# @params [QueryBuilder] query subquery to be added to the dismax query
|
60
|
+
# add a sub-query to this disjunction
|
61
|
+
# @return [DisMaxQueryBuilder]
|
62
|
+
# modified self
|
63
|
+
def add(query)
|
64
|
+
raise 'Query for Dismax is nil' if query.nil?
|
65
|
+
|
66
|
+
@queries.append(query)
|
67
|
+
self
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# Returns documents that contain a value other than null or [] in a provided field.
|
2
|
+
require_relative 'query_builder'
|
3
|
+
module Queries
|
4
|
+
class ExistsQueryBuilder < QueryBuilder
|
5
|
+
|
6
|
+
NAME = "exists"
|
7
|
+
|
8
|
+
attr_reader :field_name
|
9
|
+
|
10
|
+
=begin
|
11
|
+
@params:
|
12
|
+
field_name: name of the field the existence of whose value is checked
|
13
|
+
=end
|
14
|
+
def initialize field_name:
|
15
|
+
@field_name= field_name
|
16
|
+
end
|
17
|
+
|
18
|
+
def query
|
19
|
+
query = {}
|
20
|
+
exists_query = self.common_query
|
21
|
+
exists_query[:field] = @field_name
|
22
|
+
query[name.intern] = exists_query
|
23
|
+
return query
|
24
|
+
end
|
25
|
+
|
26
|
+
# returns field_name
|
27
|
+
def field_name_expr
|
28
|
+
return @field_name
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,170 @@
|
|
1
|
+
require_relative 'query_builder'
|
2
|
+
module Queries
|
3
|
+
class FunctionScoreQueryBuilder < QueryBuilder
|
4
|
+
|
5
|
+
NAME = "function_score"
|
6
|
+
|
7
|
+
=begin
|
8
|
+
@params:
|
9
|
+
query: query that specifies the documents to retrieve
|
10
|
+
filter_functions: function to be associated with an optional filter, meaning it will be executed only for the documents that match the given filter
|
11
|
+
score_mode: how results of individual score functions will be aggregated
|
12
|
+
boost_mode: how the combined result of score functions will influence the final score together with the sub query score
|
13
|
+
max_boost: maximum boost that will be applied by function score
|
14
|
+
min_score: used to exclude documents that do not meet a certain score threshold
|
15
|
+
score_function: function that defines how the documents will be scored
|
16
|
+
=end
|
17
|
+
|
18
|
+
def initialize query: nil, score_function: nil, filter_functions: [], boost_mode: nil
|
19
|
+
@function_query = query
|
20
|
+
@score_builder = score_function
|
21
|
+
@filter_functions = filter_functions
|
22
|
+
@score_mode = nil
|
23
|
+
@boost_mode = boost_mode
|
24
|
+
@max_boost = nil
|
25
|
+
@min_score = nil
|
26
|
+
end
|
27
|
+
|
28
|
+
def query
|
29
|
+
query = {}
|
30
|
+
fs_query = self.common_query
|
31
|
+
fs_query[:query] = @function_query.query if @function_query.present?
|
32
|
+
fs_query[:functions] = @filter_functions.map{|ff| ff.query} if @filter_functions.present?
|
33
|
+
fs_query[:score_mode] = @score_mode if @score_mode.present?
|
34
|
+
fs_query[:boost_mode] = @boost_mode if @boost_mode.present?
|
35
|
+
fs_query[:max_boost] = @max_boost if @max_boost.present?
|
36
|
+
fs_query[:min_score] = @min_score if @min_score.present?
|
37
|
+
fs_query.merge!(@score_builder.function)
|
38
|
+
query[name.intern] = fs_query
|
39
|
+
return query
|
40
|
+
end
|
41
|
+
|
42
|
+
########## Function Query ##########
|
43
|
+
# returns query
|
44
|
+
def query_expr
|
45
|
+
return @function_query
|
46
|
+
end
|
47
|
+
|
48
|
+
########## Score Function ##########
|
49
|
+
# returns score_function
|
50
|
+
def score_function_expr
|
51
|
+
return @score_builder
|
52
|
+
end
|
53
|
+
|
54
|
+
########## BOOST MODE #########################
|
55
|
+
# returns the Boost mode for the query
|
56
|
+
def boostmode_expr
|
57
|
+
return @boostmode
|
58
|
+
end
|
59
|
+
# sets the Boost mode for the query.
|
60
|
+
def boostmode combine_function
|
61
|
+
@boost_mode = combine_function.combine_function
|
62
|
+
return self
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
########## FILTER FUNCTIONS ##########
|
67
|
+
# returns the filters and functions
|
68
|
+
def filter_functions_expr
|
69
|
+
return @filter_functions
|
70
|
+
end
|
71
|
+
# sets filter function/s to query
|
72
|
+
def filter_function filter_function
|
73
|
+
@filter_functions.append(filter_function)
|
74
|
+
return self
|
75
|
+
end
|
76
|
+
|
77
|
+
######### MAX BOOST ##########
|
78
|
+
# returns max_boost
|
79
|
+
def max_boost_expr
|
80
|
+
return @max_boost
|
81
|
+
end
|
82
|
+
# sets max_boost
|
83
|
+
def max_boost value
|
84
|
+
raise "Max Boost cannot be nil value" if value.nil?
|
85
|
+
@max_boost = value.to_f
|
86
|
+
return self
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
########## SCORE MODE ##########
|
91
|
+
# returns the score mode for the query.
|
92
|
+
def score_mode_expr
|
93
|
+
return @score_mode
|
94
|
+
end
|
95
|
+
# sets the score mode for the query.
|
96
|
+
def score_mode score_mode
|
97
|
+
@score_mode = score_mode.score_mode
|
98
|
+
return self
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
########## MIN SCORE ##########
|
103
|
+
# returns the min score for the query
|
104
|
+
def min_score_expr
|
105
|
+
return @min_score
|
106
|
+
end
|
107
|
+
# sets the min score for the query
|
108
|
+
def min_score value
|
109
|
+
raise "Min Score cannot be nil value" if value.nil?
|
110
|
+
@min_score = value.to_f
|
111
|
+
return self
|
112
|
+
end
|
113
|
+
|
114
|
+
########## Filter Function Builder Class ##########
|
115
|
+
=begin
|
116
|
+
Function to be associated with an optional filter, meaning it will be executed only for the documents that match the given filter.
|
117
|
+
=end
|
118
|
+
class FilterFunctionBuilder
|
119
|
+
include AttributesReader
|
120
|
+
# attr_reader :filter, :score_builder, :weight
|
121
|
+
|
122
|
+
=begin
|
123
|
+
@params:
|
124
|
+
filter: filter to select the documents
|
125
|
+
weight: weight to be multiplied to function score before combining
|
126
|
+
score_function: function for scoring matching documents
|
127
|
+
=end
|
128
|
+
|
129
|
+
def initialize filter: nil, score_function: nil
|
130
|
+
@filter = filter
|
131
|
+
@score_builder = score_function
|
132
|
+
end
|
133
|
+
|
134
|
+
def query
|
135
|
+
query = {}
|
136
|
+
query[:filter] = @filter.query if @filter.present?
|
137
|
+
query[:weight] = @weight if @weight.present?
|
138
|
+
query.merge!(@score_builder.query) if @score_builder.present?
|
139
|
+
return query
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
########## Filter Query ##########
|
144
|
+
# returns filter query of the given filter function builder object
|
145
|
+
def filter_expr
|
146
|
+
return @filter
|
147
|
+
end
|
148
|
+
|
149
|
+
|
150
|
+
########## Score Function ##########
|
151
|
+
# returns score function of the given filter function builder object
|
152
|
+
def score_function_expr
|
153
|
+
return @score_builder
|
154
|
+
end
|
155
|
+
|
156
|
+
|
157
|
+
########## Multiplicative Weight ##########
|
158
|
+
# returns the weight for the filter function
|
159
|
+
def weight_expr
|
160
|
+
return @weight
|
161
|
+
end
|
162
|
+
# sets the weight for the filter function
|
163
|
+
def weight value
|
164
|
+
@weight = value.to_f
|
165
|
+
return self
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|