Created
June 28, 2011 14:13
-
-
Save karmi/1051213 to your computer and use it in GitHub Desktop.
Geo Distance Filter Support in Tire/ElasticSearch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'tire' | |
require 'active_support/core_ext/numeric' | |
require 'active_support/core_ext/time/zones' | |
# Tire.configure { logger STDERR, level: 'debug' } | |
class Time; DATE_FORMATS.update lucene: "%Y-%m-%dT%H:%M"; end | |
Tire.index 'venues' do | |
delete | |
# 1) Create the index with proper mapping for the `location` property | |
create mappings: { | |
venue: { | |
properties: { | |
location: { type: 'geo_point' }, | |
updated_at: { type: 'date', format: 'date_hour_minute' } | |
} | |
} | |
} | |
# 2) Store some example documents of the `venue` type | |
store type: 'venue', name: 'One', location: [40.1, -70.1], updated_at: 3.hours.ago.utc.to_s(:lucene) | |
store type: 'venue', name: 'Two', location: [40.2, -70.2], updated_at: 2.hours.ago.utc.to_s(:lucene) | |
store type: 'venue', name: 'Three', location: [50, 15], updated_at: 1.hour .ago.utc.to_s(:lucene) | |
refresh | |
end | |
s = Tire.search 'venues' do | |
# 3) Search based on geo distance | |
filter 'geo_distance', distance: '100km', location: [40, -70] | |
# 4) Sort by distance to origin | |
# sort { by :_geo_distance, location: [40, -70] } | |
# 5) Sort by `updated_at` property | |
sort { by :updated_at, 'desc' } | |
end | |
puts s.to_curl, '-'*80 | |
s.results.each do |d| | |
puts "* #{d.name} | location: #{d.location}, updated_at: #{d.updated_at}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also see karmi/retire#417 (comment)