Why use an Elasticsearch Library for .NET?

Even though Elasticsearch offers a comprehensive REST API, there are benefits to using a strongly-typed library like NEST.

Pedestrian risking their life by ignoring a guard rail

Most modern languages provide an easy way to interact with REST APIs,and Elasticsearch already offers a robust REST API that does everything. A third party like NEST adds a layer of abstraction that brings bugs, misses features and are awkward to maintain. They also extend your security footprint, adding another vector for a vulnerability.

Why would you bother taking on a third party library like this? I can think of two reasons, but first a little bit about NEST.

About NEST

NEST is the official Elasticsearch library for .NET, published by Elastic Company and open-sourced under the Apache 2 license (that's one of the "good" ones.)

NEST provides a fluent interface that lets you build Elasticsearch requests by chaining method calls together. The strongly typed library acts like guard rails, helping to prevent invalid requests and enabling development environments like Visual Studio and Visual Studio Code to provide code completion saves you typing and catches problems before you even compile your code.

NEST also provides a low-level client that you can use to send arbitrary requests to Elasticsearch in case you need to pass along requests generated outside of your system, so you aren't losing any capabilities.

Sample query:

Gets the top 10 Podcasts from "Coding Blocks" or about "Elasticsearch"

var response = client.Search<Podcast>(s => s
    .From(0)
    .Size(10)
    .Query(q => q
        .Term(t => t.Author, "Coding Blocks") || q
        .Match(mq => mq
          .Field(f => f.Episode).Query("Elasticsearch")
        )
    )
);

Now we're ready, so let's go back to our original question:

Why use an Elasticsearch Library for .NET?

Because strongly typed libraries...

  1. Protect you from generating most invalid requests at compile-time
  2. Give IDEs like Visual Studio and Code everything they need to offer code completion

You may decide that these benefits aren't worth it for your organization, but I hope you consider them!


Photo by Aravind Kumar on Unsplash