当前位置: 首页 > news >正文

net8.0一键创建支持(Elastic)

Necore项目生成器 - 在线创建Necore模板项目 | 一键下载

ElasticController.cs

 

using CSRedis;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Nest;
using System.Threading.Tasks;
using UnT.Template.Application.Responses;
using UnT.Template.Domain;namespace UnT.Template.Controllers
{[Route("api/elastics")][ApiController]public class ElasticController : ControllerBase{private readonly ElasticClient _elasticClient;public ElasticController(ElasticClient elasticClient){_elasticClient = elasticClient;}[HttpPost("insert")][Produces("application/json")][ProducesResponseType(typeof(ApiResponse<bool>), StatusCodes.Status200OK)]public async Task<IActionResult> Insert(){try{if (!_elasticClient.Indices.Exists("pro_products").Exists){var response = await _elasticClient.Indices.CreateAsync("pro_products", c => c.Map<Pro_Product>(m => m.AutoMap() // 自动映射.Properties(p => p.Number(n => n.Name(nn => nn.Id).Type(NumberType.Integer)).Text(t => t.Name(n => n.Name).Analyzer("ik_max_word")) // 使用中文分词器)));if (!response.IsValid){throw new Exception($"创建索引失败: {response.DebugInformation}");}}var responseCreateDoc = await _elasticClient.IndexAsync(new Pro_Product { Name = DateTime.Now.ToFileTime().ToString() }, i => i.Index("pro_products"));if (!responseCreateDoc.IsValid){throw new Exception($"索引文档失败: {responseCreateDoc.DebugInformation}");}return Ok(new ApiResponse<bool> { Success = true, Data = true });}catch (Exception ex){return Ok(new ApiResponse<bool> { Success = false, Message = ex.Message, Data = false });}}[HttpPost("count")][Produces("application/json")][ProducesResponseType(typeof(ApiResponse<int>), StatusCodes.Status200OK)]public async Task<IActionResult> Count(){try{var response = await _elasticClient.SearchAsync<Pro_Product>(s => s.Index("pro_products").Query(q => q.MatchAll()));if (!response.IsValid){throw new Exception($"搜索失败: {response.DebugInformation}");}var products = response.Documents;return Ok(new ApiResponse<int> { Success = true, Data = products.Count() });}catch (Exception ex){return Ok(new ApiResponse<int> { Success = false, Message = ex.Message });}}}
}

 

http://www.dtcms.com/a/301379.html

相关文章:

  • 2025C卷 - 华为OD机试七日集训第1期 - 按算法分类,由易到难,循序渐进,玩转OD
  • Spring 容器注入时查找 Bean 的完整规则
  • Flutter中 Provider 的基础用法超详细讲解(二)之ChangeNotifierProvider
  • 力扣热题100----------53最大子数组和
  • 咨询进阶——解读40页公司战略解码方法【附全文阅读】
  • sed命令
  • 通信名词解释:I2C、USART、SPI、RS232、RS485、CAN、TCP/IP、SOCKET、modbus
  • 【通识】设计模式
  • catkin_make生成的编译文件夹目录结构说明
  • uart通信
  • python---类型转换
  • Milvus 实战全流程
  • Deja Vu: 利用上下文稀疏性提升大语言模型推理效率
  • Spring 解析 XML 配置文件的过程(从读取 XML 到生成 BeanDefinition)
  • 扩展组件(uni-ui)之uni-group
  • 「iOS」————消息传递和消息转发
  • 26.删除有序数组中的重复项
  • MyBatis-Plus高效开发实战
  • 内存管理和垃圾收集-02: 操作系统如何管理内存?
  • Linux驱动开发笔记(五)——设备树(中)——节点的标准属性
  • 益莱储:明智地投资测试仪器
  • S7-1500 与 S7-1200 存储区域保持性设置特点详解
  • 电子板原理功能区解析与PlantUML图示
  • 3,Windows11安装docker保姆级教程
  • 轻量化多模态文档处理利器SmolDocling:技术原理与场景落地引言:文档智能处理的范式革命
  • 数据结构基础内容(第六篇:二叉搜索与平衡二叉树)
  • MySQL锁机制与MVCC原理剖析
  • 直播带货工具About v1.5.10 免费版
  • GEO优化实战:如何在DeepSeek、豆包等AI平台抢占推荐位?
  • MOE架构详解:原理、应用与PyTorch实现