This repository has been archived by the owner on Jun 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 532
How to work with locations (geo) in Tire
karmi edited this page Oct 28, 2011
·
9 revisions
Tire currently does not expose the (awesome!) „Geo Location and Search” support in ElasticSearch.
However, it's simple to define proper mappings for you location-based documents, and it's possible to search them, either via the Tire.search
DSL method by passing it a Hash, or descending a level down, and using Tire::Configuration.client.get <MY QUERY AS A HASH>
method.
The following example illustrates the point:
require 'tire'
require 'active_record'
Tire.index('venues') { delete }
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ":memory:" )
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define(version: 1) do
create_table :venues do |t|
t.string :title, :latitude, :longitude
end
end
class Venue < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
tire do
mapping do
indexes :title, type: 'string', analyzer: 'snowball'
indexes :lat_lon, type: 'geo_point'
end
end
def lat_lon
[latitude, longitude].join(',')
end
def to_indexed_json
to_json(only: ['title'], methods: ['lat_lon'])
end
end
Venue.create title: 'Pizzeria Pomodoro', latitude: "41.12", longitude: "-71.34"
Venue.tire.index.refresh
query = { query: { filtered: { query: { match_all: ''} }, filter: { geo_distance: { distance: '100km', lat_lon: "41,-71" } } } }
puts "", "The query:", '-'*80, query.to_json
s = Tire.search 'venues', query
puts "", "Results:", '-'*80, s.results.inspect