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

实体类Shop结构:

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; }
}

初始化客户端

var client = new ElasticsearchClient(new Uri("http://localhost:9200"));

创建索引

var response = await client.Indices.CreateAsync("dragon");

判断索引是否存在

var response = await client.Indices.ExistsAsync("dragon");

增加数据

var response = await client.IndexAsync(shop, (IndexName)"dragon");

修改数据

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

判断是否存在
 

var response = await client.ExistsAsync<Shop>(1, idx => idx.Index("dragon"));

删除数据

var response = await client.DeleteAsync((IndexName)"dragon", 1);

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

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我爱学习hahaha

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值