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
# Import the data into Elasticsearch | |
class ElasticsearchImporter | |
def call | |
create_new_index | |
swap_alias | |
import | |
delete_old_index | |
end | |
# == Parameters: |
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
class ElasticsearchShapefilesImporter | |
def call | |
ElasticsearchImporter.new( | |
index_alias: 'shapefiles', | |
mapping: mapping, | |
payload: shapefiles_body | |
).call | |
end | |
private |
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 'benchmark/ips' | |
Benchmark.ips do |x| | |
x.report("PostgreSQL join") do | |
Location.joins("LEFT JOIN shapefiles ON ST_Covers(geometry, coordinates)").where(shapefiles: { area_name: 'BR2' }).size | |
end | |
x.report("PostgreSQL param") do | |
Location.where('ST_Covers(?, coordinates)', Shapefile.where(area_name: 'BR2').first.geometry.to_s).size | |
end |
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
Location. | |
joins("LEFT JOIN shapefiles ON ST_Covers(geometry, coordinates)"). | |
where(shapefiles: { area_name: 'BR2' }). | |
size | |
# produces SQL query | |
# SELECT COUNT(*) | |
# FROM "locations" | |
# LEFT JOIN shapefiles ON ST_Covers(geometry, coordinates) | |
# WHERE "shapefiles"."area_name" = $1 [["area_name", "BR2"]] |
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 Rails.root.join('lib/shapefile_importer') | |
# PostgreSQL data preparation | |
Shapefile.delete_all | |
ActiveRecord::Base.connection.execute("TRUNCATE #{Shapefile.table_name} RESTART IDENTITY") | |
ShapefilesImporter.new(shapefile_path: Rails.root.join('db/shapefiles/postal_district.shp')).call | |
Location.delete_all | |
ActiveRecord::Base.connection.execute("TRUNCATE #{Location.table_name} RESTART IDENTITY") |
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
class Location < ApplicationRecord | |
end |
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
class CreateLocations < ActiveRecord::Migration[5.1] | |
def change | |
create_table :locations do |t| | |
t.st_point :coordinates, geographic: true | |
t.timestamps | |
end | |
end | |
end |
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
Warming up -------------------------------------- | |
PostgreSQL join 1.000 i/100ms | |
PostgreSQL param 1.000 i/100ms | |
ES indexed shape 8.000 i/100ms | |
ES shape in query 2.000 i/100ms | |
Calculating ------------------------------------- | |
PostgreSQL join 4.282 (± 0.0%) i/s - 22.000 in 5.139615s | |
PostgreSQL param 3.637 (± 0.0%) i/s - 19.000 in 5.231438s | |
ES indexed shape 115.272 (±10.4%) i/s - 576.000 in 5.054716s | |
ES shape in query 21.279 (± 4.7%) i/s - 106.000 in 5.006692s |
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
query = { | |
"_source":false, | |
"query":{ | |
"bool":{ | |
"must":{ "match_all":{} }, | |
"filter":{ | |
"geo_shape":{ | |
"location":{ | |
"relation":"within", | |
"indexed_shape": { |
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
query = { | |
"_source":false, | |
"query":{ | |
"bool":{ | |
"must":{ "match_all":{} }, | |
"filter":{ | |
"geo_shape":{ | |
"location":{ | |
"relation": "within", | |
"shape": RGeo::GeoJSON.encode(Shapefile.where(area_name: 'BR2').first.geometry) |