Skip to content

Communication with Elasticsearch using NEST client

Introduction

Elasticsearch search is a powerful search and analytics engine. Behind the scene Elasticsearch is a document-oriented database. It could be combined with other tools like Kibana, Beats and Logstash. These four tools combined together are called Elastic Stack (aka ELK). In this post I want to focus on the communication with the Elasticsearch, more precisely basic communication using NEST.

Later in the post I described the followings:

  • What is the NEST client? 
  • Practise:
    • Setup the Elasticsearch node locally using docker.
    • Quick and easy browsing stored documents within Elasticsearch using Chrome extension.
    • Present simple car searching engine based on Elasticsearch and NEST client. The app allows to add, remove, update and search the car.
        

What is the NEST?

NEST is a high level client that exposes easy to use concise API to communicate with Elasticsearch. It is one of the two official .NET clients for Elasticsearch. The latter one is called Elasticsearch.Net. Using NEST client you can do indexes and documents operations (add, remove, update), basic and complex searching and so on. I highly recommend to think of a business use case and try to implement it on your own using local Elasticsearch node and NEST client (read on to see how to setup local node).

Practice

Setup the Elasticsearch node locally using docker.

Elasticsearch can be deployed locally using the following docker command:

docker run -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.16.2

The command exposes two ports 9200 and 9300. Port 9200 is used for API calls over HTTP (the one that we need). Port 9300 is used for communications betweend nodes but it is not required for us, so the following command works as well:

docker run -d -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.16.2

Once you type and run the docker command your local Elasticsearch node should be ready to communicate with.

To remove the Elasticsearch node you should do the following:

  • Open terminal.
  • Run docker ps command. The command lists all running containers.
  • Grab the identifier (CONTAINER ID column) of the previously launched Elasticsearch node.
  • Run docker stop <CONTAINER ID> – the Elasticsearch container shouldn’t be listed for docker ps command, but should be listed for docker ps -a command.
  • Run docker rm <CONTAINER ID> – now the Elasticsearch container shouldn’t be listed for docker ps -a command.

Quick and easy browsing of the stored documents within Elasticsearch using Chrome extension

Once the local node of Elasticsearch is running it would be nice to see if it is actually available and, what is more important, to see what is inside. One way to do this is to prepare and send the HTTP request using e.g. VS Code extension (REST Client extension is simple and intuitive), Postman or other similar tool. The request itself is as easy as possible and looks like this: GET http://localhost:9200/INDEX_NAME/_search (the command syntax is based on the VS Code REST Client extension).

Instead of manual crafting a HTTP request that retrieves documents from the Elasticsearch node we can use a Chrome extension called ElasticSearch Head. I had a chance to use it and it was great. Once we add some data I will show how to use the chrome extension to look at the added documents.

Adding, searching and deleting documents using NEST

I’ve prepared a console application that is kind of a NEST playground. The application itself is as easy as possible. Here is the link to the source code: github, project: nest-api. You can clone the example or create the similar one by yourself.

The application simulates simple cars-for-sale database. It uses the NEST client and exposes following operations:

  • Insert predefined cars to the Elasticsearch.
  • Insert user defined car to the Elasticsearch.
  • Remove cars form the Elasticsearch.
  • Search cars by description.
  • List cars.

Whether you decide to use the provided sample or to write your own the aforementioned ElasticSearch Head tool might be helpful. If your local Elasticsearch node is up and running you can go to the ElasticSearch Head extension, type the address and press Connect:

Once the connection is established it is time to add some documents. If you use the provided sample you can run the application and choose 1. Add predefined cars from the menu. The application uses the NEST client to create cars-for-sale index and adds 3 documents. You can see the documents from within the Browser tab and by choosing the proper index name (cars-for-sale) from the Indices section on the left. If you press one of the added documents it displays its content:

Having the environment configured like that you are ready to start exploring NEST client and try to implement your own business cases or some random code just for the sake of self education.

Have a nice day, bye!

Published in.NET

Be First to Comment

Leave a Reply

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