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);
    }
}
相关推荐
Wzx1980121 小时前
Redis&ES——Retriever的抽象实现
数据库·人工智能·redis·elasticsearch
慧都小妮子2 小时前
SciChart WPF v9.0 更新详解:矢量场图表、堆叠箱线图与 MVVM 轴同步来了
c#·.net·wpf·数据可视化·图表控件·图表
Elasticsearch4 小时前
让向量听懂孤独:我是如何用 Elastic Agent Builder 构建一个情绪疗愈 agent 的
elasticsearch
Elasticsearch5 小时前
你的 AI 表现不佳。你的数据基础才是问题
elasticsearch
leoZ2315 小时前
Git 集成实战完全指南(三):自动化 Commit 与 PR
大数据·git·elasticsearch
鱼听禅6 小时前
C#学习笔记-Entity Framework Core基础操作学习
c#·orm·ef core
Elasticsearch7 小时前
Elastic: 将来一年中 AI 从活动转向成果
elasticsearch
Z5998178417 小时前
c#软件开发学习笔记--分组、游标与临时表、分页
笔记·学习·c#
rick9778 小时前
用 C# 从零搭建本地知识库:RAG 系统完整实战
c#
Chockmans8 小时前
春秋云境CVE-2023-51385(保姆级教学)
大数据·web安全·elasticsearch·搜索引擎·网络安全·春秋云境·cve-2023-51385