es_query_builder 2.0.4 → 2.0.5

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: 0f513758e289382abfa9120298c7d174e7978f41
4
- data.tar.gz: 526d810b1e34728d83a4d1661695fcded2172c1a
3
+ metadata.gz: 3d7ba397b283a7a351ac4454d01d263c4bc77edf
4
+ data.tar.gz: 37d5c3e4ade5419ddeba118ed89aefa42c794807
5
5
  SHA512:
6
- metadata.gz: 7eb026b02677a5013e58358e4f5c3f4dc50db925ab01faab431d5d873eda3a808c275fb907839e347062b724f6799aca5b07961a3c8b0aed58ad50466b2a70c8
7
- data.tar.gz: a1a2b0f640c393660cd10b72f8ab4e188c4c37b31cc8bfee64aa33ae09a974006f7a4502a24413d7e2990ca870d0fa1ea5ccc46a4d0a62b7b0df8025f1988500
6
+ metadata.gz: 582ff1bcca907120ff94dd1f789120ceceb3d566f528f54501b138005c899cee4b885dde85d3bfb4dc7fe037743fbd5244bba6061421dcd1025f8c732602cdf6
7
+ data.tar.gz: 2878a3bd5a238c58d63f9a6d11ab72af3e7283a86700d27d5483dbba46f637d6d35911f1100847806c93f5db99fabed384f2b39107273bb3523734a32ba48d54
@@ -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,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Misc
4
+ # Point Class
5
+ class GeoBoundingBoxPoint
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, radius: nil, latlng: nil, geo_hash: nil)
16
+ if lat.present? && lng.present?
17
+ @lat = lat.to_f
18
+ @lng = lng.to_f
19
+ @radius = radius.to_f
20
+ @type = :float
21
+ elsif latlng.present?
22
+ @latlng = latlng
23
+ @type = latlng.class.name.downcase.intern
24
+ elsif geo_hash.present?
25
+ @geo_hash = geo_hash
26
+ @type = :geohash
27
+ else
28
+ raise 'Provide Point as floating values latitude and longitude
29
+ or a string or an array or a geohash'
30
+ end
31
+ end
32
+
33
+ # @return [Hash] serialized json query for object
34
+ def settings
35
+ case @type
36
+ when :float
37
+ get_bounding_box_point
38
+ when :array || :string
39
+ @latlng
40
+ when :geohash
41
+ @geohash
42
+ end
43
+ end
44
+
45
+ # @!visibility protected
46
+ def lat_expr
47
+ @lat
48
+ end
49
+
50
+ # @!visibility protected
51
+ def lng_expr
52
+ @lng
53
+ end
54
+
55
+ # @!visibility protected
56
+ def latlng_expr
57
+ @latlng
58
+ end
59
+
60
+ # @!visibility protected
61
+ def geo_hash_expr
62
+ @geo_hash
63
+ end
64
+
65
+ def get_coordinates
66
+ radius_of_earth = Constants::RADIUS_OF_EARTH
67
+ pi = Math::PI
68
+ deg_to_rad = pi/180
69
+ lat_in_rad = @lat * deg_to_rad
70
+ lon_in_rad = @lng * deg_to_rad
71
+ delta_rad = @radius/radius_of_earth
72
+ ne_coordinates = {
73
+ latitude: (lat_in_rad + delta_rad)/ deg_to_rad,
74
+ longitude: (lon_in_rad + delta_rad)/ deg_to_rad
75
+ }
76
+ sw_coordinates = {
77
+ latitude: (lat_in_rad - delta_rad)/ deg_to_rad,
78
+ longitude: (lon_in_rad - delta_rad)/ deg_to_rad
79
+ }
80
+ nw_coordinates = {
81
+ latitude: (lat_in_rad - delta_rad)/ deg_to_rad,
82
+ longitude: (lon_in_rad + delta_rad)/ deg_to_rad
83
+ }
84
+ se_coordinates = {
85
+ latitude: (lat_in_rad + delta_rad)/ deg_to_rad,
86
+ longitude: (lon_in_rad - delta_rad)/ deg_to_rad
87
+ }
88
+ {ne_coordinates: ne_coordinates, sw_coordinates: sw_coordinates, nw_coordinates: nw_coordinates, se_coordinates: se_coordinates}
89
+ end
90
+
91
+ def get_bounding_box_point
92
+ direction_coordinates = get_coordinates
93
+ ne_lat_lng, sw_lat_lng = direction_coordinates[:ne_coordinates], direction_coordinates[:sw_coordinates]
94
+ {
95
+ top_right: {lat: ne_lat_lng[:latitude].to_f,
96
+ lon: ne_lat_lng[:longitude].to_f},
97
+ bottom_left: {lat: sw_lat_lng[:latitude].to_f,
98
+ lon: sw_lat_lng[:longitude].to_f}
99
+ }
100
+ end
101
+
102
+ end
103
+ end
104
+
@@ -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
@@ -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
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.4
4
+ version: 2.0.5
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: 2020-04-09 00:00:00.000000000 Z
14
+ date: 2021-01-08 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