【问题标题】:sum and multiply of multi field for Aggregations in elasticsearch and nest弹性搜索和嵌套中聚合的多字段的总和和相乘
【发布时间】:2017-03-28 17:09:30
【问题描述】:

我是弹性搜索的新手。我使用嵌套来查询来自 elasticsearch 的数据。

聚合后多字段的结果表达式我想要什么方式。

示例:

class public InfoComputer
{
    int Id {get;set;}
    string Name {get;set;}
    int price {get;set;}
    int quantity {get;set;}
};

var result = client.Search<InfoComputer>(s => s
    .Aggregations(a => a
        .Terms("names", st => st
            .Field(o => o.Name)               
            .Aggregations(aa => aa
                .Sum("price", m =>  m
                    .Field(o => o.price)
                )
            )
        )
    )
);

此代码仅获取 Sum 属性价格。

如何获得Sum (price * quantity) 与组属性名称?

【问题讨论】:

    标签: asp.net elasticsearch nest


    【解决方案1】:

    在较新的版本中,您需要在脚本标签中指定内联

    "aggs": {
      "oltotal": {
        "sum": {
          "script": { "inline": "doc['price'].value*doc['amount'].value" } 
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      你需要一个scripted sum aggregation:

      {
        "aggs": {
          "terms_agg": {
            "terms": {
              "field": "name"
            },
            "aggs": {
              "sum_agg": {
                "sum": {
                  "script": "doc['price'].value * doc['quantity'].value"
                }
              }
            }
          }
        }
      }
      

      【讨论】:

      • 我想和你想的一样。但我没有看到 Nest 支持功能脚本。如果可以的话,你能把elasticsearch转换成NEST的查询吗?
      【解决方案3】:

      NEST 支持脚本,您可以像这样修改聚合,即使用Script() 而不是Field()

      var result = client.Search<InfoComputer>(s => s
          .Aggregations(a => a
              .Terms("names", st => st
                  .Field(o => o.Name)               
                  .Aggregations(aa => aa
                      .Sum("price", m =>  m
                          .Script("doc['price'].value * doc['quantity'].value")
                      )
                  )
              )
          )
      );
      

      【讨论】:

      • 我试试这个代码。但总是返回为空。我看到 hit = 0。我认为 doc['price'].value 没有价值
      • 您能用索引中的一些示例文档更新您的问题吗?
      猜你喜欢
      • 2015-06-06
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 2012-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多