相关疑难解决方法(0)

像GROUP BY和HAVING这样的SQL

我想得到满足一定条件的群体数量.在SQL术语中,我想在Elasticsearch中执行以下操作.

SELECT COUNT(*) FROM
(
   SELECT
    senderResellerId,
    SUM(requestAmountValue) AS t_amount
   FROM
    transactions
   GROUP BY
    senderResellerId
   HAVING
    t_amount > 10000 ) AS dum;
Run Code Online (Sandbox Code Playgroud)

到目前为止,我可以通过term Aggsel对senderResellerId进行分组.但是当我应用过滤器时,它不能按预期工作.

弹性请求

{
  "aggregations": {
    "reseller_sale_sum": {
      "aggs": {
        "sales": {
          "aggregations": {
            "reseller_sale": {
              "sum": {
                "field": "requestAmountValue"
              }
            }
          }, 
          "filter": {
            "range": {
              "reseller_sale": { 
                "gte": 10000
              }
            }
          }
        }
      }, 
      "terms": {
        "field": "senderResellerId", 
        "order": {
          "sales>reseller_sale": "desc"
        }, 
        "size": 5
      }
    }
  }, 
  "ext": {}, 
  "query": {  "match_all": {} }, 
  "size": …
Run Code Online (Sandbox Code Playgroud)

elasticsearch elasticsearch-5

13
推荐指数
1
解决办法
1万
查看次数

Elasticsearch排除了对字段值的最高命中

{'country': 'France', 'collected': '2018-03-12', 'active': true}
{'country': 'France', 'collected': '2018-03-13', 'active': true}
{'country': 'France', 'collected': '2018-03-14', 'active': false}
{'country': 'Canada', 'collected': '2018-02-01', 'active': false}
{'country': 'Canada', 'collected': '2018-02-02', 'active': true}
Run Code Online (Sandbox Code Playgroud)

假设我有这个结果集,我想按国家对它们进行分组.按国家对它们进行分组后,结果如下:

{'country': 'France', 'collected': '2018-03-14', 'active': false}
{'country': 'Canada', 'collected': '2018-02-02', 'active': true}
Run Code Online (Sandbox Code Playgroud)

但是,我要排除结果,其中最后一排activefalse(同一个国家的老行可以是真或假并不重要,只要最后一行等于true),我怎么能在elasticsearch做到这一点?这是我的查询:

POST /test/_search?search_type=count
{
    "aggs": {
        "group": {
            "terms": {
                "field": "country"
            },
            "aggs": {
                "group_docs": {
                    "top_hits": {
                        "size": 1,
                        "sort": [
                            {
                                "collected": {
                                    "order": "desc"
                                }
                            }
                        ]
                    }
                } …
Run Code Online (Sandbox Code Playgroud)

elasticsearch

9
推荐指数
1
解决办法
607
查看次数

标签 统计

elasticsearch ×2

elasticsearch-5 ×1