2016-08-24 38 views
1

尊敬的Elastic Serach用户,弹性搜索(COUNT *),并按组条件和条件

我是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; 

感谢您

回答

5

下面的查询将实现你想要什么,也就是说,它会选择所需的date范围内,并使用所需servicedestination,然后运行terms聚集的文件(=组通过)在他们的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" 
     } 
    } 
    } 
} 
+0

嗨,先生@Val,谢谢你的支持。我可以问我继续看的另一个组合吗?如何在每个ip上执行SUM_ALL(使用的卷),最后在聚合下显示例如(ip:xxxx,volume:20mb)。 谢谢 – user647527