documentation/Running-Mastodon/Elasticsearch-guide.md

4.7 KiB

Elasticsearch Guide

The following guide explains how to use Elasticsearch.

Why you might need Elasticsearch

If you want to use Full-text search, you should setup Elasticsearch.

Note: If your VPS has not much memory, Elasticsearch can't be able to start. This guide also explains how to start with this case.

Installing Pre-required dependency

Elasticsearch is built using Java, and requires at least Java 8 in order to run.

Install Java 8:

sudo apt install openjdk-8-jre

The openjdk-8-jre package contains just the Java Runtime Environment. If you want to develop Java programs then please install the openjdk-8-jdk package.

You need to install the apt-transport-https.

sudo apt install apt-transport-https

Installing Elasticsearch

Import the Elasticsearch PGP Keyedit

Download and install the public signing key:

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

Installing from the APT repository

sudo echo "deb https://artifacts.elastic.co/packages/6.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-6.x.list
sudo apt update && sudo apt install elasticsearch

Running Elasticsearch with systemd

To configure Elasticsearch to start automatically when the system boots up, run the following commands:

sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable elasticsearch.service

Start Elasticsearch:

sudo systemctl start elasticsearch

If you want to check Elasticsearch status, run the following commands:

sudo systemctl status elasticsearch

If you can't start Elasticsearch, It's probably memory is insufficient.

RAM 1GB RAM 4GB
Elasticsearch-Guide-1GB Elasticsearch-Guide-4GB

Configuring Elasticsearch (Optional)

This guide may be useful if you can't start Elasticsearch, or if Elasticsearch uses too much memory.

First, Elasticsearch needs a lot of memory.

You should check this page.
But, as far as I can recollect, Mastodon does not require much memory for Elasticsearch.
When building Elasticsearch server on low memory VPS, It's necessary to change the configuration.

Change these lines in /etc/elasticsearch/jvm.options:

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms1g
-Xmx1g

Note: Are you worried about how much memory configuration need on your VPS? This list may be useful! In my opinion, It would be better to set it from 35% to 50% of the total memory.

Memory Xms Xmx
1GB 256m 256m
2GB 512m 512m
3GB 1g 1g
4GB 2g 2g
RAM 1GB RAM 4GB
Elasticsearch-Guide-1GB Elasticsearch-Guide-4GB

Please don't forget! This isn't necessarily the best configuration.

Warning: The memory in this list, It's for Elasticsearch. It's not total memory of VPS.

Probably you could start Elasticsearch.

After changing these lines, Start Elasticsearch:

sudo systemctl start elasticsearch

And check Elasticsearch status:

sudo systemctl status elasticsearch

Configuring Mastodon for Elasticsearch

Change Elasticsearch configuration in .env.production to:

# Optional ElasticSearch configuration
ES_ENABLED=true
ES_HOST=localhost
ES_PORT=9200

Note: You can configure the mastodon to use an Elasticsearch on the different server.

If you want to use an Elasticsearch on the different server, You should change these.

Change these line in /etc/elasticsearch/elasticsearch.yml:

# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0
#
# Set a custom port for HTTP:
#
http.port: 9200

Restart Elasticsearch:

sudo systemctl restart elasticsearch

.env.production also need to be change.

# Optional ElasticSearch configuration
ES_ENABLED=true
ES_HOST=Put the IP of Elasticsearch server here.
ES_PORT=9200

Run chewy:deploy to create & populate index

su - mastodon
cd live
RAILS_ENV=production bundle exec rails chewy:deploy

You need restart mastodon.

Resources