Class: Sort::GeoDistanceSortBuilder

Inherits:
SortBuilder show all
Defined in:
lib/sort/geo_distance_sort_builder.rb

Constant Summary collapse

NAME =
'_geo_distance'

Instance Method Summary collapse

Methods inherited from SortBuilder

#common_query, #order, #order_expr

Methods included from AbstractSortBuilder

#doEquals​?, #name

Methods included from AttributesReader

#attributes

Constructor Details

#initialize(field_name:, point:) ⇒ GeoDistanceSortBuilder

@params:

field_name: The geo point like field the distance based sort operates on.
distance_type: How to compute the distance. Can either be arc (default), 
               or plane (faster, but inaccurate on long distances and close to the poles).
distance_unit: The unit to use when computing sort values. The default is m (meters).
nested_sort: Nested path within current object.
ignore_unmapped: Indicates if the unmapped field should be treated as a missing value. 
                 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).
point: The point to create the range distance facets from.
sort_mode: What to do in case a field has several geo points. By default, the shortest distance is taken into account when sorting 
           in ascending orderand the longest distance when sorting in descending order. Supported values are min, max, median and avg.
validation_method: Set to IGNORE_MALFORMED to accept geo points with invalid latitude or longitude,
                   set to COERCE to additionally try and infer correct coordinates (default is STRICT).


24
25
26
27
28
29
30
31
32
33
# File 'lib/sort/geo_distance_sort_builder.rb', line 24

def initialize field_name:, point:
  @field_name = field_name
  @point = point
  @distance_type = nil
  @distance_unit = nil
  @nested_sort = nil
  @ignore_unmapped = nil
  @sort_mode = nil
  @validation_method = nil
end

Instance Method Details

#distance_type(distance_type) ⇒ Object

sets distance_type



69
70
71
72
# File 'lib/sort/geo_distance_sort_builder.rb', line 69

def distance_type distance_type
  @distance_type = distance_type.distance_type
  return self
end

#distance_type_exprObject

returns distance_type



65
66
67
# File 'lib/sort/geo_distance_sort_builder.rb', line 65

def distance_type_expr
  return @distance_type
end

#distance_unit(distance_unit) ⇒ Object

sets distance_unit



79
80
81
82
# File 'lib/sort/geo_distance_sort_builder.rb', line 79

def distance_unit distance_unit
  @distance_unit = distance_unit.distance_unit
  return self
end

#distance_unit_exprObject

returns distance_unit



75
76
77
# File 'lib/sort/geo_distance_sort_builder.rb', line 75

def distance_unit_expr
  return @distance_unit
end

#field_name_exprObject

returns field_name



50
51
52
# File 'lib/sort/geo_distance_sort_builder.rb', line 50

def field_name_expr
  return @field_name
end

#ignore_unmapped(ignore_unmapped) ⇒ Object

sets ignore_unmapped



99
100
101
102
# File 'lib/sort/geo_distance_sort_builder.rb', line 99

def ignore_unmapped ignore_unmapped
  @ignore_unmapped = ignore_unmapped
  return self
end

#ignore_unmapped_exprObject

returns ignore_unmapped



95
96
97
# File 'lib/sort/geo_distance_sort_builder.rb', line 95

def ignore_unmapped_expr
  return @ignore_unmapped
end

#nested_sort(nested_sort) ⇒ Object

sets nested_sort



89
90
91
92
# File 'lib/sort/geo_distance_sort_builder.rb', line 89

def nested_sort nested_sort
  @nested_sort = nested_sort
  return self
end

#nested_sort_exprObject

returns nested_sort



85
86
87
# File 'lib/sort/geo_distance_sort_builder.rb', line 85

def nested_sort_expr
  return @nested_sort
end

#point(point) ⇒ Object

sets point



59
60
61
62
# File 'lib/sort/geo_distance_sort_builder.rb', line 59

def point point
  @point = point
  return self
end

#point_exprObject

returns point



55
56
57
# File 'lib/sort/geo_distance_sort_builder.rb', line 55

def point_expr
  return @point
end

#queryObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sort/geo_distance_sort_builder.rb', line 35

def query
  query = {}
  go_query = self.common_query
  go_query[@field_name.to_s.intern] = @point.settings
  go_query[:distance_type] = @distance_type if @distance_type.present?
  go_query[:unit] = @distance_unit if @distance_unit.present?
  go_query[:nested] = @nested_sort.query if @nested_sort.present?
  go_query[:ignore_unmapped] = @ignore_unmapped if @ignore_unmapped.present?
  go_query[:sort_mode] = @sort_mode if @sort_mode.present?
  go_query[:validation_method] = @validation_method if @validation_method.present?
  query[name.intern] = go_query
  return query
end

#sort_mode(sort_mode) ⇒ Object

sets sort_mode



109
110
111
112
# File 'lib/sort/geo_distance_sort_builder.rb', line 109

def sort_mode sort_mode
  @sort_mode = sort_mode.sort_mode
  return self
end

#sort_mode_exprObject

returns sort_mode



105
106
107
# File 'lib/sort/geo_distance_sort_builder.rb', line 105

def sort_mode_expr
  return @sort_mode
end

#validation_method(validation_method) ⇒ Object



114
115
116
117
# File 'lib/sort/geo_distance_sort_builder.rb', line 114

def validation_method validation_method
  @validation_method = validation_method.validation_method
  return self
end