Getting Started With Elastic In Using .NET Nest Client

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 developer system
  • How to communicate from .net application
  • Create Index in Elastic
  • Insert data in 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. 
Getting Started With Elastic In Using .NET Nest Client
  • Once you click on the above link you will get Elastic search in .zip folder. Extract it and install in local system.
  • After File Extraction click on ElasticSearch.bat file
Getting Started With Elastic In Using .NET Nest Client 
  • Once you click on elasticsearch.bat file your server will be up and running  like the below screen:
Getting Started With Elastic In Using .NET Nest Client  
  • 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 new project from Visual Studio and add Elasticsearch.Nest reference from nuget package manager as below:
Getting Started With Elastic In Using .NET Nest Client
 
Once reference is added you need to create Elastic client connecter which is going to communicate with elasticsearch. I have added the code snippet as below.
  1. public class ElasticSearchClient  
  2.     {  
  3.         #region Elasticsearch Connection
  4.   
  5.         public ElasticClient EsClient()  
  6.         {  
  7.             var nodes = new Uri[]  
  8.             {  
  9.                 new Uri("http://localhost:9200/"),  
  10.             };  
  11.   
  12.             var connectionPool = new StaticConnectionPool(nodes);  
  13.             var connectionSettings = new ConnectionSettings(connectionPool).DisableDirectStreaming();  
  14.             var elasticClient = new ElasticClient(connectionSettings.DefaultIndex("productdetails"));  
  15.             return elasticClient;  
  16.         }  
  17.  
  18.         #endregion Elasticsearch Connection
  19.     }  
Add model and controller for inserting document to index.
  1. public class Products  
  2.     {  
  3.         public int ProductId { getset; }  
  4.         public string ProductName { getset; }  
  5.         public decimal Price { getset; }  
  6.         public string Description { getset; }  
  7.         public List<CheckBoxModel> Colors { getset; }  
  8.         public List<CheckBoxModel> Tags { getset; }  
  9.     }  
  10.   
  11.     public class CheckBoxModel  
  12.     {  
  13.         public int Value { getset; }  
  14.         public string Text { getset; }  
  15.         public bool IsChecked { getset; }  
  16.     }  
  1. public class ProductController : Controller  
  2.    {  
  3.        private readonly ElasticSearchClient _esClient;  
  4.   
  5.        public ProductController()  
  6.        {  
  7.            _esClient = new ElasticSearchClient();  
  8.        }  
  9.        // GET: Product  
  10.        public ActionResult Index()  
  11.        {  
  12.            return RedirectToAction("Create");  
  13.        }  
  14.        // GET: Product/Create  
  15.        public ActionResult Create()  
  16.        {  
  17.            var colors = new List<CheckBoxModel>()  
  18.                {  
  19.                  new CheckBoxModel {Value=1,Text="Red",IsChecked=true },  
  20.                  new CheckBoxModel {Value=1,Text="Green",IsChecked=false },  
  21.                  new CheckBoxModel {Value=1,Text="Orange",IsChecked=false },  
  22.                  new CheckBoxModel {Value=1,Text="Blue" ,IsChecked=false},  
  23.                  new CheckBoxModel {Value=1,Text="Purple",IsChecked=false },  
  24.                  new CheckBoxModel {Value=1,Text="Yellow" ,IsChecked=false},  
  25.                };  
  26.            var tags = new List<CheckBoxModel>()  
  27.                {  
  28.                  new CheckBoxModel {Value=1,Text="Mens wear",IsChecked=true },  
  29.                  new CheckBoxModel {Value=1,Text="Ladies Wear",IsChecked=false },  
  30.                  new CheckBoxModel {Value=1,Text="New Arraival",IsChecked=false },  
  31.                  new CheckBoxModel {Value=1,Text="Top Brands" ,IsChecked=false},  
  32.                  new CheckBoxModel {Value=1,Text="Kids Wear",IsChecked=false },  
  33.                  new CheckBoxModel {Value=1,Text="Jeans" ,IsChecked=false},  
  34.                };  
  35.            var productDetails = new Products  
  36.            {  
  37.                Colors = colors,  
  38.                Tags = tags  
  39.            };  
  40.   
  41.            return View(productDetails);  
  42.        }  
  43.   
  44.        // POST: Product/Create  
  45.        [HttpPost]  
  46.        public ActionResult Create(Products products)  
  47.        {  
  48.            try  
  49.            {  
  50.                var productDetail = new ProductDetails()  
  51.                {  
  52.                    ProductId = products.ProductId,  
  53.                    ProductName = products.ProductName,  
  54.                    Price = products.Price,  
  55.                    Description = products.Description,  
  56.                    Colors = products.Colors.Where(x => x.IsChecked == true).Select(x => x.Text).ToArray(),  
  57.                    Tags = products.Tags.Where(x => x.IsChecked == true).Select(x => x.Text).ToArray()  
  58.   
  59.                };  
  60.                // Insert product detail document to index  
  61.   
  62.                var defaultIndex = "productdetails";  
  63.                var indexExists = _esClient.EsClient().Indices.Exists(defaultIndex);  
  64.                if (!indexExists.Exists)  
  65.                {  
  66.                    var response = _esClient.EsClient().Indices.Create(defaultIndex,  
  67.                       index => index.Map<ProductDetails>(  
  68.                           x => x.AutoMap()  
  69.                       ));  
  70.                }  
  71.                var indexResponse1 = _esClient.EsClient().IndexDocument(productDetail);  
  72.   
  73.                return RedirectToAction("Create");  
  74.            }  
  75.            catch  
  76.            {  
  77.                return View();  
  78.            }  
  79.        }  
  80.   
  81.    }  
Add Product to index using the below screen.  Here I have added 10 products from this screen.
 
Getting Started With Elastic In Using .NET Nest Client
 
I have the below records as of now in index which are inserted from Add Product screen,
  1. [{    
  2.         "productId": 1,    
  3.         "productName""Shirt",    
  4.         "price": 1000.0,    
  5.         "description""Shirt is made from pure cotton. Very stylish shirt specially designed for men.",    
  6.         "colors": [    
  7.             "Red",    
  8.             "Green",    
  9.             "Orange",    
  10.             "Purple",    
  11.             "Yellow"    
  12.         ],    
  13.         "tags": [    
  14.             "Mens wear",    
  15.             "New Arraival"    
  16.         ]    
  17.     },    
  18.     {    
  19.         "productId": 2,    
  20.         "productName""Pures",    
  21.         "price": 1200.0,    
  22.         "description""Hand made purse for Women. Designed from pure leather.",    
  23.         "colors": [    
  24.             "Red",    
  25.             "Green",    
  26.             "Purple",    
  27.             "Yellow"    
  28.         ],    
  29.         "tags": [    
  30.             "New Arraival",    
  31.             "Top Brands"    
  32.         ]    
  33.     },    
  34.     {    
  35.         "productId": 3,    
  36.         "productName""Jacket",    
  37.         "price": 1300.0,    
  38.         "description""Jackat is made from leather. specially designed for men and womens",    
  39.         "colors": [    
  40.             "Red",    
  41.             "Green",    
  42.             "Orange",    
  43.             "Yellow"    
  44.         ],    
  45.         "tags": [    
  46.             "New Arraival",    
  47.             "Top Brands"    
  48.         ]    
  49.     },    
  50.     {    
  51.         "productId": 4,    
  52.         "productName""Blazer",    
  53.         "price": 1400.0,    
  54.         "description""Hand made blazer which are very stylish and specially designed for man.",    
  55.         "colors": [    
  56.             "Red",    
  57.             "Green",    
  58.             "Orange",    
  59.             "Blue",    
  60.             "Purple",    
  61.             "Yellow"    
  62.         ],    
  63.         "tags": [    
  64.             "Mens wear",    
  65.             "Ladies Wear",    
  66.             "New Arraival",    
  67.             "Top Brands"    
  68.         ]    
  69.     },    
  70.     {    
  71.         "productId": 5,    
  72.         "productName""Jeans Top",    
  73.         "price": 1400.0,    
  74.         "description""Jeans is for men and women. Very good quality material with latest design",    
  75.         "colors": [    
  76.             "Blue"    
  77.         ],    
  78.         "tags": [    
  79.             "Mens wear",    
  80.             "Ladies Wear",    
  81.             "New Arraival",    
  82.             "Jeans"    
  83.         ]    
  84.     },    
  85.     {    
  86.         "productId": 6,    
  87.         "productName""Girl Dresses",    
  88.         "price": 1500.0,    
  89.         "description""Girl dresses and casual wears",    
  90.         "colors": [    
  91.             "Red",    
  92.             "Orange",    
  93.             "Purple"    
  94.         ],    
  95.         "tags": [    
  96.             "Top Brands",    
  97.             "Kids Wear"    
  98.         ]    
  99.     },    
  100.     {    
  101.         "productId": 7,    
  102.         "productName""Jumpsuits",    
  103.         "price": 500.0,    
  104.         "description""Girl jumpsuit with latest design",    
  105.         "colors": [    
  106.             "Orange",    
  107.             "Blue",    
  108.             "Purple"    
  109.         ],    
  110.         "tags": [    
  111.             "New Arraival",    
  112.             "Top Brands",    
  113.             "Kids Wear",    
  114.             "Jeans"    
  115.         ]    
  116.     },    
  117.     {    
  118.         "productId": 8,    
  119.         "productName""Kurta",    
  120.         "price": 1700.0,    
  121.         "description""Men traditional and designer kurta with latest design",    
  122.         "colors": [    
  123.             "Red",    
  124.             "Green",    
  125.             "Orange",    
  126.             "Purple",    
  127.             "Yellow"    
  128.         ],    
  129.         "tags": [    
  130.             "Mens wear",    
  131.             "New Arraival",    
  132.             "Top Brands"    
  133.         ]    
  134.     },    
  135.     {    
  136.         "productId": 9,    
  137.         "productName""chididar",    
  138.         "price": 1400.0,    
  139.         "description""Ladies chudidar dress with quality fabric.",    
  140.         "colors": [    
  141.             "Red",    
  142.             "Green",    
  143.             "Orange",    
  144.             "Blue",    
  145.             "Purple",    
  146.             "Yellow"    
  147.         ],    
  148.         "tags": [    
  149.             "Ladies Wear",    
  150.             "New Arraival",    
  151.             "Top Brands"    
  152.         ]    
  153.     },    
  154.     {    
  155.         "productId": 10,    
  156.         "productName""Purse",    
  157.         "price": 600.0,    
  158.         "description""Purse is made from pure leather. 100% pure leather is used to make this purse with great finishing.",    
  159.         "colors": [    
  160.             "Red",    
  161.             "Green",    
  162.             "Orange",    
  163.             "Blue",    
  164.             "Purple",    
  165.             "Yellow"    
  166.         ],    
  167.         "tags": [    
  168.             "Ladies Wear",    
  169.             "Top Brands"    
  170.         ]    
  171.     }    
  172. ]    
Now let's add the code snippet for Searching data from elastic index.
  1. public class ProductDetails  
  2.     {  
  3.         public int ProductId { getset; }  
  4.         public string ProductName { getset; }  
  5.         public decimal Price { getset; }  
  6.         public string Description { getset; }  
  7.         public string[] Colors { getset; }  
  8.         public string[] Tags { getset; }  
  9.     }  
  1. public class SearchController : Controller  
  2.     {  
  3.         private readonly ElasticSearchClient _esClient;  
  4.         public SearchController()  
  5.         {  
  6.             _esClient = new ElasticSearchClient();  
  7.         }  
  8.   
  9.         [HttpGet]  
  10.         public ActionResult Search()  
  11.         {  
  12.             return View("Search");  
  13.         }  
  14.         public JsonResult DataSearch(string term)  
  15.         {  
  16.   
  17.             var responsedata = _esClient.EsClient().Search<ProductDetails>(s => s.Source()  
  18.                                 .Query(q => q  
  19.                                 .QueryString(qs => qs  
  20.                                 .AnalyzeWildcard()  
  21.                                    .Query("*" + term.ToLower() + "*")  
  22.                                    .Fields(fs => fs  
  23.                                        .Fields(f1 => f1.ProductName,  
  24.                                                f2 => f2.Description,  
  25.                                                f3 => f3.Tags,  
  26.                                                f4 => f4.Colors  
  27.                                                )  
  28.                                    )  
  29.                                    )));  
  30.   
  31.             var datasend = responsedata.Documents.ToList();  
  32.   
  33.   
  34.             return Json(datasend, behavior: JsonRequestBehavior.AllowGet);  
  35.         }  
  36.     }  
  • In the above code snippet I have added a model with properties and search controller for searching data from elastic index. I have used wild card search for 4 fields, ProductName, Description, Tags and colors. If search term is available in any of the fields then search method will returns all matched documents as response.
  • Now let's search with a few terms which will return wildcard. Search in elastic search, i.e. search terms "shirt", Shirt is there in product name so only one record is returned, as below.
Getting Started With Elastic In Using .NET Nest Client 
  •  Now take one more example. Search with the term "men". This word exists in the below documents so it retuns 6 documents as below.
Getting Started With Elastic In Using .NET Nest Client 
  • Now let's search with color column; i.e. Yellow 
     
    Getting Started With Elastic In Using .NET Nest Client 
  • Let's search one more scenario for tags column with term "Top Brand" 
Getting Started With Elastic In Using .NET Nest Client
 

Conclusion

 
In this article we learned about how to configure elasticsearch in developer machine, how to create index and insert document, and how to search data using wildcard search on multiple fields. This is a simple example of wildcard search in elasticsearch using nest client. There are lots of options available in elastic search to write search queries, filters, Analyzer, Tokenizer and many more which I am going to cover in upcoming articles.