Back to home

Why ElasticSearch?Pulling ElasticSearch with DockerTesting cluster healthInteracting with the ElasticSearch ClusterSetting up the clientBasic doc creationBasic search
Intro To Elasticsearch main image

Intro To Elasticsearch

  • Open-source
  • Boardly distributable
  • Readily scalable
  • Enterprise-grade
  • Multi-tenancy (can separate user documents)

ElasticSearch is Java-based and designed to operate in real-time. ElasticSearch has the capability to search and index document files in a variety of formats.

ElasticSearch achieves incredible performance as it searches for indexes instead of text directly. A comparison of this can be made to searching keywords.

A typical setup with extend search capabilities through the use of APIs and query DSLs and there are a bunch of ready-to-go clients.

Pulling ElasticSearch with Docker

To install version 6.7.1 of ElasticSearch:

1 > docker pull docker.elastic.co/elasticsearch/elasticsearch:6.7.1

To then start the image for development or testing:

1 > docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.7.1

If you want to start a cluster with two ElasticSearch nodes, use the docker-compose.yml file given from the main website - also copied below:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 version: "2.2" services: elasticsearch: image: docker.elastic.co/elasticsearch/elasticsearch:6.7.1 container_name: elasticsearch environment: - cluster.name=docker-cluster - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ulimits: memlock: soft: -1 hard: -1 volumes: - esdata1:/usr/share/elasticsearch/data ports: - 9200:9200 networks: - esnet elasticsearch2: image: docker.elastic.co/elasticsearch/elasticsearch:6.7.1 container_name: elasticsearch2 environment: - cluster.name=docker-cluster - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" - "discovery.zen.ping.unicast.hosts=elasticsearch" ulimits: memlock: soft: -1 hard: -1 volumes: - esdata2:/usr/share/elasticsearch/data networks: - esnet volumes: esdata1: driver: local esdata2: driver: local networks: esnet:

Run docker-compose up to run the cluster and docker-compose down to tear down. Use docker-compose down -v to also destroy the data volumes, otherwise the cluster data volumes persist.

Testing cluster health

Simply run curl http://127.0.0.1:9200/_cat/health

Interacting with the ElasticSearch Cluster

In this case, we will use the NodeJS client to run a few commands to interact with the local cluster.

The official repo for this client can be found here.

Setting up the client

1 2 3 4 > mkdir hello-elasticsearch > cd hello-elasticsearch > yarn init -y > yarn add @elastic/elasticsearch@7

Basic doc creation

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 const { Client } = require("@elastic/elasticsearch"); const client = new Client({ node: "http://localhost:9200" }); // promise API const result = await client.create({ index: "my-index", body: { foo: "bar" }, }); // callback API client.create( { index: "my-index", body: { foo: "bar" }, }, (err, result) => { if (err) console.log(err); } );
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 const { Client } = require("@elastic/elasticsearch"); const client = new Client({ node: "http://localhost:9200" }); // promise API const result = await client.search({ index: "my-index", body: { foo: "bar" }, }); // callback API client.search( { index: "my-index", body: { foo: "bar" }, }, (err, result) => { if (err) console.log(err); } );

For more examples using CURL and the options available, TutorialsPoint have a basic intro for more info found here

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on workingoutloud.dev, Den Dribbles and LandPad .

Related articles


1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.