go - ElasticSearch:条件总和
问题描述
我有包含数量列的记录,可以是负数也可以是正数。我想计算值为正的总量。
我知道我可以使用 来计算总和elastic.NewSumAggregation().Field("quantity")
,但我不确定如何仅包含正数行。
任何机构都可以提供帮助吗?谢谢
解决方案
添加工作示例
索引示例文档
{
"quantity": 10
}
{
"quantity": -10
}
{
"quantity": 25
}
{
"quantity": -25
}
搜索查询
{
"query": {
"range": {
"quantity": {. // filter only for positive quantity.
"gte": 0
}
}
},
"aggs": {
"quantity": {
"sum": {
"field": "quantity"
}
}
}
}
和搜索结果
"hits": [
{
"_index": "65188985",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"quantity": 10
}
},
{
"_index": "65188985",
"_type": "_doc",
"_id": "4",
"_score": 1.0,
"_source": {
"quantity": 25
}
}
]
},
"aggregations": { // note this aggregation part, which is sum of 10 and 25.
"quantity": {
"value": 35.0
}
}
如果您只想提取聚合部分,请给出size=0
查询部分。
{
"size":0,
"query": {
"range": {
"quantity": {
"gte": 0
}
}
},
"aggs": {
"quantity": {
"sum": {
"field": "quantity"
}
}
}
}
以上将输出以下响应
"aggregations": {
"quantity": {
"value": 35.0
}
}
推荐阅读
- google-apps-script - 将行转置到按 Google 表格中的列分组的列
- docker - 从 Docker Swarm 容器连接 Postgresql
- docker - Docker - 新的 contianer 继续使用旧的用户数据
- python - pymysql.connect 命令使 Windows 上的 python shell 崩溃
- python - 如何绘制带有空格的 3 列和 2 行图像?
- ios - Xcode 13 beta:“xcrun:错误:未能找到‘仪器’”
- android-studio - Android Studio 本机代码导航不起作用
- mysql - 编辑记录 CodeIgniter
- java - 如何使用对象列表中的字符串填充 ComboBox?
- delay - AnyLogic:停止延迟块中特定代理的延迟