Skip to content

Instantly share code, notes, and snippets.

@vhyza
Created February 6, 2012 12:19
Show Gist options
  • Save vhyza/1751827 to your computer and use it in GitHub Desktop.
Save vhyza/1751827 to your computer and use it in GitHub Desktop.
Tire issue #228
require 'tire'
Tire.index 'users_tire_issue' do
delete
create mappings: {
user: {
properties: {
email: { type: 'string' },
names: { type: 'string', analyzer: 'snowball' },
medical_conditions: {
properties: {
condition: { type: 'string' },
notes: { type: 'string' }
}
},
addresses: {
properties: {
lat_lon: { type: 'geo_point' }
}
}
}
}
}
store({ _type: 'user', email: 'email@example.com', names: ["chance dinkins","abraham lincoln"], addresses: [{ lat_lon: '34.078567,-81.173366' }] })
store({ _type: 'user', email: 'another_email@example.com', names: ["chance dinkins","abraham lincoln"], addresses: [{ lat_lon: '34,-81.173366' }] })
store({ _type: 'user', email: 'another_another_email@example.com', names: ["chance dinkins","chancey dinkins"], addresses: [{ lat_lon: '34.078567,-81' }] })
store({ _type: 'user', email: 'nothing@example.com', names: 'nothing', addresses: [{ lat_lon: '34.078567,-81' }] })
refresh
end
class AgentUserSearch
include Tire::Model::Search
def name
'chance'
end
def lat_lon
"34.078567,-81.173366"
end
def search_users
me = self
search = Tire.search 'users_tire_issue' do |search|
search.query do |query|
query.string "names: #{me.name}" unless me.name.empty?
end
search.filter :geo_distance, distance: "10mi", 'addresses.lat_lon' => me.lat_lon
end
search
end
end
search = AgentUserSearch.new.search_users
search.results.each do |r|
puts "email: #{r.email}"
puts "addresses: #{r.addresses.first.lat_lon}"
puts "-----"
end
p search.to_curl
p search.results.count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment