DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Building REST API Backend Easily With Ballerina Language
  • Aggregating REST APIs Calls Using Apache Camel
  • Using Slash GraphQL to Create InstaMeme—A Meme Sharing App
  • Understanding the Fan-Out/Fan-In API Integration Pattern

Trending

  • GitHub Copilot's New AI Coding Agent Saves Developers Time – And Requires Their Oversight
  • Monolith: The Good, The Bad and The Ugly
  • Go 1.24+ Native FIPS Support for Easier Compliance
  • SaaS in an Enterprise - An Implementation Roadmap
  1. DZone
  2. Data Engineering
  3. Databases
  4. Working With Documents Using Elastic-Search and Node

Working With Documents Using Elastic-Search and Node

I am going to use ElasticSearch as document persistence mechanism and Node.js to work with elastic-search.

By 
Jawad Hasan Shani user avatar
Jawad Hasan Shani
DZone Core CORE ·
Oct. 05, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
9.7K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

Working with documents in software is fun. It means that storage fit your code not the other way around. This removes the object relational impedance mismatch between how you model your application and how you store those models.

Even if you do not have immediate use of documents, learning how to use documents will broaden your perspective of storage systems.

Recently I have written a post on DZone about document databases in postgreSQL and build a small application using .NET Core. However, this time, I am going to use ElasticSearch as document persistence mechanism and Node.js to work with elastic-search. Elastic Stack (ELK) have other components e.g. Kibana, Log-Stash etc., but I will be only using elastic-search component for discussion.

Elastic Search and Index

Elasticsearch is a search engine based on the Lucene library. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents. Elasticsearch is developed in Java.

What Is an Index

  • The word index itself has different meanings in different context in elastic-search.
  • Unlike conventional database, In ES, an index is a place to store related documents.
  • An index is a collection of documents that have somewhat similar characteristics.
  • The act of storing documents in an index is known indexing.
  • An index is identified by a name, which is used to refer to the index when performing indexing, search, update and delete operations against the documents in it.
  • We’ll see later how to create index, delete index and store documents in the index etc.

Installation

You can download installer for elastic-search from official website and install it or alternatively you can use some cloud services for same purpose.

For the demo, I installed it on my local machine and then browse to following URL to verify the installation.

localhost

Document

Before we start working with elastic-search, lets talk a little bit about documents. You can also read my previous post if you need more details about documents. Documents are essentially a set of key-value pair.

customer list

A document is a basic unit of information that can be indexed e.g. you can have a document for a single customer, another document or single product and yet another for a single order.

The document is expressed in JSON which is ubiquitous internet data interchange format.

Within an index/type, you can store as many documents as you want. You can imagine those records in a relational database thinking.

Elastic-Search Restful API

Elastic-Search has quite a few APIS:

  • “cluster” API: to manage clusters.
  • “index” API: Give access to our indices, mapping, aliases etc.
  • “search” API: To query, count, filter, data access multiple indices and types.
  • “document” API: to add data etc.

Here are few of the API calls using browser:

API calls

ElasticSearch With Node

Now we will build a simple Node.js application to perform some operations on elastic-search. I initialized the source-code folder with npm init command:

JSON

Install Elasticsearch Package

Next, I installed elasticsearch npm package as follows:

terminal

Connection to ElasticSearch

Connection.js file encapsulate connection to elastic-search.

elasticsearch

To verify the connection I add the following code:

client.cluster

Let's run it and see if the connection works:

NodeApp

Ok, our connection is working and lets continue to work with index and documents.

Build an Index API

NPM elasticsearch package exposes many methods that we can use in our application. We can create index, delete them, add documents to them, etc. The index name should be lower case. If index already exists, you get ‘index_already_exists_exception’. You can have an index for customer data, another for product catalog, and yet another index for order data.

For this demo, I will create an API IndexManager to encapsulate these concerns and then we can just use that API. I will keep the implementation simple, however, feel free to adjust as per your style. The API will have the following functionality:

  • Create Index
  • Delete Index
  • Check if Index exist
  • Add documents to index

Here is the code for the API which is self explanatory, however if something is not clear, ask in comment:

API

Here is the method implementation code:

console.log

Client Code

Now, as we have functionality related to connection and working with index and documents, let execute these commands:

Create Index ‘Blog’:

NodeApp

Add document to Index

I created a class for Post and this will be searlized to json and save as document into index blog:

Add document to Index

and execute the code:

execute the code

and we can see that data is inserted to elasticsearch:

elasticsearch

Import Documents (Blogs) into ElasticSearch Index ‘Blog’

The following code will read the data from a JSON file, parse it, and then save it in the elasticsearch index. you can use this method to populate documents.

JSON Data File

JSON Data File

JSON Data Loader

the following code, read JSON data file and return the documents objects:

JSON Data Loader

and also update the client-code, as follows:

client-code

Here is the data of all documents after executing the code:

all documents

Summary

Elastic-Search is a very powerful and easy to integrate options for your persistence mechanism. The REST API provides very useful services to work with elasticsearch. You can download the code for this application from this git repo. Till next time, Happy Coding.

References

  • https://www.elastic.co/
  • https://hexquote.com/net-core-postgresql-and-document-database/
  • https://github.com/jawadhasan/esdemo.git
code style Database Relational database Data file Elasticsearch API application Search engine (computing) Connection (dance) JSON Web Protocols

Published at DZone with permission of Jawad Hasan Shani. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building REST API Backend Easily With Ballerina Language
  • Aggregating REST APIs Calls Using Apache Camel
  • Using Slash GraphQL to Create InstaMeme—A Meme Sharing App
  • Understanding the Fan-Out/Fan-In API Integration Pattern

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!