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.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/lib/aggregations/buckets/date_histogram_aggregation_builder.rb +64 -0
  3. data/lib/aggregations/buckets/date_range_aggregation_builder.rb +25 -0
  4. data/lib/aggregations/buckets/filter_aggregation_builder.rb +30 -0
  5. data/lib/aggregations/buckets/filters_aggregation_builder.rb +32 -0
  6. data/lib/aggregations/buckets/geo_grid_aggregation_builder.rb +50 -0
  7. data/lib/aggregations/buckets/histogram_aggregation_builder.rb +63 -0
  8. data/lib/aggregations/buckets/nested_aggregation_builder.rb +33 -0
  9. data/lib/aggregations/buckets/range_aggregation_builder.rb +25 -0
  10. data/lib/aggregations/buckets/reverse_nested_aggregation_builder.rb +23 -0
  11. data/lib/aggregations/buckets/terms_aggregation_builder.rb +87 -0
  12. data/lib/aggregations/buckets/top_hits_aggregation_builder.rb +78 -0
  13. data/lib/aggregations/helpers/abstract_aggregation_helper.rb +37 -0
  14. data/lib/aggregations/helpers/abstract_range_helper.rb +40 -0
  15. data/lib/aggregations/helpers/aggregation_query_builder_helper.rb +61 -0
  16. data/lib/aggregations/helpers/values_source_aggregation_helper.rb +53 -0
  17. data/lib/aggregations/metrics/avg_aggregation_builder.rb +24 -0
  18. data/lib/aggregations/metrics/max_aggregation_builder.rb +24 -0
  19. data/lib/aggregations/metrics/min_aggregation_builder.rb +24 -0
  20. data/lib/enums/combine_functions.rb +54 -0
  21. data/lib/enums/distance_types.rb +36 -0
  22. data/lib/enums/distance_units.rb +71 -0
  23. data/lib/enums/fuzziness.rb +56 -0
  24. data/lib/enums/orders.rb +17 -0
  25. data/lib/enums/score_modes.rb +54 -0
  26. data/lib/enums/script_sort_types.rb +32 -0
  27. data/lib/enums/script_types.rb +19 -0
  28. data/lib/enums/shape_relations.rb +47 -0
  29. data/lib/enums/sort_modes.rb +59 -0
  30. data/lib/enums/sort_orders.rb +33 -0
  31. data/lib/enums/validation_methods.rb +44 -0
  32. data/lib/enums/zero_terms_query.rb +37 -0
  33. data/lib/function_scores/random_score_function_builder.rb +53 -0
  34. data/lib/function_scores/score_function_builder.rb +19 -0
  35. data/lib/function_scores/script_score_function_builder.rb +30 -0
  36. data/lib/function_scores/weighted_score_function_builder.rb +38 -0
  37. data/lib/misc/bucket_order.rb +33 -0
  38. data/lib/misc/geo_point.rb +64 -0
  39. data/lib/misc/range.rb +44 -0
  40. data/lib/misc/script.rb +62 -0
  41. data/lib/misc/terms_lookup.rb +45 -0
  42. data/lib/queries/abstract_query_builder.rb +31 -0
  43. data/lib/queries/base_query_builder.rb +27 -0
  44. data/lib/queries/bool_query_builder.rb +148 -0
  45. data/lib/queries/constant_score_query_builder.rb +32 -0
  46. data/lib/queries/dis_max_query_builder.rb +70 -0
  47. data/lib/queries/exists_query_builder.rb +32 -0
  48. data/lib/queries/function_score_query_builder.rb +170 -0
  49. data/lib/queries/geo_distance_query_builder.rb +126 -0
  50. data/lib/queries/match_all_query_builder.rb +16 -0
  51. data/lib/queries/match_query_builder.rb +181 -0
  52. data/lib/queries/nested_query_builder.rb +55 -0
  53. data/lib/queries/nested_sort_query_builder.rb +44 -0
  54. data/lib/queries/query_builder.rb +33 -0
  55. data/lib/queries/range_query_builder.rb +153 -0
  56. data/lib/queries/term_query_builder.rb +44 -0
  57. data/lib/queries/terms_query_builder.rb +54 -0
  58. data/lib/sort/abstract_sort_builder.rb +15 -0
  59. data/lib/sort/field_sort_builder.rb +70 -0
  60. data/lib/sort/geo_distance_sort_builder.rb +120 -0
  61. data/lib/sort/nested_sort_builder.rb +68 -0
  62. data/lib/sort/score_sort_builder.rb +16 -0
  63. data/lib/sort/script_sort_builder.rb +83 -0
  64. data/lib/sort/sort_builder.rb +28 -0
  65. metadata +64 -1
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Enums
4
+ # Enumerator class for zer terms query.
5
+ # If the analyzer used removes all tokens in a query like a stop filter does,
6
+ # the default behavior is to match no documents at all.
7
+ # In order to change that the zero_terms_query option can be used,
8
+ # which accepts none (default) and all which corresponds to a match_all query.
9
+ class ZeroTermsQuery
10
+ # @!visibility protected
11
+ attr_reader :zero_term
12
+ # @!visibility protected
13
+ ZERO_TERMS_QUERY = { all: 'all', none: 'none' }.freeze
14
+
15
+ # @!visibility protected
16
+ ZERO_TERMS_QUERY.each do |zero_term_query, es_value|
17
+ define_singleton_method(zero_term_query) do
18
+ return new(es_value)
19
+ end
20
+ end
21
+
22
+ # @!scope class
23
+ # @!method all
24
+ # @return [ValidationMethods] ValidationMethods object
25
+ # corresponding to elasticsearch option of 'all'
26
+
27
+ # @!scope class
28
+ # @!method none
29
+ # @return [ValidationMethods] ValidationMethods object
30
+ # corresponding to elasticsearch option of 'none'
31
+
32
+ # @!visibility protected
33
+ def initialize(zero_term)
34
+ @zero_term = zero_term
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'score_function_builder'
4
+ module FunctionScores
5
+ # A function that computes a random score for the matched documents
6
+ # Query Components:
7
+ # seed: Seed value based on which the random number will be generated
8
+ # field: Field value based on which the random number will be generated
9
+ class RandomScoreFunctionBuilder < ScoreFunctionBuilder
10
+ # @!visibility protected
11
+ NAME = 'random_score'
12
+
13
+ # @return [Hash] serailized json query for the object
14
+ def function
15
+ function = {}
16
+ rs_query = super
17
+ rs_query[:field] = @field if field.present?
18
+ rs_query[:seed] = @seed if seed.present?
19
+ function[name.intern] = rs_query
20
+ function
21
+ end
22
+
23
+ # @!visibility protected
24
+ # Returns the seed value.
25
+ def seed_expr
26
+ @seed
27
+ end
28
+
29
+ # @params [Numeric] Seed Value
30
+ # Sets the seed value.
31
+ # @return [RandomScoreFunctionBuilder]
32
+ # modified self
33
+ def seed(value)
34
+ @seed = value
35
+ self
36
+ end
37
+
38
+ # @!visibility protected
39
+ # Returns the field value.
40
+ def field_expr
41
+ @field
42
+ end
43
+
44
+ # @params [String] field name
45
+ # Sets the field value.
46
+ # @return [RandomScoreFunctionBuilder]
47
+ # modified self
48
+ def field(value)
49
+ @field = value
50
+ self
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FunctionScores
4
+ # Base class for function score builder classes
5
+ class ScoreFunctionBuilder
6
+ include AttributesReader
7
+
8
+ # @return [Hash] serialized json query for the object
9
+ def function
10
+ {}
11
+ end
12
+
13
+ # Elasticsearch key for the score function class.
14
+ # @return [String] es key for the score function class
15
+ def name
16
+ self.class::NAME
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'score_function_builder'
4
+ module FunctionScores
5
+ # A function that uses a script to compute or influence the
6
+ # score of documents that match with the inner query or filter.
7
+ class ScriptScoreFunctionBuilder < ScoreFunctionBuilder
8
+ # @!visibility protected
9
+ NAME = 'script_score'
10
+
11
+ # @params [Misc::Script] script script object for the function
12
+ def initialize(script:)
13
+ @script = script
14
+ end
15
+
16
+ # @return [Hash] serialized json query for the object
17
+ def function
18
+ function = {}
19
+ ss_query = super
20
+ ss_query[:script] = @script.settings
21
+ function[name.intern] = ss_query
22
+ function
23
+ end
24
+
25
+ # @!visibility protected
26
+ def script_expr
27
+ @script
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'score_function_builder'
4
+ module FunctionScores
5
+ # A query that multiplies the weight to the score.
6
+ class WeightedScoreFunctionBuilder < ScoreFunctionBuilder
7
+ # @!visibility protected
8
+ NAME = 'weight'
9
+ # @!visibility protected
10
+ DEFAULT_WEIGHT = 1
11
+
12
+ def initialize
13
+ @weight = DEFAULT_WEIGHT
14
+ end
15
+
16
+ # @return [Hash] serialized json query for the object
17
+ def function
18
+ function = {}
19
+ function[name.intern] = @weight
20
+ function
21
+ end
22
+
23
+ # Returns the weight value
24
+ # @!visibility protected
25
+ def weight_expr
26
+ @weight
27
+ end
28
+
29
+ # @params [Numeric] value weight for the weighted function
30
+ # Sets the weight value.
31
+ # @returns [WeightedScoreFunctionBuilder]
32
+ # modified self
33
+ def weight(value)
34
+ @weight = value
35
+ self
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Misc
4
+ # Aggregations::Buckets ordering strategy.
5
+ class BucketOrder
6
+ include AttributesReader
7
+
8
+ # @param [String|Symbol] field
9
+ # @param [Enums::SortOrders] order : Enums::SortOrders.(desc|asc),
10
+ # defaults to Enums::SortOrders.desc
11
+ def initialize(field:, order: Enums::SortOrders.desc)
12
+ @field = field.intern
13
+ @order = order
14
+ end
15
+
16
+ # return [Hash]
17
+ def settings
18
+ {@field => @order.sort_order}
19
+ end
20
+
21
+ # @return [String]
22
+ # @!visibility protected
23
+ def field_expr
24
+ @field.to_s
25
+ end
26
+
27
+ # @return [Enums::SortOrders]
28
+ # @!visibility protected
29
+ def order_expr
30
+ @order
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Misc
4
+ # Point Class
5
+ class GeoPoint
6
+ include AttributesReader
7
+
8
+ # @params [Numeric] lat latitude
9
+ # @params [Numeric] lng longitude
10
+ # @params [Array|String] latlng
11
+ # latitude and longitude as array or string
12
+ # Format in [lon, lat] when array, such as [-70,40]
13
+ # Format in lat,lon when string, such as '40,-70'
14
+ # @params [String] geohash point geohash
15
+ def initialize(lat: nil, lng: nil, latlng: nil, geo_hash: nil)
16
+ if lat.present? && lng.present?
17
+ @lat = lat.to_f
18
+ @lng = lng.to_f
19
+ @type = :float
20
+ elsif latlng.present?
21
+ @latlng = latlng
22
+ @type = latlng.class.name.downcase.intern
23
+ elsif geo_hash.present?
24
+ @geo_hash = geo_hash
25
+ @type = :geohash
26
+ else
27
+ raise 'Provide Point as floating values latitude and longitude
28
+ or a string or an array or a geohash'
29
+ end
30
+ end
31
+
32
+ # @return [Hash] serialized json query for object
33
+ def settings
34
+ case @type
35
+ when :float
36
+ { lat: @lat, lon: @lng }
37
+ when :array || :string
38
+ @latlng
39
+ when :geohash
40
+ @geohash
41
+ end
42
+ end
43
+
44
+ # @!visibility protected
45
+ def lat_expr
46
+ @lat
47
+ end
48
+
49
+ # @!visibility protected
50
+ def lng_expr
51
+ @lng
52
+ end
53
+
54
+ # @!visibility protected
55
+ def latlng_expr
56
+ @latlng
57
+ end
58
+
59
+ # @!visibility protected
60
+ def geo_hash_expr
61
+ @geo_hash
62
+ end
63
+ end
64
+ end
data/lib/misc/range.rb ADDED
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Misc
4
+ # A bucket associated with a specific range
5
+ class Range
6
+ include AttributesReader
7
+
8
+ # @param [String|Float] from
9
+ # @param [String|Float] to
10
+ # @param [String] key
11
+ def initialize(from: nil, to: nil, key: '')
12
+ @from = from if from.present?
13
+ @to = to if to.present?
14
+ @key = key if key.present?
15
+ end
16
+
17
+ # return [Hash] serialized json query for object
18
+ def settings
19
+ settings = {}
20
+ settings[:key] = @key if @key.present?
21
+ settings[:from] = @from if @from.present?
22
+ settings[:to] = @to if @to.present?
23
+ settings
24
+ end
25
+
26
+ # @return [String|Float]
27
+ # @!visibility protected
28
+ def from_expr
29
+ @from
30
+ end
31
+
32
+ # @return [String|Float]
33
+ # @!visibility protected
34
+ def to_expr
35
+ @to
36
+ end
37
+
38
+ # @return [String]
39
+ # @!visibility protected
40
+ def key_expr
41
+ @key
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Misc
4
+ # Script represents used-defined input that can be used to compile and
5
+ # execute a script from the ScriptService based on the ScriptType.
6
+ class Script
7
+ include AttributesReader
8
+
9
+ # @param [String] id_or_code
10
+ # @param [Hash] params
11
+ # @param [Enums::ScriptTypes] type : Enums::ScriptTypes.(inline|stored),
12
+ # defaults to Enums::ScriptTypes.inline
13
+ # @param [String] lang
14
+ # @param [Hash] options
15
+ def initialize(id_or_code:, params: {}, type: Enums::ScriptTypes.inline, lang: 'painless', options: {})
16
+ @id_or_code = id_or_code
17
+ @params = params
18
+ @type = type
19
+ @lang = lang
20
+ @options = options
21
+ end
22
+
23
+ # return [Hash]
24
+ def settings
25
+ settings = {}
26
+ if @type.script_type == Enums::ScriptTypes.inline.script_type
27
+ settings[:source] = @id_or_code
28
+ else
29
+ settings[:id] = @id_or_code
30
+ end
31
+ settings[:lang] = @lang if @lang.present?
32
+ settings[:params] = @params if @params.present?
33
+ settings[:options] = @options if @options.present?
34
+ settings
35
+ end
36
+
37
+ # @return [String]
38
+ def id_or_code_expr
39
+ @id_or_code
40
+ end
41
+
42
+ # @return [Hash]
43
+ def params_expr
44
+ @params
45
+ end
46
+
47
+ # @return [Enums::ScriptTypes]
48
+ def type_expr
49
+ @type
50
+ end
51
+
52
+ # @return [String]
53
+ def lang_expr
54
+ @lang
55
+ end
56
+
57
+ # @return [Hash]
58
+ def options_expr
59
+ @options
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Misc
4
+ # Terms lookup fetches the field values of an existing document.
5
+ # Elasticsearch then uses those values as search terms. This can be helpful
6
+ # when searching for a large set of terms. Because terms lookup fetches values
7
+ # from a document, the _source mapping field must be enabled to
8
+ # use terms lookup. The _source field is enabled by default.
9
+ class TermsLookup
10
+ include AttributesReader
11
+
12
+ # @params [String] index index name
13
+ # @params [String] id document id
14
+ # @params [String] path name of the field to fetch values from
15
+ def initialize(index:, id:, path:)
16
+ @index = index
17
+ @id = id
18
+ @path = path
19
+ end
20
+
21
+ # @return [Hash] serialized json query for the object
22
+ def settings
23
+ settings = {}
24
+ settings[:index] = @index.intern
25
+ settings[:id] = @id.intern
26
+ settings[:path] = @path.intern
27
+ settings
28
+ end
29
+
30
+ # @!visibility protected
31
+ def index_expr
32
+ @index
33
+ end
34
+
35
+ # @!visibility protected
36
+ def id_expr
37
+ @id
38
+ end
39
+
40
+ # @!visibility protected
41
+ def path_expr
42
+ @path
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Queries
4
+ # General methods for query classes
5
+ module AbstractQueryBuilder
6
+ # @params [QueryBuilder] query query object to be matched
7
+ # Checks if both the query objects are same
8
+ # @return [Boolean] if both queries are same
9
+ def do_equals?(query)
10
+ # Checks if given query object is same as this query object
11
+ self.query == query.query
12
+ end
13
+
14
+ # @!visibility protected
15
+ def common_query
16
+ common_query = {}
17
+ common_query[:boost] = @boost if @boost.present?
18
+ common_query
19
+ end
20
+
21
+ def name
22
+ self.class::NAME.to_s
23
+ end
24
+
25
+ # @!visibility protected
26
+ def wrap_query
27
+ query = { query: self.query }
28
+ query
29
+ end
30
+ end
31
+ end