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);
    }
}
相关推荐
徐*红2 分钟前
Elasticsearch 8.+ 版本查询方式
大数据·elasticsearch
码爸19 分钟前
flink 例子(scala)
大数据·elasticsearch·flink·scala
friklogff43 分钟前
【无标题】云端之C#:全面解析6种云服务提供商的SDK
开发语言·flask·c#
txtsteve1 小时前
es由一个集群迁移到另外一个集群es的数据迁移
大数据·elasticsearch·搜索引擎
工作中的程序员1 小时前
ES 索引或索引模板
大数据·数据库·elasticsearch
c#上位机2 小时前
C#事件的用法
java·javascript·c#
chnyi6_ya2 小时前
一些写leetcode的笔记
笔记·leetcode·c#
IT规划师2 小时前
C#|.net core 基础 - 扩展数组添加删除性能最好的方法
c#·.netcore·数组
时光追逐者3 小时前
分享6个.NET开源的AI和LLM相关项目框架
人工智能·microsoft·ai·c#·.net·.netcore
friklogff3 小时前
【C#生态园】提升C#开发效率:深入了解自然语言处理库与工具
开发语言·c#·区块链