Unlocking the Power of Full-Text Search with ElasticSearch and Docker

The Quest for Efficient Data Storage and Retrieval

When it comes to storing and searching large amounts of data, traditional databases can fall short. That’s where ElasticSearch comes in – a search engine server built on top of Lucene, offering unparalleled performance and scalability. In this article, we’ll embark on a journey to build a simple REST application, dubbed “The Quotes Database,” which leverages ElasticSearch and Docker to store and search thousands of quotes with ease.

Setting Up Docker: The Foundation of Our Application

To ensure a seamless development experience, we’ll utilize Docker to orchestrate both our Node.js server and ElasticSearch instance within a container. This approach allows us to deploy a production-ready application with all the necessary dependencies. Let’s create a Dockerfile inside our project root folder:

FROM node:10.15.3-alpine
WORKDIR /usr/src/app
COPY package*.json./
RUN npm install
RUN npm install -g pm2
COPY../
EXPOSE 3000
EXPOSE 9200
CMD ["npm", "run", "start"]

Orchestrating Multiple Containers with Docker-Compose

To manage multiple Docker containers, we’ll employ docker-compose. This tool enables us to create connections between containers and streamline our development process. Let’s craft a docker-compose.yml file:

version: '3.6'
services:
api:
build:.
ports:
- "3000:3000"
environment:
- ELASTICSEARCH_URL=http://elasticsearch:9200
depends_on:
- elasticsearch
links:
- elasticsearch
volumes:
-./quotes:/usr/src/app/quotes
networks:
- esnet
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.9.2
volumes:
- esdata:/usr/share/elasticsearch/data
networks:
- esnet
volumes:
esdata:
networks:
esnet:

Bootstrapping the Node.js App

With our Docker setup in place, let’s create our Node.js application. We’ll start by defining our package.json file and installing the necessary dependencies. Next, we’ll implement our ElasticSearch connector in Node.js, creating a new /src/elastic.js file:
“`
const { Client } = require(‘@elastic/elasticsearch’);
const client = new Client({ node: ‘http://elasticsearch:9200’ });
const index = ‘quotes’;
const type = ‘quotes’;

async function createIndex() {
// Create an index on ElasticSearch
}

async function createMapping() {
// Define the schema and types of our document
}

async function checkESConnection() {
// Check if the ES server is ready
}
“`
Populating ElasticSearch with Quotes

Now that we have our ElasticSearch connector in place, let’s populate our ES instance with quotes. We’ll create a new file in /src/data/index.js:
“`
const elastic = require(‘../elastic’);
const quotes = require(‘./quotes.json’);

async function populateES() {
// Insert quotes into ElasticSearch
}
“`
Creating a RESTful API

With our data in place, let’s create a RESTful server using Express.js. We’ll define two endpoints: one for retrieving quotes based on query string parameters and another for posting new quotes.
“`
const express = require(‘express’);
const app = express();

app.get(‘/’, async (req, res) => {
// Retrieve quotes matching query string parameters
});

app.post(‘/new’, async (req, res) => {
// Post a new quote to ElasticSearch
});
“`
Launching the Application

We’re now ready to start our application using docker-compose! Simply run the following command:

docker-compose up

Testing Our Application

Let’s test our application with a few cURL calls:

curl http://localhost:3000/?text=love&limit=3
curl http://localhost:3000/quotes/new -X POST -H "Content-Type: application/json" -d '{"author": "John Doe", "quote": "Life is beautiful"}'

And that’s it! We’ve successfully built a RESTful application leveraging ElasticSearch and Docker. From here, the possibilities are endless – explore scaling your container, creating new endpoints, and optimizing your application’s performance.

Leave a Reply

Your email address will not be published. Required fields are marked *