首页 > 编程语言 >c# Elasticsearch统计Nested的数据

c# Elasticsearch统计Nested的数据

时间:2024-10-21 09:32:09浏览次数:1  
标签:financial get c# profit Nested FinancialData Elasticsearch data public

我是直接问百度ai

    es统计是nested结构的数据,数据有年份:reportyear,名称:accountnamezh,类型:reportperiodtype,利润:originalvalue,,需要统计:2024年,名称:营业收入,类型:2,的利润合计是多少,利润金额比较大,需要除以100000000

然后生成的es是

POST /your_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "financial_data",
            "query": {
              "bool": {
                "must": [
                  { "term": { "financial_data.reportyear": 2024 } },
                  { "term": { "financial_data.accountnamezh": "营业收入" } },
                  { "term": { "financial_data.reportperiodtype": 2 } }
                ]
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "total_profit_in_millions": {
      "nested": {
        "path": "financial_data"
      },
      "aggs": {
        "sum_profit": {
          "sum": {
            "script": {
              "source": "doc['financial_data.originalvalue'].value / 100000000"
            }
          }
        }
      }
    }
  }
}

  生成c#代码

using Nest;
using System;
using System.Threading.Tasks;

public class ElasticsearchDocument
{
    public NestedField FinancialData { get; set; }
}

public class FinancialData
{
    public int ReportYear { get; set; }
    public string AccountNameZh { get; set; }
    public int ReportPeriodType { get; set; }
    public double OriginalValue { get; set; }
}

// 注意:这里的NestedField和Elasticsearch的嵌套字段类型相对应,
// 但实际上NEST中的NestedField通常用于查询时的映射,而不是文档模型的一部分。
// 在查询时,我们会用到一个与FinancialData结构相似的匿名类型来构建查询。
// 这里的模型主要是为了展示目的。

class Program
{
    private static ElasticClient _client;

    static async Task Main(string[] args)
    {
        _client = new ElasticClient(settings: new ConnectionSettings(new Uri("http://localhost:9200")));

        var response = await _client.SearchAsync<ElasticsearchDocument>(s => s
            .Index("your_index")
            .Query(q => q
                .Nested(n => n
                    .Path(p => p.FinancialData)
                    .Query(nq => nq
                        .Bool(b => b
                            .Must(m => m
                                .Term(t => t
                                    .Field(f => f.FinancialData.ReportYear)
                                    .Value(2024))
                                .Term(t => t
                                    .Field(f => f.FinancialData.AccountNameZh)
                                    .Value("营业收入"))
                                .Term(t => t
                                    .Field(f => f.FinancialData.ReportPeriodType)
                                    .Value(2)))))
            .Aggregations(a => a
                .Nested("total_profit_in_millions", n => n
                    .Path(p => p.FinancialData)
                    .Aggregations(na => na
                        .Sum("sum_profit", s => s
                            .Script(sc => sc
                                .Source("doc['financial_data.original_value'].value / 100000000")))))))
            .Size(0)); // 我们不需要实际的文档,只需要聚合结果

        // 处理响应
        if (response.IsValid)
        {
            var totalProfitInMillions = response.Aggregations.Nested("total_profit_in_millions")
                ?.Sum("sum_profit")
                ?.Value;

            Console.WriteLine($"Total Profit in Millions: {totalProfitInMillions}");
        }
        else
        {
            Console.WriteLine("Search failed: " + response.ServerError.Error.Reason);
        }
    }
}

然后在这个基础上调整就完成了

 

标签:financial,get,c#,profit,Nested,FinancialData,Elasticsearch,data,public
From: https://www.cnblogs.com/shuaimeng/p/18488360

相关文章

  • Day10 备战CCF-CSP练习
    Day10题目描述十滴水是一个非常经典的小游戏。小\(C\)正在玩一个一维版本的十滴水游戏。我们通过一个例子描述游戏的基本规则。游戏在一个$1×c$的网格上进行,格子用整数$x(1≤x≤c)$编号,编号从左往右依次递增。网格内\(m\)个格子里有\(1∼4\)滴水,其余格子里没有......
  • PbootCMS附件上传报错UNKNOW: Code: 8192; Desc: stripos()
    错误分析:错误信息 UNKNOW:Code:8192;Desc:stripos() 表明在调用 stripos 函数时出现了问题。Code:8192 通常表示PHP的E_DEPRECATED警告,意味着某个函数或功能已被弃用,但仍可使用。修改建议:根据你的描述,需要修改 /core/function/file.php 文件中的 stri......
  • pbootcms网站自动清理runtime缓存方法
    为了实现PbootCMS系统自动清理缓存目录,可以按照以下步骤操作:备份文件:在进行任何修改之前,务必备份 /apps/home/controller/ExtLabelController.php 文件,以防出现意外情况。修改 ExtLabelController.php 文件:打开 /apps/home/controller/ExtLabelController.php ......
  • pbootcms访问页面出现PHP Fatal error: Allowed memory size of 13421
    问题描述客户在使用PbootCMS时,访问首页出现以下错误:PHPFatalerror:Allowedmemorysizeof134217728bytesexhausted(triedtoallocate262144bytes)inxxx\core\database\Sqlite.phponline173PHPFatalerror:Allowedmemorysizeof134217728bytesexh......
  • css面试题
    总结不易,点个赞在走吧文章目录1.介绍一下CSS的盒子模型2.怪异盒模型/IE盒模型如何启用怪异盒模型使用场景3.line-height和height的区别?heightline-height主要区别代码实现height示例line-height示例总结4.CSS选择符有哪些?5.哪些属性可以继承?6.CSS优先级算法如何设......
  • windows系统配置nginx环境运行pbootcms访问首页直接404的问题
    问题描述客户在安装PbootCMS后,访问后台 /admin.php 正常,但直接访问首页或其他页面时出现404错误。运行环境为Windows+Nginx+PHP。详细经过伪静态规则问题:客户反映伪静态规则一直无法生效。代码放到服务器后,除了后台 /admin.php 可以访问,其他页面均返回404......
  • 在cmd中获取系统信息
    在Windows操作系统的命令提示符(cmd)中,可以通过输入特定的命令来获取系统信息。以下是一些常用的cmd命令及其获取的系统信息:获取电脑设备序列号:输入命令:wmicbiosgetserialnumber输出示例:SerialNumber*************(设备号)获取CPU序列号:输入命令:wmiccpugetprocessor......
  • TCP和UDP的报文格式
    TCP和UDP的报文格式  概要 了解TCP和UDP的报文格式对于网络通信、系统设计、故障排查和安全性等多个方面都非常重要。 一、TCP报文格式(TransmissionControlProtocol) TCP是面向连接、可靠的传输协议,其报文格式较复杂。TCP报文的格式如下:  上图简化如下:|......
  • pbootcms域名授权码怎么获取,获取后怎么授权
    一、获取授权码登录PbootCMS官网:访问PbootCMS官方网站:https://www.pbootcms.com进入授权页面:在官网首页导航栏中找到“授权”或“免费授权”选项,点击进入授权页面。授权地址为:https://www.pbootcms.com/freesn/输入网站地址:在授权页面中,输入你要获取授权码的......
  • 少儿Scratch图形化编程案例100课——010美妙的图形
     ......