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);
    }
}
相关推荐
努力的小郑5 小时前
Elasticsearch 避坑指南:我在项目中总结的 14 条实用经验
后端·elasticsearch·性能优化
hixiong1235 小时前
C# OpenCVSharp使用 读光-票证检测矫正模型
人工智能·opencv·c#
霜绛5 小时前
C#知识补充(二)——命名空间、泛型、委托和事件
开发语言·学习·unity·c#
好望角雾眠6 小时前
第四阶段C#通讯开发-6:Socket之UDP
开发语言·笔记·学习·udp·c#
霜绛7 小时前
C#知识补充(一)——ref和out、成员属性、万物之父和装箱拆箱、抽象类和抽象方法、接口
开发语言·笔记·学习·c#
爱编程的鱼7 小时前
C# var 关键字详解:从入门到精通
开发语言·c#·solr
玩泥巴的8 小时前
解放双手!使用Roslyn生成代码让你的 HTTP 客户端开发变得如此简单
c#·.net·代码生成·roslyn
星释10 小时前
Rust 练习册 :Pig Latin与语言游戏
游戏·rust·c#
qq_54702617910 小时前
Canal实时同步MySQL数据到Elasticsearch
数据库·mysql·elasticsearch
蒲公英源码11 小时前
超市进销存源码
sqlserver·c#·.net