Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql elasticsearch-总额的总和大于使用聚合的某个金额_Sql_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Group By_Kibana - Fatal编程技术网 elasticsearch,group-by,kibana,Sql,elasticsearch,Group By,Kibana" /> elasticsearch,group-by,kibana,Sql,elasticsearch,Group By,Kibana" />

Sql elasticsearch-总额的总和大于使用聚合的某个金额

Sql elasticsearch-总额的总和大于使用聚合的某个金额,sql,elasticsearch,group-by,kibana,Sql,elasticsearch,Group By,Kibana,我正在尝试获取的记录中,我的合计总数大于某个数量,例如1000。下面是文档示例 [ { "_index": "orders_stage", "_type": "order", "_id": "AV3FtHR8lArSPNJl_rcp", "_score": 1, "_source": { "total_amount": 650, "custid": "2", "client_id": 1 } },

我正在尝试获取的记录中,我的合计总数大于某个数量,例如1000。下面是文档示例

 [
  {
    "_index": "orders_stage",
    "_type": "order",
    "_id": "AV3FtHR8lArSPNJl_rcp",
    "_score": 1,
    "_source": {
      "total_amount": 650,
      "custid": "2",
      "client_id": 1
    }
  },
  {
    "_index": "orders_stage",
    "_type": "order",
    "_id": "AV3F5UfjlArSPNJl_rlu",
    "_score": 1,
    "_source": {
      "total_amount": 200,
      "custid": "1",
      "client_id": 1
    }
  },
  {
    "_index": "orders_stage",
    "_type": "order",
    "_id": "AV3F5UfjlArSPNJl_rxm",
    "_score": 1,
    "_source": {
      "total_amount": 1400,
      "custid": "1",
      "client_id": 1
    }
  }
]
首先,我使用custid对记录进行分组(agg),然后我想要那些总金额大于某个金额(比如1000)的记录。我尝试了以下查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "client_id": 1
          }
        },
        {
          "range": {
            "amount_spent": {
              "gte": 1000
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "customers": {
      "terms": {
        "field": "custid"
      },
      "aggs": {
        "amount_spent": {
          "sum": {
            "field": "total_amount"
          }
        }
      }
    }
  }
}
当我运行此查询时,我没有得到任何信息,请有人指导我筛选聚合结果

谢谢

您需要使用,您不能在查询部分使用:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "client_id": 1
          }
        }
      ]
    }
  },
  "aggs": {
    "customers": {
      "terms": {
        "field": "custid"
      },
      "aggs": {
        "amount_spent": {
          "sum": {
            "field": "total_amount"
          }
        },
        "amount_spent_filter": {
          "bucket_selector": {
            "buckets_path": {
              "amountSpent": "amount_spent"
            },
            "script": "params.amountSpent > 1000"
          }
        }
      }
    }
  }
}

你有一个输入错误:
amount\u-spend
应该是
total\u-amount
在你的
范围内
query如果我把它设为total\u-amount,它不会返回总金额大于1000的总和,相反,它只会检查我已经提到的每个记录,我想分组,然后应用过滤器wesome,很高兴我能帮上忙!请您也指导我获取唯一的存储桶计数,因为它返回一级聚合计数,即在上面的示例中,它必须返回1,因为我们过滤的记录>1000,谢谢您没有过滤记录,而是存储桶,有什么方法可以让我在多个聚合中获得所有桶数?请随意提出一个新问题(因为这个问题已经结束),并解释您的确切需求。