c#使用Elastic.Clients.Elasticsearch 库进行ElasticSearch的增删改查操作,根据变量动态构建查询条件。

实体类Shop结构:

cs 复制代码
public class Shop
{
    public string UUID { set; get; }

    public string ItemType { set; get; }

    public long ItemId { set; get; }

    public string ItemName { set; get; }

    public long Gold { set; get; }

    public long Number { set; get; }

    public string Data { set; get; }

    public string Quality { set; get; }

    public string Category { set; get; }

    public string SellerIp { set; get; }
}

初始化客户端

cs 复制代码
var client = new ElasticsearchClient(new Uri("http://localhost:9200"));

创建索引

cs 复制代码
var response = await client.Indices.CreateAsync("dragon");

判断索引是否存在

cs 复制代码
var response = await client.Indices.ExistsAsync("dragon");

增加数据

cs 复制代码
var response = await client.IndexAsync(shop, (IndexName)"dragon");

修改数据

cs 复制代码
var response = await client.UpdateAsync<Shop, Shop>((IndexName)"dragon", 1, u => u.Doc(shop));

判断是否存在

cs 复制代码
var response = await client.ExistsAsync<Shop>(1, idx => idx.Index("dragon"));

删除数据

cs 复制代码
var response = await client.DeleteAsync((IndexName)"dragon", 1);

查询数据(根据4个变量是否为空,动态构建查询条件).其中,Term是精确匹配,Match是模糊匹配。

cs 复制代码
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
    var client = new ElasticsearchClient(new Uri("http://localhost:9200"));

    string category = "";
    string quality = "";
    string itemName = "";
    string itemType = "";

    List<Action<QueryDescriptor<Shop>>> configure = new List<Action<QueryDescriptor<Shop>>>();

    if (!string.IsNullOrEmpty(quality))
    {
        configure.Add(m => m.Term(t => t.Field(f => f.Quality).Value(FieldValue.String(quality))));
    }
    if (!string.IsNullOrEmpty(category))
    {
        configure.Add(m => m.Term(t => t.Field(f => f.Category).Value(FieldValue.String(category))));
    }
    if (!string.IsNullOrEmpty(itemType))
    {
        configure.Add(m => m.Term(t => t.Field(f => f.ItemType).Value(FieldValue.String(itemType))));
    }
    if (!string.IsNullOrEmpty(itemName))
    {
        configure.Add(m => m.Match(t => t.Field(f => f.ItemName).Query(itemName)));
    }

    var response = await client.SearchAsync<Shop>(s => s
        .Index("dragon")
        .From(0)
        .Size(16)
        .Query(q =>
            q.Bool(b => b.Must(configure.ToArray()))
        )
    );

    if (response.IsValidResponse)
    {
        //本次查询获得的数据
        List<Shop> shops = response.Documents.ToList();
        //本次查询取得的数据量
        int count = response.Documents.Count;
        //满足查询条件的总数据量
        long total = response.HitsMetadata.Total.Match(x => x.Value, y => y);
    }
}
相关推荐
reasonsummer42 分钟前
【办公类-115-06】20250920职称资料上传04——docx复制、docx转PDF(课程表11个)
开发语言·windows·python·c#
Elastic 中国社区官方博客4 小时前
根据用户行为数据中的判断列表在 Elasticsearch 中训练 LTR 模型
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
风清再凯7 小时前
04_es原理&filebeat使用
大数据·elasticsearch·搜索引擎
补三补四12 小时前
Git 基础操作指南
大数据·git·elasticsearch
Aubrey-J14 小时前
Linux中快速部署Elasticsearch(基础&TLS配置)
linux·服务器·elasticsearch
Elasticsearch15 小时前
Elasticsearch:创建一个定制的 DeepSeek 嵌入推理端点
elasticsearch
William_cl16 小时前
一、前置基础(MVC学习前提)_核心特性_【C# 泛型入门】为什么说 List<T>是程序员的 “万能收纳盒“?避坑指南在此
学习·c#·mvc
风清再凯17 小时前
02_ES索引规范&kibana
大数据·elasticsearch·搜索引擎
c#上位机20 小时前
wpf之命令
c#·wpf
失散1320 小时前
分布式专题——46 ElasticSearch高级查询语法Query DSL实战
java·分布式·elasticsearch·架构