Skip to content

Instantly share code, notes, and snippets.

@mzaidannas
Last active June 4, 2020 00:48
Show Gist options
  • Save mzaidannas/3c6adedc3dde4cd4f617c46407555004 to your computer and use it in GitHub Desktop.
Save mzaidannas/3c6adedc3dde4cd4f617c46407555004 to your computer and use it in GitHub Desktop.
Full Text searching setup using elasticsearch

Searching with elasticsearch

In this tutorial we are going to setup Elasticsearch and it's integration with rails. We will be using Searchkick gem for rails integration

Installing Elasticsearch

Make sure you already have openjdk10 or greater

MacOS

brew tap adoptopenjdk/openjdk
brew tap elastic/tap
brew cask install java
brew install elastic/tap/elasticsearch-full

Debian/Ubuntu

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

Arch/Manjaro

sudo pacman -S elasticsearch

Using Docker for specific elasticsearch version

Mac

Install docker for mac from this link Docker For Mac

Debian/Ubuntu

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

Arch/Manjaro

sudo pacman -S docker

For all OSes

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

Rails setup

  1. Make sure redis and postgresql services are also running apart from elasticsearch
  2. Go to project root. Checkout elasticsearch branch
git checkout -b feature/elasticsearch-implementation
  1. And install required gems
bundle
  1. 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'
  1. Run sidekiq background worker to process indices
bundle exec sidekiq
  1. Index all required models
rake searchkick:reindex_all
  1. Start rails server
bundle exec rails server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment