Introduction
In this article, I will demonstrate CRUD operations using elasticsearch and the.net core API. Here I will take an example that will help us understand how to insert, update, read, and delete documents by a parameter in elastic search. I have also covered UpdateByQueryAsync to update and DeleteByQuery to delete the elastic documents.
How do you set up an elastic search in your development system? You can refer to my previous article. Getting Started With Elastic In Using .NET Nest Client.
Here I will create a window console application using.NET Core. Using this application, I will explain CRUD operations with elastic search and the web API. I will test my crud operation using the Swagger UI. Let's start with step-by-step explanations. First, I will explain how to add an elastic client to the start-up file.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// Configure elastic search URI and default index
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node);
settings.DefaultIndex("products");
// Add elstic cliend dependency
var client = new ElasticClient(settings);
services.AddSingleton<IElasticClient>(client);
// Add product service dependency
services.AddScoped<IProductService, ProductService>();
//Enable swagger here
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "ElasticSearchWebApi", Version = "v1" });
});
}
In the start-up class, I added elastic client, swagger, and dependency for the product service.
Let's create a model for our example, which we will use to create, read, update, and delete documents using elastic search.
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string ProductDescription { get; set; }
public decimal Price { get; set; }
public DateTime LaunchDate { get; set; }
public string Category { get; set; }
}
Let's create a product service interface and a product service class.
public interface IProductService
{
Product AddProduct(Product product);
List<Product> GetAllProducts();
Product GetProductById(int productId);
void UpdateProduct(Product product);
void DeleteProduct(int productId);
}
public class ProductService : IProductService
{
public readonly IElasticClient _elasticClient;
public ProductService(IElasticClient elasticClient)
{
_elasticClient = elasticClient;
}
public Product AddProduct(Product product)
{
var response = _elasticClient.IndexDocument(product);
if (response.IsValid)
{
return product;
}
else
{
return new Product();
}
}
public void DeleteProduct(int productId)
{
_elasticClient.DeleteByQuery<Product>(p => p.Query(q1 => q1
.Match(m => m
.Field(f => f.ProductId)
.Query(productId.ToString()
)
)));
}
public List<Product> GetAllProducts()
{
var esResponse = _elasticClient.Search<Product>().Documents;
return esResponse.ToList();
}
public Product GetProductById(int productId)
{
var esResponse = _elasticClient.Search<Product>(x => x.
Query(q1 => q1.Bool(b => b.Must(m =>
m.Terms(t => t.Field(f => f.ProductId)
.Terms<int>(productId))))));
return esResponse.Documents.FirstOrDefault();
}
public void UpdateProduct(Product product)
{
if (product != null)
{
var updateResponse = _elasticClient.UpdateByQueryAsync<Product>(q =>
q.Query(q1 => q1.Bool(b => b.Must(m =>
m.Match(x => x.Field(f =>
f.ProductId == product.ProductId)))))
.Script(s => s.Source(
"ctx._source.price = params.price;" +
"ctx._source.productDescription = params.productDescription;" +
"ctx._source.category = params.category;")
.Lang("painless")
.Params(p => p.Add("price", product.Price)
.Add("productDescription", product.ProductDescription)
.Add("category", product.Category)
)).Conflicts(Conflicts.Proceed));
}
}
}









Join the conversation! Your thoughts help the community grow.