Managing Large Data Volumes Made Easy: A Step-by-Step Tutorial on Retrieving Data from MSSQL and Storing it in Elasticsearch with .NET 7 Web API and Entity Framework Core

Stanley Okpala
3 min readApr 10, 2023

--

Introduction: In today’s world, data is everything, and with an ever-increasing amount of data, it has become a challenging task to manage it. Elasticsearch is one such powerful tool that makes managing large amounts of data seamless. Elasticsearch is an open-source, distributed search and analytics engine designed to store, search, and analyze large volumes of data quickly and in near real-time. In this tutorial, we will be focusing on how to retrieve data from MSSQL and store it in Elasticsearch, and then search for that data using .NET 7 Web API and Entity Framework Core.

Step by Step Tutorial:

Step 1: Install Elasticsearch: The first step is to download and install Elasticsearch. You can download Elasticsearch from the official website (https://www.elastic.co/downloads/elasticsearch). Once downloaded, follow the instructions to install Elasticsearch on your system.

Step 2: Install the Elasticsearch .NET Client: Next, we need to install the Elasticsearch .NET Client. You can install it via NuGet Package Manager. Open Visual Studio and right-click on your project name in the Solution Explorer. Click on “Manage NuGet Packages” and then search for “Elasticsearch.Net” and “NEST.” Install both the packages.

Step 3: Connect to MSSQL Server: To connect to MSSQL Server, we need to install the Entity Framework Core package. Install it via NuGet Package Manager. Once installed, create a new class that derives from DbContext. In this class, you need to override the OnConfiguring method to configure the database connection. For example:

public class MyDbContext : DbContext
{
public DbSet<MyEntity> MyEntities { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Data Source=<servername>;Initial Catalog=<databasename>;User ID=<username>;Password=<password>;");
}
}

Step 4: Create an Elasticsearch Index: To store data in Elasticsearch, we first need to create an index. An index is a collection of documents that have somewhat similar characteristics. In this tutorial, we will be creating an index called “myindex.” To create an index, add the following code:

var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
.DefaultIndex("myindex");
var client = new ElasticClient(settings);
if (!client.IndexExists("myindex").Exists)
{
client.CreateIndex("myindex", c => c
.Mappings(m => m
.Map<MyEntity>(mp => mp.AutoMap())
)
);
}

Step 5: Retrieve Data from MSSQL and Store it in Elasticsearch: To retrieve data from MSSQL and store it in Elasticsearch, we need to use the Entity Framework Core to retrieve the data and the Elasticsearch .NET Client to store it in Elasticsearch. Add the following code to your controller to retrieve the data and store it in Elasticsearch:

[HttpGet]
public async Task<IActionResult> IndexData()
{
using (var db = new MyDbContext())
{
var data = await db.MyEntities.ToListAsync();
var bulkAllObservable = client.BulkAll(data, b => b
.Index("myindex")
.BackOffRetries(2)
.BackOffTime("30s")
.RefreshOnCompleted()
);
await bulkAllObservable.WaitAsync(TimeSpan.FromMinutes(15));
}
return Ok();
}

Step 6: Search Data in Elasticsearch:

To search data in Elasticsearch, we can use the Elasticsearch .NET Client. Add the following code to your controller to search for data in Elasticsearch:

[HttpGet("{search}")]
public async Task<IActionResult> SearchData(string search)
{
var searchResponse = await client.SearchAsync<MyEntity>(s => s
.Index("myindex")
.Query(q => q
.Match(m => m
.Field(f => f.MyField)
.Query(search)
)
)
);
var results = searchResponse.Documents;
return Ok(results);
}

In this code, we are using the SearchAsync method to search for data in Elasticsearch. We are passing the search query as a parameter to this method. The search query is used to match the field in the Elasticsearch index. In this example, we are searching for data that matches the “MyField” field. The search results are then returned as a list of “MyEntity” objects.

Conclusion: Elasticsearch is a powerful tool that can be used to manage large amounts of data. In this tutorial, we have seen how to retrieve data from MSSQL and store it in Elasticsearch, and then search for that data using .NET 7 Web API and Entity Framework Core. With Elasticsearch, you can easily manage, search, and analyze large volumes of data, making it an essential tool for any modern application.

--

--