【问题标题】:Elastic Search (COUNT*) with group by and where condition具有分组依据和位置条件的弹性搜索 (COUNT*)
【发布时间】:2016-08-24 12:10:23
【问题描述】:

尊敬的 Elastic Serach 用户,

我是 ElasticSearch 的新手。

我对如何将以下 sql 命令转换为 elasticSearch DSL 查询感到困惑?任何人都可以帮助我。

SELECT ip, count(*) as c  FROM elastic WHERE  date 
BETWEEN '2016-08-20  00:00:00' and '2016-08-22 13:41:09' 
AND service='http' AND destination='10.17.102.1' GROUP BY ip ORDER BY c DESC;

谢谢你

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    以下查询将完全实现您想要的,即它将选择所需 date 范围内的文档,并使用所需的 servicedestination,然后运行 ​​terms 聚合(=group by)在他们的ip 字段上并按递减计数顺序排列后者。

    {
      "size": 0,
      "query": {
        "bool": {
          "filter": [
            {
              "range": {
                "date": {
                  "gt": "2016-08-22T00:00:00.000Z",
                  "lt": "2016-08-22T13:41:09.000Z"
                }
              }
            },
            {
              "term": {
                "service": "http"
              }
            },
            {
              "term": {
                "destination": "10.17.102.1"
              }
            }
          ]
        }
      },
      "aggs": {
        "group_by_ip": {
          "terms": {
            "field": "ip"
          }
        }
      }
    }
    

    【讨论】:

    • 您好@Val 先生,感谢您的协助。我可以问另一个我一直在寻找的组合吗?如何在每个 ip 上做 SUM_ALL(volume that used),最后在聚合下显示例如(ip:xxxx, volume:20mb)。谢谢
    猜你喜欢
    • 2012-02-12
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    相关资源
    最近更新 更多