Skip to content
Permalink
Browse files

Use searchkick & elasticsearch to power geosearch

  • Loading branch information...
excid3 committed Sep 25, 2017
1 parent cdbf2c4 commit a872a6934c0f09b12c208b6594bebe671700edf0
Showing with 51 additions and 7 deletions.
  1. +1 −0 Gemfile
  2. +17 −0 Gemfile.lock
  3. +27 −7 app/controllers/transactions_controller.rb
  4. +6 −0 app/models/transaction.rb
@@ -64,3 +64,4 @@ gem 'sidekiq', '~> 5.0'
gem 'foreman', '~> 0.84.0'

gem 'geocoder'
gem 'searchkick'
@@ -99,14 +99,25 @@ GEM
railties (>= 4.1.0, < 5.2)
responders
warden (~> 1.2.3)
elasticsearch (5.0.4)
elasticsearch-api (= 5.0.4)
elasticsearch-transport (= 5.0.4)
elasticsearch-api (5.0.4)
multi_json
elasticsearch-transport (5.0.4)
faraday
multi_json
erubi (1.6.1)
execjs (2.7.0)
faraday (0.13.1)
multipart-post (>= 1.2, < 3)
ffi (1.9.18)
foreman (0.84.0)
thor (~> 0.19.1)
geocoder (1.4.4)
globalid (0.4.0)
activesupport (>= 4.2.0)
hashie (3.5.6)
i18n (0.8.6)
jbuilder (2.7.0)
activesupport (>= 4.2.0)
@@ -146,6 +157,7 @@ GEM
momentjs-rails (2.17.1)
railties (>= 3.1)
multi_json (1.12.2)
multipart-post (2.0.0)
nio4r (2.1.0)
nokogiri (1.8.1)
mini_portile2 (~> 2.3.0)
@@ -203,6 +215,10 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
searchkick (2.3.2)
activemodel (>= 4.1)
elasticsearch (>= 1)
hashie
selectize-rails (0.12.4)
selenium-webdriver (3.6.0)
childprocess (~> 0.5)
@@ -272,6 +288,7 @@ DEPENDENCIES
puma (~> 3.7)
rails (~> 5.1.2)
sass-rails (~> 5.0)
searchkick
selenium-webdriver
sidekiq (~> 5.0)
spring
@@ -6,16 +6,36 @@ class TransactionsController < ApplicationController
def index
@transactions = if params[:l]
sw_lat, sw_lng, ne_lat, ne_lng = params[:l].split(",")
center = Geocoder::Calculations.geographic_center([[sw_lat, sw_lng], [ne_lat, ne_lng]])
distance = Geocoder::Calculations.distance_between(center, [sw_lat, sw_lng])
box = Geocoder::Calculations.bounding_box(center, distance)
Transaction.within_bounding_box(box)
Transaction.search("*", page: params[:page], per_page: 5, where: {
location: {
top_left: {
lat: ne_lat,
lon: sw_lng
},
bottom_right: {
lat: sw_lat,
lon: ne_lng
}
}
})
elsif params[:near]
Transaction.near(params[:near])
#Transaction.near(params[:near]).page(params[:page]).per(5)

location = Geocoder.search(params[:near]).first
Transaction.search "*", page: params[:page], per_page: 5,
boost_by_distance: {location: {origin: {lat: location.latitude, lon: location.longitude}}},
where: {
location: {
near: {
lat: location.latitude,
lon: location.longitude
},
within: "5mi"
}
}
else
Transaction.all
Transaction.all.page(params[:page]).per(5)
end
@transactions = @transactions.page(params[:page]).per(5)
end

# GET /transactions/1
@@ -2,11 +2,17 @@ class Transaction < ApplicationRecord
geocoded_by :address
after_validation :geocode, if: :address_changed?

searchkick locations: [:location]

def address
[street, city, zip, state].compact.join(", ")
end

def address_changed?
street_changed? || city_changed? || zip_changed? || state_changed?
end

def search_data
attributes.merge location: { lat: latitude, lon: longitude }
end
end

0 comments on commit a872a69

Please sign in to comment.
You can’t perform that action at this time.