This file contains hidden or 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) |
This file contains hidden or 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 ElasticsearchLocationsImporter | |
| def call | |
| ElasticsearchImporter.new( | |
| index_alias: 'locations', | |
| mapping: mapping, | |
| payload: locations_body | |
| ).call | |
| end | |
| private |
This file contains hidden or 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 hidden or 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
| es_params = { urls: ENV.fetch('ELASTICSEARCH_URL', 'localhost:9200'), | |
| retry_on_failure: 5, | |
| retry_on_status: [400, 500, 502, 503], | |
| reload_connections: 10_000, | |
| reload_on_failure: true, | |
| sniffer_timeout: 15, | |
| request_timeout: 5 * 60} | |
| if ENV['ELASTICSEARCH_LOGGER_ENABLED'].present? |
This file contains hidden or 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
| gem 'connection_pool' | |
| gem 'elasticsearch' |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
| default: &default | |
| adapter: postgis | |
| encoding: unicode | |
| pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> | |
| development: | |
| <<: *default | |
| database: MapFilters_development | |
| test: |