Elasticsearch聚合 之 Ip Range IP地址范围聚合

相对于range和date range,这个聚合就是能够表示IP的范围。

普通IP模式

DSL命令:

{
    "aggs":{
        "ip_ranges":{
            "ip_range":{
                "field":"ip",
                "ranges":[
                    {"to":"10.0.0.5"},
                    {"from":"10.0.0.5"}
                ]
            }
        }
    }
}

返回结果:

{
...
"aggregations":{
    "ip_ranges":{
        "buckets":[
            {
                "to":167772165,
                "to_as_string":"10.0.0.5",
                "doc_count":4
            },{
                "from":167772165,
                "from_as_string":"10.0.0.5",
                "doc_count":6
            }
        ]
        }
    }
}

掩码模式

命令:

{
    "aggs":{
        "ip_ranges":{
            "ip_range":{
                "field":"ip",
                "ranges":[
                    {"mask":"10.0.0.0/25"},
                    {"mask":"10.0.0.127/25"}
                ]
            }
        }
    }
}

返回

{
    "aggregations":{
        "ip_ranges":{
            "buckets":[
                {
                    "key":"10.0.0.0/25",
                    "from":1.6777216E+8,
                    "from_as_string":"10.0.0.0",
                    "to":167772287,
                    "to_as_string":"10.0.0.127",
                    "doc_count":127
                },{
                    "key":"10.0.0.127/25",
                    "from":1.6777216E+8,
                    "from_as_string":"10.0.0.0",
                    "to":167772287,
                    "to_as_string":"10.0.0.127",
                    "doc_count":127
                }
            ]
        }
    }
}

 

posted @ 2015-11-12 22:26  xingoo  阅读(3958)  评论(0编辑  收藏  举报