PART II | Adding and Searching Document With Elastic Search, Express&TypeScipt

Nacef Otay
eDonec
Published in
4 min readMay 21, 2021
www.elastic.co

Elasticsearch is a distributed, RESTful search and analytics engine capable of addressing a growing number of use cases. As the heart of the Elastic Stack, it centrally stores your data for lightning fast search, fine‑tuned relevancy, and powerful analytics that scale with ease.

In this article, we’ll take a look at using Node Js with an Elasticsearch deployment, index some documents and perform a simple text search.

Elasticsearch will therefore need to know which words are used in each document. To do this, Elasticsearch integrates a Lucene engine that will take care of extracting words from a collection of documents and preparing columns of words.

Indeed, elasticsearch is a column-oriented NoSQL database: a word = a column of documents with the value of the word weight in each document. The distribution layer performed by Elasticsearch makes it possible to route requests, parallelize processing, replicate data in the event of a failure, and increase Lucene’s indexing capacity.

Installation

Server: Install and configure

Client:

yarn add @elastic/elasticsearch
or
npm install @elastic/elasticsearch

Elastic Cloud

We’ll use Elastic’s free trial version of the Elastic Cloud Service to quickly get a hosted cluster up and running for the sake of convenience. To start your free trial and deploy an Elasticsearch cluster, go here and follow the steps.

we need to add our Elasticsearch credentials to enable communication between our server and our Elasticsearch cluster

lets Create a src/config.ts file in your root folder and add the following code:

This code first requires the Elasticsearch library and then sets up a new Elasticsearch client for local and cloud server

As you can see, we need to add the cloud id and the auth (You can use API key instead) for Cloud Client

and for Local elastic Client you need just to connect to default host 127.0.0.1:9200

you can add other params like :

// Max retries and timeout for each request. 
maxRetries: 3,
requestTimeout: 5000,

Let’s start by creating a index.ts file in your root folder and add the following code:

Now start the server to check the status of Elasticsearch

Adding One Documents To Indexes :

An Elasticsearch index, unlike a traditional database, is a location where similar documents can be stored. For our example, to store data of type photos, you’ll create an index album

You get an index_already_exists exception if the index already exists. Otherwise, a new index ready to store your documents is created.

  • id: The unique identifier for the added document.
  • index: The name of the index.
  • type: for the category
  • Body: We have defined a body object with key parameters of our document

If the document already exists and we didn’t specify a version argument, the existing document will be simply updated by the new one and specified as its second version

Adding Bulk Documents To Indexes :

the bulk method Performs multiple indexing or delete operations in a single API call. This reduces overhead and can greatly increase indexing speed.

Run it and you should get a response that shows a count of added documents.

Deleting a document is as easy as indexing it

Searching

Obviously, one of the things you’re going to want to do with your Elasticsearch index is search it

will return a list of quotes matching our query passed in req.query.search to find albums with a title that match the search term

We’re now ready to start our application and testing

Just call the /photos?search=meduimendpoint and the response will be:

Full Code:

Conclusion

Cross-shard search, scrolling, bulk operations in a single API call, and more are all supported by elastic search. This library is the best option for your Node.js application because it covers a wide range of low-level Elasticsearch functions and leverages the power of Javascript asynchronous calls.

This has been developed by myself at eDonec .

--

--