In this tutorial we are going to setup Elasticsearch and it's integration with rails. We will be using Searchkick gem for rails integration
Make sure you already have openjdk10 or greater
brew tap adoptopenjdk/openjdk
brew tap elastic/tap
brew cask install java
brew install elastic/tap/elasticsearch-full
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
sudo apt-get install apt-transport-https
echo "deb https://artifacts.elastic.co/packages/6.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-6.x.list
sudo apt-get install default-jre
sudo apt-get update && sudo apt-get install elasticsearch
sudo pacman -S elasticsearch
Install docker for mac from this link Docker For Mac
sudo apt-get remove docker docker-engine docker.io containerd runc
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
sudo pacman -S docker
Add docker group to current user to allow docker to run without root
sudo sysctl -w vm.max_map_count=262144
sudo usermod -G docker -a $USER
reboot
After rebooting run elasticsearch version with docker using
docker volume create elasticsearch_data # For persistant storage
docker run --name es -d -p 9200:9200 -e http.port=9200 -e http.cors.enabled=true -e http.cors.allow-origin=http://localhost:3000,http://127.0.0.1:3000,http://localhost:1358,http://127.0.0.1:1358 -e http.cors.allow-headers=X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization -e http.cors.allow-credentials=true -v elasticsearch_data:/usr/share/elasticsearch/data docker.elastic.co/elasticsearch/elasticsearch:6.8.9
- Make sure redis and postgresql services are also running apart from elasticsearch
- Go to project root. Checkout elasticsearch branch
git checkout -b feature/elasticsearch-implementation
- And install required gems
bundle
- Add environment variables for postgres, redis, elasticsearch in
config/application.yml
like
default_env: &default_env
REDIS_URL: 'redis://localhost:6379/0'
# Database envs
PG_HOST: 'localhost'
PG_PORT: 5432
PG_USER: 'postgres'
PG_PASSWORD: ''
ELASTICSEARCH_URL: 'http://localhost:9200'
- Run sidekiq background worker to process indices
bundle exec sidekiq
- Index all required models
rake searchkick:reindex_all
- Start rails server
bundle exec rails server