Background
We already know about MVC. Let’s explore the Elastic Search search engine and integrate it in an ASP.NET MVC application.
Elastic Search is a search engine based on Lucene. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents. Elastic search is developed in Java and is released as open source under the terms of the Apache License.
Elastic search uses standard RESTful APIs and JSON. It’s a NoSQL. Elastic Search contains multiple indices which contain multiple types.
An index is like a ‘database’ in a relational database. It has a mapping which defines multiple types.
SQL Server => Databases => Tables => Columns/Rows
Elastic search => Indices => Types => Documents with Properties
These types hold multiple Documents (rows), and each document has Properties or Fields (columns).
For more information about Elastic Search, I would suggest visiting the official website of it.
More about how to work with .NET
To connect Elastic Search using C#, MVC, Web API, or ASP.NET Core, we use .NET Elastic client, i.e., NEST.
In the given example, I have created 2 projects under the same solution,
- DBSetUp
It is a console application which will create your Index (Database) and Type (table) in the Elastic Search by using a common library.
- MVC project (Web application)
To perform the crud operation in Elastic search, the application is created by using the common library.
NEST package should be added to both the projects.
While working with Elastic Search, please ensure that Elastic Search is installed on your system and running properly. Please have a look at the below screen as the Elastic Search Engine is running.
Solution
Perform the following steps to secure the Customer PII data using CLE.
Step 1
Create MVC project and the console application in the solution.
Step 2Add the .Net Elastic client (NEST)
Use the below code to create the Index and Type in Elastic search.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Nest;
- using Elasticsearch.Net;
- using Common.Data;
-
- namespace Elastic.DB
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Started creating Database in Elastic search");
- CreateIndex();
- CreateMappings();
- Console.WriteLine("Database created.");
- }
-
-
-
-
- public static void CreateIndex()
- {
- ConnectionSettings settings = new ConnectionSettings(new Uri("http://localhost:9200"));
- settings.DefaultIndex("employeestore");
- ElasticClient client = new ElasticClient(settings);
- client.DeleteIndex(Indices.Index("employeestore"));
- var indexSettings = client.IndexExists("employeestore");
- if(!indexSettings.Exists)
- {
- var indexName = new IndexName();
- indexName.Name = "employeestore";
- var response = client.CreateIndex(indexName);
- }
-
- if(indexSettings.Exists)
- {
- Console.WriteLine("Created");
- }
-
- }
-
-
-
-
- public static void CreateMappings()
- {
- ConnectionSettings settings = new ConnectionSettings(new Uri("http://localhost:9200"));
- settings.DefaultIndex("employeestore");
- ElasticClient esClient = new ElasticClient(settings);
- esClient.Map<Employee>(m =>
- {
- var putMappingDescriptor = m.Index(Indices.Index("employeestore")).AutoMap();
- return putMappingDescriptor;
- });
- }
-
- }
- }
Step 3
Go to solution explorer. Right click on MVC project and set as a Start-up project.
Add the UserController and design your View. Below is the code snippet you can use to add the user.
-
-
-
-
-
- [HttpPost]
- public async Task<ActionResult> Registration(UserModel um)
- {
- ConnectionSettings settings = new ConnectionSettings(new Uri("http://localhost:9200"));
-
- settings.DefaultIndex("employeestore");
- ElasticClient esClient = new ElasticClient(settings);
- Employee emp = new Employee { EmployeeID = um.ID, EmployeeName = um.Name,Address=um.Address };
- var response = await esClient.IndexAsync<Employee>(emp, i => i
- .Index("employeestore")
- .Type(TypeName.From<Employee>())
- .Id(um.ID)
- .Refresh(Elasticsearch.Net.Refresh.True));
-
-
- return RedirectToAction("GetUsers");
- }
You can design your View in your way. Please refer the below screen which I have designed for User Management.
Please download the attached sample code for complete CRUD operation.