2015-10-20 119 views
0

我是elasticsearch的新手。我使用nest来从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 (price * quantity)与组属性名称?

回答

0

你需要一个scripted sum aggregation

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

我想像你想的那样。但是我没有看到Nest支持函数Script。如果可以,你可以将elasticsearch转换成NEST的查询吗? – ngaotruyen

0

有巢脚本支持,您可以修改您的聚集这样的,即通过使用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") 
       ) 
      ) 
     ) 
    ) 
); 
+0

我试试这个代码。但是,所有返回值都是null。我看到命中= 0.我认为doc ['价格']。价值没有得到价值 – ngaotruyen

+0

你可以用索引中的一些示例文档更新你的问题吗? – Val

1

在新版本中,你需要在脚本中指定内联标签

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