这里提供一种可能的实现方式:

using Nest;
using System;

class Program
{
    static void Main(string[] args)
    {
        var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
            .DefaultIndex("my_index")
            .DefaultMappingFor<MyDocument>(m => m
                .PropertyName(p => p.Jcx, "jcx")
                .PropertyName(p => p.Sjid, "sjid")
            );
        var client = new ElasticClient(settings);

        var searchResponse = client.Search<MyDocument>(s => s
            .Size(0)
            .Aggregations(a => a
                .Terms("jcx_groups", t => t
                    .Field(f => f.Jcx)
                    .Aggregations(a1 => a1
                        .Cardinality("distinct_sjids", c => c
                            .Field(f => f.Sjid)
                        )
                    )
                )
            )
        );

        Console.WriteLine("Jcx\tDistinct Sjids");
        foreach (var bucket in searchResponse.Aggregations.Terms("jcx_groups").Buckets)
        {
            Console.WriteLine($"{bucket.Key}\t{bucket.Cardinality("distinct_sjids").Value}");
        }
    }
}

class MyDocument
{
    public string Jcx { get; set; }
    public string Sjid { get; set; }
}

这里假设:

  • ElasticSearch服务运行在本地的9200端口。
  • 要查询的索引名为my_index
  • 要查询的文档类型为MyDocument,其中包含JcxSjid两个字符串属性。

在查询中,我们使用了Nest库提供的Fluent API来构建查询。具体来说,我们:

  • 通过ConnectionSettings类配置了连接ElasticSearch的设置,包括默认索引名和文档类型的属性名映射。
  • 使用Search方法进行查询,其中:
    • Size(0)表示只返回聚合结果,不返回文档内容。
    • Aggregations方法指定了一个聚合查询,包括:
      • Terms聚合,按Jcx属性分组。
      • Cardinality聚合,对Sjid属性进行去重计数。
  • 最后,我们遍历聚合结果的所有分组桶(buckets),输出每个分组的Jcx属性值和Sjid属性去重计数。

注意,这里的代码仅供参考,具体实现方式可能因不同的数据模型、查询需求等而有所不同。

用c#写一个ElasticSearch中根据jcx分组统计sjid去重的结果

原文地址: http://www.cveoy.top/t/topic/bHYu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录