Skip to main content

Setup Elasticsearch playground

Greetings

When learning new technology, installation is the first burden and it is irritating. However, in modern-day development, we developers usually don't have to bother too much with operations and there are dedicated teams assigned for that purpose. Let's see how we can get started with Elasticsearch.
For someone who starts exploring Elasticsearch, there are two main options.
  • Cloud free trial
  • Docker container

Cloud free trial

This is the easiest way for someone new to Elasticsearch. You just register for the free trial without a credit card. The only drawback is it expires in 14 days. But that is enough for initial experience. If you know how to use emails with plus at the end, you can have endless accounts.
https://www.elastic.co/cloud/elasticsearch-service/signup

Docker container

The other option is to spin up a Docker container. That is also an easy option. However, you need to know just enough Docker to use it. With Elasticsearch 8 security is enabled by default but we don't need that much security for our local setup. Hence we run the container by disabling security. Also, we can allocate memory as well.

Start Elasticsearch with Docker

This is easy. First, we need to create a network for our Elasticsearch Docker container.
docker network create elastic
docker container run \
  --name elasticsearch \
  --net elastic \
  -p 9200:9200 \
  -e discovery.type=single-node \
  -e ES_JAVA_OPTS="-Xms1g -Xmx1g" \
  -e xpack.security.enabled=false \
  -it docker.elastic.co/elasticsearch/elasticsearch:8.6.2
Try it!
curl localhost:9200
{
  "name" : "94d0b7b27340",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "oSE4QwFrRQWH4DYxclohFg",
  "version" : {
    "number" : "8.6.2",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "2d58d0f136141f03239816a4e360a8d17b6d8f29",
    "build_date" : "2023-02-13T09:35:20.314882762Z",
    "build_snapshot" : false,
    "lucene_version" : "9.4.2",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}

Access with Kibana

We can access index API using any REST client. However, it would be easy if we have a user interface. That is why we are going to use Kibana. Now let's create a Kibana instance as well.
docker container run \
  --name kibana \
  --net elastic \
  -p 5601:5601 \
  docker.elastic.co/kibana/kibana:8.6.2
Open localhost:5601 from your browser. You should be able to see Kibana UI where you can enter Elasticsearch queries.

Docker compose

We don't have to run the Docker command in this way every time. We can simplify the process using Docker compose.
version: "3.9"
services:
  elasticsearch:
    image: elasticsearch:8.6.2
    environment:
    - xpack.security.enabled=false
    - discovery.type=single-node
    - ES_JAVA_OPTS=-Xms1g -Xmx1g
    volumes:
    - es_data:/usr/share/elasticsearch/data
    ports:
    - target: 9200
      published: 9200
    networks:
    - elastic

  kibana:
    image: kibana:8.6.2
    ports:
    - target: 5601
      published: 5601
    depends_on:
    - elasticsearch
    networks:
    - elastic

volumes:
  es_data:
    driver: local

networks:
  elastic:
    name: elastic
    driver: bridge
docker-compose -f elasticsearch-docker-compose.yml up
Note that I have named my docker-compose file elasticsearch-docker-compose.yml

Connect to the local instance

As we didn't specify security, we don't have a password to use. For example, a curl command is as below.
curl localhost:9200
Let's say we want to connect with nodejs. Then we just need to add the host only.
const { Client } = require('@elastic/elasticsearch')
const client = new Client({
  node: 'localhost:9200'
})

Conclusion

In this article we quickly looked into options we can have to set up Elasticsearch for our learning purpose. Let's explore initial queries in an upcoming article.

Happy learning

References

https://levelup.gitconnected.com/how-to-run-elasticsearch-8-on-docker-for-local-development-401fd3fff829
https://lynn-kwong.medium.com/learn-elasticsearch-from-practical-examples-495f2f8db83e

Comments