Skip to content

Instantly share code, notes, and snippets.

@karol-blaszczyk
Created January 9, 2014 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karol-blaszczyk/8332124 to your computer and use it in GitHub Desktop.
Save karol-blaszczyk/8332124 to your computer and use it in GitHub Desktop.
Localization
def self.search_pricings_for(service_ids, postal_code)
pcc = PostalCodeCoordinates.find_by_postal_code(postal_code) if postal_code
lat_lon = pcc.lat_lon_string if pcc
unless pcc
search = Tire.search do |search|
search.size 0
end
return search
end
search = Tire.search 'service_pricings' do |search|
search.size 1000
if lat_lon
search.filter :or, [
{ term: { locations: "virtual" }},
{ and: [
{term: { locations: "stationary" }},
{geo_distance: {
distance: "#{MainSearcher::DEFAULT_RANGE}km",
service_location: lat_lon
}}
]},
{ and: [
{ missing: { field: "locations" }},
{geo_distance: {
distance: "#{MainSearcher::DEFAULT_RANGE}km",
service_location: lat_lon
}}
]},
{ and: [
{term: { locations: "customer" }},
{
or: Location::DRIVE_RADIUSES.collect do |radius|
{
and:
[
{geo_distance: {
distance: "#{radius}km",
service_location: lat_lon
}
},
{term: { drive_radius: radius }}
]
}
end
}
]}
]
# sorting does not work
search.sort do
by :_geo_distance, {mode: "min", service_location: lat_lon, unit: 'km', order: 'asc'}
end
end
search.filter :terms, service_id: service_ids
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment