使用group by和where条件的ElasticSearch(COUNT*)

kxe2p93d  于 2022-12-03  发布在  ElasticSearch
关注(0)|答案(1)|浏览(161)

尊敬的Elastic Search用户:
我是新手在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;

谢谢你

9fkzdhlc

9fkzdhlc1#

下面的查询将完全实现您所需的功能,即,它将选择所需date范围内的文档以及所需的servicedestination,然后对它们的ip字段运行terms聚合(=group by),并按计数降序对后者进行排序。

{
  "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"
      }
    }
  }
}

相关问题