Elasticsearch client for C# (.NET): NEST and Elastic.Clients.Elasticsearch
Elasticsearch is a powerful distributed search and analytics system based on Apache Lucene. To work with it from C# (.NET) applications, there are several official and third-party clients. In this article, we will take a detailed look at two main libraries: NEST (high-level client) and Elastic.Clients.Elasticsearch (new official client from Elastic). You will learn how to install, configure, and use them for indexing, searching, and data management.
1. NEST — high-level client (recommended for complex projects)
NEST is an old but proven client that provides a strongly typed API using POCO classes. It automatically serializes/deserializes JSON to C# objects and supports fluent syntax for building queries.
Installation
dotnet add package NESTExample of using NEST
using Nest;using System;
// POCO class for the documentpublic class Article{ public int Id { get; set; } public string Title { get; set; } public string Content { get; set; } public DateTime PublishDate { get; set; }}
class Program{ static async Task Main(string[] args) { // Connection setup var settings = new ConnectionSettings(new Uri("http://localhost:9200")) .DefaultIndex("articles") .EnableDebugMode(); // Enable request logging
var client = new ElasticClient(settings);
// 1. Indexing a document var article = new Article { Id = 1, Title = "Elasticsearch in C#", Content = "Detailed guide on working with Elasticsearch from .NET", PublishDate = DateTime.UtcNow };
var indexResponse = await client.IndexDocumentAsync(article); Console.WriteLine($"Indexing: {indexResponse.IsValid}");
// 2. Search var searchResponse = await client.SearchAsync<Article>(s => s .Query(q => q .Match(m => m .Field(f => f.Title) .Query("Elasticsearch") ) ) );
foreach (var hit in searchResponse.Hits) { Console.WriteLine($"Found: {hit.Source.Title}"); } }}2. Elastic.Clients.Elasticsearch — new official client
Starting with version 8.x, Elastic released a new client Elastic.Clients.Elasticsearch (also known as Elasticsearch.Net). It is lighter, asynchronous, and compliant with the latest versions of Elasticsearch. Unlike NEST, it does not require mandatory use of POCO classes, although it supports them.
Installation
dotnet add package Elastic.Clients.ElasticsearchExample of using the new client
using Elastic.Clients.Elasticsearch;using Elastic.Clients.Elasticsearch.QueryDsl;using System;
class Program{ static async Task Main(string[] args) { // Connection setup var settings = new ElasticsearchClientSettings(new Uri("http://localhost:9200")) .DefaultIndex("articles");
var client = new ElasticsearchClient(settings);
// 1. Indexing with POCO var article = new Article { Id = 2, Title = "New Elasticsearch client", Content = "Overview of Elastic.Clients.Elasticsearch capabilities", PublishDate = DateTime.UtcNow };
var indexResponse = await client.IndexAsync(article, idx => idx.Index("articles")); Console.WriteLine($"Indexing: {indexResponse.IsValidResponse}");
// 2. Search using lambda syntax var se