弹性搜索和嵌套中聚合的多字段的和和乘

时间:2015-10-20 15:45:36

标签: asp.net elasticsearch nest

我是elasticsearch的新手。我用来嵌套来查询elasticsearch中的数据。

我希望在聚合后获得多个字段的结果表达式的方式。

示例:

createClient()

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

如何使用组属性名称获取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) ) ) ) ) );

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

您需要scripted sum aggregation

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

答案 2 :(得分:0)

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")
                )
            )
        )
    )
);