Elasticsearch
What is and how to use Elasticsearch.
Elasticsearch is developed specifically with search and layered navigation in mind. Because of this Elasticsearch is much more suitable for e-commerce sites navigation and search features. Traditional solutions usually use MySQL, this results in a much bigger impact on the server load and negative performance of the application.
Elasticsearch is controlled using the Hipex CLI docker commands. For the rest of this tutorial we assume you are familiar with the docker basics.
Service configuratie
First we configure the service using a docker-compose.yml
. In this file the elasticsearch version and port is defined.
In these examples version 7.8 and port 19200
is used.
We recommend using a seperate folder for your elasticsearch instance so your configuration file would end up in:
~/elasticsearch/docker-compose.yml
.
version: "3.2"
services:
elasticsearch:
image: elasticsearch:7.8.0
restart: "always"
environment:
- LOG4J_FORMAT_MSG_NO_LOOKUPS=true
- discovery.type=single-node
- ES_JAVA_OPTS=-Xmx3g
- xpack.security.enabled=true
- xpack.security.audit.enabled=true
- ELASTIC_PASSWORD=password
volumes:
- ./data:/var/elasticsearch
ports:
- "19200:9200"
Since we are using a mounted volume we also need to create the data directory.
cd ~/elasticsearch/
mkdir data
chmod 644 data
Now the service can be started using docker:compose:up
.
cd ~/elasticsearch/
hipex docker:compose:up --detach
Security test Log4j
To check if the environment variables are set correctly, you can run the test below:
If the value is true, this is set correctly.
[16:29:36]-[Servername]-[Sshuser]-[~/elasticsearch]$ hipex docker:compose:exec elasticsearch bash
[root@5c3facac6acc elasticsearch]# echo $LOG4J_FORMAT_MSG_NO_LOOKUPS
true
With the command exit you can exit the docker container again.
Memory settings
By default ElasticSearch allocates half of the available memory on the server. To prevent this from happening you can add an extra variable to the Docker configuration. If you add the variable '- ESJAVAOPTS=-Xmx3g' to the Docker compose file, ElasticSearch will allocate a maximum of 3GB of memory. If more memory is needed/desired you can increase the amount of GB.
Port bind error
When receiving a Bind for 0.0.0.0:19200 failed: port is already allocated
error, the configured port 19200
is
already in use and another port must be configured.
Usage
Now you should be able to access your elasticsearch instance on port 19200
.
curl localhost:19200
{
"name" : "512decbc664a",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "hZ1_YRATQdOOw1tIz4nn2Q",
"version" : {
"number" : "7.8.0",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "757314695644ea9a1dc2fecd26d1a43856725e65",
"build_date" : "2020-06-14T19:35:50.234439Z",
"build_snapshot" : false,
"lucene_version" : "8.5.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
Plugins
When you need plugins like analysis-icu
or analysis-phonetic
for example the suggested way to do this is to create
your own docker image like this:
FROM elasticsearch:6.8.0
RUN elasticsearch-plugin install -b analysis-icu
RUN elasticsearch-plugin install -b analysis-phonetic
If building your own docker image is not an option you can overwrite the default startup command to install them at startup.
version: "3.2"
services:
elasticsearch:
image: elasticsearch:7.8.0
restart: "always"
volumes:
- ./data:/var/elasticsearch
environment:
discovery.type: "single-node"
command: bash -c "elasticsearch-plugin list | grep analysis-icu || elasticsearch-plugin install -b analysis-icu && elasticsearch-plugin list | grep analysis-phonetic || elasticsearch-plugin install -b analysis-phonetic && docker-entrypoint.sh"
ports:
- "19201:9200"
The elasticsearch-plugin list | grep analysis-icu || elasticsearch-plugin install -b analysis-icu
part will
install the analysis-icu
plugin if not installed. And we end with the default entrypoint docker-entrypoint.sh
.