Skip to content
This repository has been archived by the owner on Aug 21, 2019. It is now read-only.

Commit

Permalink
Merge 9db3f9a into 3e4f463
Browse files Browse the repository at this point in the history
  • Loading branch information
yih-en committed Jan 11, 2018
2 parents 3e4f463 + 9db3f9a commit 9c424e6
Show file tree
Hide file tree
Showing 21 changed files with 550 additions and 284 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ language: ruby
rvm:
- 2.3.5
before_install:
- curl -O https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.4.deb && sudo dpkg -i elasticsearch-1.4.4.deb && true
- 'echo ''script.disable_dynamic: false'' | sudo tee --append /etc/elasticsearch/elasticsearch.yml'
- 'echo ''index.number_of_shards: 1'' | sudo tee --append /etc/elasticsearch/elasticsearch.yml'
- curl -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.5.deb && sudo dpkg -i --force-confnew elasticsearch-5.6.5.deb && true
- 'printf "script:\n inline: true\n stored: true\n" | sudo tee --append /etc/elasticsearch/elasticsearch.yml'
- sudo service elasticsearch start
6 changes: 4 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ end
gem 'rails', '5.1.4'
gem 'rails-controller-testing', '~> 1.0'
gem 'nokogiri', '~> 1.8.0'
gem 'tire', '~> 0.6.2' #deprecated in 2013
gem 'tire-contrib', '~> 0.1.2'
gem 'oj', '~> 3.1.3' # Unused?
gem 'faraday_middleware', '~> 0.12.2'
gem 'net-http-persistent', '~> 2.8'
Expand All @@ -20,6 +18,10 @@ gem 'rack-cors', '~> 1.0.2'
gem 'us_states', '~> 0.1.1', git: 'https://github.com/GSA/us_states.git'
gem 'newrelic_rpm', '~> 4.6.0'
gem 'rake', '~> 11.0'
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
gem 'elasticsearch-dsl'
gem 'whenever'

group :development, :test do
gem 'puma', '~> 3.7'
Expand Down
36 changes: 22 additions & 14 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ GEM
airbrake (7.1.0)
airbrake-ruby (~> 2.5)
airbrake-ruby (2.6.2)
ansi (1.5.0)
arel (8.0.0)
builder (3.2.3)
capistrano (2.15.4)
Expand All @@ -56,6 +55,7 @@ GEM
net-sftp (>= 2.0.0)
net-ssh (>= 2.0.14)
net-ssh-gateway (>= 1.1.0)
chronic (0.10.2)
concurrent-ruby (1.0.5)
coveralls (0.7.0)
multi_json (~> 1.3)
Expand All @@ -68,6 +68,20 @@ GEM
docile (1.1.3)
domain_name (0.5.20170404)
unf (>= 0.0.5, < 1.0.0)
elasticsearch (5.0.4)
elasticsearch-api (= 5.0.4)
elasticsearch-transport (= 5.0.4)
elasticsearch-api (5.0.4)
multi_json
elasticsearch-dsl (0.1.5)
elasticsearch-model (5.0.2)
activesupport (> 3)
elasticsearch (~> 5)
hashie
elasticsearch-rails (5.0.2)
elasticsearch-transport (5.0.4)
faraday
multi_json
erubi (1.7.0)
faraday (0.13.1)
multipart-post (>= 1.2, < 3)
Expand All @@ -76,7 +90,7 @@ GEM
ffi (1.9.18)
globalid (0.4.1)
activesupport (>= 4.2.0)
hashr (0.0.22)
hashie (3.5.7)
highline (1.6.19)
http-cookie (1.0.3)
domain_name (~> 0.5)
Expand Down Expand Up @@ -204,16 +218,6 @@ GEM
thor (0.20.0)
thread_safe (0.3.6)
tins (0.13.2)
tire (0.6.2)
activemodel (>= 3.0)
activesupport
ansi
hashr (~> 0.0.19)
multi_json (~> 1.3)
rake
rest-client (~> 1.6)
tire-contrib (0.1.2)
tire
tzinfo (1.2.4)
thread_safe (~> 0.1)
unf (0.1.4)
Expand All @@ -222,6 +226,8 @@ GEM
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
whenever (0.10.0)
chronic (>= 0.6.3)

PLATFORMS
ruby
Expand All @@ -230,6 +236,9 @@ DEPENDENCIES
airbrake (~> 7.1)
capistrano (~> 2.15.4)
coveralls (~> 0.7.0)
elasticsearch-dsl
elasticsearch-model
elasticsearch-rails
faraday_middleware (~> 0.12.2)
jbuilder (~> 2.7.0)
listen (>= 3.0.5, < 3.2)
Expand All @@ -250,9 +259,8 @@ DEPENDENCIES
spring
spring-watcher-listen (~> 2.0.0)
test-unit (~> 3.0)
tire (~> 0.6.2)
tire-contrib (~> 0.1.2)
us_states (~> 0.1.1)!
whenever

BUNDLED WITH
1.16.0
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ We use bundler to manage gems. You can install bundler and other required gems l

### Elasticsearch

We're using [Elasticsearch](http://www.elasticsearch.org/) (>= 1.4.0) for fulltext search. On a Mac, it's easy to install with [Homebrew](http://mxcl.github.com/homebrew/).
We're using [Elasticsearch](http://www.elasticsearch.org/) (>= 5.6) for fulltext search. On a Mac, it's easy to install with [Homebrew](http://mxcl.github.com/homebrew/).

$ brew install elasticsearch
$ brew install elasticsearch@5.6

Otherwise, follow the [instructions](http://www.elasticsearch.org/download/) to download and run it.

Expand All @@ -32,21 +32,21 @@ Otherwise, follow the [instructions](http://www.elasticsearch.org/download/) to
Install Docker if you haven't done so yet. Follow the instruction [here](https://www.docker.com/community-edition)
Once you have Docker installed on your machine, run the following command in your terminal

$ docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:1.4.5
$ docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:5.6

This will download an docker image containing elasticsearch=1.4.5 from docker hub, run it, and expose port 9200 & 9300 to your machine. You can verify your setup with the following command.
This will download an docker image containing elasticsearch=5.6.5 from docker hub, run it, and expose port 9200 & 9300 to your machine. You can verify your setup with the following command.

$ curl localhost:9200
{
"status" : 200,
"name" : "Aegis",
"name" : "u2bQgL2",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "qZ-Xas_PR_2ARtHpY724Ug",
"version" : {
"number" : "1.4.5",
"build_hash" : "2aaf797f2a571dcb779a3b61180afe8390ab61f9",
"build_timestamp" : "2015-04-27T08:06:06Z",
"number" : "5.6.5",
"build_hash" : "6a37571",
"build_date" : "2017-12-04T07:50:10.466Z",
"build_snapshot" : false,
"lucene_version" : "4.10.4"
"lucene_version" : "6.6.1"
},
"tagline" : "You Know, for Search"
}
Expand Down
135 changes: 89 additions & 46 deletions app/models/geoname.rb
Original file line number Diff line number Diff line change
@@ -1,75 +1,118 @@
require 'active_model'
require 'elasticsearch/dsl'

class Geoname
include Tire::Model::Search
include ActiveModel::Model
include Elasticsearch::Model
include Elasticsearch::DSL

INDEX_NAME = "#{Rails.env}:geonames".freeze

SYNONYMS = [
"afb, air force base",
"afs, air force station",
"ang, air national guard",
"cavecreek, cave creek",
"ft, fort",
"junc, junction",
"natl, nat, national",
"newcastle, new castle",
"pk, park",
"spgs, springs",
"st, saint"
].freeze

index_name("#{Rails.env}:geonames".freeze)
SETTINGS = {
analysis: {
filter: {
synonym: {
type: 'synonym',
synonyms: SYNONYMS
}
},
analyzer: {
custom_analyzer: {
type: 'custom',
tokenizer: 'whitespace',
filter: %w(standard lowercase synonym)
}
}
}
}

SYNONYMS = ["ft, fort", "st, saint", "afb, air force base", "afs, air force station", "ang, air national guard", "junc, junction", "spgs, springs", "natl, nat, national", "pk, park", "newcastle, new castle", "cavecreek, cave creek"]
settings index: SETTINGS do
mappings dynamic: 'false' do
indexes :type, type: 'keyword'
indexes :location, type: 'text', analyzer: 'custom_analyzer'
indexes :state, type: 'text', analyzer: 'keyword'
indexes :geo, type: 'geo_point'
indexes :id, type: 'keyword', index: false
end
end

class << self

def client
@client ||= Geoname.__elasticsearch__.client
end

def create_search_index
Tire.index index_name do
create(
settings: {
index: {
analysis: {
analyzer: {custom_analyzer: {type: 'custom', tokenizer: 'whitespace', filter: %w(standard lowercase synonym)}},
filter: {synonym: {type: 'synonym', synonyms: SYNONYMS}}
}
}
},
mappings: {
geoname: {
properties: {
type: {type: 'string'},
location: {type: 'string', analyzer: 'custom_analyzer'},
state: {type: 'string', analyzer: 'keyword'},
geo: {type: 'geo_point'},
id: {type: 'string', index: :not_analyzed, include_in_all: false}
}
}
}
)
end
client.indices.create(
index: INDEX_NAME,
body: { settings: settings.to_hash, mappings: mappings.to_hash }
)
end

def geocode(options = {})
search_for(options.merge(size: 1)).results.first.geo.to_hash rescue nil
search_for(options.merge(size: 1)).results.first.geo rescue nil
end

def search_for(options)
Tire.search index_name do
search_definition = Elasticsearch::DSL::Search.search do
query do
boolean do
must { match :location, options[:location], operator: 'AND' }
must { term :state, options[:state] }
bool do
must do
match :location do
query options[:location]
operator 'and'
end
end

must { term state: options[:state] }
end
end

size options[:size]
end
end.to_hash

Geoname.search(search_definition, index: INDEX_NAME)
end

def delete_search_index
search_index.delete
client.indices.delete index: INDEX_NAME if search_index_exists?
end

def search_index
Tire.index(index_name)
def search_index_exists?
client.indices.exists? index: INDEX_NAME
end

def import(geonames)
Tire.index index_name do
import geonames do |docs|
docs.each do |doc|
doc[:id] = "#{doc[:location]}:#{doc[:state]}"
end
end
refresh
geonames.each do |doc|
client.index(
index: INDEX_NAME,
type: 'geoname',
id: "#{doc[:location]}:#{doc[:state]}",
body: {
location: doc[:location],
geo: doc[:geo],
state: doc[:state]
}
)
end

#Tire.index index_name
Rails.logger.info "Imported #{geonames.size} Geonames to #{index_name}"
end
__elasticsearch__.refresh_index! index: INDEX_NAME

Rails.logger.info "Imported #{geonames.size} Geonames to #{INDEX_NAME}"
end
end
end
end

0 comments on commit 9c424e6

Please sign in to comment.