es_query_builder 2.0.2 → 2.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 48081a664a93e6ba790601ed3e4bf39f3239991f
4
- data.tar.gz: 5f10af03250a30bb84830ea03c7cc589cdef6e4b
3
+ metadata.gz: 6a1838cd288b76c74d37cbc4c6199e4eadd30ef8
4
+ data.tar.gz: 76a7fb2124afa3408b1438bc02ff2e76d1921aeb
5
5
  SHA512:
6
- metadata.gz: d02ada8f7df40f7892310a6c9e981eafa9d16b75c6afa2b25237671219bbaee5983b38b62d040f3d0d28435021b82d3c90447786db4c4e19107adc948739352e
7
- data.tar.gz: 54a60e9ed8baaf829113378fe2d9d7a5bd899a8d9fe54ad26a4e6ae5e93e4ece06dc9b1c34038d39ff79c572fdc34e07120055cd3aafedc8f814115006eb9343
6
+ metadata.gz: 73227dcb19043fc35af84edf55c8c1b9eaae72a09c066bd3e9850e866009ab384815d778ce14a3debeab793f0fb3f2163f846255ca8ce3200865c80a17c820e8
7
+ data.tar.gz: d2a6823a4e719c9d51d022a4c141542a118078f8fab6c024d207262d4dfbfb840dc4d254fe7e2288f7ab56140d8376360d99aec2d51fd01ba2fcd69a365a655b
@@ -1,4 +1,5 @@
1
1
  module Constants
2
2
  MAX_BULK_INDEX_SIZE = 2000
3
3
  FUNCTION_SCORE_METHODS = %w(multiply sum avg first max min)
4
+ RADIUS_OF_EARTH = 6378.1
4
5
  end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+ require_relative '../constants'
3
+
4
+ module Misc
5
+ # Point Class
6
+ class GeoBoundingBoxPoint
7
+ include AttributesReader
8
+
9
+ # @params [Numeric] lat latitude
10
+ # @params [Numeric] lng longitude
11
+ # @params [Array|String] latlng
12
+ # latitude and longitude as array or string
13
+ # Format in [lon, lat] when array, such as [-70,40]
14
+ # Format in lat,lon when string, such as '40,-70'
15
+ # @params [String] geohash point geohash
16
+ def initialize(lat: nil, lng: nil, radius: nil, latlng: nil, geo_hash: nil)
17
+ if lat.present? && lng.present?
18
+ @lat = lat.to_f
19
+ @lng = lng.to_f
20
+ @radius = radius.to_f
21
+ @type = :float
22
+ elsif latlng.present?
23
+ @latlng = latlng
24
+ @type = latlng.class.name.downcase.intern
25
+ elsif geo_hash.present?
26
+ @geo_hash = geo_hash
27
+ @type = :geohash
28
+ else
29
+ raise 'Provide Point as floating values latitude and longitude
30
+ or a string or an array or a geohash'
31
+ end
32
+ end
33
+
34
+ # @return [Hash] serialized json query for object
35
+ def settings
36
+ case @type
37
+ when :float
38
+ get_bounding_box_point
39
+ when :array || :string
40
+ @latlng
41
+ when :geohash
42
+ @geohash
43
+ end
44
+ end
45
+
46
+ # @!visibility protected
47
+ def lat_expr
48
+ @lat
49
+ end
50
+
51
+ # @!visibility protected
52
+ def lng_expr
53
+ @lng
54
+ end
55
+
56
+ # @!visibility protected
57
+ def latlng_expr
58
+ @latlng
59
+ end
60
+
61
+ # @!visibility protected
62
+ def geo_hash_expr
63
+ @geo_hash
64
+ end
65
+
66
+ def get_coordinates
67
+ radius_of_earth = Constants::RADIUS_OF_EARTH
68
+ pi = Math::PI
69
+ deg_to_rad = pi/180
70
+ lat_in_rad = @lat * deg_to_rad
71
+ lon_in_rad = @lng * deg_to_rad
72
+ delta_rad = @radius/radius_of_earth
73
+ ne_coordinates = {
74
+ latitude: (lat_in_rad + delta_rad)/ deg_to_rad,
75
+ longitude: (lon_in_rad + delta_rad)/ deg_to_rad
76
+ }
77
+ sw_coordinates = {
78
+ latitude: (lat_in_rad - delta_rad)/ deg_to_rad,
79
+ longitude: (lon_in_rad - delta_rad)/ deg_to_rad
80
+ }
81
+ nw_coordinates = {
82
+ latitude: (lat_in_rad - delta_rad)/ deg_to_rad,
83
+ longitude: (lon_in_rad + delta_rad)/ deg_to_rad
84
+ }
85
+ se_coordinates = {
86
+ latitude: (lat_in_rad + delta_rad)/ deg_to_rad,
87
+ longitude: (lon_in_rad - delta_rad)/ deg_to_rad
88
+ }
89
+ {ne_coordinates: ne_coordinates, sw_coordinates: sw_coordinates, nw_coordinates: nw_coordinates, se_coordinates: se_coordinates}
90
+ end
91
+
92
+ def get_bounding_box_point
93
+ direction_coordinates = get_coordinates
94
+ ne_lat_lng, sw_lat_lng = direction_coordinates[:ne_coordinates], direction_coordinates[:sw_coordinates]
95
+ {
96
+ top_right: {lat: ne_lat_lng[:latitude].to_f,
97
+ lon: ne_lat_lng[:longitude].to_f},
98
+ bottom_left: {lat: sw_lat_lng[:latitude].to_f,
99
+ lon: sw_lat_lng[:longitude].to_f}
100
+ }
101
+ end
102
+
103
+ end
104
+ end
105
+
@@ -0,0 +1,123 @@
1
+ # Filters documents that include only hits that exists within a specific distance from a geo point.
2
+ require_relative 'query_builder'
3
+ module Queries
4
+ class GeoBoundingBoxQueryBuilder < QueryBuilder
5
+
6
+ NAME = 'geo_bounding_box'
7
+ DEFAULT_DISTANCE_UNIT = ::Enums::DistanceUnits.meters
8
+
9
+ =begin
10
+ @params:
11
+ field_name: geo_point field in the document which is matched with the given query
12
+ point: center point for this query
13
+ distance: The radius of the circle centred on the specified location. Points which fall into this circle are considered to be matches.
14
+ distance_unit: The distance can be specified in various units. See Distance Units.
15
+ distance_type: How to compute the distance. Can either be arc (default),
16
+ or plane (faster, but inaccurate on long distances and close to the poles)
17
+ writable_name: Optional name field to identify the query
18
+ validation_method: Set to IGNORE_MALFORMED to accept geo points with invalid latitude or longitude,
19
+ set to COERCE to additionally try and infer correct coordinates (default is STRICT).
20
+ ignore_unmapped: When set to true the ignore_unmapped option will ignore an unmapped field and will not match any documents for this query.
21
+ When set to false (the default value) the query will throw an exception if the field is not mapped.
22
+ =end
23
+
24
+ def initialize field_name:
25
+ @field_name = field_name
26
+ @point = nil
27
+ @distance = nil
28
+ @distance_unit = DEFAULT_DISTANCE_UNIT.distance_unit
29
+ @writable_name = nil
30
+ @validation_method = nil
31
+ @ignore_unmapped = nil
32
+ end
33
+
34
+ def query
35
+ query = {}
36
+ geo_query = self.common_query
37
+ geo_query[@field_name] = @point.settings
38
+ geo_query[:writable_name] = @writable_name
39
+ geo_query[:validation_method] = @validation_method
40
+ geo_query[:ignore_unmapped] = @ignore_unmapped
41
+ query[name.intern] = geo_query
42
+ return query
43
+ end
44
+
45
+ # Returns point
46
+ def point_expr
47
+ return @point
48
+ end
49
+
50
+ # Sets point
51
+ def point point
52
+ @point = point
53
+ return self
54
+ end
55
+
56
+ # Returns field_name
57
+ def field_name_expr
58
+ return @field_name
59
+ end
60
+
61
+ # Returns distance
62
+ def distance_expr
63
+ return @distance
64
+ end
65
+
66
+ # Returns distance_unit
67
+ def distance_unit_expr
68
+ return @distance_unit
69
+ end
70
+
71
+ # Sets distance and distance_unit
72
+ def distance distance, distance_unit= nil
73
+ @distance = distance
74
+ @distance_unit = distance_unit.distance_unit if distance_unit.present?
75
+ return self
76
+ end
77
+
78
+ # Returns distance_type
79
+ def distance_type_expr
80
+ return @distance_type
81
+ end
82
+
83
+ # Sets distance_type
84
+ def distance_type distance_type
85
+ @distance_type = distance_type.distance_type
86
+ return self
87
+ end
88
+
89
+ # Returns writable_name
90
+ def writable_name_expr
91
+ return @writable_name
92
+ end
93
+
94
+ # Sets writable_name
95
+ def writable_name writable_name
96
+ @writable_name = writable_name
97
+ return self
98
+ end
99
+
100
+ # Returns validation_method
101
+ def validation_method_expr
102
+ return @validation_method
103
+ end
104
+
105
+ # Sets validation_method
106
+ def validation_method validation_method
107
+ @validation_method = validation_method.validation_method
108
+ return self
109
+ end
110
+
111
+ # Returns ignore_unmapped
112
+ def ignore_unmapped_expr
113
+ return @ignore_unmapped
114
+ end
115
+
116
+ # Sets ignore_unmapped
117
+ def ignore_unmapped ignore_unmapped
118
+ @ignore_unmapped = ignore_unmapped
119
+ return self
120
+ end
121
+
122
+ end
123
+ end
@@ -32,7 +32,7 @@ module Queries
32
32
  def query
33
33
  query = {}
34
34
  terms_query = self.common_query
35
- terms_query[@field_name.intern] = @values if @values.present?
35
+ terms_query[@field_name.intern] = @values
36
36
  terms_query[@field_name.intern] = @terms_lookup.settings if @terms_lookup.present?
37
37
  query[name.intern] = terms_query
38
38
  return query
@@ -102,4 +102,13 @@ class QueryBuilders
102
102
  def self.match_all_query
103
103
  Queries::MatchAllQueryBuilder.new
104
104
  end
105
+
106
+ # @params [String] field_name
107
+ # field on which exists query to be performed
108
+ # @return [Queries::GeoDistanceQueryBuilder]
109
+ # geo_bounding_box_query_builder object
110
+ def self.geo_bounding_box_query(*args)
111
+ Queries::GeoBoundingBoxQueryBuilder.new(*args)
112
+ end
113
+
105
114
  end
@@ -7,6 +7,7 @@ class SearchSourceBuilder
7
7
  @aggregations = []
8
8
  @sort = []
9
9
  @source = []
10
+ @_source = {}
10
11
  @size = nil
11
12
  @explain = nil
12
13
  @from = nil
@@ -22,7 +23,7 @@ class SearchSourceBuilder
22
23
  body[:query] = @query.query
23
24
  body[:aggregations] = @aggregations.map{|agg| agg.query}.reduce({}, :merge) if @aggregations.present?
24
25
  body[:sort] = @sort.map{|sort_object| sort_object.query} if @sort.present?
25
- body[:_source] = @source
26
+ body[:_source] = @_source.presence || @source
26
27
  body[:size] = @size
27
28
  body[:explain] = @explain
28
29
  body[:from] = @from
@@ -80,6 +81,32 @@ class SearchSourceBuilder
80
81
  return self
81
82
  end
82
83
 
84
+ # returns source_fields
85
+ def source_include_expr
86
+ return [] unless @_source[:include].present?
87
+ @_source[:include]
88
+ end
89
+
90
+ def source_exclude_expr
91
+ return [] unless @_source[:exclude].present?
92
+ @_source[:exclude]
93
+ end
94
+
95
+ # sets source include fields
96
+ def source_include(include_list)
97
+ @_source[:include] = [] if @_source[:include].blank?
98
+ include_list.is_a?(Array) ? (@_source[:include] += include_list) : @_source[:include].append(include_list)
99
+ self
100
+ end
101
+
102
+ # sets source exclude fields
103
+ def source_exclude(exclude_list)
104
+ @_source[:exclude] = [] if @_source[:exclude].blank?
105
+ exclude_list.is_a?(Array) ? (@_source[:exclude] += exclude_list) : @_source[:exclude].append(exclude_list)
106
+ self
107
+ end
108
+
109
+
83
110
  # returns size
84
111
  def size_expr
85
112
  return @size
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: es_query_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mohib Yousuf
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2019-12-03 00:00:00.000000000 Z
14
+ date: 2021-01-11 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rails
@@ -129,6 +129,7 @@ files:
129
129
  - lib/function_scores/weighted_score_function_builder.rb
130
130
  - lib/indexer.rb
131
131
  - lib/misc/bucket_order.rb
132
+ - lib/misc/geo_bounding_box_point.rb
132
133
  - lib/misc/geo_point.rb
133
134
  - lib/misc/range.rb
134
135
  - lib/misc/script.rb
@@ -141,6 +142,7 @@ files:
141
142
  - lib/queries/dis_max_query_builder.rb
142
143
  - lib/queries/exists_query_builder.rb
143
144
  - lib/queries/function_score_query_builder.rb
145
+ - lib/queries/geo_bounding_box_query_builder.rb
144
146
  - lib/queries/geo_distance_query_builder.rb
145
147
  - lib/queries/match_all_query_builder.rb
146
148
  - lib/queries/match_query_builder.rb