This article demonstrates how to use elastic search in .net applications using Nest client. I will demonstrate how to set up Elastic search in a developer machine and how to communicate with Elastic from a .net application. I am going to cover the below points in the article.
- How to download and install Elastic in the developer system?
- How to communicate from the .net application?
- Create an Index in Elastic
- Insert data in the Index.
- Read data from Index.
What is elastic search?
According to the official Elastic search website, Elastic. co
"Elasticsearch is a distributed, open-source search and analytics engine for all types of data, including textual, numerical, geospatial, structured, and unstructured.
Elasticsearch is built on Apache Lucene and was first released in 2010 by Elasticsearch N.V. (now known as Elastic). Known for its simple REST APIs, distributed nature, speed, and scalability, Elasticsearch is the central component of the Elastic Stack, a set of open source tools for data ingestion, enrichment, storage, analysis, and visualization. Commonly referred to as the ELK Stack (after Elasticsearch, Logstash, and Kibana), the Elastic Stack now includes a rich collection of lightweight shipping agents known as Beats for sending data to Elasticsearch."
Let's start step by step. First, we will install Elastic in the developer machine.
How to download and install Elastic in a developer system?
- Download Elastic Search from this link Elastic Search 7.8 and follow the below steps to configure it.

- Once you click on the above link you will get Elastic search in the .zip folder. Extract it and install it in the local system.
- After File Extraction click on ElasticSearch.bat file.

- Once you click on the elasticsearch.bat file your server will be up and running like the below screen:

- If elasticsearch.bat doesn't run properly then you need to check if jdk is installed in the system or not. If not then you need to install it. Here we have used Elasticsearch.Nest client - 7.8 and jdk-14.0.1
- Add a new project from Visual Studio and add Elasticsearch.Nest reference from nuget package manager as below.

Once a reference is added you need to create an Elastic client connecter which is going to communicate with Elasticsearch. I have added the code snippet below.
public class ElasticSearchClient
{
#region Elasticsearch Connection
public ElasticClient EsClient()
{
var nodes = new Uri[]
{
new Uri("http://localhost:9200/"),
};
var connectionPool = new StaticConnectionPool(nodes);
var connectionSettings = new ConnectionSettings(connectionPool).DisableDirectStreaming();
var elasticClient = new ElasticClient(connectionSettings.DefaultIndex("productdetails"));
return elasticClient;
}
#endregion Elasticsearch Connection
}
Add model and controller for inserting document to index.
public class Products
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public decimal Price { get; set; }
public string Description { get; set; }
public List<CheckBoxModel> Colors { get; set; }
public List<CheckBoxModel> Tags { get; set; }
}
public class CheckBoxModel
{
public int Value { get; set; }
public string Text { get; set; }
public bool IsChecked { get; set; }
}
public class ProductController : Controller
{
private readonly ElasticSearchClient _esClient;
public ProductController()
{
_esClient = new ElasticSearchClient();
}
// GET: Product
public ActionResult Index()
{
return RedirectToAction("Create");
}
// GET: Product/Create
public ActionResult Create()
{
var colors = new List<CheckBoxModel>()
{
new CheckBoxModel { Value = 1, Text = "Red", IsChecked = true },
new CheckBoxModel { Value = 1, Text = "Green", IsChecked = false },
new CheckBoxModel { Value = 1, Text = "Orange", IsChecked = false },
new CheckBoxModel { Value = 1, Text = "Blue", IsChecked = false },
new CheckBoxModel { Value = 1, Text = "Purple", IsChecked = false },
new CheckBoxModel { Value = 1, Text = "Yellow", IsChecked = false },
};
var tags = new List<CheckBoxModel>()
{
new CheckBoxModel { Value = 1, Text = "Mens wear", IsChecked = true },
new CheckBoxModel { Value = 1, Text = "Ladies Wear", IsChecked = false },
new CheckBoxModel { Value = 1, Text = "New Arrival", IsChecked = false },
new CheckBoxModel { Value = 1, Text = "Top Brands", IsChecked = false },
new CheckBoxModel { Value = 1, Text = "Kids Wear", IsChecked = false },
new CheckBoxModel { Value = 1, Text = "Jeans", IsChecked = false },
};
var productDetails = new Products
{
Colors = colors,
Tags = tags
};
return View(productDetails);
}
// POST: Product/Create
[HttpPost]
public ActionResult Create(Products products)
{
try
{
var productDetail = new ProductDetails()
{
ProductId = products.ProductId,
ProductName = products.ProductName,
Price = products.Price,
Description = products.Description,
Colors = products.Colors.Where(x => x.IsChecked == true).Select(x => x.Text).ToArray(),
Tags = products.Tags.Where(x => x.IsChecked == true).Select(x => x.Text).ToArray()
};
// Insert product detail document to index
var defaultIndex = "productdetails";
var indexExists = _esClient.EsClient().Indices.Exists(defaultIndex);
if (!indexExists.Exists)
{
var response = _esClient.EsClient().Indices.Create(defaultIndex,
index => index.Map<ProductDetails>(
x => x.AutoMap()
));
}
var indexResponse1 = _esClient.EsClient().IndexDocument(productDetail);
return RedirectToAction("Create");
}
catch
{
return View();
}
}
}






Ammar ShaukatPosted Apr 25, 2024, 10:22 AM
Nice addition for beginners
bongsoo shinPosted Apr 17, 2023, 5:18 AM
How do I delete in Elastic Search .net applications
Manoj KumarPosted Apr 12, 2022, 9:45 AM
It requires me to download lot's of class files why???
Jignesh KumarPosted Sep 8, 2021, 6:02 AM
@milan raval you can use IndexMany() which does same thing. var res = _esClient.IndexMany(YourDataModel);
milan ravalPosted Sep 7, 2021, 2:49 PM
Hi Jignesh, do you have any example of updating existing document in bulk ?
Pankajkumar PatelPosted Dec 1, 2020, 10:42 PM
Nice article especially for beginners.