Class: Queries::GeoDistanceQueryBuilder

Inherits:
QueryBuilder show all
Defined in:
lib/queries/geo_distance_query_builder.rb

Constant Summary collapse

NAME =
'geo_distance'
DEFAULT_DISTANCE_UNIT =
::Enums::DistanceUnits.meters

Instance Method Summary collapse

Methods inherited from QueryBuilder

#boost

Methods included from AttributesReader

#attributes

Methods included from AbstractQueryBuilder

#do_equals?, #name

Constructor Details

#initialize(field_name:) ⇒ GeoDistanceQueryBuilder

@params:

field_name: geo_point field in the document which is matched with the given query
point: center point for this query
distance: The radius of the circle centred on the specified location. Points which fall into this circle are considered to be matches.
distance_unit: The distance can be specified in various units. See Distance Units.
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)
writable_name: Optional name field to identify the query
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).
ignore_unmapped: When set to true the ignore_unmapped option will ignore an unmapped field and will not match any documents for this query.
                 When set to false (the default value) the query will throw an exception if the field is not mapped.


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

def initialize field_name:
  @field_name = field_name
  @point = nil
  @distance = nil
  @distance_unit = DEFAULT_DISTANCE_UNIT.distance_unit
  @distance_type = nil
  @writable_name = nil
  @validation_method = nil
  @ignore_unmapped = nil
end

Instance Method Details

#distance(distance, distance_unit = nil) ⇒ Object

Sets distance and distance_unit



75
76
77
78
79
# File 'lib/queries/geo_distance_query_builder.rb', line 75

def distance distance, distance_unit= nil
  @distance = distance
  @distance_unit = distance_unit.distance_unit if distance_unit.present?
  return self
end

#distance_exprObject

Returns distance



65
66
67
# File 'lib/queries/geo_distance_query_builder.rb', line 65

def distance_expr
  return @distance
end

#distance_type(distance_type) ⇒ Object

Sets distance_type



87
88
89
90
# File 'lib/queries/geo_distance_query_builder.rb', line 87

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

#distance_type_exprObject

Returns distance_type



82
83
84
# File 'lib/queries/geo_distance_query_builder.rb', line 82

def distance_type_expr
  return @distance_type
end

#distance_unit_exprObject

Returns distance_unit



70
71
72
# File 'lib/queries/geo_distance_query_builder.rb', line 70

def distance_unit_expr
  return @distance_unit
end

#field_name_exprObject

Returns field_name



60
61
62
# File 'lib/queries/geo_distance_query_builder.rb', line 60

def field_name_expr
  return @field_name
end

#ignore_unmapped(ignore_unmapped) ⇒ Object

Sets ignore_unmapped



120
121
122
123
# File 'lib/queries/geo_distance_query_builder.rb', line 120

def ignore_unmapped ignore_unmapped
  @ignore_unmapped = ignore_unmapped
  return self
end

#ignore_unmapped_exprObject

Returns ignore_unmapped



115
116
117
# File 'lib/queries/geo_distance_query_builder.rb', line 115

def ignore_unmapped_expr
  return @ignore_unmapped
end

#point(point) ⇒ Object

Sets point



54
55
56
57
# File 'lib/queries/geo_distance_query_builder.rb', line 54

def point point
  @point = point
  return self
end

#point_exprObject

Returns point



49
50
51
# File 'lib/queries/geo_distance_query_builder.rb', line 49

def point_expr
  return @point
end

#queryObject



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

def query
  query = {}
  geo_query = self.common_query
  geo_query[@field_name] = @point.settings
  geo_query[:distance] = @distance.to_s + @distance_unit.to_s
  geo_query[:distance_type] = @distance_type
  geo_query[:writable_name] = @writable_name
  geo_query[:validation_method] = @validation_method
  geo_query[:ignore_unmapped] = @ignore_unmapped
  query[name.intern] = geo_query
  return query
end

#validation_method(validation_method) ⇒ Object

Sets validation_method



109
110
111
112
# File 'lib/queries/geo_distance_query_builder.rb', line 109

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

#validation_method_exprObject

Returns validation_method



104
105
106
# File 'lib/queries/geo_distance_query_builder.rb', line 104

def validation_method_expr
  return @validation_method
end

#writable_name(writable_name) ⇒ Object

Sets writable_name



98
99
100
101
# File 'lib/queries/geo_distance_query_builder.rb', line 98

def writable_name writable_name
  @writable_name = writable_name
  return self
end

#writable_name_exprObject

Returns writable_name



93
94
95
# File 'lib/queries/geo_distance_query_builder.rb', line 93

def writable_name_expr
  return @writable_name
end