What is Elasticsearch?
According to Wikipedia - Elasticsearch is a search engine based on the Lucene library. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents. Elasticsearch is developed in Java and is released as open source under the terms of the Apache License.
ElasticSearch is a Document-Oriented Database, which stores data in JSON format. It stores data in unstructured form. Elasticsearch uses Lucene StandardAnalyzer for indexing, automatic type guessing and more precision. Every feature of Elasticsearch is exposed as a REST API. With the help of API, we can Get, Post, Delete, Search the data.
So Let's follow these steps to configure and use Elasticsearch.
Step 1
Step 2
https://www.elastic.co/downloads/elasticsearch
Step 3
http://localhost:9200/
Step 4
Either take it from this link https://www.nuget.org/packages/NEST/ OR Use Install-Package NEST -Version 6.5.0
Now you are ready to work on Elasticsearch through .NET.
Step 5
- using Nest;
Here points to note is that "index" is Database and "type" is Table. so you need to create INDEX(Database) first, then you need to create TYPE(Table) so that you can insert data in table in the format of JSON.
Step 6
- [System.Web.Http.Route("ConnectAndInsertData")]
- [System.Web.Http.HttpPost]
- public HttpResponseMessage ConnectAndInsertData(RawData model) // Here RawData is a object of a Class. you can create a class with few sample properties and can pass in the form of JSON from Postman
- {
- HttpResponseMessage response = new HttpResponseMessage();
- var jsonResult = "";
- try
- {
- if (model != null)
- {
- Uri EsInstance = new Uri("http://localhost:9200");
- ConnectionSettings EsConfiguration = new ConnectionSettings(EsInstance );
- ElasticClient EsClient = new ElasticClient(EsConfiguration);
- var settings = new IndexSettings { NumberOfReplicas = 1, NumberOfShards = 2 };
- var indexConfig = new IndexState
- {
- Settings = settings
- };
- if (!EsClient.IndexExists("sample").Exists) // I am creating database named "sample". check if exist before creating the new
- {
- EsClient.CreateIndex("sample", c => c
- .InitializeUsing(indexConfig)
- .Mappings(m => m.Map<RawData>(mp => mp.AutoMap())));
- }
- var getTableData = EsClient.Count<RawData>(s => s.Index("sample").Type("rawdata")); // This will return Count of records in "rawdata" table
- if (EsClient.IndexExists("sample").Exists)
- {
- // Here model is the Object containing the data which we passed to API and want to insert in Table/type
- var result = EsClient.Index(model, i => i
- .Index("sample")
- .Type("rawdata")
- .Id(getTableData.Count + 1)
- .Refresh(Elasticsearch.Net.Refresh.True)
- );
- if (Convert.ToString(result.Result) == "Created")
- {
- jsonResult = JsonConvert.SerializeObject("{\"result\":true\"}");
- response = Request.CreateResponse(HttpStatusCode.OK);
- response.Content = new StringContent(jsonResult, Encoding.UTF8, "application/json");
- }
- }
- }
- }
- catch (Exception ex)
- {
- jsonResult = JsonConvert.SerializeObject("{\"result\":false\"}");
- response = Request.CreateResponse(HttpStatusCode.InternalServerError);
- response.Content = new StringContent(jsonResult, Encoding.UTF8, "application/json");
- }
- if (string.IsNullOrEmpty(jsonResult))
- {
- jsonResult = JsonConvert.SerializeObject("{\"result\":false\"}");
- response = Request.CreateResponse(HttpStatusCode.ExpectationFailed);
- response.Content = new StringContent(jsonResult, Encoding.UTF8, "application/json");
- }
- return response;
- }
Step 7
- [System.Web.Http.Route("GetData")]
- [System.Web.Http.HttpGet]
- public HttpResponseMessage GetData()
- {
- HttpResponseMessage response;
- Uri EsInstance = new Uri("http://localhost:9200");
- ConnectionSettings EsConfiguration = new ConnectionSettings(EsInstance );
- ElasticClient EsClient = new ElasticClient(EsConfiguration);
- var getTableData = EsClient.Search<RawData>(s => s.Index("sample").Type("rawdata").Size(300000).Take(10000)); // you can get maximum 10000 records at a time
- var jsonResult = JsonConvert.SerializeObject("{\"result\":true\"}");
- List<RawData> lstData = new List<RawData>();
- foreach (var hit in getTableData.Hits)
- {
- lstData.Add(hit.Source);
- }
- if (lstData != null)
- {
- jsonResult = JsonConvert.SerializeObject(getTableData);
- }
- response = Request.CreateResponse(HttpStatusCode.OK);
- response.Content = new StringContent(jsonResult, Encoding.UTF8, "application/json");
- return response;
- }
So, this way, we can insert and get data in elasticsearch using .NET. This is a very basic example for beginners. I will come up with next blog for deleting data and full fledge search.
-- Happy Coding --

Srikanth Kumar RajulapatiPosted Jun 18, 2024, 3:50 PM
Hi Dinesh, Thanks a lot for your nice Article. But, it look likes that the Sample Application is missing in this Article.
Angeline YunaPosted Apr 1, 2019, 3:24 PM
Wow thanks for making the article i really like it.
Mahesh ChandPosted Apr 1, 2019, 2:24 PM
Thank you, Dinesh. You're missing a link in Step 1. Thanks