【愚公系列】2022年06月 .NET架构班 068-分布式中间件 Elasticsearch实际业务落地
一、聚合商品业务场景落地
1.代码实现
聚合:就是统计。做报表的核心方式
1、先在ProductController类中添加代码
/// <summary>
/// 商品控制器
/// </summary>
[ApiController]
[Route("Product")]
public class ProductController : ControllerBase
{
private readonly IProductService _productService;
public ProductController(IProductService productService)
{
_productService = productService;
}
/// <summary>
/// 聚合查询
/// </summary>
/// <param name="productDto"></param>
/// <returns></returns>
[HttpGet("AggreateTextSearch")]
public ActionResult<ValueAggregate> GetProductsAggreateTextSearch([FromQuery] ProductDto productDto)
{
var products = _productService.GetProductsAggreateTextSearch(productDto);
if (products == null)
{
return NotFound();
}
return products;
}
}
2、然后在ProductService类中添加代码
/// <summary>
/// 商品服务实现
/// </summary>
public class ProductService : IProductService
{
private readonly IMongoCollection<Product> _products;
public ProductService()
{
#region 1、单实例连接
{
var node = new Uri("http://localhost:9200");
// var defaultIndex = "products";
var settings = new ConnectionSettings(node);
//.DefaultIndex(defaultIndex);
elasticClient = new ElasticClient(settings);
}
#endregion
}
public ValueAggregate GetProductsAggreateTextSearch(ProductDto productDto)
{
#region 1、聚合查询(平均值)
{
var ducmentsss = elasticClient.Search<Product>(s => s
.Index("products")
.Query(q => q.Match(mq => mq.Field(f => f.ProductTitle).Query(productDto.ProductTitle)))
.Aggregations(a => a.Average("ProductPrice_Average", aa => aa.Field(f => f.ProductPrice)))
).Aggregations.Average("ProductPrice_Average");
return ducmentsss;
}
#endregion
}
}
3、客户端访问,进入到浏览器进行访问
2.聚合商品数据原理
行式存储:是一行一行进行存储,
列式存储:是一列一列进存储。列式存储有利于做聚合操作。
二、查询商品分页业务场景落地
分析:当客户端通过电商微服务往Elasticsearch中添加商品数据的时候,商品数据已经被成功添加到Elasticsearch中。如何从Elasticsearch中搜索商品数据?
方案:商品分页
1、先在电商网站中创建ProductController类,在ProductController类中添加代码
/// <summary>
/// 商品控制器
/// </summary>
[ApiController]
[Route("Product")]
public class ProductController : ControllerBase
{
private readonly IProductService _productService;
public ProductController(IProductService productService)
{
_productService = productService;
}
/// <summary>
/// 查询商品(分页查询)
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{id}")]
public ActionResult<IEnumerable<Product>> GetProductsByPage(int page, int pageSize)
{
var products = _productService.GetProductsByPage(page, pageSize).ToList();
if (products == null)
{
return NotFound();
}
return products;
}
}
2、然后在电商网站中创建ProductService类,在ProductService类中添加代码
/// <summary>
/// 商品服务实现
/// </summary>
public class ProductService : IProductService
{
private readonly IMongoCollection<Product> _products;
public ProductService()
{
// 1、建立Elasticsearch连接
var client = new MongoClient("Elasticsearch://localhost:27017");
// 2、获取商品库(自己创建商品数据)
var database = client.GetDatabase("ProductDB");
// 3、获取商品表(自己创建商品数)
_products = database.GetCollection<Product>("Product");
}
public IEnumerable<Product> GetProductsByPage(int page, int pageSize)
{
#region 1、直接查询
{
var request = new SearchRequest("products");
request.From = (page-1)* pageSize;
request.Size = pageSize;
return elasticClient.Search<Product>(request).Documents;
}
#endregion
#region 2、委托查询
{
return elasticClient.Search<Product>(s => s
.Index("products")
.From((page - 1) * pageSize)
.Size(pageSize)
).Documents;
}
#endregion
}
}
三、删除商品业务场景落地
分析:当客户端通过电商微服务往Elasticsearch中添加商品的时候,数据量非常大,这个时候,客户端希望能够删除商品,如何删除商品?
方案:删除商品
1、先在电商网站中创建ProductController类,在ProductController类中添加代码
/// <summary>
/// 商品控制器
/// </summary>
[ApiController]
[Route("Product")]
public class ProductController : ControllerBase
{
private readonly IProductService _productService;
public ProductController(IProductService productService)
{
_productService = productService;
}
/// <summary>
/// 删除商品
/// </summary>
/// <param name="id"></param>
/// <param name="product"></param>
/// <returns></returns>
[HttpDelete("{id}")]
public IActionResult DeleteProduct(string id)
{
try
{
_productService.Delete(id);
}
catch (Exception)
{
throw;
}
return NoContent();
}
}
2、然后在电商网站中创建ProductService类,在ProductService类中添加代码
/// <summary>
/// 商品服务实现
/// </summary>
public class ProductService : IProductService
{
private readonly IMongoCollection<Product> _products;
public ProductService()
{
// 1、建立Elasticsearch连接
var client = new MongoClient("Elasticsearch://localhost:27017");
// 2、获取商品库(自己创建商品数据)
var database = client.GetDatabase("ProductDB");
// 3、获取商品表(自己创建商品数)
_products = database.GetCollection<Product>("Product");
}
public void Delete(string Id)
{
elasticClient.Delete<Product>(Id, idx => idx.Index("products"));
}
}
四、修改商品业务场景落地
分析:当客户端通过电商微服务往Elasticsearch中添加商品的时候,商品添加错误,希望能够修改商品,这个时候,客户端希望能够修改商品,如何修改商品?
方案:修改商品
1、先在电商网站中创建ProductController类,在ProductController类中添加代码
/// <summary>
/// 商品控制器
/// </summary>
[ApiController]
[Route("Product")]
public class ProductController : ControllerBase
{
private readonly IProductService _productService;
public ProductController(IProductService productService)
{
_productService = productService;
}
/// <summary>
/// 修改商品
/// </summary>
/// <param name="id"></param>
/// <param name="product"></param>
/// <returns></returns>
[HttpPut("{id}")]
public IActionResult PutProduct(string id, Product product)
{
if (id != product.Id)
{
return BadRequest();
}
try
{
_productService.Update(product);
}
catch (Exception)
{
throw;
}
return NoContent();
}
}
2、然后在电商网站中创建ProductService类,在ProductService类中添加代码
/// <summary>
/// 商品服务实现
/// </summary>
public class ProductService : IProductService
{
private readonly IMongoCollection<Product> _products;
public ProductService()
{
// 1、建立Elasticsearch连接
var client = new MongoClient("Elasticsearch://localhost:27017");
// 2、获取商品库(自己创建商品数据)
var database = client.GetDatabase("ProductDB");
// 3、获取商品表(自己创建商品数)
_products = database.GetCollection<Product>("Product");
}
/// <summary>
/// 更新
/// </summary>
/// <param name="Product"></param>
public void Update(Product Product)
{
elasticClient.Update<Product>(Product.Id, idx =>
idx.Upsert(Product).Index("products")
);
}
}
- 点赞
- 收藏
- 关注作者
评论(0)