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,153 @@
|
|
1
|
+
# Matches documents with fields that have terms within a certain range.
|
2
|
+
require_relative 'query_builder'
|
3
|
+
module Queries
|
4
|
+
class RangeQueryBuilder < QueryBuilder
|
5
|
+
|
6
|
+
NAME = "range"
|
7
|
+
|
8
|
+
=begin
|
9
|
+
params:
|
10
|
+
@gt: Greater-than
|
11
|
+
@gte: Greater-than or equal to
|
12
|
+
@lt: Less-than
|
13
|
+
@lte: Less-than or equal to
|
14
|
+
@format: Formatted dates will be parsed using the format specified on the date field by default,
|
15
|
+
but it can be overridden by passing the format parameter to the range query
|
16
|
+
@time_zone: Dates can be converted from another timezone to UTC either by specifying the time zone in the date value itself
|
17
|
+
(if the format accepts it), or it can be specified as the time_zone parameter
|
18
|
+
|
19
|
+
@relation: range queries can be used on fields of type range, allowing to match a range specified in the query with a range field value in the document.
|
20
|
+
The relation parameter controls how these two ranges are matched:
|
21
|
+
WITHIN
|
22
|
+
Matches documents who’s range field is entirely within the query’s range.
|
23
|
+
CONTAINS
|
24
|
+
Matches documents who’s range field entirely contains the query’s range.
|
25
|
+
INTERSECTS
|
26
|
+
Matches documents who’s range field intersects the query’s range. This is the default value when querying range fields.
|
27
|
+
|
28
|
+
Date math and rounding
|
29
|
+
When using date math to round dates to the nearest day, month, hour, etc, the rounded dates depend on whether the ends of the ranges
|
30
|
+
are inclusive or exclusive.Rounding up moves to the last millisecond of the rounding scope, and rounding down to the first millisecond
|
31
|
+
of the rounding scope. For example:
|
32
|
+
gt
|
33
|
+
Greater than the date rounded up: 2014-11-18||/M becomes 2014-11-30T23:59:59.999, ie excluding the entire month.
|
34
|
+
gte
|
35
|
+
Greater than or equal to the date rounded down: 2014-11-18||/M becomes 2014-11-01, ie including the entire month.
|
36
|
+
lt
|
37
|
+
Less than the date rounded down: 2014-11-18||/M becomes 2014-11-01, ie excluding the entire month.
|
38
|
+
lte
|
39
|
+
Less than or equal to the date rounded up: 2014-11-18||/M becomes 2014-11-30T23:59:59.999, ie including the entire month.
|
40
|
+
=end
|
41
|
+
|
42
|
+
def initialize field_name:
|
43
|
+
@field_name = field_name
|
44
|
+
@gt= nil
|
45
|
+
@gte= nil
|
46
|
+
@lt= nil
|
47
|
+
@lte= nil
|
48
|
+
@format= nil
|
49
|
+
@time_zone = nil
|
50
|
+
@relation= nil
|
51
|
+
end
|
52
|
+
|
53
|
+
def query
|
54
|
+
query = {}
|
55
|
+
range_query = {}
|
56
|
+
field_options = self.common_query
|
57
|
+
field_options[:gt] = @gt if @gt.present?
|
58
|
+
field_options[:gte] = @gte if @gte.present?
|
59
|
+
field_options[:lt] = @lt if @lt.present?
|
60
|
+
field_options[:lte] = @lte if @lte.present?
|
61
|
+
field_options[:format] = @format if @format.present?
|
62
|
+
field_options[:time_zone] = @time_zone if @time_zone.present?
|
63
|
+
field_options[:relation] = @relation if @relation.present?
|
64
|
+
range_query[@field_name.intern] = field_options
|
65
|
+
query[name.intern] = range_query
|
66
|
+
return query
|
67
|
+
end
|
68
|
+
|
69
|
+
# returns field_name
|
70
|
+
def field_name_expr
|
71
|
+
return @field_name
|
72
|
+
end
|
73
|
+
|
74
|
+
########## Greater Than ##########
|
75
|
+
# returns gt
|
76
|
+
def gt_expr
|
77
|
+
return @gt
|
78
|
+
end
|
79
|
+
# sets gt
|
80
|
+
def gt value
|
81
|
+
@gt = value
|
82
|
+
return self
|
83
|
+
end
|
84
|
+
|
85
|
+
########## Greater Than Or Equal To ##########
|
86
|
+
# returns gte
|
87
|
+
def gte_expr
|
88
|
+
return @gte
|
89
|
+
end
|
90
|
+
# sets gte
|
91
|
+
def gte value
|
92
|
+
@gte = value
|
93
|
+
return self
|
94
|
+
end
|
95
|
+
|
96
|
+
########## Less Than ##########
|
97
|
+
# return lt
|
98
|
+
def lt_expr
|
99
|
+
return @lt
|
100
|
+
end
|
101
|
+
# sets lt
|
102
|
+
def lt value
|
103
|
+
@lt = value
|
104
|
+
return self
|
105
|
+
end
|
106
|
+
|
107
|
+
########## Less Than Or Equal To ##########
|
108
|
+
# returns lte
|
109
|
+
def lte_expr
|
110
|
+
return @lte
|
111
|
+
end
|
112
|
+
# sets lte
|
113
|
+
def lte value
|
114
|
+
@lte = value
|
115
|
+
return self
|
116
|
+
end
|
117
|
+
|
118
|
+
########## Format ##########
|
119
|
+
# returns format
|
120
|
+
def format_expr
|
121
|
+
return @format
|
122
|
+
end
|
123
|
+
# sets format
|
124
|
+
def format value
|
125
|
+
@format = value
|
126
|
+
return self
|
127
|
+
end
|
128
|
+
|
129
|
+
########## Time Zone ##########
|
130
|
+
# returns time_zone
|
131
|
+
def time_zone_expr
|
132
|
+
return @time_zone
|
133
|
+
end
|
134
|
+
# sets time_zone
|
135
|
+
def time_zone value
|
136
|
+
@time_zone = value
|
137
|
+
return self
|
138
|
+
end
|
139
|
+
|
140
|
+
########## RANGE RELATION ##########
|
141
|
+
# returns relation
|
142
|
+
def relation_expr
|
143
|
+
return @relation
|
144
|
+
end
|
145
|
+
# sets relation, input: ShapeRelation object
|
146
|
+
def relation shape_relation
|
147
|
+
@relation = shape_relation.relation
|
148
|
+
return self
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
=begin
|
2
|
+
Returns documents that contain an exact term in a provided field.
|
3
|
+
You can use the term query to find documents based on a precise value such as a price, a product ID, or a username.
|
4
|
+
=end
|
5
|
+
require_relative 'query_builder'
|
6
|
+
module Queries
|
7
|
+
class TermQueryBuilder < QueryBuilder
|
8
|
+
|
9
|
+
NAME = "term"
|
10
|
+
|
11
|
+
=begin
|
12
|
+
@params:
|
13
|
+
field_name: name of the field in the document which is being queried
|
14
|
+
value: is being matched to the data in that field.
|
15
|
+
=end
|
16
|
+
def initialize field_name:, value: nil
|
17
|
+
@field_name = field_name
|
18
|
+
@value = value
|
19
|
+
end
|
20
|
+
|
21
|
+
def query
|
22
|
+
query = {}
|
23
|
+
term_query = {}
|
24
|
+
field_options = self.common_query
|
25
|
+
field_options[:value] = @value
|
26
|
+
term_query[@field_name.intern] = field_options
|
27
|
+
query[name.intern] = term_query
|
28
|
+
return query
|
29
|
+
end
|
30
|
+
|
31
|
+
########## FIELD NAME ##########
|
32
|
+
# returns field_name
|
33
|
+
def field_name_expr
|
34
|
+
return @field_name
|
35
|
+
end
|
36
|
+
|
37
|
+
########## FIELD VALUE ##########
|
38
|
+
# returns value
|
39
|
+
def value_expr
|
40
|
+
return @value
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
=begin
|
2
|
+
Returns documents that contain one or more exact terms in a provided field.
|
3
|
+
The terms query is the same as the term query, except you can search for multiple values.
|
4
|
+
=end
|
5
|
+
require_relative 'query_builder'
|
6
|
+
module Queries
|
7
|
+
class TermsQueryBuilder < QueryBuilder
|
8
|
+
|
9
|
+
NAME = "terms"
|
10
|
+
|
11
|
+
=begin
|
12
|
+
@params:
|
13
|
+
field_name: name of the field in the document which is being queried
|
14
|
+
values: values which are matched to the data in that field can be:
|
15
|
+
Data Set:
|
16
|
+
Directly matched to the data in the given field.
|
17
|
+
Terms Lookup:
|
18
|
+
Terms lookup fetches the field values of an existing document.
|
19
|
+
Elasticsearch then uses those values as search terms. This can be helpful when searching for a large set of terms.
|
20
|
+
Because terms lookup fetches values from a document, the _source mapping field must be enabled to use terms lookup.
|
21
|
+
The _source field is enabled by default.
|
22
|
+
=end
|
23
|
+
def initialize field_name:, values: []
|
24
|
+
@field_name = field_name
|
25
|
+
if values.is_a?(Misc::TermsLookup)
|
26
|
+
@terms_lookup = values
|
27
|
+
else
|
28
|
+
@values = values.is_a?(Array) ? values : values.split(',')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def query
|
33
|
+
query = {}
|
34
|
+
terms_query = self.common_query
|
35
|
+
terms_query[@field_name.intern] = @values if @values.present?
|
36
|
+
terms_query[@field_name.intern] = @terms_lookup.settings if @terms_lookup.present?
|
37
|
+
query[name.intern] = terms_query
|
38
|
+
return query
|
39
|
+
end
|
40
|
+
|
41
|
+
########## FIELD NAME ##########
|
42
|
+
# returns field_name
|
43
|
+
def field_name_expr
|
44
|
+
return @field_name
|
45
|
+
end
|
46
|
+
|
47
|
+
########## FIELD VALUE ##########
|
48
|
+
# returns value
|
49
|
+
def values_expr
|
50
|
+
return @terms_lookup || @values
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# A sort builder to sort based on a document field.
|
2
|
+
require_relative 'sort_builder'
|
3
|
+
module Sort
|
4
|
+
class FieldSortBuilder < SortBuilder
|
5
|
+
|
6
|
+
NAME = "field_sort"
|
7
|
+
|
8
|
+
def initialize field_name:
|
9
|
+
@field_name = field_name
|
10
|
+
end
|
11
|
+
|
12
|
+
def query
|
13
|
+
query = {}
|
14
|
+
field_query = self.common_query
|
15
|
+
field_query[:mode] = @sort_mode if @sort_mode.present?
|
16
|
+
field_query[:missing] = @missing if @missing.present?
|
17
|
+
field_query[:unmapped_type] = @unmapped_type if @unmapped_type.present?
|
18
|
+
field_query[:nested] = @nested_sort.query[:nested] if @nested_sort.present?
|
19
|
+
query[@field_name.intern] = field_query
|
20
|
+
return query
|
21
|
+
end
|
22
|
+
|
23
|
+
# returns field_name
|
24
|
+
def field_name_expr
|
25
|
+
return @field_name
|
26
|
+
end
|
27
|
+
|
28
|
+
# returns nested_sort
|
29
|
+
def nested_sort_expr
|
30
|
+
return @nested_sort
|
31
|
+
end
|
32
|
+
# sets nested_sort
|
33
|
+
def nested_sort nested_sort
|
34
|
+
raise ArgumentError.new("pass a nested query object") if nested_sort.name != "nested"
|
35
|
+
@nested_sort = nested_sort
|
36
|
+
return self
|
37
|
+
end
|
38
|
+
|
39
|
+
# returns missing
|
40
|
+
def missing_expr
|
41
|
+
return @missing
|
42
|
+
end
|
43
|
+
# sets missing
|
44
|
+
def missing missing
|
45
|
+
@missing = missing.is_a?(Missing) ? missing.missing : missing
|
46
|
+
return self
|
47
|
+
end
|
48
|
+
|
49
|
+
# returns sort_mode
|
50
|
+
def sort_mode_expr
|
51
|
+
return @sort_mode
|
52
|
+
end
|
53
|
+
# sets sort_mode
|
54
|
+
def sort_mode sort_mode
|
55
|
+
@sort_mode = sort_mode.sort_mode
|
56
|
+
return self
|
57
|
+
end
|
58
|
+
|
59
|
+
# returns unmapped_type
|
60
|
+
def unmapped_type_expr
|
61
|
+
return @unmapped_type
|
62
|
+
end
|
63
|
+
# sets unmapped_type
|
64
|
+
def unmapped_type unmapped_type
|
65
|
+
@unmapped_type = unmapped_type
|
66
|
+
return self
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
# A geo distance based sorting on a geo point like field.
|
2
|
+
require_relative 'sort_builder'
|
3
|
+
module Sort
|
4
|
+
class GeoDistanceSortBuilder < SortBuilder
|
5
|
+
|
6
|
+
NAME = '_geo_distance'
|
7
|
+
|
8
|
+
=begin
|
9
|
+
@params:
|
10
|
+
field_name: The geo point like field the distance based sort operates on.
|
11
|
+
distance_type: How to compute the distance. Can either be arc (default),
|
12
|
+
or plane (faster, but inaccurate on long distances and close to the poles).
|
13
|
+
distance_unit: The unit to use when computing sort values. The default is m (meters).
|
14
|
+
nested_sort: Nested path within current object.
|
15
|
+
ignore_unmapped: Indicates if the unmapped field should be treated as a missing value.
|
16
|
+
Setting it to true is equivalent to specifying an unmapped_type in the field sort. The default is false (unmapped field cause the search to fail).
|
17
|
+
point: The point to create the range distance facets from.
|
18
|
+
sort_mode: What to do in case a field has several geo points. By default, the shortest distance is taken into account when sorting
|
19
|
+
in ascending orderand the longest distance when sorting in descending order. Supported values are min, max, median and avg.
|
20
|
+
validation_method: Set to IGNORE_MALFORMED to accept geo points with invalid latitude or longitude,
|
21
|
+
set to COERCE to additionally try and infer correct coordinates (default is STRICT).
|
22
|
+
=end
|
23
|
+
|
24
|
+
def initialize field_name:, point:
|
25
|
+
@field_name = field_name
|
26
|
+
@point = point
|
27
|
+
@distance_type = nil
|
28
|
+
@distance_unit = nil
|
29
|
+
@nested_sort = nil
|
30
|
+
@ignore_unmapped = nil
|
31
|
+
@sort_mode = nil
|
32
|
+
@validation_method = nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def query
|
36
|
+
query = {}
|
37
|
+
go_query = self.common_query
|
38
|
+
go_query[@field_name.to_s.intern] = @point.settings
|
39
|
+
go_query[:distance_type] = @distance_type if @distance_type.present?
|
40
|
+
go_query[:unit] = @distance_unit if @distance_unit.present?
|
41
|
+
go_query[:nested] = @nested_sort.query if @nested_sort.present?
|
42
|
+
go_query[:ignore_unmapped] = @ignore_unmapped if @ignore_unmapped.present?
|
43
|
+
go_query[:sort_mode] = @sort_mode if @sort_mode.present?
|
44
|
+
go_query[:validation_method] = @validation_method if @validation_method.present?
|
45
|
+
query[name.intern] = go_query
|
46
|
+
return query
|
47
|
+
end
|
48
|
+
|
49
|
+
# returns field_name
|
50
|
+
def field_name_expr
|
51
|
+
return @field_name
|
52
|
+
end
|
53
|
+
|
54
|
+
# returns point
|
55
|
+
def point_expr
|
56
|
+
return @point
|
57
|
+
end
|
58
|
+
# sets point
|
59
|
+
def point point
|
60
|
+
@point = point
|
61
|
+
return self
|
62
|
+
end
|
63
|
+
|
64
|
+
# returns distance_type
|
65
|
+
def distance_type_expr
|
66
|
+
return @distance_type
|
67
|
+
end
|
68
|
+
# sets distance_type
|
69
|
+
def distance_type distance_type
|
70
|
+
@distance_type = distance_type.distance_type
|
71
|
+
return self
|
72
|
+
end
|
73
|
+
|
74
|
+
# returns distance_unit
|
75
|
+
def distance_unit_expr
|
76
|
+
return @distance_unit
|
77
|
+
end
|
78
|
+
# sets distance_unit
|
79
|
+
def distance_unit distance_unit
|
80
|
+
@distance_unit = distance_unit.distance_unit
|
81
|
+
return self
|
82
|
+
end
|
83
|
+
|
84
|
+
# returns nested_sort
|
85
|
+
def nested_sort_expr
|
86
|
+
return @nested_sort
|
87
|
+
end
|
88
|
+
# sets nested_sort
|
89
|
+
def nested_sort nested_sort
|
90
|
+
@nested_sort = nested_sort
|
91
|
+
return self
|
92
|
+
end
|
93
|
+
|
94
|
+
# returns ignore_unmapped
|
95
|
+
def ignore_unmapped_expr
|
96
|
+
return @ignore_unmapped
|
97
|
+
end
|
98
|
+
# sets ignore_unmapped
|
99
|
+
def ignore_unmapped ignore_unmapped
|
100
|
+
@ignore_unmapped = ignore_unmapped
|
101
|
+
return self
|
102
|
+
end
|
103
|
+
|
104
|
+
# returns sort_mode
|
105
|
+
def sort_mode_expr
|
106
|
+
return @sort_mode
|
107
|
+
end
|
108
|
+
# sets sort_mode
|
109
|
+
def sort_mode sort_mode
|
110
|
+
@sort_mode = sort_mode.sort_mode
|
111
|
+
return self
|
112
|
+
end
|
113
|
+
|
114
|
+
def validation_method validation_method
|
115
|
+
@validation_method = validation_method.validation_method
|
116
|
+
return self
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
end
|